Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions example/src/screens/UnitTestScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import React, { useEffect, useState } from 'react'
import { ScrollView, Text } from 'react-native'
import type { MochaTestResult } from '../tests/MochaSetup'
import { runTests } from '../tests/MochaSetup'
import { registerUnitTests } from '../tests/unitTests.spec'
import {
registerUnitTests,
/* registerTypeORMUnitTests, */
} from '../tests/unit'
import { ScreenStyles } from '../styles'

export function UnitTestScreen() {
Expand All @@ -12,7 +15,7 @@ export function UnitTestScreen() {
setResults([])
runTests(
registerUnitTests,
// registerTypeORMTests
// registerTypeORMUnitTests
).then(setResults)
}, [])

Expand Down
35 changes: 35 additions & 0 deletions example/src/tests/unit/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Chance } from 'chance'
import {
NitroSQLiteConnection,
enableSimpleNullHandling,
} from 'react-native-nitro-sqlite'
import { testDb as testDbInternal, resetTestDb } from '../db'
import chai from 'chai'

export function isError(e: unknown): e is Error {
return e instanceof Error
}

export const expect = chai.expect
export const chance = new Chance()

export let testDb: NitroSQLiteConnection

export function setupTestDb() {
enableSimpleNullHandling(false)

try {
resetTestDb()

if (testDbInternal == null) throw new Error('Failed to reset test database')

testDbInternal.execute('DROP TABLE IF EXISTS User;')
testDbInternal.execute(
'CREATE TABLE User ( id REAL PRIMARY KEY, name TEXT NOT NULL, age REAL, networth REAL) STRICT;',
)

testDb = testDbInternal!
} catch (e) {
console.warn('Error resetting user database', e)
}
}
20 changes: 20 additions & 0 deletions example/src/tests/unit/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { beforeEach, describe } from '../MochaRNAdapter'
import { setupTestDb } from './common'
import registerExecuteUnitTests from './specs/execute.spec'
import registerTransactionUnitTests from './specs/transaction.spec'
import registerExecuteBatchUnitTests from './specs/executeBatch.spec'
import registerTypeORMUnitTestsSpecs from './specs/typeorm.spec'

export function registerUnitTests() {
beforeEach(setupTestDb)

describe('Operations', () => {
registerExecuteUnitTests()
registerTransactionUnitTests()
registerExecuteBatchUnitTests()
})
}

export function registerTypeORMUnitTests() {
registerTypeORMUnitTestsSpecs()
}
172 changes: 172 additions & 0 deletions example/src/tests/unit/specs/execute.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import { chance, expect, isError, testDb } from '../common'
import {
enableSimpleNullHandling,
NITRO_SQLITE_NULL,
} from 'react-native-nitro-sqlite'
import { describe, it } from '../../MochaRNAdapter'

export default function registerExecuteUnitTests() {
describe('execute', () => {
describe('Insert', () => {
it('Insert', () => {
const id = chance.integer()
const name = chance.name()
const age = chance.integer()
const networth = chance.floating()
const res = testDb.execute(
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
)

expect(res.rowsAffected).to.equal(1)
expect(res.insertId).to.equal(1)
expect(res.rows?._array).to.eql([])
expect(res.rows?.length).to.equal(0)
expect(res.rows?.item).to.be.a('function')
})

it('Insert with null', () => {
const id = chance.integer()
const name = chance.name()
const age = NITRO_SQLITE_NULL
const networth = NITRO_SQLITE_NULL
const res = testDb.execute(
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
)

expect(res.rowsAffected).to.equal(1)
expect(res.insertId).to.equal(1)
expect(res.rows?._array).to.eql([])
expect(res.rows?.length).to.equal(0)
expect(res.rows?.item).to.be.a('function')

const selectRes = testDb.execute('SELECT * FROM User')
expect(selectRes.rows?._array).to.eql([
{
id,
name,
age,
networth,
},
])
})

it('Insert with null (simple null handling)', () => {
enableSimpleNullHandling(true)

const id = chance.integer()
const name = chance.name()
const age = undefined
const networth = null
const res = testDb.execute(
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
)

expect(res.rowsAffected).to.equal(1)
expect(res.insertId).to.equal(1)
expect(res.rows?._array).to.eql([])
expect(res.rows?.length).to.equal(0)
expect(res.rows?.item).to.be.a('function')

const selectRes = testDb.execute('SELECT * FROM User')
expect(selectRes.rows?._array).to.eql([
{
id,
name,
age: null,
networth: null,
},
])
})

it('Failed insert', () => {
const id = chance.integer()
const name = chance.name()
const age = chance.string()
const networth = chance.string()

try {
testDb.execute(
'INSERT INTO User (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
)
} catch (e: unknown) {
if (isError(e)) {
expect(e.message).to.include(
'cannot store TEXT value in REAL column User.age',
)
} else {
expect.fail('Should have thrown a valid NitroSQLiteException')
}
}
})

it('Insertion correctly throws', () => {
const id = chance.string()
const name = chance.name()
const age = chance.integer()
const networth = chance.floating()
try {
testDb.execute(
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
)
} catch (e: unknown) {
expect(e).to.not.equal(undefined)
}
})
})

describe('Select', () => {
it('Query without params', () => {
const id = chance.integer()
const name = chance.name()
const age = chance.integer()
const networth = chance.floating()
testDb.execute(
'INSERT INTO User (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
)

const res = testDb.execute('SELECT * FROM User')

expect(res.rowsAffected).to.equal(1)
expect(res.insertId).to.equal(1)
expect(res.rows?._array).to.eql([
{
id,
name,
age,
networth,
},
])
})

it('Query with params', () => {
const id = chance.integer()
const name = chance.name()
const age = chance.integer()
const networth = chance.floating()
testDb.execute(
'INSERT INTO User (id, name, age, networth) VALUES(?, ?, ?, ?)',
[id, name, age, networth],
)

const res = testDb.execute('SELECT * FROM User WHERE id = ?', [id])

expect(res.rowsAffected).to.equal(1)
expect(res.insertId).to.equal(1)
expect(res.rows?._array).to.eql([
{
id,
name,
age,
networth,
},
])
})
})
})
}
80 changes: 80 additions & 0 deletions example/src/tests/unit/specs/executeBatch.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { chance, expect, testDb } from '../common'
import type { BatchQueryCommand } from 'react-native-nitro-sqlite'
import { describe, it } from '../../MochaRNAdapter'

export default function registerExecuteBatchUnitTests() {
describe('executeBatch', () => {
it('executeBatch', () => {
const id1 = chance.integer()
const name1 = chance.name()
const age1 = chance.integer()
const networth1 = chance.floating()

const id2 = chance.integer()
const name2 = chance.name()
const age2 = chance.integer()
const networth2 = chance.floating()
const commands: BatchQueryCommand[] = [
{
query:
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
params: [id1, name1, age1, networth1],
},
{
query:
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
params: [id2, name2, age2, networth2],
},
]

testDb.executeBatch(commands)

const res = testDb.execute('SELECT * FROM User')
expect(res.rows?._array).to.eql([
{ id: id1, name: name1, age: age1, networth: networth1 },
{
id: id2,
name: name2,
age: age2,
networth: networth2,
},
])
})

it('Async batch execute', async () => {
const id1 = chance.integer()
const name1 = chance.name()
const age1 = chance.integer()
const networth1 = chance.floating()
const id2 = chance.integer()
const name2 = chance.name()
const age2 = chance.integer()
const networth2 = chance.floating()
const commands: BatchQueryCommand[] = [
{
query:
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
params: [id1, name1, age1, networth1],
},
{
query:
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
params: [id2, name2, age2, networth2],
},
]

await testDb.executeBatchAsync(commands)

const res = testDb.execute('SELECT * FROM User')
expect(res.rows?._array).to.eql([
{ id: id1, name: name1, age: age1, networth: networth1 },
{
id: id2,
name: name2,
age: age2,
networth: networth2,
},
])
})
})
}
Loading
Loading