|
| 1 | +import oracledb from "oracledb"; |
| 2 | +import { getImage } from "../../../testcontainers/src/utils/test-helper"; |
| 3 | +import { OracleDbContainer, StartedOracleDbContainer } from "./oraclefree-container"; |
| 4 | + |
| 5 | +const IMAGE = getImage(__dirname); |
| 6 | + |
| 7 | +describe("OracleFreeContainer", { timeout: 240_000 }, () => { |
| 8 | + describe("default configuration", () => { |
| 9 | + let container: StartedOracleDbContainer; |
| 10 | + |
| 11 | + // start one container for all tests in this block to save on resources |
| 12 | + beforeAll(async () => { |
| 13 | + container = await new OracleDbContainer(IMAGE).start(); |
| 14 | + }, 120_000); |
| 15 | + |
| 16 | + afterAll(async () => { |
| 17 | + await container.stop(); |
| 18 | + }); |
| 19 | + |
| 20 | + it("should connect and return a query result", async () => { |
| 21 | + const connection = await oracledb.getConnection({ |
| 22 | + user: container.getUsername(), |
| 23 | + password: container.getPassword(), |
| 24 | + connectString: container.getUrl(), |
| 25 | + }); |
| 26 | + |
| 27 | + const result = await connection.execute("SELECT 1 FROM DUAL"); |
| 28 | + expect(result.rows![0]).toEqual([1]); |
| 29 | + |
| 30 | + await connection.close(); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should work with connection descriptor", async () => { |
| 34 | + const connection = await oracledb.getConnection({ |
| 35 | + user: container.getUsername(), |
| 36 | + password: container.getPassword(), |
| 37 | + connectString: container.getConnectionDescriptor(), |
| 38 | + }); |
| 39 | + |
| 40 | + const result = await connection.execute("SELECT 1 FROM DUAL"); |
| 41 | + expect(result.rows![0]).toEqual([1]); |
| 42 | + |
| 43 | + await connection.close(); |
| 44 | + }); |
| 45 | + |
| 46 | + it("should have default database name", async () => { |
| 47 | + const connection = await oracledb.getConnection({ |
| 48 | + user: container.getUsername(), |
| 49 | + password: container.getPassword(), |
| 50 | + connectString: container.getUrl(), |
| 51 | + }); |
| 52 | + |
| 53 | + const result = await connection.execute("SELECT SYS_CONTEXT('USERENV', 'CON_NAME') FROM DUAL"); |
| 54 | + expect(result.rows![0]).toEqual(["FREEPDB1"]); |
| 55 | + |
| 56 | + await connection.close(); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it("should treat default database names as no-op and reject empty names", () => { |
| 61 | + const container = new OracleDbContainer(IMAGE); |
| 62 | + expect(() => container.withDatabase("FREEPDB1")).not.toThrow(); |
| 63 | + expect(() => container.withDatabase("freepdb1")).not.toThrow(); |
| 64 | + expect(() => container.withDatabase("")).toThrow("Database name cannot be empty."); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should set the custom database and user", async () => { |
| 68 | + // customDatabase { |
| 69 | + const customDatabase = "TESTDB"; |
| 70 | + const customUsername = "CUSTOMUSER"; |
| 71 | + const customPassword = "customPassword"; |
| 72 | + await using container = await new OracleDbContainer(IMAGE) |
| 73 | + .withDatabase(customDatabase) |
| 74 | + .withUsername(customUsername) |
| 75 | + .withPassword(customPassword) |
| 76 | + .start(); |
| 77 | + |
| 78 | + const connection = await oracledb.getConnection({ |
| 79 | + user: container.getUsername(), |
| 80 | + password: container.getPassword(), |
| 81 | + connectString: container.getUrl(), |
| 82 | + }); |
| 83 | + |
| 84 | + const result = await connection.execute("SELECT SYS_CONTEXT('USERENV', 'CON_NAME') FROM DUAL"); |
| 85 | + expect(result.rows![0]).toEqual([customDatabase]); |
| 86 | + |
| 87 | + const resultUser = await connection.execute("SELECT USER FROM DUAL"); |
| 88 | + expect(resultUser.rows![0]).toEqual([customUsername]); |
| 89 | + |
| 90 | + await connection.close(); |
| 91 | + // } |
| 92 | + }); |
| 93 | + |
| 94 | + it("should work with restarted container", async () => { |
| 95 | + const container = await new OracleDbContainer(IMAGE).start(); |
| 96 | + await container.restart(); |
| 97 | + |
| 98 | + const connection = await oracledb.getConnection({ |
| 99 | + user: container.getUsername(), |
| 100 | + password: container.getPassword(), |
| 101 | + connectString: container.getUrl(), |
| 102 | + }); |
| 103 | + |
| 104 | + const result = await connection.execute("SELECT 1 FROM DUAL"); |
| 105 | + expect(result.rows![0]).toEqual([1]); |
| 106 | + |
| 107 | + await connection.close(); |
| 108 | + }); |
| 109 | +}); |
0 commit comments