-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more tests for error cases when ordering/filtering (#6505)
- Loading branch information
Showing
5 changed files
with
165 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@keystone-next/keystone': patch | ||
'@keystone-next/api-tests-legacy': patch | ||
--- | ||
|
||
Improved error message for bad relationship filter inputs. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,24 @@ | ||
import { text, relationship } from '@keystone-next/keystone/fields'; | ||
import { createSchema, list } from '@keystone-next/keystone'; | ||
import { setupTestRunner } from '@keystone-next/keystone/testing'; | ||
import { apiTestConfig } from '../utils'; | ||
import { apiTestConfig, expectInternalServerError } from '../utils'; | ||
|
||
const runner = setupTestRunner({ | ||
config: apiTestConfig({ | ||
lists: createSchema({ | ||
User: list({ | ||
fields: { | ||
noDash: text({ isFilterable: true }), | ||
noDash: text({ isFilterable: true, isOrderable: true }), | ||
single_dash: text({ isFilterable: true }), | ||
many_many_many_dashes: text({ isFilterable: true }), | ||
multi____dash: text({ isFilterable: true }), | ||
email: text({ isIndexed: 'unique', isFilterable: true }), | ||
}, | ||
}), | ||
SecondaryList: list({ | ||
fields: { | ||
someUser: relationship({ ref: 'User', isFilterable: true }), | ||
otherUsers: relationship({ ref: 'User', isFilterable: true, many: true }), | ||
}, | ||
}), | ||
}), | ||
|
@@ -68,3 +70,121 @@ describe('filtering on field name', () => { | |
}) | ||
); | ||
}); | ||
|
||
describe('filtering on relationships', () => { | ||
test( | ||
'findMany throws error with null relationship query', | ||
runner(async ({ graphQLRequest }) => { | ||
const { body } = await graphQLRequest({ | ||
query: '{ secondaryLists(where: { otherUsers: null }) { id } }', | ||
}); | ||
// Returns null and throws an error | ||
expect(body.data).toEqual({ secondaryLists: null }); | ||
expectInternalServerError(body.errors, false, [ | ||
{ | ||
message: 'A many relation filter cannot be set to null', | ||
path: ['secondaryLists'], | ||
}, | ||
]); | ||
}) | ||
); | ||
|
||
test( | ||
'findMany throws error with null relationship query value', | ||
runner(async ({ graphQLRequest }) => { | ||
const { body } = await graphQLRequest({ | ||
query: '{ secondaryLists(where: { otherUsers: { some: null } }) { id } }', | ||
}); | ||
// Returns null and throws an error | ||
expect(body.data).toEqual({ secondaryLists: null }); | ||
expectInternalServerError(body.errors, false, [ | ||
{ | ||
message: 'The key "some" in a many relation filter cannot be set to null', | ||
path: ['secondaryLists'], | ||
}, | ||
]); | ||
}) | ||
); | ||
|
||
test( | ||
'findMany returns all items with empty relationship query value', | ||
runner(async ({ context, graphQLRequest }) => { | ||
await context.lists.SecondaryList.createOne({ | ||
data: { otherUsers: { create: [{ noDash: 'a' }, { noDash: 'b' }] } }, | ||
}); | ||
const { body } = await graphQLRequest({ | ||
query: | ||
'{ secondaryLists(where: { otherUsers: {} }) { otherUsers(orderBy: { noDash: asc }) { noDash } } }', | ||
}); | ||
// Returns all the data | ||
expect(body.errors).toBe(undefined); | ||
expect(body.data).toEqual({ | ||
secondaryLists: [{ otherUsers: [{ noDash: 'a' }, { noDash: 'b' }] }], | ||
}); | ||
}) | ||
); | ||
}); | ||
|
||
describe('searching by unique fields', () => { | ||
test( | ||
'findOne works on a unique text field', | ||
runner(async ({ context }) => { | ||
const item = await context.lists.User.createOne({ data: { email: '[email protected]' } }); | ||
const { data, errors } = await context.graphql.raw({ | ||
query: '{ user(where: { email: "[email protected]" }) { id email } }', | ||
}); | ||
expect(errors).toBe(undefined); | ||
expect(data).toEqual({ user: { id: item.id, email: '[email protected]' } }); | ||
}) | ||
); | ||
|
||
test( | ||
'findOne throws error with zero where values', | ||
runner(async ({ graphQLRequest }) => { | ||
const { body } = await graphQLRequest({ query: '{ user(where: {}) { id email } }' }); | ||
// Returns null and throws an error | ||
expect(body.data).toEqual({ user: null }); | ||
expectInternalServerError(body.errors, false, [ | ||
{ | ||
message: 'Exactly one key must be passed in a unique where input but 0 keys were passed', | ||
path: ['user'], | ||
}, | ||
]); | ||
}) | ||
); | ||
|
||
test( | ||
'findOne throws error with more than one where values', | ||
runner(async ({ context, graphQLRequest }) => { | ||
const item = await context.lists.User.createOne({ data: { email: '[email protected]' } }); | ||
const { body } = await graphQLRequest({ | ||
query: `{ user(where: { id: "${item.id}" email: "[email protected]" }) { id email } }`, | ||
}); | ||
// Returns null and throws an error | ||
expect(body.data).toEqual({ user: null }); | ||
expectInternalServerError(body.errors, false, [ | ||
{ | ||
message: 'Exactly one key must be passed in a unique where input but 2 keys were passed', | ||
path: ['user'], | ||
}, | ||
]); | ||
}) | ||
); | ||
|
||
test( | ||
'findOne throws error with null where values', | ||
runner(async ({ graphQLRequest }) => { | ||
const { body } = await graphQLRequest({ | ||
query: '{ user(where: { email: null }) { id email } }', | ||
}); | ||
// Returns null and throws an error | ||
expect(body.data).toEqual({ user: null }); | ||
expectInternalServerError(body.errors, false, [ | ||
{ | ||
message: 'The unique value provided in a unique where input must not be null', | ||
path: ['user'], | ||
}, | ||
]); | ||
}) | ||
); | ||
}); |
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