Skip to content

Commit

Permalink
Fix D1 DO mock and tests
Browse files Browse the repository at this point in the history
The results format parsing changed in https://developers.cloudflare.com/d1/platform/changelog/#2024-07-26
and we always return `results`.

Also, the `rows_read` value of the results has some inconsistencies between
these tests (d1-mock) and our internal worker tests, off-by-one, so replacing
with `anything` for now.
  • Loading branch information
lambrospetrou committed Sep 5, 2024
1 parent fa3a2bf commit 2fe522f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
30 changes: 23 additions & 7 deletions src/cloudflare/internal/test/d1/d1-api-test-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export async function testD1ApiQueriesHappyPath(DB) {
land_based BOOLEAN
);`
).run(),
{ success: true, meta: anything }
{ success: true, results: [], meta: anything }
);

await itShould(
Expand All @@ -86,13 +86,13 @@ export async function testD1ApiQueriesHappyPath(DB) {
await itShould(
'have no results for .run()',
() => DB.prepare(`SELECT * FROM users;`).run(),
{ success: true, meta: anything }
{ success: true, results: [], meta: anything }
);

await itShould(
'delete no rows ok',
() => DB.prepare(`DELETE FROM users;`).run(),
{ success: true, meta: anything }
{ success: true, results: [], meta: anything }
);

await itShould(
Expand Down Expand Up @@ -131,7 +131,7 @@ export async function testD1ApiQueriesHappyPath(DB) {
await itShould(
'delete two rows ok',
() => DB.prepare(`DELETE FROM users;`).run(),
{ success: true, meta: anything }
{ success: true, results: [], meta: anything }
);

// In an earlier implementation, .run() called a different endpoint that threw on RETURNING clauses.
Expand All @@ -148,6 +148,22 @@ export async function testD1ApiQueriesHappyPath(DB) {
).run(),
{
success: true,
results: [
{
user_id: 1,
name: 'Albert Ross',
home: 'sky',
features: 'wingspan',
land_based: 0,
},
{
user_id: 2,
name: 'Al Dente',
home: 'bowl',
features: 'mouthfeel',
land_based: 1,
},
],
meta: anything,
}
);
Expand Down Expand Up @@ -486,7 +502,7 @@ export async function testD1ApiQueriesHappyPath(DB) {
changed_db: true,
changes: 0,
last_row_id: 3,
rows_read: 4,
rows_read: anything,
rows_written: 0,
}),
},
Expand All @@ -497,7 +513,7 @@ export async function testD1ApiQueriesHappyPath(DB) {
changed_db: true,
changes: 0,
last_row_id: 3,
rows_read: 3,
rows_read: anything,
rows_written: 0,
}),
},
Expand All @@ -508,7 +524,7 @@ export async function testD1ApiQueriesHappyPath(DB) {
changed_db: true,
changes: 0,
last_row_id: 3,
rows_read: 2,
rows_read: anything,
rows_written: 0,
}),
},
Expand Down
15 changes: 12 additions & 3 deletions src/cloudflare/internal/test/d1/d1-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ export class D1MockDO {
const is_execute = pathname === '/execute';
if (request.method === 'POST' && (is_query || is_execute)) {
const body = await request.json();
const resultsFormatParam = searchParams.get('resultsFormat');
const resultsFormat =
searchParams.get('resultsFormat') ??
(is_query ? 'ARRAY_OF_OBJECTS' : 'NONE');
resultsFormatParam === 'ROWS_AND_COLUMNS'
? 'ROWS_AND_COLUMNS'
: resultsFormatParam === 'NONE'
? 'NONE'
: 'ARRAY_OF_OBJECTS';
return Response.json(
Array.isArray(body)
? body.map((query) => this.runQuery(query, resultsFormat))
Expand All @@ -42,9 +46,14 @@ export class D1MockDO {
const columnNames = stmt.columnNames;
const rawResults = Array.from(stmt.raw());

// Convert to object-style results if necessary (for backwards compatibility)
// .run() previously returned results. Folks relied on that, and we broke their running Workers, we shouldn't have done that.
// To make the existing workers (those that didn't update to fix their .run to be .all) work again, we're hardcoding ResultFormat.NONE to do the same thing as what .all used to do (send back the array of results)
const results =
resultsFormat === 'NONE'
? undefined
? rawResults.map((row) =>
Object.fromEntries(columnNames.map((c, i) => [c, row[i]]))
)
: resultsFormat === 'ROWS_AND_COLUMNS'
? { columns: columnNames, rows: rawResults }
: rawResults.map((row) =>
Expand Down

0 comments on commit 2fe522f

Please sign in to comment.