I'm trying to test the repository layer of my TypeScript project using Jest. My approach is to create an in-memory Postgres DB and use it for test purpose. So I've chosen pg-mem. It does not implement any existing postgres extension and most of the functions. But it has options to mock them. I've used sequelize-typescript for DB models as ORM.
Some of my DB models contains enums. And postgres manages the enums using the pg_enum system catalog. But when I'm trying to run sequelize.sync({ force: true }), it is failing in a specific query and complaining RelationNotFound [Error]: relation "pg_enum" does not exist.
The failing query:
select
t.typname enum_name,
array_agg(e.enumlabel order by enumsortorder) enum_value
from
pg_type t
join pg_enum e on
t.oid = e.enumtypid
join pg_catalog.pg_namespace n on
n.oid = t.typnamespace
where
n.nspname = 'public'
and t.typname = 'enum_assets_status'
group by
1;
NB: I'm reusing the existing model declaration for test cases also (created using sequelize-typescript).
How I set it up:
import { DataType, newDb } from "pg-mem";
import { Sequelize } from "sequelize-typescript";
import { modelA, modelB } from "../../database/models";
describe("test", () => {
const db = newDb();
const sequelize = new Sequelize({
dialect: "postgres",
dialectModule: db.adapters.createPg(),
models: [
modelA,
modelB
]
});
sequelize.sync({ force: true });
//////////// Test Cases //////////////
//////////////////////////////////////
//////////////////////////////////////
});
What I've already tried:
As it seems pg_enum table does not exists, I tried to create one to see if it helps in some way. Reference: this comment. (Seems I've got it wrong!!)
db.registerLanguage("plpgsql", ({ code, args, returns }) => {
return () => {
db.public.none(`CREATE TABLE pg_enum (
oid OID,
enumtypid OID,
enumsortorder NUMBER,
enumlabel TEXT
)`);
db.public.none(`INSERT INTO pg_enum ("oid",enumtypid,enumsortorder,enumlabel) VALUES
(29188,29184,2.0,'INCOMPLETE');`);
};
});
I've found a similar issue. How can I try this in a pg_enum context knowing that pg_enum table holds oid, enumtypid, enumsortorder and enumlabel column? Reference: this link
Or is there any other approach I can follow?
Thanks!