Skip to content

Commit 01f77bb

Browse files
authored
Add migration tests (#75)
1 parent b647499 commit 01f77bb

15 files changed

+289
-6
lines changed

.eslintignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"scripts": {
1414
"build": "tsup src/index.ts --format cjs,esm --dts",
1515
"lint": "npx @biomejs/biome check src/ tests/ || (npx @biomejs/biome check --write src/ tests/; exit 1)",
16-
"test": "vitest run",
16+
"test": "vitest run --root tests",
1717
"prepare": "husky install"
1818
},
1919
"publishConfig": {

tests/bindings.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export type Env = {
2+
DB: D1Database
3+
}
4+
5+
declare module 'cloudflare:test' {
6+
interface ProvidedEnv extends Env {}
7+
}

tests/integration/crud.test.ts

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import { env } from 'cloudflare:test'
2+
import { describe, expect, it } from 'vitest'
3+
import { D1QB } from '../../src'
4+
5+
describe('Simple operations', () => {
6+
it('all operations', async () => {
7+
const qb = new D1QB(env.DB)
8+
9+
await qb
10+
.createTable({
11+
tableName: 'testTable',
12+
schema: `
13+
id INTEGER PRIMARY KEY AUTOINCREMENT,
14+
name TEXT NOT NULL
15+
`,
16+
})
17+
.execute()
18+
19+
expect(
20+
(
21+
await qb
22+
.insert({
23+
tableName: 'testTable',
24+
data: [
25+
{
26+
name: 'example',
27+
},
28+
{
29+
name: 'test2',
30+
},
31+
],
32+
returning: '*',
33+
})
34+
.execute()
35+
).results
36+
).toEqual([
37+
{
38+
id: 1,
39+
name: 'example',
40+
},
41+
{
42+
id: 2,
43+
name: 'test2',
44+
},
45+
])
46+
47+
expect((await qb.select('testTable').all()).results).toEqual([
48+
{
49+
id: 1,
50+
name: 'example',
51+
},
52+
{
53+
id: 2,
54+
name: 'test2',
55+
},
56+
])
57+
58+
expect(
59+
(
60+
await qb
61+
.update({
62+
tableName: 'testTable',
63+
where: {
64+
conditions: 'name = ?1',
65+
params: ['example'],
66+
},
67+
data: {
68+
name: 'newName',
69+
},
70+
returning: '*',
71+
})
72+
.execute()
73+
).results
74+
).toEqual([
75+
{
76+
id: 1,
77+
name: 'newName',
78+
},
79+
])
80+
81+
expect(
82+
(
83+
await qb
84+
.delete({
85+
tableName: 'testTable',
86+
where: {
87+
conditions: 'name = ?1',
88+
params: ['test2'],
89+
},
90+
returning: '*',
91+
})
92+
.execute()
93+
).results
94+
).toEqual([
95+
{
96+
id: 2,
97+
name: 'test2',
98+
},
99+
])
100+
101+
expect(
102+
(
103+
await qb
104+
.delete({
105+
tableName: 'testTable',
106+
where: {
107+
conditions: 'name = ?1',
108+
params: ['abc'],
109+
},
110+
returning: '*',
111+
})
112+
.execute()
113+
).results
114+
).toEqual([])
115+
116+
expect((await qb.select('testTable').all()).results).toEqual([
117+
{
118+
id: 1,
119+
name: 'newName',
120+
},
121+
])
122+
123+
await qb
124+
.dropTable({
125+
tableName: 'testTable',
126+
})
127+
.execute()
128+
129+
expect(
130+
(
131+
await env.DB.prepare(`SELECT name, sql
132+
FROM sqlite_master
133+
WHERE type = 'table'
134+
AND name not in ('_cf_KV', 'sqlite_sequence', '_cf_METADATA')`).all()
135+
).results
136+
).toEqual([])
137+
})
138+
})
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import { env } from 'cloudflare:test'
2+
import { describe, expect, it } from 'vitest'
3+
import { D1QB, Migration } from '../../src'
4+
5+
export const migrations: Migration[] = [
6+
{
7+
name: '100000000000000_add_logs_table.sql',
8+
sql: `
9+
create table logs
10+
(
11+
id INTEGER PRIMARY KEY AUTOINCREMENT,
12+
name TEXT NOT NULL
13+
);`,
14+
},
15+
]
16+
17+
describe('Migrations', () => {
18+
it('initialize', async () => {
19+
const qb = new D1QB(env.DB)
20+
21+
expect(
22+
(
23+
await env.DB.prepare(`SELECT name
24+
FROM sqlite_master
25+
WHERE type = 'table'`).all()
26+
).results
27+
).toEqual([])
28+
29+
await qb.migrations({ migrations }).initialize()
30+
31+
expect(
32+
(
33+
await env.DB.prepare(`SELECT name
34+
FROM sqlite_master
35+
WHERE type = 'table'
36+
AND name <> 'sqlite_sequence'`).all()
37+
).results
38+
).toEqual([
39+
{
40+
name: 'migrations',
41+
},
42+
])
43+
})
44+
45+
it('apply', async () => {
46+
const qb = new D1QB(env.DB)
47+
48+
expect(
49+
(
50+
await env.DB.prepare(`SELECT name
51+
FROM sqlite_master
52+
WHERE type = 'table'`).all()
53+
).results
54+
).toEqual([])
55+
56+
const applyResp = await qb.migrations({ migrations }).apply()
57+
58+
expect(applyResp.length).toEqual(1)
59+
expect(applyResp[0]?.name).toEqual('100000000000000_add_logs_table.sql')
60+
61+
expect(
62+
(
63+
await env.DB.prepare(`SELECT name
64+
FROM sqlite_master
65+
WHERE type = 'table'
66+
AND name <> 'sqlite_sequence'`).all()
67+
).results
68+
).toEqual([
69+
{
70+
name: 'migrations',
71+
},
72+
{
73+
name: 'logs',
74+
},
75+
])
76+
77+
const applyResp2 = await qb.migrations({ migrations }).apply()
78+
expect(applyResp2.length).toEqual(0)
79+
})
80+
81+
it('incremental migrations', async () => {
82+
const qb = new D1QB(env.DB)
83+
84+
expect(
85+
(
86+
await env.DB.prepare(`SELECT name
87+
FROM sqlite_master
88+
WHERE type = 'table'`).all()
89+
).results
90+
).toEqual([])
91+
92+
const applyResp = await qb.migrations({ migrations }).apply()
93+
94+
expect(applyResp.length).toEqual(1)
95+
expect(applyResp[0]?.name).toEqual('100000000000000_add_logs_table.sql')
96+
97+
expect(
98+
(
99+
await env.DB.prepare(`SELECT name
100+
FROM sqlite_master
101+
WHERE type = 'table'
102+
AND name <> 'sqlite_sequence'`).all()
103+
).results
104+
).toEqual([
105+
{
106+
name: 'migrations',
107+
},
108+
{
109+
name: 'logs',
110+
},
111+
])
112+
113+
const updatedMigrations = [
114+
...migrations,
115+
{
116+
name: '100000000000001_add_second_table.sql',
117+
sql: `
118+
create table logs_two
119+
(
120+
id INTEGER PRIMARY KEY AUTOINCREMENT,
121+
name TEXT NOT NULL
122+
);`,
123+
},
124+
]
125+
126+
const applyResp2 = await qb.migrations({ migrations: updatedMigrations }).apply()
127+
128+
expect(applyResp2.length).toEqual(1)
129+
expect(applyResp2[0]?.name).toEqual('100000000000001_add_second_table.sql')
130+
131+
const applyResp3 = await qb.migrations({ migrations: updatedMigrations }).apply()
132+
expect(applyResp3.length).toEqual(0)
133+
})
134+
})

tests/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
"moduleResolution": "bundler",
55
"types": ["@cloudflare/workers-types/experimental", "@cloudflare/vitest-pool-workers"]
66
},
7-
"include": ["./**/*.ts", "../src/env.d.ts"]
7+
"include": ["./**/*.ts", "./bindings.d.ts"]
88
}

0 commit comments

Comments
 (0)