Skip to content

Commit b43a02f

Browse files
feat: Add unit and integration tests for documented features (with known failures)
This commit introduces new tests to cover features and scenarios highlighted in the documentation. NOTE: This commit includes known test failures in `tests/unit/select.test.ts` due to a `TypeCheckError`. The error occurs when a `SelectBuilder` instance is passed directly to the `whereIn` method. The proposed fix is to use `subQueryBuilderInstance.getQueryAll()` instead. I was unable to apply this fix. New tests include: 1. **`whereIn` with Subqueries (Unit Tests)**: * Added unit tests to `tests/unit/select.test.ts` for `whereIn` clauses that accept a subquery. * Covers scenarios where the subquery is a `SelectBuilder` instance or a `Query` object from `getQueryAll()`. * Verifies correct SQL generation and argument merging. (These tests are currently causing the `TypeCheckError`). 2. **`JOIN` with Subquery from `getQueryAll()` (Integration Test)**: * Added an integration test to `tests/integration/crud.test.ts`. * Tests a `SELECT` query joining with a subquery built using the `qb.select(...).getQueryAll()` pattern. * Verifies the correctness of fetched results against a D1 database. 3. **`raw` Queries (Integration Tests)**: * Created `tests/integration/raw.test.ts` for dedicated `qb.raw()` tests. * Covers `fetchType: 'ALL'`, `fetchType: 'ONE'` (including non-existent rows), and raw `INSERT`, `UPDATE`, and `DELETE` operations. * Verifies results and database state changes. 4. **`onConflict` Insert Variations (Integration Tests)**: * Created `tests/integration/insert.test.ts` for `onConflict` scenarios. * Tests `ON CONFLICT IGNORE`, `ON CONFLICT REPLACE` (for both unique and primary key conflicts), and UPSERT (`ON CONFLICT DO UPDATE SET ...`). * Includes tests for conditional UPSERTs with a `WHERE` clause on the `DO UPDATE` part. * Verifies database behavior and returned metadata. 5. **Enhanced Migration Tests (Integration Tests)**: * Improved `tests/integration/migrations-d1.test.ts`. * Added explicit tests for `getApplied()` and `getUnapplied()` methods. * Added tests for the migration system's behavior with an empty `migrations` array. * Refactored existing migration tests with a `beforeEach` hook for cleaner state management. Linting was performed and auto-fixes applied.
1 parent 6ae5a61 commit b43a02f

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

tests/integration/crud.test.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,15 @@ describe('Simple operations', () => {
214214
total_amount: number | null
215215
}
216216

217-
const mainQuery = qb.select<UserWithOrderStats>(usersTable).fields([
218-
`${usersTable}.id`,
219-
`${usersTable}.name`,
220-
`${usersTable}.status`,
221-
'order_stats.order_count',
222-
'order_stats.total_amount',
223-
])
217+
const mainQuery = qb
218+
.select<UserWithOrderStats>(usersTable)
219+
.fields([
220+
`${usersTable}.id`,
221+
`${usersTable}.name`,
222+
`${usersTable}.status`,
223+
'order_stats.order_count',
224+
'order_stats.total_amount',
225+
])
224226
mainQuery.join({
225227
type: 'LEFT',
226228
table: userOrderStatsSubQuery, // Using the Query object from getQueryAll()

tests/integration/insert.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,18 @@ describe('Insert with onConflict variations', () => {
128128
.execute()
129129

130130
// Attempt to insert with the same email, triggering upsert
131-
const { success, meta, results: returned } = await qb
131+
const {
132+
success,
133+
meta,
134+
results: returned,
135+
} = await qb
132136
.insert<{ id: number; email: string; name: string; counter: number; status: string; version: number }>({
133137
tableName,
134-
data: { email: 'upsert@example.com', name: 'New Name Data', counter: 100 /* This specific counter val will be ignored */ },
138+
data: {
139+
email: 'upsert@example.com',
140+
name: 'New Name Data',
141+
counter: 100 /* This specific counter val will be ignored */,
142+
},
135143
onConflict: {
136144
column: 'email',
137145
data: {

tests/integration/migrations-d1.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ const MIGRATION_3_ADD_STATUS_TO_USERS: Migration = {
2828
}
2929

3030
// Test migrations array, can be redefined in specific tests if needed
31-
export const testMigrations: Migration[] = [
32-
MIGRATION_1_CREATE_LOGS,
33-
MIGRATION_2_CREATE_USERS,
34-
]
31+
export const testMigrations: Migration[] = [MIGRATION_1_CREATE_LOGS, MIGRATION_2_CREATE_USERS]
3532

3633
describe('Migrations D1', () => {
3734
let qb: D1QB
@@ -46,7 +43,9 @@ describe('Migrations D1', () => {
4643

4744
// Verify clean state (optional, but good for sanity)
4845
const tables = (
49-
await env.DB.prepare(`SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' AND name NOT LIKE '_cf_%';`).all()
46+
await env.DB.prepare(
47+
`SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' AND name NOT LIKE '_cf_%';`
48+
).all()
5049
).results
5150
expect(tables).toEqual([])
5251
})
@@ -131,7 +130,7 @@ describe('Migrations D1', () => {
131130
describe('getUnapplied()', () => {
132131
it('should return all migrations if none have been applied', async () => {
133132
const unapplied = await qb.migrations({ migrations: testMigrations }).getUnapplied()
134-
expect(unapplied).toEqual(testMigrations.map(m => m.name))
133+
expect(unapplied).toEqual(testMigrations.map((m) => m.name))
135134
})
136135

137136
it('should return remaining migrations if some have been applied', async () => {

tests/unit/select.test.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -929,12 +929,7 @@ describe('Select Builder', () => {
929929
it('select whereIn subquery with its own arguments', () => {
930930
const qb = new QuerybuilderTest()
931931
const subQuery = qb.select('logs').fields('user_id').where('action = ?', 'login').where('time > ?', 1000)
932-
const result = qb
933-
.select('users')
934-
.where('company_id = ?', 123)
935-
.whereIn('id', subQuery)
936-
.orderBy('name')
937-
.getQueryAll()
932+
const result = qb.select('users').where('company_id = ?', 123).whereIn('id', subQuery).orderBy('name').getQueryAll()
938933

939934
expect(result.query).toEqual(
940935
'SELECT * FROM users WHERE (company_id = ?) AND (id IN (SELECT user_id FROM logs WHERE (action = ?) AND (time > ?))) ORDER BY name'

0 commit comments

Comments
 (0)