Skip to content

Commit 6d356f1

Browse files
committed
refactor: restructure test db setup
1 parent d3ab2b4 commit 6d356f1

File tree

6 files changed

+25
-29
lines changed

6 files changed

+25
-29
lines changed

example/src/tests/db.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,33 @@ import type {
44
BatchQueryCommand,
55
} from 'react-native-nitro-sqlite'
66
import { open } from 'react-native-nitro-sqlite'
7+
import {
8+
getDatabaseQueue,
9+
type DatabaseQueue,
10+
} from '../../../package/src/DatabaseQueue'
711

812
const chance = new Chance()
913

1014
export const TEST_DB_NAME = 'test'
1115

12-
export let testDb: NitroSQLiteConnection | undefined
16+
export let testDb: NitroSQLiteConnection
17+
export let testDbQueue: DatabaseQueue
1318
export function resetTestDb() {
1419
try {
1520
if (testDb != null) {
1621
testDb.close()
1722
testDb.delete()
1823
}
24+
1925
testDb = open({
2026
name: TEST_DB_NAME,
2127
})
28+
testDbQueue = getDatabaseQueue(TEST_DB_NAME)
29+
30+
testDb.execute('DROP TABLE IF EXISTS User;')
31+
testDb.execute(
32+
'CREATE TABLE User ( id REAL PRIMARY KEY, name TEXT NOT NULL, age REAL, networth REAL) STRICT;',
33+
)
2234
} catch (e) {
2335
console.warn('Error resetting user database', e)
2436
}

example/src/tests/unit/common.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { Chance } from 'chance'
2-
import {
3-
NitroSQLiteConnection,
4-
enableSimpleNullHandling,
5-
} from 'react-native-nitro-sqlite'
6-
import { testDb as testDbInternal, resetTestDb } from '../db'
2+
import { enableSimpleNullHandling } from 'react-native-nitro-sqlite'
3+
import { resetTestDb } from '../db'
74
import chai from 'chai'
85

96
export function isError(e: unknown): e is Error {
@@ -13,23 +10,7 @@ export function isError(e: unknown): e is Error {
1310
export const expect = chai.expect
1411
export const chance = new Chance()
1512

16-
export let testDb: NitroSQLiteConnection
17-
1813
export function setupTestDb() {
1914
enableSimpleNullHandling(false)
20-
21-
try {
22-
resetTestDb()
23-
24-
if (testDbInternal == null) throw new Error('Failed to reset test database')
25-
26-
testDbInternal.execute('DROP TABLE IF EXISTS User;')
27-
testDbInternal.execute(
28-
'CREATE TABLE User ( id REAL PRIMARY KEY, name TEXT NOT NULL, age REAL, networth REAL) STRICT;',
29-
)
30-
31-
testDb = testDbInternal!
32-
} catch (e) {
33-
console.warn('Error resetting user database', e)
34-
}
15+
resetTestDb()
3516
}

example/src/tests/unit/specs/execute.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { chance, expect, isError, testDb } from '../common'
1+
import { chance, expect, isError } from '../common'
22
import {
33
enableSimpleNullHandling,
44
NITRO_SQLITE_NULL,
55
} from 'react-native-nitro-sqlite'
66
import { describe, it } from '../../MochaRNAdapter'
7+
import { testDb } from '../../db'
78

89
export default function registerExecuteUnitTests() {
910
describe('execute', () => {

example/src/tests/unit/specs/executeBatch.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { chance, expect, testDb } from '../common'
1+
import { chance, expect } from '../common'
22
import type { BatchQueryCommand } from 'react-native-nitro-sqlite'
33
import { describe, it } from '../../MochaRNAdapter'
4+
import { testDb } from '../../db'
45

56
export default function registerExecuteBatchUnitTests() {
67
describe('executeBatch', () => {

example/src/tests/unit/specs/transaction.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { chance, expect, isError, testDb } from '../common'
1+
import { chance, expect, isError } from '../common'
22
import { describe, it } from '../../MochaRNAdapter'
33
import type { User } from '../../../model/User'
4+
import { testDb, testDbQueue } from '../../db'
45

56
export default function registerTransactionUnitTests() {
67
describe('transaction', () => {

package/src/DatabaseQueue.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import NitroSQLiteError from './NitroSQLiteError'
22

3-
interface QueuedOperation {
3+
export interface QueuedOperation {
44
/**
55
* Starts the operation
66
*/
77
start: () => void
88
}
99

10-
type DatabaseQueue = {
10+
export type DatabaseQueue = {
1111
queue: QueuedOperation[]
1212
inProgress: boolean
1313
}
@@ -47,7 +47,7 @@ export function throwIfDatabaseIsNotOpen(dbName: string) {
4747
)
4848
}
4949

50-
function getDatabaseQueue(dbName: string) {
50+
export function getDatabaseQueue(dbName: string) {
5151
throwIfDatabaseIsNotOpen(dbName)
5252

5353
const queue = databaseQueues.get(dbName)!

0 commit comments

Comments
 (0)