Skip to content

Commit

Permalink
Add failing test case for cloudflare#3160
Browse files Browse the repository at this point in the history
Miniflare squashes columns with the same name, which is inconsistent with expected behavior and production where the problem seemingly was fixed.
  • Loading branch information
kossnocorp committed May 25, 2024
1 parent 81dfb17 commit 022971e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
38 changes: 32 additions & 6 deletions packages/miniflare/test/plugins/d1/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ import { Miniflare, MiniflareOptions } from "miniflare";
import { useTmp, utf8Encode } from "../../test-shared";
import { binding, getDatabase, opts, test } from "./test";

export const SCHEMA = (tableColours: string, tableKitchenSink: string) => `
export const SCHEMA = (
tableColours: string,
tableKitchenSink: string,
tablePalettes: string
) => `
CREATE TABLE ${tableColours} (id INTEGER PRIMARY KEY, name TEXT NOT NULL, rgb INTEGER NOT NULL);
CREATE TABLE ${tableKitchenSink} (id INTEGER PRIMARY KEY, int INTEGER, real REAL, text TEXT, blob BLOB);
CREATE TABLE ${tablePalettes} (id INTEGER PRIMARY KEY, name TEXT NOT NULL, colour_id INTEGER NOT NULL, FOREIGN KEY (colour_id) REFERENCES ${tableColours}(id));
INSERT INTO ${tableColours} (id, name, rgb) VALUES (1, 'red', 0xff0000);
INSERT INTO ${tableColours} (id, name, rgb) VALUES (2, 'green', 0x00ff00);
INSERT INTO ${tableColours} (id, name, rgb) VALUES (3, 'blue', 0x0000ff);
INSERT INTO ${tablePalettes} (id, name, colour_id) VALUES (1, 'Night', 3);
`;

export interface ColourRow {
Expand All @@ -32,13 +38,15 @@ test.beforeEach(async (t) => {
)}`;
const tableColours = `colours_${ns}`;
const tableKitchenSink = `kitchen_sink_${ns}`;
const tablePalettes = `palettes_${ns}`;

const db = await getDatabase(t.context.mf);
await db.exec(SCHEMA(tableColours, tableKitchenSink));
await db.exec(SCHEMA(tableColours, tableKitchenSink, tablePalettes));

t.context.db = db;
t.context.tableColours = tableColours;
t.context.tableKitchenSink = tableKitchenSink;
t.context.tablePalettes = tablePalettes;
});

function throwCause<T>(promise: Promise<T>): Promise<T> {
Expand Down Expand Up @@ -395,7 +403,7 @@ test("D1PreparedStatement: raw", async (t) => {
});

test("operations persist D1 data", async (t) => {
const { tableColours, tableKitchenSink } = t.context;
const { tableColours, tableKitchenSink, tablePalettes } = t.context;

// Create new temporary file-system persistence directory
const tmp = await useTmp(t);
Expand All @@ -405,7 +413,7 @@ test("operations persist D1 data", async (t) => {
let db = await getDatabase(mf);

// Check execute respects persist
await db.exec(SCHEMA(tableColours, tableKitchenSink));
await db.exec(SCHEMA(tableColours, tableKitchenSink, tablePalettes));
await db
.prepare(
`INSERT INTO ${tableColours} (id, name, rgb) VALUES (4, 'purple', 0xff00ff);`
Expand All @@ -431,7 +439,7 @@ test("operations persist D1 data", async (t) => {
});

test.serial("operations permit strange database names", async (t) => {
const { tableColours, tableKitchenSink } = t.context;
const { tableColours, tableKitchenSink, tablePalettes } = t.context;

// Set option, then reset after test
const id = "my/ Database";
Expand All @@ -441,7 +449,7 @@ test.serial("operations permit strange database names", async (t) => {

// Check basic operations work

await db.exec(SCHEMA(tableColours, tableKitchenSink));
await db.exec(SCHEMA(tableColours, tableKitchenSink, tablePalettes));

await db
.prepare(
Expand All @@ -453,3 +461,21 @@ test.serial("operations permit strange database names", async (t) => {
.first<Pick<ColourRow, "name">>();
t.deepEqual(result, { name: "pink" });
});

// A test case for this issue: https://github.com/cloudflare/workers-sdk/issues/3160
test.serial(
"columns with the same name arem't squashed when joining",
async (t) => {
const { tableColours, tablePalettes } = t.context;
const db = await getDatabase(t.context.mf);

const results = await db
.prepare(
`SELECT ${tableColours}.name, ${tablePalettes}.name FROM ${tableColours} JOIN ${tablePalettes} ON ${tableColours}.id = ${tablePalettes}.colour_id`
)
.raw();

const expectedResults = [["blue", "Night"]];
t.deepEqual(results, expectedResults);
}
);
1 change: 1 addition & 0 deletions packages/miniflare/test/plugins/d1/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface Context extends MiniflareTestContext {
db: D1Database;
tableColours: string;
tableKitchenSink: string;
tablePalettes: string
}

export let binding: string;
Expand Down

0 comments on commit 022971e

Please sign in to comment.