@clickup/pg-microsharding
Version:
Microshards support for PostgreSQL
89 lines (77 loc) • 2.19 kB
text/typescript
import { chooseDsns } from "../chooseDsns";
let savedEnv: typeof process.env;
beforeEach(() => {
savedEnv = { ...process.env };
process.env["PGPORT"] = "";
process.env["PGUSER"] = "test";
process.env["PGPASSWORD"] = "test";
process.env["PGDATABASE"] = "test";
});
afterEach(() => {
Object.assign(process.env, savedEnv);
});
test("matches", () => {
expect(
chooseDsns({
patterns: ["ho"],
existingDsns: ["postgresql://host/db"],
requireMatch: true,
}),
).toEqual([["postgresql://test:test@host/db"], []]);
expect(
chooseDsns({
patterns: ["host2"],
existingDsns: ["postgresql://host1/db", "postgresql://host2/db"],
requireMatch: true,
}),
).toEqual([["postgresql://test:test@host2/db"], []]);
expect(
chooseDsns({
patterns: ["host/db"],
existingDsns: ["postgresql://host/db", "postgresql://host/db2"],
requireMatch: true,
}),
).toEqual([["postgresql://test:test@host/db"], []]);
});
test("no match", () => {
expect(
chooseDsns({
patterns: ["some"],
existingDsns: ["postgresql://host1/db", "postgresql://host2/db"],
requireMatch: false,
}),
).toEqual([["postgresql://test:test@some/test"], []]);
expect(() =>
chooseDsns({
patterns: ["some"],
existingDsns: ["postgresql://host1/db", "postgresql://host2/db"],
requireMatch: true,
}),
).toThrow('No DSNs match "some".');
});
test("multiple matches", () => {
expect(
chooseDsns({
patterns: ["ho"],
existingDsns: ["postgresql://host1/db", "postgresql://host2/db"],
requireMatch: false,
}),
).toEqual([
["postgresql://test:test@ho/test"],
['Hint: multiple DSNs match "ho", so treating it as an entire DSN.'],
]);
expect(() =>
chooseDsns({
patterns: ["ho"],
existingDsns: ["postgresql://host1/db", "postgresql://host2/db"],
requireMatch: true,
}),
).toThrow('Multiple DSNs match "ho".');
expect(
chooseDsns({
patterns: ["host1"],
existingDsns: ["postgresql://host1/db", "postgresql://host2/db"],
requireMatch: true,
}),
).toEqual([["postgresql://test:test@host1/db"], []]);
});