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 ,
57} from 'react-native-nitro-sqlite'
68import { beforeEach , describe , it } from './MochaRNAdapter'
79import chai from 'chai'
@@ -19,6 +21,8 @@ export function registerUnitTests() {
1921 let testDb : NitroSQLiteConnection
2022
2123 beforeEach ( ( ) => {
24+ enableSimpleNullHandling ( false )
25+
2226 try {
2327 resetTestDb ( )
2428
@@ -54,6 +58,62 @@ export function registerUnitTests() {
5458 expect ( res . rows ?. item ) . to . be . a ( 'function' )
5559 } )
5660
61+ it ( 'Insert with null' , ( ) => {
62+ const id = chance . integer ( )
63+ const name = chance . name ( )
64+ const age = NITRO_SQLITE_NULL
65+ const networth = NITRO_SQLITE_NULL
66+ const res = testDb . execute (
67+ 'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)' ,
68+ [ id , name , age , networth ]
69+ )
70+
71+ expect ( res . rowsAffected ) . to . equal ( 1 )
72+ expect ( res . insertId ) . to . equal ( 1 )
73+ expect ( res . rows ?. _array ) . to . eql ( [ ] )
74+ expect ( res . rows ?. length ) . to . equal ( 0 )
75+ expect ( res . rows ?. item ) . to . be . a ( 'function' )
76+
77+ const selectRes = testDb . execute ( 'SELECT * FROM User' )
78+ expect ( selectRes . rows ?. _array ) . to . eql ( [
79+ {
80+ id,
81+ name,
82+ age,
83+ networth,
84+ } ,
85+ ] )
86+ } )
87+
88+ it ( 'Insert with null (simple null handling)' , ( ) => {
89+ enableSimpleNullHandling ( true )
90+
91+ const id = chance . integer ( )
92+ const name = chance . name ( )
93+ const age = undefined
94+ const networth = null
95+ const res = testDb . execute (
96+ 'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)' ,
97+ [ id , name , age , networth ]
98+ )
99+
100+ expect ( res . rowsAffected ) . to . equal ( 1 )
101+ expect ( res . insertId ) . to . equal ( 1 )
102+ expect ( res . rows ?. _array ) . to . eql ( [ ] )
103+ expect ( res . rows ?. length ) . to . equal ( 0 )
104+ expect ( res . rows ?. item ) . to . be . a ( 'function' )
105+
106+ const selectRes = testDb . execute ( 'SELECT * FROM User' )
107+ expect ( selectRes . rows ?. _array ) . to . eql ( [
108+ {
109+ id,
110+ name,
111+ age : null ,
112+ networth : null ,
113+ } ,
114+ ] )
115+ } )
116+
57117 it ( 'Query without params' , ( ) => {
58118 const id = chance . integer ( )
59119 const name = chance . name ( )
0 commit comments