|
| 1 | +import { createClient } from "@clickhouse/client"; |
| 2 | +import { ClickHouseContainer } from "./clickhouse-container"; |
| 3 | + |
| 4 | +interface ClickHouseQueryResponse<T> { |
| 5 | + data: T[]; |
| 6 | +} |
| 7 | + |
| 8 | +describe("ClickHouseContainer", { timeout: 180_000 }, () => { |
| 9 | + // connectWithOptions { |
| 10 | + it("should connect using the client options object", async () => { |
| 11 | + const container = await new ClickHouseContainer().start(); |
| 12 | + const client = createClient(container.getClientOptions()); |
| 13 | + |
| 14 | + const result = await client.query({ |
| 15 | + query: "SELECT 1 AS value", |
| 16 | + format: "JSON", |
| 17 | + }); |
| 18 | + const data = (await result.json()) as ClickHouseQueryResponse<{ value: number }>; |
| 19 | + expect(data?.data?.[0]?.value).toBe(1); |
| 20 | + |
| 21 | + await client.close(); |
| 22 | + await container.stop(); |
| 23 | + }); |
| 24 | + // } |
| 25 | + |
| 26 | + // connectWithUrl { |
| 27 | + it("should connect using the URL", async () => { |
| 28 | + const container = await new ClickHouseContainer().start(); |
| 29 | + const client = createClient({ |
| 30 | + url: container.getConnectionUrl(), |
| 31 | + }); |
| 32 | + |
| 33 | + const result = await client.query({ |
| 34 | + query: "SELECT 1 AS value", |
| 35 | + format: "JSON", |
| 36 | + }); |
| 37 | + |
| 38 | + const data = (await result.json()) as ClickHouseQueryResponse<{ value: number }>; |
| 39 | + expect(data?.data?.[0]?.value).toBe(1); |
| 40 | + |
| 41 | + await client.close(); |
| 42 | + await container.stop(); |
| 43 | + }); |
| 44 | + // } |
| 45 | + |
| 46 | + // connectWithUsernameAndPassword { |
| 47 | + it("should connect using the username and password", async () => { |
| 48 | + const container = await new ClickHouseContainer() |
| 49 | + .withUsername("customUsername") |
| 50 | + .withPassword("customPassword") |
| 51 | + .start(); |
| 52 | + |
| 53 | + const client = createClient({ |
| 54 | + url: container.getHttpUrl(), |
| 55 | + username: container.getUsername(), |
| 56 | + password: container.getPassword(), |
| 57 | + }); |
| 58 | + |
| 59 | + const result = await client.query({ |
| 60 | + query: "SELECT 1 AS value", |
| 61 | + format: "JSON", |
| 62 | + }); |
| 63 | + |
| 64 | + const data = (await result.json()) as ClickHouseQueryResponse<{ value: number }>; |
| 65 | + expect(data?.data?.[0]?.value).toBe(1); |
| 66 | + |
| 67 | + await client.close(); |
| 68 | + await container.stop(); |
| 69 | + }); |
| 70 | + // } |
| 71 | + |
| 72 | + // setDatabase { |
| 73 | + it("should set database", async () => { |
| 74 | + const customDatabase = "customDatabase"; |
| 75 | + const container = await new ClickHouseContainer().withDatabase(customDatabase).start(); |
| 76 | + |
| 77 | + const client = createClient(container.getClientOptions()); |
| 78 | + |
| 79 | + const result = await client.query({ |
| 80 | + query: "SELECT currentDatabase() AS current_database", |
| 81 | + format: "JSON", |
| 82 | + }); |
| 83 | + |
| 84 | + const data = (await result.json()) as ClickHouseQueryResponse<{ current_database: string }>; |
| 85 | + expect(data?.data?.[0]?.current_database).toBe(customDatabase); |
| 86 | + |
| 87 | + await client.close(); |
| 88 | + await container.stop(); |
| 89 | + }); |
| 90 | + // } |
| 91 | + |
| 92 | + // setUsername { |
| 93 | + it("should set username", async () => { |
| 94 | + const customUsername = "customUsername"; |
| 95 | + const container = await new ClickHouseContainer().withUsername(customUsername).start(); |
| 96 | + |
| 97 | + const client = createClient(container.getClientOptions()); |
| 98 | + |
| 99 | + const result = await client.query({ |
| 100 | + query: "SELECT currentUser() AS current_user", |
| 101 | + format: "JSON", |
| 102 | + }); |
| 103 | + |
| 104 | + const data = (await result.json()) as ClickHouseQueryResponse<{ current_user: string }>; |
| 105 | + expect(data?.data?.[0]?.current_user).toBe(customUsername); |
| 106 | + |
| 107 | + await client.close(); |
| 108 | + await container.stop(); |
| 109 | + }); |
| 110 | + // } |
| 111 | + |
| 112 | + it("should work with restarted container", async () => { |
| 113 | + const container = await new ClickHouseContainer().start(); |
| 114 | + await container.restart(); |
| 115 | + |
| 116 | + const client = createClient(container.getClientOptions()); |
| 117 | + |
| 118 | + const result = await client.query({ |
| 119 | + query: "SELECT 1 AS value", |
| 120 | + format: "JSON", |
| 121 | + }); |
| 122 | + |
| 123 | + const data = (await result.json()) as ClickHouseQueryResponse<{ value: number }>; |
| 124 | + expect(data?.data?.[0]?.value).toBe(1); |
| 125 | + |
| 126 | + await client.close(); |
| 127 | + await container.stop(); |
| 128 | + }); |
| 129 | +}); |
0 commit comments