Skip to content

Commit

Permalink
feat(query): add gt/gte/lt/lte to string comparators (#246)
Browse files Browse the repository at this point in the history
Adds the ability to perform greater than, greater than or equal to,
less than and less than or equal to operators on string data values.

Discussion: #244
  • Loading branch information
ericchernuka committed Nov 4, 2022
1 parent 951b2c8 commit d7f180f
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/comparators/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ export const stringComparators: QueryToComparator<StringQuery> = {
notContains(expected, actual) {
return !stringComparators.contains(expected, actual)
},
gt(expected, actual) {
return actual > expected
},
gte(expected, actual) {
return actual >= expected
},
lt(expected, actual) {
return actual < expected
},
lte(expected, actual) {
return actual <= expected
},
in(expected, actual) {
return expected.includes(actual)
},
Expand Down
4 changes: 4 additions & 0 deletions src/query/queryTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ export interface StringQuery {
notEquals: string
contains: string
notContains: string
gt: string
gte: string
lt: string
lte: string
in: string[]
notIn: string[]
}
Expand Down
2 changes: 1 addition & 1 deletion test/comparators/number-comparators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ test('gte', () => {
expect(numberComparators.gte(4, 2)).toEqual(false)
})

test('gt', () => {
test('lt', () => {
expect(numberComparators.lt(5, 2)).toEqual(true)
expect(numberComparators.lt(20, 9)).toEqual(true)
expect(numberComparators.lt(20, 20)).toEqual(false)
Expand Down
100 changes: 100 additions & 0 deletions test/comparators/string-comparators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,106 @@ test('notContains', () => {
expect(stringComparators.notContains('foo', 'footer')).toBe(false)
})

test('gt', () => {
expect(stringComparators.gt('bar', 'foo')).toEqual(true)
expect(stringComparators.gt('001', '002')).toEqual(true)
expect(stringComparators.gt('foo', 'footer')).toEqual(true)
expect(
stringComparators.gt(
'c971d070-9b87-5492-9df6-b53091ae3874',
'c9ac1210-e5e9-5422-acad-1839535989fe',
),
).toEqual(true)
expect(
stringComparators.gt(
'2022-01-01T15:00:00.000Z',
'2022-01-01T15:00:00.001Z',
),
).toEqual(true)
expect(stringComparators.gt('foo', 'foo')).toEqual(false)
expect(
stringComparators.gt(
'c9ac1210-e5e9-5422-acad-1839535989fe',
'c971d070-9b87-5492-9df6-b53091ae3874',
),
).toEqual(false)
expect(
stringComparators.gt(
'2022-01-01T15:00:00.000Z',
'2022-01-01T14:00:00.000Z',
),
).toEqual(false)
})

test('gte', () => {
expect(stringComparators.gte('bar', 'foo')).toEqual(true)
expect(stringComparators.gte('001', '002')).toEqual(true)
expect(stringComparators.gte('foo', 'footer')).toEqual(true)
expect(
stringComparators.gte(
'2022-01-01T15:00:00.000Z',
'2022-01-01T15:00:00.000Z',
),
).toEqual(true)
expect(
stringComparators.gte(
'2022-01-01T15:00:00.000Z',
'2022-01-01T14:00:00.000Z',
),
).toEqual(false)
expect(stringComparators.gte('footer', 'foot')).toEqual(false)
})

test('lt', () => {
expect(stringComparators.lt('foo', 'bar')).toEqual(true)
expect(stringComparators.lt('002', '001')).toEqual(true)
expect(stringComparators.lt('footer', 'foo')).toEqual(true)
expect(
stringComparators.lt(
'c9ac1210-e5e9-5422-acad-1839535989fe',
'c971d070-9b87-5492-9df6-b53091ae3874',
),
).toEqual(true)
expect(
stringComparators.lt(
'2022-01-01T15:00:00.001Z',
'2022-01-01T15:00:00.000Z',
),
).toEqual(true)
expect(stringComparators.lt('abc', 'abc')).toEqual(false)
expect(
stringComparators.lt(
'c971d070-9b87-5492-9df6-b53091ae3874',
'c9ac1210-e5e9-5422-acad-1839535989fe',
),
).toEqual(false)
expect(
stringComparators.lt(
'2022-01-01T14:00:00.000Z',
'2022-01-01T15:00:00.000Z',
),
).toEqual(false)
})

test('lte', () => {
expect(stringComparators.lte('foo', 'bar')).toEqual(true)
expect(stringComparators.lte('002', '001')).toEqual(true)
expect(stringComparators.lte('footer', 'foot')).toEqual(true)
expect(
stringComparators.lte(
'2022-01-01T14:00:00.000Z',
'2022-01-01T14:00:00.000Z',
),
).toEqual(true)
expect(
stringComparators.lte(
'2022-01-01T14:00:00.000Z',
'2022-01-01T15:00:00.000Z',
),
).toEqual(false)
expect(stringComparators.lte('foot', 'footer')).toEqual(false)
})

test('in', () => {
expect(stringComparators.in(['a', 'foo'], 'a')).toBe(true)
expect(stringComparators.in(['a', 'foo'], 'foo')).toBe(true)
Expand Down
8 changes: 8 additions & 0 deletions test/model/toGraphQLSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ test('generates a graphql schema', () => {
notEquals: ID
contains: ID
notContains: ID
gt: ID
gte: ID
lt: ID
lte: ID
in: [ID]
notIn: [ID]
}
Expand All @@ -44,6 +48,10 @@ test('generates a graphql schema', () => {
notEquals: String
contains: String
notContains: String
gt: String
gte: String
lt: String
lte: String
in: [String]
notIn: [String]
}
Expand Down

0 comments on commit d7f180f

Please sign in to comment.