-
Notifications
You must be signed in to change notification settings - Fork 393
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[import] Allow importing from array (#228)
- Loading branch information
Showing
9 changed files
with
167 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/@sanity/import/test/__snapshots__/import.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`employee creation 1`] = ` | ||
Object { | ||
"mutations": Array [ | ||
Object { | ||
"create": Object { | ||
"_id": "espen", | ||
"_type": "employee", | ||
"name": "Espen", | ||
}, | ||
}, | ||
Object { | ||
"create": Object { | ||
"_id": "pk", | ||
"_type": "employee", | ||
"name": "Per-Kristian", | ||
}, | ||
}, | ||
], | ||
} | ||
`; | ||
|
||
exports[`employee creation 2`] = ` | ||
Object { | ||
"mutations": Array [ | ||
Object { | ||
"create": Object { | ||
"_id": "espen", | ||
"_type": "employee", | ||
"name": "Espen", | ||
}, | ||
}, | ||
Object { | ||
"create": Object { | ||
"_id": "pk", | ||
"_type": "employee", | ||
"name": "Per-Kristian", | ||
}, | ||
}, | ||
], | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const noop = require('lodash/noop') | ||
const sanityClient = require('@sanity/client') | ||
const {injectResponse} = require('get-it/middleware') | ||
|
||
const defaultClientOptions = { | ||
projectId: 'foo', | ||
dataset: 'bar', | ||
token: 'abc123', | ||
useCdn: false | ||
} | ||
|
||
const getSanityClient = (inject = noop, opts = {}) => { | ||
const requester = sanityClient.requester.clone() | ||
requester.use(injectResponse({inject})) | ||
const req = {requester: requester} | ||
const client = sanityClient(Object.assign(defaultClientOptions, req, opts)) | ||
return client | ||
} | ||
|
||
module.exports = { | ||
getSanityClient | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./helpers') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,98 @@ | ||
/* eslint-disable no-sync */ | ||
const fs = require('fs') | ||
const path = require('path') | ||
const sanityClient = require('@sanity/client') | ||
const {getSanityClient} = require('./helpers') | ||
const importer = require('../') | ||
|
||
const client = sanityClient({ | ||
const defaultClient = sanityClient({ | ||
projectId: 'foo', | ||
dataset: 'bar', | ||
useCdn: false, | ||
token: 'foo' | ||
}) | ||
|
||
const options = {client} | ||
const importOptions = {client: defaultClient} | ||
const fixturesDir = path.join(__dirname, 'fixtures') | ||
const getFixture = fix => { | ||
const fixPath = path.join(fixturesDir, `${fix}.ndjson`) | ||
return fs.createReadStream(fixPath, 'utf8') | ||
} | ||
const getFixturePath = fix => path.join(fixturesDir, `${fix}.ndjson`) | ||
const getFixtureStream = fix => fs.createReadStream(getFixturePath(fix), 'utf8') | ||
const getFixtureArray = fix => | ||
fs | ||
.readFileSync(getFixturePath(fix), 'utf8') | ||
.trim() | ||
.split('\n') | ||
.map(JSON.parse) | ||
|
||
test('rejects on invalid input type (null/undefined)', async () => { | ||
await expect(importer(null, importOptions)).rejects.toHaveProperty( | ||
'message', | ||
'Stream does not seem to be a readable stream or an array' | ||
) | ||
}) | ||
|
||
test('rejects on invalid input type (non-array)', async () => { | ||
await expect(importer({}, importOptions)).rejects.toHaveProperty( | ||
'message', | ||
'Stream does not seem to be a readable stream or an array' | ||
) | ||
}) | ||
|
||
test('rejects on invalid JSON', async () => { | ||
await expect(importer(getFixture('invalid-json'), options)).rejects.toHaveProperty( | ||
await expect(importer(getFixtureStream('invalid-json'), importOptions)).rejects.toHaveProperty( | ||
'message', | ||
'Failed to parse line #3: Unexpected token _ in JSON at position 1' | ||
) | ||
}) | ||
|
||
test('rejects on missing `_id` property', async () => { | ||
await expect(importer(getFixture('invalid-id'), options)).rejects.toHaveProperty( | ||
test('rejects on invalid `_id` property', async () => { | ||
await expect(importer(getFixtureStream('invalid-id'), importOptions)).rejects.toHaveProperty( | ||
'message', | ||
'Failed to parse line #2: Document contained an invalid "_id" property - must be a string' | ||
) | ||
}) | ||
|
||
test('rejects on missing `_type` property', async () => { | ||
await expect(importer(getFixture('missing-type'), options)).rejects.toHaveProperty( | ||
await expect(importer(getFixtureStream('missing-type'), importOptions)).rejects.toHaveProperty( | ||
'message', | ||
'Failed to parse line #3: Document did not contain required "_type" property of type string' | ||
) | ||
}) | ||
|
||
test('rejects on missing `_type` property (from array)', async () => { | ||
await expect(importer(getFixtureArray('missing-type'), importOptions)).rejects.toHaveProperty( | ||
'message', | ||
'Failed to parse document at index #2: Document did not contain required "_type" property of type string' | ||
) | ||
}) | ||
|
||
test('accepts an array as source', async () => { | ||
const docs = getFixtureArray('employees') | ||
const client = getSanityClient(getMockEmployeeHandler()) | ||
const res = await importer(docs, {client}) | ||
expect(res).toBe(2) | ||
}) | ||
|
||
test('accepts a stream as source', async () => { | ||
const client = getSanityClient(getMockEmployeeHandler()) | ||
const res = await importer(getFixtureStream('employees'), {client}) | ||
expect(res).toBe(2) | ||
}) | ||
|
||
function getMockEmployeeHandler() { | ||
return req => { | ||
const options = req.context.options | ||
const uri = options.uri || options.url | ||
|
||
if (uri.includes('/data/mutate')) { | ||
const body = JSON.parse(options.body) | ||
expect(body).toMatchSnapshot('employee creation') | ||
const results = body.mutations.map(mut => ({ | ||
id: mut.create.id, | ||
operation: 'create' | ||
})) | ||
return {body: {results}} | ||
} | ||
|
||
return {statusCode: 400, body: {error: `"${uri}" should not be called`}} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters