Skip to content

Commit

Permalink
Add tests for the hyperdrive binding
Browse files Browse the repository at this point in the history
  • Loading branch information
tmthecoder committed Oct 17, 2023
1 parent 476b3fa commit 0b19306
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions packages/miniflare/test/plugins/hyperdrive/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Server, createServer } from "net";
import { Hyperdrive } from "@cloudflare/workers-types/experimental";
import { MiniflareOptions, ReplaceWorkersTypes } from "miniflare";
import {
MiniflareDurableObjectControlStub,
MiniflareTestContext,
Namespaced,
miniflareTest,
namespace,
} from "../../test-shared";

interface Context extends MiniflareTestContext {
ns: string;
hyperdrive: Namespaced<ReplaceWorkersTypes<Hyperdrive>>;
object: MiniflareDurableObjectControlStub;
}

const TEST_CONN_STRING = `postgresql://user:password@localhost:5432/database`;

const opts: Partial<MiniflareOptions> = {
hyperdrives: {
hyperdrive: TEST_CONN_STRING,
},
};

const test = miniflareTest<unknown, Context>(opts, async (global) => {
return new global.Response(null, { status: 404 });
});

test.beforeEach(async (t) => {
const ns = `${Date.now()}_${Math.floor(
Math.random() * Number.MAX_SAFE_INTEGER
)}`;
t.context.ns = ns;
t.context.hyperdrive = namespace(
ns,
await t.context.mf.getHyperdrive("hyperdrive")
);
});

test("connectionString: different from configuration", async (t) => {
const hyperdrive = t.context.hyperdrive;
t.not(hyperdrive.connectionString, TEST_CONN_STRING);
});

test("user: matches configuration", async (t) => {
const hyperdrive = t.context.hyperdrive;
t.is(hyperdrive.user, "user");
});

test("password: matches configuration", async (t) => {
const hyperdrive = t.context.hyperdrive;
t.is(hyperdrive.password, "password");
});

test("database: matches configuration", async (t) => {
const hyperdrive = t.context.hyperdrive;
t.is(hyperdrive.database, "database");
});

test("host: randomized", async (t) => {
const hyperdrive = t.context.hyperdrive;
t.not(hyperdrive.host, "localhost");
});

0 comments on commit 0b19306

Please sign in to comment.