Skip to content

Commit

Permalink
feat(query): add in and notIn filters to NumberQuery (#150)
Browse files Browse the repository at this point in the history
* feat(query): add `in` and `notIn` filters to NumberQuery

* fix(graphql): update `between`, `notBetween`, `in`, and `notIn` filters to be lists

* test(numberComparators): add "in" and "notIn" unit tests

Co-authored-by: Artem Zakharchenko <[email protected]>
  • Loading branch information
ruisaraiva19 and kettanaito authored Oct 29, 2021
1 parent c4a5208 commit 3beed3f
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 36 deletions.
6 changes: 6 additions & 0 deletions src/comparators/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ export const numberComparators: QueryToComparator<NumberQuery> = {
lte(expected, actual) {
return actual <= expected
},
in(expected, actual) {
return expected.includes(actual)
},
notIn(expected, actual) {
return !numberComparators.in(expected, actual)
},
}
3 changes: 2 additions & 1 deletion src/model/generateGraphQLHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ function createComparatorGraphQLInputType(
name,
fields: Object.keys(comparators).reduce<GraphQLInputFieldConfigMap>(
(fields, comparatorFn) => {
fields[comparatorFn] = { type }
const fieldType = ['between', 'notBetween', 'in', 'notIn'].includes(comparatorFn) ? GraphQLList(type) : type
fields[comparatorFn] = { type: fieldType }
return fields
},
{},
Expand Down
2 changes: 2 additions & 0 deletions src/query/queryTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ export interface NumberQuery {
gte: number
lt: number
lte: number
in: number[]
notIn: number[]
}

export interface BooleanQuery {
Expand Down
76 changes: 47 additions & 29 deletions test/comparators/number-comparators.test.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,72 @@
import { numberComparators } from '../../src/comparators/number'

test('equals', () => {
expect(numberComparators.equals(1, 1)).toBe(true)
expect(numberComparators.equals(234, 234)).toBe(true)
expect(numberComparators.equals(2, 5)).toBe(false)
expect(numberComparators.equals(1, 1)).toEqual(true)
expect(numberComparators.equals(234, 234)).toEqual(true)
expect(numberComparators.equals(2, 5)).toEqual(false)
})

test('notEquals', () => {
expect(numberComparators.notEquals(2, 5)).toBe(true)
expect(numberComparators.notEquals(0, 10)).toBe(true)
expect(numberComparators.notEquals(1, 1)).toBe(false)
expect(numberComparators.notEquals(2, 5)).toEqual(true)
expect(numberComparators.notEquals(0, 10)).toEqual(true)
expect(numberComparators.notEquals(1, 1)).toEqual(false)
})

test('between', () => {
expect(numberComparators.between([5, 10], 7)).toBe(true)
expect(numberComparators.between([5, 10], 5)).toBe(true)
expect(numberComparators.between([5, 10], 7)).toBe(true)
expect(numberComparators.between([5, 10], 24)).toBe(false)
expect(numberComparators.between([5, 10], 7)).toEqual(true)
expect(numberComparators.between([5, 10], 5)).toEqual(true)
expect(numberComparators.between([5, 10], 7)).toEqual(true)
expect(numberComparators.between([5, 10], 24)).toEqual(false)
})

test('notBetween', () => {
expect(numberComparators.notBetween([5, 10], 4)).toBe(true)
expect(numberComparators.notBetween([5, 10], 11)).toBe(true)
expect(numberComparators.notBetween([5, 10], 5)).toBe(false)
expect(numberComparators.notBetween([5, 10], 10)).toBe(false)
expect(numberComparators.notBetween([5, 10], 4)).toEqual(true)
expect(numberComparators.notBetween([5, 10], 11)).toEqual(true)
expect(numberComparators.notBetween([5, 10], 5)).toEqual(false)
expect(numberComparators.notBetween([5, 10], 10)).toEqual(false)
})

test('gt', () => {
expect(numberComparators.gt(2, 5)).toBe(true)
expect(numberComparators.gt(9, 20)).toBe(true)
expect(numberComparators.gt(20, 20)).toBe(false)
expect(numberComparators.gt(2, 5)).toEqual(true)
expect(numberComparators.gt(9, 20)).toEqual(true)
expect(numberComparators.gt(20, 20)).toEqual(false)
})

test('gte', () => {
expect(numberComparators.gte(2, 5)).toBe(true)
expect(numberComparators.gte(9, 20)).toBe(true)
expect(numberComparators.gte(20, 20)).toBe(true)
expect(numberComparators.gte(4, 2)).toBe(false)
expect(numberComparators.gte(2, 5)).toEqual(true)
expect(numberComparators.gte(9, 20)).toEqual(true)
expect(numberComparators.gte(20, 20)).toEqual(true)
expect(numberComparators.gte(4, 2)).toEqual(false)
})

test('gt', () => {
expect(numberComparators.lt(5, 2)).toBe(true)
expect(numberComparators.lt(20, 9)).toBe(true)
expect(numberComparators.lt(20, 20)).toBe(false)
expect(numberComparators.lt(5, 20)).toBe(false)
expect(numberComparators.lt(5, 2)).toEqual(true)
expect(numberComparators.lt(20, 9)).toEqual(true)
expect(numberComparators.lt(20, 20)).toEqual(false)
expect(numberComparators.lt(5, 20)).toEqual(false)
})

test('lte', () => {
expect(numberComparators.lte(5, 2)).toBe(true)
expect(numberComparators.lte(20, 9)).toBe(true)
expect(numberComparators.lte(20, 20)).toBe(true)
expect(numberComparators.lte(5, 20)).toBe(false)
expect(numberComparators.lte(5, 2)).toEqual(true)
expect(numberComparators.lte(20, 9)).toEqual(true)
expect(numberComparators.lte(20, 20)).toEqual(true)
expect(numberComparators.lte(5, 20)).toEqual(false)
})

test('in', () => {
expect(numberComparators.in([5], 5)).toEqual(true)
expect(numberComparators.in([5, 10], 5)).toEqual(true)
expect(numberComparators.in([1, 3, 5], 3)).toEqual(true)

expect(numberComparators.in([5], 3)).toEqual(false)
expect(numberComparators.in([3, 5], 4)).toEqual(false)
})

test('notIn', () => {
expect(numberComparators.notIn([5], 2)).toEqual(true)
expect(numberComparators.notIn([5, 10], 7)).toEqual(true)
expect(numberComparators.notIn([1, 3, 5], 4)).toEqual(true)

expect(numberComparators.notIn([5], 5)).toEqual(false)
expect(numberComparators.notIn([3, 5], 3)).toEqual(false)
})
14 changes: 8 additions & 6 deletions test/model/toGraphQLSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,30 @@ test('generates a graphql schema', () => {
notEquals: ID
contains: ID
notContains: ID
in: ID
notIn: ID
in: [ID]
notIn: [ID]
}
input StringQueryType {
equals: String
notEquals: String
contains: String
notContains: String
in: String
notIn: String
in: [String]
notIn: [String]
}
input IntQueryType {
equals: Int
notEquals: Int
between: Int
notBetween: Int
between: [Int]
notBetween: [Int]
gt: Int
gte: Int
lt: Int
lte: Int
in: [Int]
notIn: [Int]
}
type Mutation {
Expand Down
28 changes: 28 additions & 0 deletions test/query/number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,31 @@ test('queries entities that are younger or equal a number', () => {
const names = users.map((user) => user.firstName)
expect(names).toEqual(['John', 'Alice'])
})

test('queries entities where property is not contained into the array', () => {
const db = setup()

const users = db.user.findMany({
where: {
age: {
notIn: [16, 24],
},
},
})
const names = users.map((user) => user.firstName)
expect(names).toEqual(['Kate'])
})

test('queries entities where property is contained into the array', () => {
const db = setup()

const users = db.user.findMany({
where: {
age: {
in: [16, 24],
},
},
})
const names = users.map((user) => user.firstName)
expect(names).toEqual(['John', 'Alice'])
})

0 comments on commit 3beed3f

Please sign in to comment.