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" import postgres from "../../../../packages/server/src/integrations/postgres"
jest.unmock("pg") jest.unmock("pg")
describe("getExternalSchema", () => { describe("getExternalSchema", () => {
describe("postgres", () => { describe("postgres", () => {
let host: string
let port: number
let config: any let config: any
let container: StartedTestContainer
beforeAll(async () => { beforeAll(async () => {
const container = await new GenericContainer("postgres") container = await new GenericContainer("postgres")
.withExposedPorts(5432) .withExposedPorts(5432)
.withEnv("POSTGRES_PASSWORD", "password") .withEnv("POSTGRES_PASSWORD", "password")
.start() .start()
host = container.getContainerIpAddress() const host = container.getContainerIpAddress()
port = container.getMappedPort(5432) const port = container.getMappedPort(5432)
config = { config = {
host, host,
@ -30,6 +29,8 @@ describe("getExternalSchema", () => {
} }
}) })
afterAll(() => container.stop())
it("can export an empty database", async () => { it("can export an empty database", async () => {
const integration = new postgres.integration(config) const integration = new postgres.integration(config)
const result = await integration.getExternalSchema() 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) const integration = new postgres.integration(config)
await integration.internalQuery({ await integration.internalQuery(
{
sql: ` sql: `
CREATE TABLE "users" ( CREATE TABLE "users" (
"id" SERIAL, "id" SERIAL,
@ -75,9 +77,13 @@ describe("getExternalSchema", () => {
"id" SERIAL, "id" SERIAL,
"name" VARCHAR(100) NOT NULL, "name" VARCHAR(100) NOT NULL,
"price" DECIMAL NOT NULL, "price" DECIMAL NOT NULL,
"owner" INTEGER NULL,
PRIMARY KEY ("id") PRIMARY KEY ("id")
);`, );
}) ALTER TABLE "products" ADD CONSTRAINT "fk_owner" FOREIGN KEY ("owner") REFERENCES "users" ("id");`,
},
false
)
const result = await integration.getExternalSchema() const result = await integration.getExternalSchema()
expect(result).toMatchInlineSnapshot(` expect(result).toMatchInlineSnapshot(`
@ -110,7 +116,8 @@ describe("getExternalSchema", () => {
CREATE TABLE public.products ( CREATE TABLE public.products (
id integer NOT NULL, id integer NOT NULL,
name character varying(100) 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); 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 -- PostgreSQL database dump complete
-- --