11import 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'
69import { beforeEach , describe , it } from './MochaRNAdapter'
710import 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