-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathclickhouseFactory.test.ts
More file actions
130 lines (102 loc) · 4.6 KB
/
Copy pathclickhouseFactory.test.ts
File metadata and controls
130 lines (102 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { describe, expect } from "vitest";
vi.mock("~/db.server", () => ({ prisma: {}, $replica: {} }));
import { postgresTest } from "@internal/testcontainers";
import { OrganizationDataStoresRegistry } from "~/services/dataStores/organizationDataStoresRegistry.server";
import { ClickhouseConnectionSchema } from "~/services/clickhouse/clickhouseSecretSchemas.server";
import { ClickhouseFactory } from "~/services/clickhouse/clickhouseFactory.server";
vi.setConfig({ testTimeout: 60_000 });
const TEST_URL = "https://default:password@ch-org.example.com:8443";
const TEST_URL_2 = "https://default:password@ch-other.example.com:8443";
describe("ClickHouse Factory", () => {
postgresTest(
"returns default client when org has no data store",
async ({ prisma }) => {
const registry = new OrganizationDataStoresRegistry(prisma, prisma);
await registry.loadFromDatabase();
const factory = new ClickhouseFactory(registry);
const client = await factory.getClickhouseForOrganization("org-no-store", "standard");
expect(client).toBeDefined();
}
);
postgresTest(
"returns org-specific client when a data store is configured",
async ({ prisma }) => {
const registry = new OrganizationDataStoresRegistry(prisma, prisma);
await registry.addDataStore({
key: "factory-store",
kind: "CLICKHOUSE",
organizationIds: ["org-custom"],
config: ClickhouseConnectionSchema.parse({ url: TEST_URL }),
});
await registry.loadFromDatabase();
const factory = new ClickhouseFactory(registry);
const client = await factory.getClickhouseForOrganization("org-custom", "standard");
expect(client).toBeDefined();
// Default client is a different instance from the org-specific one
const defaultClient = await factory.getClickhouseForOrganization("org-no-store", "standard");
expect(client).not.toBe(defaultClient);
}
);
postgresTest(
"two orgs sharing the same data store get the same cached client",
async ({ prisma }) => {
const registry = new OrganizationDataStoresRegistry(prisma, prisma);
await registry.addDataStore({
key: "shared-factory-store",
kind: "CLICKHOUSE",
organizationIds: ["org-shared-1", "org-shared-2"],
config: ClickhouseConnectionSchema.parse({ url: TEST_URL }),
});
await registry.loadFromDatabase();
const factory = new ClickhouseFactory(registry);
const client1 = await factory.getClickhouseForOrganization("org-shared-1", "standard");
const client2 = await factory.getClickhouseForOrganization("org-shared-2", "standard");
// Same hostname → same cached client instance
expect(client1).toBe(client2);
}
);
postgresTest(
"two data stores with different URLs produce different clients",
async ({ prisma }) => {
const registry = new OrganizationDataStoresRegistry(prisma, prisma);
await registry.addDataStore({
key: "store-a",
kind: "CLICKHOUSE",
organizationIds: ["org-a"],
config: ClickhouseConnectionSchema.parse({ url: TEST_URL }),
});
await registry.addDataStore({
key: "store-b",
kind: "CLICKHOUSE",
organizationIds: ["org-b"],
config: ClickhouseConnectionSchema.parse({ url: TEST_URL_2 }),
});
await registry.loadFromDatabase();
const factory = new ClickhouseFactory(registry);
const clientA = await factory.getClickhouseForOrganization("org-a", "standard");
const clientB = await factory.getClickhouseForOrganization("org-b", "standard");
expect(clientA).not.toBe(clientB);
}
);
postgresTest(
"after reload with a deleted store, org falls back to default",
async ({ prisma }) => {
const registry = new OrganizationDataStoresRegistry(prisma, prisma);
await registry.addDataStore({
key: "removable-store",
kind: "CLICKHOUSE",
organizationIds: ["org-removable"],
config: ClickhouseConnectionSchema.parse({ url: TEST_URL }),
});
await registry.loadFromDatabase();
const factory = new ClickhouseFactory(registry);
const before = await factory.getClickhouseForOrganization("org-removable", "standard");
const defaultClient = await factory.getClickhouseForOrganization("org-no-store", "standard");
expect(before).not.toBe(defaultClient);
await registry.deleteDataStore({ key: "removable-store", kind: "CLICKHOUSE" });
await registry.reload();
const after = await factory.getClickhouseForOrganization("org-removable", "standard");
expect(after).toBe(defaultClient);
}
);
});