|
| 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 | +} |
0 commit comments