Skip to content

Commit 0a99a6c

Browse files
committed
test: add unit tests for passing and receiving null values
1 parent ef0abeb commit 0a99a6c

File tree

1 file changed

+64
-3
lines changed

1 file changed

+64
-3
lines changed

example/src/tests/unitTests.spec.ts

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import Chance from 'chance'
2-
import type {
3-
NitroSQLiteConnection,
4-
BatchQueryCommand,
2+
import {
3+
type NitroSQLiteConnection,
4+
type BatchQueryCommand,
5+
NITRO_SQLITE_NULL,
6+
enableSimpleNullHandling,
7+
isSimpleNullHandlingEnabled,
58
} from 'react-native-nitro-sqlite'
69
import { beforeEach, describe, it } from './MochaRNAdapter'
710
import chai from 'chai'
@@ -19,6 +22,8 @@ export function registerUnitTests() {
1922
let testDb: NitroSQLiteConnection
2023

2124
beforeEach(() => {
25+
enableSimpleNullHandling(false)
26+
2227
try {
2328
resetTestDb()
2429

@@ -54,6 +59,62 @@ export function registerUnitTests() {
5459
expect(res.rows?.item).to.be.a('function')
5560
})
5661

62+
it('Insert with null', () => {
63+
const id = chance.integer()
64+
const name = chance.name()
65+
const age = NITRO_SQLITE_NULL
66+
const networth = NITRO_SQLITE_NULL
67+
const res = testDb.execute(
68+
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
69+
[id, name, age, networth]
70+
)
71+
72+
expect(res.rowsAffected).to.equal(1)
73+
expect(res.insertId).to.equal(1)
74+
expect(res.rows?._array).to.eql([])
75+
expect(res.rows?.length).to.equal(0)
76+
expect(res.rows?.item).to.be.a('function')
77+
78+
const selectRes = testDb.execute('SELECT * FROM User')
79+
expect(selectRes.rows?._array).to.eql([
80+
{
81+
id,
82+
name,
83+
age,
84+
networth,
85+
},
86+
])
87+
})
88+
89+
it('Insert with null (simple null handling)', () => {
90+
enableSimpleNullHandling(true)
91+
92+
const id = chance.integer()
93+
const name = chance.name()
94+
const age = undefined
95+
const networth = null
96+
const res = testDb.execute(
97+
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
98+
[id, name, age, networth]
99+
)
100+
101+
expect(res.rowsAffected).to.equal(1)
102+
expect(res.insertId).to.equal(1)
103+
expect(res.rows?._array).to.eql([])
104+
expect(res.rows?.length).to.equal(0)
105+
expect(res.rows?.item).to.be.a('function')
106+
107+
const selectRes = testDb.execute('SELECT * FROM User')
108+
expect(selectRes.rows?._array).to.eql([
109+
{
110+
id,
111+
name,
112+
age: null,
113+
networth: null,
114+
},
115+
])
116+
})
117+
57118
it('Query without params', () => {
58119
const id = chance.integer()
59120
const name = chance.name()

0 commit comments

Comments
 (0)