Skip to content

Commit

Permalink
add test for basic success case of getGroupRDF.downloadAllRdfForGroup()
Browse files Browse the repository at this point in the history
pull up some mockSinopiaClient setup code that can be shared from a prior test, and import fs functions
  • Loading branch information
jmartin-sul committed Aug 5, 2019
1 parent fc92916 commit fab6432
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions __tests__/getGroupRDF.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import config from 'config'
import fs from 'fs'
import * as getGroupRDF from '../src/getGroupRDF'
import SinopiaServer from 'sinopia_server'

const groupResources = { 'group1': ['resource1', 'resource2', 'resource3'] }
const resourceContent = { 'resource1': 'resource1content', 'resource2': 'resource2content', 'resource3': 'resource3content' }

const mockSinopiaClient = jest.fn()
mockSinopiaClient.getGroupWithHttpInfo = jest.fn()
mockSinopiaClient.getResourceWithHttpInfo = jest.fn()
mockSinopiaClient.apiClient = jest.fn()
mockSinopiaClient.apiClient = jest.fn() // need this field present so sinopiaClient() function can set .basePath on it.
mockSinopiaClient.getGroupWithHttpInfo = async (groupName) => { return (!groupResources[groupName] ? null : { response: { body: {contains: groupResources[groupName] } } }) }
mockSinopiaClient.getResourceWithHttpInfo = async (groupName, resourceName) => { return (!groupResources[groupName] ? null : { response: { text: resourceContent[resourceName] } }) }

jest.spyOn(SinopiaServer, 'LDPApi').mockImplementation(() => mockSinopiaClient)

Expand All @@ -18,13 +22,36 @@ describe('getGroupRDF', () => {

describe('getResourceTextFromServer', () => {
it('retrieves the specified resource from the server and returns the RDF as text', async () => {
const groupResources = { 'group1': ['resource1', 'resource2', 'resource3'] }
const resourceContent = { 'resource1': 'resource1content', 'resource2': 'resource2content', 'resource3': 'resource3content' }
expect(await getGroupRDF.getResourceTextFromServer('group1', 'resource2')).toEqual('resource2content')
})
})

mockSinopiaClient.getGroupWithHttpInfo = async (groupName) => { return (!groupResources[groupName] ? null : { response: { body: {contains: resourceList } } }) }
mockSinopiaClient.getResourceWithHttpInfo = async (groupName, resourceName) => { return (!groupResources[groupName] ? null : { response: { text: resourceContent[resourceName] } }) }
describe('downloadAllRdfForGroup', () => {
it('retrieves the RDF resources for the specified group, and saves the RDF text (on file per resource, in a dated sub-directory)', async () => {
const dlDateLowerBound = new Date()
await getGroupRDF.downloadAllRdfForGroup('group1')
const dlDateUpperBound = new Date()

expect(await getGroupRDF.getResourceTextFromServer('group1', 'resource2')).toEqual('resource2content')
// assume we just want the latest output dir in the export base dir, since we should be the only ones writing there
const exportBaseDirContents = fs.readdirSync(`./${config.get('exportBasePath')}`)
const groupDirName = exportBaseDirContents[exportBaseDirContents.length-1]

// RDF for group exported to subdirectory with name like 'group1_2019-08-05T01:34:13.143Z'
const groupDirDate = new Date(/^group1_(.*)$/.exec(groupDirName)[1])
// .toBeGreaterThan and .toBeLessThan only work for numbers
expect(dlDateLowerBound < groupDirDate && groupDirDate < dlDateUpperBound).toBeTruthy()

const groupDirPath = `./${config.get('exportBasePath')}/${groupDirName}`
const groupDirContents = fs.readdirSync(groupDirPath)
expect(groupDirContents).toEqual(['complete.log', 'resource1', 'resource2', 'resource3'])

expect(fs.readFileSync(`${groupDirPath}/resource1`).toString()).toEqual('resource1content')
expect(fs.readFileSync(`${groupDirPath}/resource2`).toString()).toEqual('resource2content')
expect(fs.readFileSync(`${groupDirPath}/resource3`).toString()).toEqual('resource3content')
const completionLogText = fs.readFileSync(`${groupDirPath}/complete.log`).toString()
// completion log text should have date, should fall between group dir creation and downloadAllRdfForGroup resolving
const completionLogDate = new Date(/^completed export of group1 at (.*)$/.exec(completionLogText)[1])
expect(groupDirDate < completionLogDate && completionLogDate < dlDateUpperBound)
})
})
})

0 comments on commit fab6432

Please sign in to comment.