import { assert } from "chai"; import { describe, it, beforeEach, afterEach } from "node:test"; import * as m from "../index"; import * as config from "./config"; import mockModule from "./mock"; describe("CADInterfaceMetaData", function() { let models: m.BackendModels, mongoose: m.MongooseModule; let testItem: Partial; beforeEach(async function() { const c = await m.connect(config.url); models = c.models; mongoose = c.mongoose; const mock = mockModule({ mongoose }); testItem = { systemType: "Tritech Inform" }; await mock.beforeEach(); }); afterEach(async function() { await mongoose.disconnect(); }); it("is saved", async function() { const item = new models.CADInterfaceMetaData(testItem); const sut = await item.save(); assert.isNotNull(sut._id); assert.equal(sut.systemType, testItem.systemType); assert.isNotNull(sut.createdAt); assert.isNotNull(sut.updatedAt); }); it("enforces unique systemType index", async function() { const item1 = new models.CADInterfaceMetaData(testItem); await item1.save(); const item2 = new models.CADInterfaceMetaData({ systemType: testItem.systemType }); try { await item2.save(); assert.fail("Should have failed unique index constraint"); } catch (err: any) { assert.isNotNull(err); // Mongo unique index error code is 11000 assert.equal(err.code, 11000); } }); });