Skip to content

Commit fba906b

Browse files
authored
refactor: re-arrange unit tests (#236)
1 parent 22d2aed commit fba906b

File tree

7 files changed

+338
-298
lines changed

7 files changed

+338
-298
lines changed

example/src/screens/UnitTestScreen.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import React, { useEffect, useState } from 'react'
22
import { ScrollView, Text } from 'react-native'
33
import type { MochaTestResult } from '../tests/MochaSetup'
44
import { runTests } from '../tests/MochaSetup'
5-
import { registerUnitTests } from '../tests/unitTests.spec'
5+
import {
6+
registerUnitTests,
7+
/* registerTypeORMUnitTests, */
8+
} from '../tests/unit'
69
import { ScreenStyles } from '../styles'
710

811
export function UnitTestScreen() {
@@ -12,7 +15,7 @@ export function UnitTestScreen() {
1215
setResults([])
1316
runTests(
1417
registerUnitTests,
15-
// registerTypeORMTests
18+
// registerTypeORMUnitTests
1619
).then(setResults)
1720
}, [])
1821

example/src/tests/unit/common.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Chance } from 'chance'
2+
import {
3+
NitroSQLiteConnection,
4+
enableSimpleNullHandling,
5+
} from 'react-native-nitro-sqlite'
6+
import { testDb as testDbInternal, resetTestDb } from '../db'
7+
import chai from 'chai'
8+
9+
export function isError(e: unknown): e is Error {
10+
return e instanceof Error
11+
}
12+
13+
export const expect = chai.expect
14+
export const chance = new Chance()
15+
16+
export let testDb: NitroSQLiteConnection
17+
18+
export function setupTestDb() {
19+
enableSimpleNullHandling(false)
20+
21+
try {
22+
resetTestDb()
23+
24+
if (testDbInternal == null) throw new Error('Failed to reset test database')
25+
26+
testDbInternal.execute('DROP TABLE IF EXISTS User;')
27+
testDbInternal.execute(
28+
'CREATE TABLE User ( id REAL PRIMARY KEY, name TEXT NOT NULL, age REAL, networth REAL) STRICT;',
29+
)
30+
31+
testDb = testDbInternal!
32+
} catch (e) {
33+
console.warn('Error resetting user database', e)
34+
}
35+
}

example/src/tests/unit/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { beforeEach, describe } from '../MochaRNAdapter'
2+
import { setupTestDb } from './common'
3+
import registerExecuteUnitTests from './specs/execute.spec'
4+
import registerTransactionUnitTests from './specs/transaction.spec'
5+
import registerExecuteBatchUnitTests from './specs/executeBatch.spec'
6+
import registerTypeORMUnitTestsSpecs from './specs/typeorm.spec'
7+
8+
export function registerUnitTests() {
9+
beforeEach(setupTestDb)
10+
11+
describe('Operations', () => {
12+
registerExecuteUnitTests()
13+
registerTransactionUnitTests()
14+
registerExecuteBatchUnitTests()
15+
})
16+
}
17+
18+
export function registerTypeORMUnitTests() {
19+
registerTypeORMUnitTestsSpecs()
20+
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
import { chance, expect, isError, testDb } from '../common'
2+
import {
3+
enableSimpleNullHandling,
4+
NITRO_SQLITE_NULL,
5+
} from 'react-native-nitro-sqlite'
6+
import { describe, it } from '../../MochaRNAdapter'
7+
8+
export default function registerExecuteUnitTests() {
9+
describe('execute', () => {
10+
describe('Insert', () => {
11+
it('Insert', () => {
12+
const id = chance.integer()
13+
const name = chance.name()
14+
const age = chance.integer()
15+
const networth = chance.floating()
16+
const res = testDb.execute(
17+
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
18+
[id, name, age, networth],
19+
)
20+
21+
expect(res.rowsAffected).to.equal(1)
22+
expect(res.insertId).to.equal(1)
23+
expect(res.rows?._array).to.eql([])
24+
expect(res.rows?.length).to.equal(0)
25+
expect(res.rows?.item).to.be.a('function')
26+
})
27+
28+
it('Insert with null', () => {
29+
const id = chance.integer()
30+
const name = chance.name()
31+
const age = NITRO_SQLITE_NULL
32+
const networth = NITRO_SQLITE_NULL
33+
const res = testDb.execute(
34+
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
35+
[id, name, age, networth],
36+
)
37+
38+
expect(res.rowsAffected).to.equal(1)
39+
expect(res.insertId).to.equal(1)
40+
expect(res.rows?._array).to.eql([])
41+
expect(res.rows?.length).to.equal(0)
42+
expect(res.rows?.item).to.be.a('function')
43+
44+
const selectRes = testDb.execute('SELECT * FROM User')
45+
expect(selectRes.rows?._array).to.eql([
46+
{
47+
id,
48+
name,
49+
age,
50+
networth,
51+
},
52+
])
53+
})
54+
55+
it('Insert with null (simple null handling)', () => {
56+
enableSimpleNullHandling(true)
57+
58+
const id = chance.integer()
59+
const name = chance.name()
60+
const age = undefined
61+
const networth = null
62+
const res = testDb.execute(
63+
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
64+
[id, name, age, networth],
65+
)
66+
67+
expect(res.rowsAffected).to.equal(1)
68+
expect(res.insertId).to.equal(1)
69+
expect(res.rows?._array).to.eql([])
70+
expect(res.rows?.length).to.equal(0)
71+
expect(res.rows?.item).to.be.a('function')
72+
73+
const selectRes = testDb.execute('SELECT * FROM User')
74+
expect(selectRes.rows?._array).to.eql([
75+
{
76+
id,
77+
name,
78+
age: null,
79+
networth: null,
80+
},
81+
])
82+
})
83+
84+
it('Failed insert', () => {
85+
const id = chance.integer()
86+
const name = chance.name()
87+
const age = chance.string()
88+
const networth = chance.string()
89+
90+
try {
91+
testDb.execute(
92+
'INSERT INTO User (id, name, age, networth) VALUES(?, ?, ?, ?)',
93+
[id, name, age, networth],
94+
)
95+
} catch (e: unknown) {
96+
if (isError(e)) {
97+
expect(e.message).to.include(
98+
'cannot store TEXT value in REAL column User.age',
99+
)
100+
} else {
101+
expect.fail('Should have thrown a valid NitroSQLiteException')
102+
}
103+
}
104+
})
105+
106+
it('Insertion correctly throws', () => {
107+
const id = chance.string()
108+
const name = chance.name()
109+
const age = chance.integer()
110+
const networth = chance.floating()
111+
try {
112+
testDb.execute(
113+
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
114+
[id, name, age, networth],
115+
)
116+
} catch (e: unknown) {
117+
expect(e).to.not.equal(undefined)
118+
}
119+
})
120+
})
121+
122+
describe('Select', () => {
123+
it('Query without params', () => {
124+
const id = chance.integer()
125+
const name = chance.name()
126+
const age = chance.integer()
127+
const networth = chance.floating()
128+
testDb.execute(
129+
'INSERT INTO User (id, name, age, networth) VALUES(?, ?, ?, ?)',
130+
[id, name, age, networth],
131+
)
132+
133+
const res = testDb.execute('SELECT * FROM User')
134+
135+
expect(res.rowsAffected).to.equal(1)
136+
expect(res.insertId).to.equal(1)
137+
expect(res.rows?._array).to.eql([
138+
{
139+
id,
140+
name,
141+
age,
142+
networth,
143+
},
144+
])
145+
})
146+
147+
it('Query with params', () => {
148+
const id = chance.integer()
149+
const name = chance.name()
150+
const age = chance.integer()
151+
const networth = chance.floating()
152+
testDb.execute(
153+
'INSERT INTO User (id, name, age, networth) VALUES(?, ?, ?, ?)',
154+
[id, name, age, networth],
155+
)
156+
157+
const res = testDb.execute('SELECT * FROM User WHERE id = ?', [id])
158+
159+
expect(res.rowsAffected).to.equal(1)
160+
expect(res.insertId).to.equal(1)
161+
expect(res.rows?._array).to.eql([
162+
{
163+
id,
164+
name,
165+
age,
166+
networth,
167+
},
168+
])
169+
})
170+
})
171+
})
172+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { chance, expect, testDb } from '../common'
2+
import type { BatchQueryCommand } from 'react-native-nitro-sqlite'
3+
import { describe, it } from '../../MochaRNAdapter'
4+
5+
export default function registerExecuteBatchUnitTests() {
6+
describe('executeBatch', () => {
7+
it('executeBatch', () => {
8+
const id1 = chance.integer()
9+
const name1 = chance.name()
10+
const age1 = chance.integer()
11+
const networth1 = chance.floating()
12+
13+
const id2 = chance.integer()
14+
const name2 = chance.name()
15+
const age2 = chance.integer()
16+
const networth2 = chance.floating()
17+
const commands: BatchQueryCommand[] = [
18+
{
19+
query:
20+
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
21+
params: [id1, name1, age1, networth1],
22+
},
23+
{
24+
query:
25+
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
26+
params: [id2, name2, age2, networth2],
27+
},
28+
]
29+
30+
testDb.executeBatch(commands)
31+
32+
const res = testDb.execute('SELECT * FROM User')
33+
expect(res.rows?._array).to.eql([
34+
{ id: id1, name: name1, age: age1, networth: networth1 },
35+
{
36+
id: id2,
37+
name: name2,
38+
age: age2,
39+
networth: networth2,
40+
},
41+
])
42+
})
43+
44+
it('Async batch execute', async () => {
45+
const id1 = chance.integer()
46+
const name1 = chance.name()
47+
const age1 = chance.integer()
48+
const networth1 = chance.floating()
49+
const id2 = chance.integer()
50+
const name2 = chance.name()
51+
const age2 = chance.integer()
52+
const networth2 = chance.floating()
53+
const commands: BatchQueryCommand[] = [
54+
{
55+
query:
56+
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
57+
params: [id1, name1, age1, networth1],
58+
},
59+
{
60+
query:
61+
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
62+
params: [id2, name2, age2, networth2],
63+
},
64+
]
65+
66+
await testDb.executeBatchAsync(commands)
67+
68+
const res = testDb.execute('SELECT * FROM User')
69+
expect(res.rows?._array).to.eql([
70+
{ id: id1, name: name1, age: age1, networth: networth1 },
71+
{
72+
id: id2,
73+
name: name2,
74+
age: age2,
75+
networth: networth2,
76+
},
77+
])
78+
})
79+
})
80+
}

0 commit comments

Comments
 (0)