Skip to content

Commit a1a1413

Browse files
committed
basic count and exists store functions integration tests
1 parent 7fe9929 commit a1a1413

File tree

6 files changed

+83
-1
lines changed

6 files changed

+83
-1
lines changed

src/integrationTests/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { executeTestGroups } from './common'
22
import { ORM, provisionOrm } from './orm'
3+
import { countTests } from './tests/count'
34
import { createTests } from './tests/create'
45
import { deleteTests } from './tests/delete'
6+
import { existsTests } from './tests/exists'
57
import { getTests } from './tests/get'
68
import { updateTests } from './tests/update'
79

@@ -13,6 +15,8 @@ const init = async () => {
1315
updateTests,
1416
deleteTests,
1517
createTests,
18+
countTests,
19+
existsTests,
1620
)
1721
await ORM.db.client.end()
1822
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { DataFilterLogic, Operator } from '@samhuk/data-filter/dist/types'
2+
import { test } from '../../common'
3+
4+
export const basicTest = test('basic', async (stores, assert) => {
5+
const result = await stores.user.count({
6+
filter: {
7+
logic: DataFilterLogic.OR,
8+
nodes: [
9+
{ field: 'name', op: Operator.EQUALS, val: 'User 1' },
10+
{ field: 'name', op: Operator.EQUALS, val: 'User 2' },
11+
],
12+
},
13+
})
14+
15+
assert(result, 2)
16+
17+
const result2 = await stores.user.count({
18+
filter: {
19+
logic: DataFilterLogic.AND,
20+
nodes: [
21+
{ field: 'name', op: Operator.EQUALS, val: 'User 1' },
22+
{ field: 'name', op: Operator.EQUALS, val: 'User 2' },
23+
],
24+
},
25+
})
26+
27+
assert(result2, 0)
28+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { testGroup } from '../../common'
2+
import { basicTest } from './basic'
3+
4+
export const countTests = testGroup(
5+
'count',
6+
basicTest,
7+
)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { DataFilterLogic, Operator } from '@samhuk/data-filter/dist/types'
2+
import { test } from '../../common'
3+
4+
export const basicTest = test('basic', async (stores, assert) => {
5+
const result = await stores.user.exists({
6+
filter: {
7+
logic: DataFilterLogic.OR,
8+
nodes: [
9+
{ field: 'name', op: Operator.EQUALS, val: 'User 1' },
10+
{ field: 'name', op: Operator.EQUALS, val: 'User 2' },
11+
],
12+
},
13+
})
14+
15+
assert(result, true)
16+
17+
const result2 = await stores.user.exists({
18+
filter: {
19+
logic: DataFilterLogic.AND,
20+
nodes: [
21+
{ field: 'name', op: Operator.EQUALS, val: 'User 1' },
22+
{ field: 'name', op: Operator.EQUALS, val: 'User 2' },
23+
],
24+
},
25+
})
26+
27+
assert(result2, false)
28+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { testGroup } from '../../common'
2+
import { basicTest } from './basic'
3+
4+
export const existsTests = testGroup(
5+
'exists',
6+
basicTest,
7+
)

src/store/count/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,13 @@ export const count = async (
3434
const sql = sqlParts.filter(s => s != null).join('\n')
3535

3636
const row = await db.queryGetFirstRow(sql)
37-
return (row as any).exact_count
37+
const countRaw = (row as any).exact_count
38+
if (countRaw == null)
39+
return null
40+
if (typeof countRaw === 'number')
41+
return countRaw
42+
if (typeof countRaw === 'string')
43+
return parseInt(countRaw)
44+
45+
return null
3846
}

0 commit comments

Comments
 (0)