Test foreign keys

This commit is contained in:
Adria Navarro 2023-06-21 10:53:32 +01:00
parent 12f2a1ad4c
commit bedddc15aa
1 changed files with 38 additions and 23 deletions

View File

@ -1,22 +1,21 @@
import { GenericContainer } from "testcontainers"
import { GenericContainer, StartedTestContainer } from "testcontainers"
import postgres from "../../../../packages/server/src/integrations/postgres"
jest.unmock("pg")
describe("getExternalSchema", () => {
describe("postgres", () => {
let host: string
let port: number
let config: any
let container: StartedTestContainer
beforeAll(async () => {
const container = await new GenericContainer("postgres")
container = await new GenericContainer("postgres")
.withExposedPorts(5432)
.withEnv("POSTGRES_PASSWORD", "password")
.start()
host = container.getContainerIpAddress()
port = container.getMappedPort(5432)
const host = container.getContainerIpAddress()
const port = container.getMappedPort(5432)
config = {
host,
@ -30,6 +29,8 @@ describe("getExternalSchema", () => {
}
})
afterAll(() => container.stop())
it("can export an empty database", async () => {
const integration = new postgres.integration(config)
const result = await integration.getExternalSchema()
@ -60,10 +61,11 @@ describe("getExternalSchema", () => {
`)
})
it("can export a database with tables", async () => {
it.only("can export a database with tables", async () => {
const integration = new postgres.integration(config)
await integration.internalQuery({
await integration.internalQuery(
{
sql: `
CREATE TABLE "users" (
"id" SERIAL,
@ -75,9 +77,13 @@ describe("getExternalSchema", () => {
"id" SERIAL,
"name" VARCHAR(100) NOT NULL,
"price" DECIMAL NOT NULL,
"owner" INTEGER NULL,
PRIMARY KEY ("id")
);`,
})
);
ALTER TABLE "products" ADD CONSTRAINT "fk_owner" FOREIGN KEY ("owner") REFERENCES "users" ("id");`,
},
false
)
const result = await integration.getExternalSchema()
expect(result).toMatchInlineSnapshot(`
@ -110,7 +116,8 @@ describe("getExternalSchema", () => {
CREATE TABLE public.products (
id integer NOT NULL,
name character varying(100) NOT NULL,
price numeric NOT NULL
price numeric NOT NULL,
owner integer
);
@ -203,6 +210,14 @@ describe("getExternalSchema", () => {
ADD CONSTRAINT users_pkey PRIMARY KEY (id);
--
-- Name: products fk_owner; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.products
ADD CONSTRAINT fk_owner FOREIGN KEY (owner) REFERENCES public.users(id);
--
-- PostgreSQL database dump complete
--