Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

tauri-sql-driver.test.ts

156 lines4.2 KB

1import { mkdtempSync, rmSync } from 'node:fs'
2import { tmpdir } from 'node:os'
3import { join } from 'node:path'
4import { afterEach, expect, it } from 'vitest'
5import { InvalidPersistedCollectionConfigError } from '../../db-sqlite-persistence-core/src'
6import { TauriSQLiteDriver } from '../src/tauri-sql-driver'
7import { createTauriSQLiteTestDatabase } from './helpers/tauri-sql-test-db'
8​
9const activeCleanupFns: Array<() => void | Promise<void>> = []
10​
11afterEach(async () => {
12 while (activeCleanupFns.length > 0) {
13 const cleanupFn = activeCleanupFns.pop()
14 await Promise.resolve(cleanupFn?.())
15 }
16})
17​
18function createTempSqlitePath(): string {
19 const tempDirectory = mkdtempSync(join(tmpdir(), `db-tauri-test-`))
20 const dbPath = join(tempDirectory, `state.sqlite`)
21 activeCleanupFns.push(() => {
22 rmSync(tempDirectory, { recursive: true, force: true })
23 })
24 return dbPath
26​
27it(`keeps literal question marks untouched while binding positional parameters`, async () => {
28 const dbPath = createTempSqlitePath()
29 const database = createTauriSQLiteTestDatabase({ filename: dbPath })
30 activeCleanupFns.push(() => {
31 database.close()
32 })
33​
34 const driver = new TauriSQLiteDriver({ database })
35 await driver.exec(
36 `CREATE TABLE prompts (id TEXT PRIMARY KEY, title TEXT NOT NULL, notes TEXT NOT NULL)`,
37 )
38 await driver.run(
39 `INSERT INTO prompts (id, title, notes) VALUES (?, ?, 'literal ? kept in SQL')`,
40 [`1`, `Why?`],
41 )
42​
43 const rows = await driver.query<{ title: string; notes: string }>(
44 `SELECT title, notes
45 FROM prompts
46 WHERE title = ?`,
47 [`Why?`],
48 )
49​
50 expect(rows).toEqual([
51 {
52 title: `Why?`,
53 notes: `literal ? kept in SQL`,
54 },
55 ])
56})
57​
58it(`does not convert question marks inside line comments`, async () => {
59 let capturedSql = ``
60 const driver = new TauriSQLiteDriver({
61 database: {
62 path: `sqlite:test.db`,
63 execute: async (sql) => {
64 capturedSql = sql
65 return { rowsAffected: 0 }
66 },
67 select: async <TRow>() => [] as unknown as TRow,
68 close: async () => true,
69 },
70 })
71​
72 await driver.run(
73 `UPDATE prompts
74 SET notes = 'changed'
75 -- keep this literal ? untouched
76 WHERE id = ?`,
77 [`1`],
78 )
79​
80 expect(capturedSql).toContain(`-- keep this literal ? untouched`)
81 expect(capturedSql).toContain(`WHERE id = $1`)
82})
83​
84it(`does not convert question marks inside block comments`, async () => {
85 let capturedSql = ``
86 const driver = new TauriSQLiteDriver({
87 database: {
88 path: `sqlite:test.db`,
89 execute: async () => ({ rowsAffected: 0 }),
90 select: async <TRow>(sql: string) => {
91 capturedSql = sql
92 return [] as unknown as TRow
93 },
94 close: async () => true,
95 },
96 })
97​
98 await driver.query(
99 `SELECT title
100 FROM prompts
101 /* keep this literal ? untouched */
102 WHERE id = ?`,
103 [`1`],
104 )
105​
106 expect(capturedSql).toContain(`/* keep this literal ? untouched */`)
107 expect(capturedSql).toContain(`WHERE id = $1`)
108})
109​
110it(`keeps escaped single-quote literals unchanged while converting bindings`, async () => {
111 let capturedSql = ``
112 const driver = new TauriSQLiteDriver({
113 database: {
114 path: `sqlite:test.db`,
115 execute: async (sql) => {
116 capturedSql = sql
117 return { rowsAffected: 0 }
118 },
119 select: async <TRow>() => [] as unknown as TRow,
120 close: async () => true,
121 },
122 })
123​
124 await driver.run(
125 `INSERT INTO prompts (id, title, notes)
126 VALUES (?, 'it''s still a literal ?', ?)`,
127 [`1`, `note`],
128 )
129​
130 expect(capturedSql).toContain(`VALUES ($1, 'it''s still a literal ?', $2)`)
131})
132​
133it(`closes the underlying database when close is available`, async () => {
134 let closeCount = 0
135 const driver = new TauriSQLiteDriver({
136 database: {
137 path: `sqlite:test.db`,
138 execute: async () => ({ rowsAffected: 0 }),
139 select: async <TRow>() => [] as unknown as TRow,
140 close: async () => {
141 closeCount++
142 return true
143 },
144 },
145 })
146​
147 await driver.close()
148​
149 expect(closeCount).toBe(1)
150})
151​
152it(`throws config error when execute/select methods are missing`, () => {
153 expect(() => new TauriSQLiteDriver({ database: {} as never })).toThrowError(
154 InvalidPersistedCollectionConfigError,
155 )
156})