|
| 1 | +import { Client } from "pg"; |
| 2 | +import { CockroachDbContainer } from "./cockroachdb-container"; |
| 3 | + |
| 4 | +describe("CockroachDbContainer", () => { |
| 5 | + jest.setTimeout(180_000); |
| 6 | + |
| 7 | + // connect { |
| 8 | + it("should connect and return a query result", async () => { |
| 9 | + const container = await new CockroachDbContainer().start(); |
| 10 | + |
| 11 | + const client = new Client({ |
| 12 | + host: container.getHost(), |
| 13 | + port: container.getPort(), |
| 14 | + database: container.getDatabase(), |
| 15 | + user: container.getUsername(), |
| 16 | + ssl: false, |
| 17 | + }); |
| 18 | + |
| 19 | + await client.connect(); |
| 20 | + |
| 21 | + const result = await client.query("SELECT 1"); |
| 22 | + expect(result.rows[0]).toEqual({ "?column?": "1" }); |
| 23 | + |
| 24 | + await client.end(); |
| 25 | + await container.stop(); |
| 26 | + }); |
| 27 | + // } |
| 28 | + |
| 29 | + // uriConnect { |
| 30 | + it("should work with database URI", async () => { |
| 31 | + const container = await new CockroachDbContainer().start(); |
| 32 | + |
| 33 | + const client = new Client({ |
| 34 | + connectionString: container.getConnectionUri(), |
| 35 | + }); |
| 36 | + await client.connect(); |
| 37 | + |
| 38 | + const result = await client.query("SELECT 1"); |
| 39 | + expect(result.rows[0]).toEqual({ "?column?": "1" }); |
| 40 | + |
| 41 | + await client.end(); |
| 42 | + await container.stop(); |
| 43 | + }); |
| 44 | + // } |
| 45 | + |
| 46 | + // setDatabase { |
| 47 | + it("should set database", async () => { |
| 48 | + const container = await new CockroachDbContainer().withDatabase("custom_database").start(); |
| 49 | + |
| 50 | + const client = new Client({ |
| 51 | + host: container.getHost(), |
| 52 | + port: container.getPort(), |
| 53 | + database: container.getDatabase(), |
| 54 | + user: container.getUsername(), |
| 55 | + }); |
| 56 | + await client.connect(); |
| 57 | + |
| 58 | + const result = await client.query("SELECT current_database()"); |
| 59 | + expect(result.rows[0]).toEqual({ current_database: "custom_database" }); |
| 60 | + |
| 61 | + await client.end(); |
| 62 | + await container.stop(); |
| 63 | + }); |
| 64 | + // } |
| 65 | + |
| 66 | + // setUsername { |
| 67 | + it("should set username", async () => { |
| 68 | + const container = await new CockroachDbContainer().withUsername("custom_username").start(); |
| 69 | + |
| 70 | + const client = new Client({ |
| 71 | + host: container.getHost(), |
| 72 | + port: container.getPort(), |
| 73 | + database: container.getDatabase(), |
| 74 | + user: container.getUsername(), |
| 75 | + }); |
| 76 | + await client.connect(); |
| 77 | + |
| 78 | + const result = await client.query("SELECT current_user"); |
| 79 | + expect(result.rows[0]).toEqual({ current_user: "custom_username" }); |
| 80 | + |
| 81 | + await client.end(); |
| 82 | + await container.stop(); |
| 83 | + }); |
| 84 | + // } |
| 85 | + |
| 86 | + it("should work with restarted container", async () => { |
| 87 | + const container = await new CockroachDbContainer().start(); |
| 88 | + await container.restart(); |
| 89 | + |
| 90 | + const client = new Client({ |
| 91 | + host: container.getHost(), |
| 92 | + port: container.getPort(), |
| 93 | + database: container.getDatabase(), |
| 94 | + user: container.getUsername(), |
| 95 | + }); |
| 96 | + await client.connect(); |
| 97 | + |
| 98 | + const result = await client.query("SELECT 1"); |
| 99 | + expect(result.rows[0]).toEqual({ "?column?": "1" }); |
| 100 | + |
| 101 | + await client.end(); |
| 102 | + await container.stop(); |
| 103 | + }); |
| 104 | + |
| 105 | + it("should allow custom healthcheck", async () => { |
| 106 | + const container = new CockroachDbContainer().withHealthCheck({ |
| 107 | + test: ["CMD-SHELL", "exit 1"], |
| 108 | + interval: 100, |
| 109 | + retries: 0, |
| 110 | + timeout: 0, |
| 111 | + }); |
| 112 | + |
| 113 | + await expect(() => container.start()).rejects.toThrow(); |
| 114 | + }); |
| 115 | +}); |
0 commit comments