You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: Add unit and integration tests for documented features
This commit introduces new tests to cover features and scenarios highlighted in the documentation, ensuring robustness and correctness of the library's behavior.
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.
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 and standardized migration definitions.
.whereIn('role_id', adminRoleIdsSubquery) // Subquery for IN clause
343
+
.execute();
344
+
345
+
console.log('Users who are admins:', usersWhoAreAdmins.results);
277
346
```
278
347
279
-
**Important:** Note the corrected example using `whereIn` method on the `SelectBuilder` for IN clauses, instead of trying to put a subquery directly into the `where` option, which is not intended for `IN` clause subqueries in this builder's API design.
348
+
**Important:**When using `whereIn`, provide either an array of values or a `SelectBuilder`instance for a subquery. Do not pass the result of `getQueryAll()` from a subquery directly into the `where` option if you intend to create an `IN` clause; use the `whereIn` method with the `SelectBuilder` instance itself.
280
349
281
350
## Group By and Having
282
351
@@ -287,7 +356,12 @@ Use the `groupBy` method to group rows with the same values in one or more colum
287
356
```typescript
288
357
import { D1QB } from'workers-qb';
289
358
290
-
// ... (D1QB initialization) ...
359
+
// Example D1QB initialization:
360
+
// interface Env {
361
+
// DB: D1Database;
362
+
// }
363
+
// const env: Env = /* your environment object */;
364
+
// const qb = new D1QB(env.DB);
291
365
292
366
typeRoleUserCount= {
293
367
roleName:string;
@@ -315,7 +389,12 @@ The `having` method filters groups based on aggregate functions, similar to WHER
315
389
```typescript
316
390
import { D1QB } from'workers-qb';
317
391
318
-
// ... (D1QB initialization) ...
392
+
// Example D1QB initialization:
393
+
// interface Env {
394
+
// DB: D1Database;
395
+
// }
396
+
// const env: Env = /* your environment object */;
397
+
// const qb = new D1QB(env.DB);
319
398
320
399
typeRoleUserCount= {
321
400
roleName:string;
@@ -346,7 +425,12 @@ Use the `orderBy` method to sort the result set by one or more columns. By defau
346
425
```typescript
347
426
import { D1QB } from'workers-qb';
348
427
349
-
// ... (D1QB initialization) ...
428
+
// Example D1QB initialization:
429
+
// interface Env {
430
+
// DB: D1Database;
431
+
// }
432
+
// const env: Env = /* your environment object */;
433
+
// const qb = new D1QB(env.DB);
350
434
351
435
typeUser= {
352
436
id:number;
@@ -369,7 +453,19 @@ Specify the sorting direction (ASC for ascending, DESC for descending) for each
369
453
```typescript
370
454
import { D1QB } from'workers-qb';
371
455
372
-
// ... (D1QB initialization) ...
456
+
// Example D1QB initialization:
457
+
// interface Env {
458
+
// DB: D1Database;
459
+
// }
460
+
// const env: Env = /* your environment object */;
461
+
// const qb = new D1QB(env.DB);
462
+
463
+
// Assuming User type is defined as in the previous example:
0 commit comments