Skip to content

Commit

Permalink
Add migration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym committed Nov 9, 2024
1 parent b647499 commit 7220de0
Show file tree
Hide file tree
Showing 15 changed files with 289 additions and 6 deletions.
2 changes: 0 additions & 2 deletions .eslintignore

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"scripts": {
"build": "tsup src/index.ts --format cjs,esm --dts",
"lint": "npx @biomejs/biome check src/ tests/ || (npx @biomejs/biome check --write src/ tests/; exit 1)",
"test": "vitest run",
"test": "vitest run --root tests",
"prepare": "husky install"
},
"publishConfig": {
Expand Down
7 changes: 7 additions & 0 deletions tests/bindings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type Env = {
DB: D1Database
}

declare module 'cloudflare:test' {
interface ProvidedEnv extends Env {}
}
138 changes: 138 additions & 0 deletions tests/integration/crud.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { env } from 'cloudflare:test'
import { describe, expect, it } from 'vitest'
import { D1QB } from '../../src'

describe('Simple operations', () => {
it('all operations', async () => {
const qb = new D1QB(env.DB)

await qb
.createTable({
tableName: 'testTable',
schema: `
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
`,
})
.execute()

expect(
(
await qb
.insert({
tableName: 'testTable',
data: [
{
name: 'example',
},
{
name: 'test2',
},
],
returning: '*',
})
.execute()
).results
).toEqual([
{
id: 1,
name: 'example',
},
{
id: 2,
name: 'test2',
},
])

expect((await qb.select('testTable').all()).results).toEqual([
{
id: 1,
name: 'example',
},
{
id: 2,
name: 'test2',
},
])

expect(
(
await qb
.update({
tableName: 'testTable',
where: {
conditions: 'name = ?1',
params: ['example'],
},
data: {
name: 'newName',
},
returning: '*',
})
.execute()
).results
).toEqual([
{
id: 1,
name: 'newName',
},
])

expect(
(
await qb
.delete({
tableName: 'testTable',
where: {
conditions: 'name = ?1',
params: ['test2'],
},
returning: '*',
})
.execute()
).results
).toEqual([
{
id: 2,
name: 'test2',
},
])

expect(
(
await qb
.delete({
tableName: 'testTable',
where: {
conditions: 'name = ?1',
params: ['abc'],
},
returning: '*',
})
.execute()
).results
).toEqual([])

expect((await qb.select('testTable').all()).results).toEqual([
{
id: 1,
name: 'newName',
},
])

await qb
.dropTable({
tableName: 'testTable',
})
.execute()

expect(
(
await env.DB.prepare(`SELECT name, sql
FROM sqlite_master
WHERE type = 'table'
AND name not in ('_cf_KV', 'sqlite_sequence', '_cf_METADATA')`).all()
).results
).toEqual([])
})
})
134 changes: 134 additions & 0 deletions tests/integration/migrations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { env } from 'cloudflare:test'
import { describe, expect, it } from 'vitest'
import { D1QB, Migration } from '../../src'

export const migrations: Migration[] = [
{
name: '100000000000000_add_logs_table.sql',
sql: `
create table logs
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);`,
},
]

describe('Migrations', () => {
it('initialize', async () => {
const qb = new D1QB(env.DB)

expect(
(
await env.DB.prepare(`SELECT name
FROM sqlite_master
WHERE type = 'table'`).all()
).results
).toEqual([])

await qb.migrations({ migrations }).initialize()

expect(
(
await env.DB.prepare(`SELECT name
FROM sqlite_master
WHERE type = 'table'
AND name <> 'sqlite_sequence'`).all()
).results
).toEqual([
{
name: 'migrations',
},
])
})

it('apply', async () => {
const qb = new D1QB(env.DB)

expect(
(
await env.DB.prepare(`SELECT name
FROM sqlite_master
WHERE type = 'table'`).all()
).results
).toEqual([])

const applyResp = await qb.migrations({ migrations }).apply()

expect(applyResp.length).toEqual(1)
expect(applyResp[0]?.name).toEqual('100000000000000_add_logs_table.sql')

expect(
(
await env.DB.prepare(`SELECT name
FROM sqlite_master
WHERE type = 'table'
AND name <> 'sqlite_sequence'`).all()
).results
).toEqual([
{
name: 'migrations',
},
{
name: 'logs',
},
])

const applyResp2 = await qb.migrations({ migrations }).apply()
expect(applyResp2.length).toEqual(0)
})

it('incremental migrations', async () => {
const qb = new D1QB(env.DB)

expect(
(
await env.DB.prepare(`SELECT name
FROM sqlite_master
WHERE type = 'table'`).all()
).results
).toEqual([])

const applyResp = await qb.migrations({ migrations }).apply()

expect(applyResp.length).toEqual(1)
expect(applyResp[0]?.name).toEqual('100000000000000_add_logs_table.sql')

expect(
(
await env.DB.prepare(`SELECT name
FROM sqlite_master
WHERE type = 'table'
AND name <> 'sqlite_sequence'`).all()
).results
).toEqual([
{
name: 'migrations',
},
{
name: 'logs',
},
])

const updatedMigrations = [
...migrations,
{
name: '100000000000001_add_second_table.sql',
sql: `
create table logs_two
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);`,
},
]

const applyResp2 = await qb.migrations({ migrations: updatedMigrations }).apply()

expect(applyResp2.length).toEqual(1)
expect(applyResp2[0]?.name).toEqual('100000000000001_add_second_table.sql')

const applyResp3 = await qb.migrations({ migrations: updatedMigrations }).apply()
expect(applyResp3.length).toEqual(0)
})
})
2 changes: 1 addition & 1 deletion tests/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"moduleResolution": "bundler",
"types": ["@cloudflare/workers-types/experimental", "@cloudflare/vitest-pool-workers"]
},
"include": ["./**/*.ts", "../src/env.d.ts"]
"include": ["./**/*.ts", "./bindings.d.ts"]
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 7 additions & 1 deletion tests/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ export default defineWorkersConfig({
test: {
poolOptions: {
workers: {
wrangler: { configPath: './wrangler.toml' },
miniflare: {
compatibilityFlags: ['nodejs_compat'],
d1Databases: {
DB: '00000000-0000-0000-0000-000000000000',
},
compatibilityDate: '2024-09-10',
},
},
},
},
Expand Down
Empty file removed tests/wrangler.toml
Empty file.
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"noUncheckedIndexedAccess": true,
"noEmit": true
},
"include": ["src/*.ts", "src/**/*.ts", "tests/*.ts", "tests/**/*.ts"]
"include": ["src/*.ts", "src/**/*.ts"]
}

0 comments on commit 7220de0

Please sign in to comment.