|
| 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 | + console.log(container.getDatabase(), container.getHost(), container.getPort()); |
| 12 | + |
| 13 | + const client = new Client({ |
| 14 | + host: container.getHost(), |
| 15 | + port: container.getPort(), |
| 16 | + database: container.getDatabase(), |
| 17 | + user: container.getUsername(), |
| 18 | + ssl: false, |
| 19 | + }); |
| 20 | + |
| 21 | + await client.connect(); |
| 22 | + |
| 23 | + const result = await client.query("SELECT 1"); |
| 24 | + expect(result.rows[0]).toEqual({ "?column?": "1" }); |
| 25 | + |
| 26 | + await client.end(); |
| 27 | + await container.stop(); |
| 28 | + }); |
| 29 | + // } |
| 30 | + |
| 31 | + // uriConnect { |
| 32 | + it("should work with database URI", async () => { |
| 33 | + const container = await new CockroachDbContainer().start(); |
| 34 | + |
| 35 | + const client = new Client({ |
| 36 | + connectionString: container.getConnectionUri(), |
| 37 | + }); |
| 38 | + await client.connect(); |
| 39 | + |
| 40 | + const result = await client.query("SELECT 1"); |
| 41 | + expect(result.rows[0]).toEqual({ "?column?": "1" }); |
| 42 | + |
| 43 | + await client.end(); |
| 44 | + await container.stop(); |
| 45 | + }); |
| 46 | + // } |
| 47 | + |
| 48 | + // setDatabase { |
| 49 | + it("should set database", async () => { |
| 50 | + const container = await new CockroachDbContainer().withDatabase("custom_database").start(); |
| 51 | + |
| 52 | + const client = new Client({ |
| 53 | + host: container.getHost(), |
| 54 | + port: container.getPort(), |
| 55 | + database: container.getDatabase(), |
| 56 | + user: container.getUsername(), |
| 57 | + }); |
| 58 | + await client.connect(); |
| 59 | + |
| 60 | + const result = await client.query("SELECT current_database()"); |
| 61 | + expect(result.rows[0]).toEqual({ current_database: "custom_database" }); |
| 62 | + |
| 63 | + await client.end(); |
| 64 | + await container.stop(); |
| 65 | + }); |
| 66 | + // } |
| 67 | + |
| 68 | + // setUsername { |
| 69 | + it("should set username", async () => { |
| 70 | + const container = await new CockroachDbContainer().withUsername("custom_username").start(); |
| 71 | + |
| 72 | + const client = new Client({ |
| 73 | + host: container.getHost(), |
| 74 | + port: container.getPort(), |
| 75 | + database: container.getDatabase(), |
| 76 | + user: container.getUsername(), |
| 77 | + }); |
| 78 | + await client.connect(); |
| 79 | + |
| 80 | + const result = await client.query("SELECT current_user"); |
| 81 | + expect(result.rows[0]).toEqual({ current_user: "custom_username" }); |
| 82 | + |
| 83 | + await client.end(); |
| 84 | + await container.stop(); |
| 85 | + }); |
| 86 | + // } |
| 87 | + |
| 88 | + // workWithRestartedContainer { |
| 89 | + it("should work with restarted container", async () => { |
| 90 | + const container = await new CockroachDbContainer().start(); |
| 91 | + const port = container.getFirstMappedPort(); |
| 92 | + await container.restart(); |
| 93 | + expect(port).not.toEqual(container.getFirstMappedPort()); |
| 94 | + |
| 95 | + const client = new Client({ |
| 96 | + host: container.getHost(), |
| 97 | + port: container.getPort(), |
| 98 | + database: container.getDatabase(), |
| 99 | + user: container.getUsername(), |
| 100 | + }); |
| 101 | + await client.connect(); |
| 102 | + |
| 103 | + const result = await client.query("SELECT 1"); |
| 104 | + expect(result.rows[0]).toEqual({ "?column?": "1" }); |
| 105 | + |
| 106 | + await client.end(); |
| 107 | + await container.stop(); |
| 108 | + }); |
| 109 | + // } |
| 110 | +}); |
0 commit comments