Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix D1 DO mock and tests #2663

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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