1
- import { AbstractPowerSyncDatabase , Column , ColumnType , CrudEntry , Schema , Table , UpdateType } from '@powersync/common' ;
2
- import { PowerSyncDatabase } from '@powersync/web' ;
1
+ import { Column , ColumnType , CrudEntry , Schema , Table , UpdateType } from '@powersync/common' ;
3
2
import pDefer from 'p-defer' ;
4
3
import { v4 as uuid } from 'uuid' ;
5
- import { afterEach , beforeEach , describe , expect , it } from 'vitest' ;
4
+ import { describe , expect , it } from 'vitest' ;
6
5
import { generateTestDb } from './utils/testDb' ;
7
6
8
7
const testId = '2290de4f-0488-4e50-abed-f8e8eb1d0b42' ;
9
8
10
- describe ( 'CRUD Tests' , ( ) => {
11
- let powersync : AbstractPowerSyncDatabase ;
12
-
13
- beforeEach ( async ( ) => {
14
- powersync = generateTestDb ( ) ;
15
- } ) ;
16
-
17
- afterEach ( async ( ) => {
18
- await powersync . disconnectAndClear ( ) ;
19
- await powersync . close ( ) ;
20
- } ) ;
21
-
9
+ describe ( 'CRUD Tests' , { sequential : true } , ( ) => {
22
10
it ( 'INSERT' , async ( ) => {
11
+ const powersync = generateTestDb ( ) ;
12
+
23
13
expect ( await powersync . getAll ( 'SELECT * FROM ps_crud' ) ) . empty ;
24
14
25
15
await powersync . execute ( 'INSERT INTO assets(id, description) VALUES(?, ?)' , [ testId , 'test' ] ) ;
@@ -37,6 +27,8 @@ describe('CRUD Tests', () => {
37
27
} ) ;
38
28
39
29
it ( 'BATCH INSERT' , async ( ) => {
30
+ const powersync = generateTestDb ( ) ;
31
+
40
32
expect ( await powersync . getAll ( 'SELECT * FROM ps_crud' ) ) . empty ;
41
33
42
34
const query = `INSERT INTO assets(id, description) VALUES(?, ?)` ;
@@ -63,6 +55,8 @@ describe('CRUD Tests', () => {
63
55
} ) ;
64
56
65
57
it ( 'INSERT OR REPLACE' , async ( ) => {
58
+ const powersync = generateTestDb ( ) ;
59
+
66
60
await powersync . execute ( 'INSERT INTO assets(id, description) VALUES(?, ?)' , [ testId , 'test' ] ) ;
67
61
await powersync . execute ( 'DELETE FROM ps_crud WHERE 1' ) ;
68
62
@@ -85,6 +79,8 @@ describe('CRUD Tests', () => {
85
79
} ) ;
86
80
87
81
it ( 'UPDATE' , async ( ) => {
82
+ const powersync = generateTestDb ( ) ;
83
+
88
84
await powersync . execute ( 'INSERT INTO assets(id, description, make) VALUES(?, ?, ?)' , [ testId , 'test' , 'test' ] ) ;
89
85
await powersync . execute ( 'DELETE FROM ps_crud WHERE 1' ) ;
90
86
@@ -105,6 +101,8 @@ describe('CRUD Tests', () => {
105
101
} ) ;
106
102
107
103
it ( 'BATCH UPDATE' , async ( ) => {
104
+ const powersync = generateTestDb ( ) ;
105
+
108
106
await powersync . executeBatch ( 'INSERT INTO assets(id, description, make) VALUES(?, ?, ?)' , [
109
107
[ testId , 'test' , 'test' ] ,
110
108
[ 'mockId' , 'test' , 'test' ]
@@ -137,6 +135,8 @@ describe('CRUD Tests', () => {
137
135
} ) ;
138
136
139
137
it ( 'DELETE' , async ( ) => {
138
+ const powersync = generateTestDb ( ) ;
139
+
140
140
await powersync . execute ( 'INSERT INTO assets(id, description, make) VALUES(?, ?, ?)' , [ testId , 'test' , 'test' ] ) ;
141
141
await powersync . execute ( 'DELETE FROM ps_crud WHERE 1' ) ;
142
142
@@ -153,6 +153,8 @@ describe('CRUD Tests', () => {
153
153
} ) ;
154
154
155
155
it ( 'UPSERT not supported' , async ( ) => {
156
+ const powersync = generateTestDb ( ) ;
157
+
156
158
// Just shows that we cannot currently do this
157
159
await expect (
158
160
powersync . execute ( 'INSERT INTO assets(id, description) VALUES(?, ?) ON CONFLICT DO UPDATE SET description = ?' , [
@@ -164,9 +166,7 @@ describe('CRUD Tests', () => {
164
166
} ) ;
165
167
166
168
it ( 'INSERT-only tables' , async ( ) => {
167
- await powersync . disconnectAndClear ( ) ;
168
-
169
- powersync = new PowerSyncDatabase ( {
169
+ const powersync = generateTestDb ( {
170
170
/**
171
171
* Deleting the IndexDB seems to freeze the test.
172
172
* Use a new DB for each run to keep CRUD counters
@@ -212,6 +212,8 @@ describe('CRUD Tests', () => {
212
212
} ) ;
213
213
214
214
it ( 'big numbers - integer' , async ( ) => {
215
+ const powersync = generateTestDb ( ) ;
216
+
215
217
const bigNumber = 1 << 62 ;
216
218
await powersync . execute ( 'INSERT INTO assets(id, quantity) VALUES(?, ?)' , [ testId , bigNumber ] ) ;
217
219
@@ -233,6 +235,8 @@ describe('CRUD Tests', () => {
233
235
} ) ;
234
236
235
237
it ( 'big numbers - text' , async ( ) => {
238
+ const powersync = generateTestDb ( ) ;
239
+
236
240
const bigNumber = 1 << 62 ;
237
241
await powersync . execute ( 'INSERT INTO assets(id, quantity) VALUES(?, ?)' , [ testId , `${ bigNumber } ` ] ) ;
238
242
@@ -263,6 +267,8 @@ describe('CRUD Tests', () => {
263
267
} ) ;
264
268
265
269
it ( 'Transaction grouping' , async ( ) => {
270
+ const powersync = generateTestDb ( ) ;
271
+
266
272
expect ( await powersync . getAll ( 'SELECT * FROM ps_crud' ) ) . empty ;
267
273
await powersync . writeTransaction ( async ( tx ) => {
268
274
await tx . execute ( 'INSERT INTO assets(id, description) VALUES(?, ?)' , [ testId , 'test1' ] ) ;
@@ -292,6 +298,8 @@ describe('CRUD Tests', () => {
292
298
} ) ;
293
299
294
300
it ( 'Transaction exclusivity' , async ( ) => {
301
+ const powersync = generateTestDb ( ) ;
302
+
295
303
const outside = pDefer ( ) ;
296
304
const inTx = pDefer ( ) ;
297
305
@@ -313,6 +321,8 @@ describe('CRUD Tests', () => {
313
321
} ) ;
314
322
315
323
it ( 'CRUD Batch Limits' , async ( ) => {
324
+ const powersync = generateTestDb ( ) ;
325
+
316
326
const initialBatch = await powersync . getCrudBatch ( ) ;
317
327
expect ( initialBatch , 'Initial CRUD batch should be null' ) . null ;
318
328
0 commit comments