UNPKG

@webda/core

Version:

Expose API with Lambda

160 lines 5.81 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; import { suite, test } from "@testdeck/mocha"; import * as assert from "assert"; import * as sinon from "sinon"; import { WebdaTest } from "../test.js"; import MapperService from "./mapper.js"; let MapperTest = class MapperTest extends WebdaTest { async cov() { let service = this.getService("MemoryIdentsMapper"); await service.recompute(); let identStore = this.getService("MemoryIdents"); let ident = new identStore._model(); identStore.newModel(ident); assert.notStrictEqual(ident.getUuid(), undefined); // Test guard-rails (seems hardly reachable so might be useless) assert.strictEqual(service.getMapper([], "id"), -1); assert.strictEqual( // @ts-ignore await service._handleUpdatedMap(ident, { idents: [] }, {}), undefined); assert.strictEqual( // @ts-ignore await service._handleDeletedMap(undefined, {}), undefined); let stb = sinon.stub(service.targetStore, "upsertItemToCollection").callsFake(async () => new Date()); await service._handleUpdatedMapMapper(ident, // @ts-ignore { idents: [], getUuid: () => "t" }, { uuid: "t" }); assert.strictEqual(stb.getCall(0).args.length, 3); await this.getService("MemoryUsers").emitSync("Store.Deleted", { // @ts-ignore object: {} }); } async n_n_mapping_array() { return this.n_n_mapping(args => args); } async n_n_mapping_object() { return this.n_n_mapping(args => { let res = {}; args.forEach(i => (res[i] = true)); return res; }); } async n_n_mapping(generatorAttribute) { const mapper = new MapperService(this.webda, "nn", { source: "MemoryIdents", target: "MemoryUsers", attribute: "test", targetAttribute: "otherIdents", fields: ["email"] }); let identStore = this.getService("MemoryIdents"); let userStore = this.getService("MemoryUsers"); this.registerService(mapper); mapper.resolve(); await mapper.init(); let user1 = await userStore.save({ uuid: "user1" }); let user2 = await userStore.save({ uuid: "user2" }); let user3 = await userStore.save({ uuid: "user3" }); let ident1 = await identStore.save({ uuid: "ident1", test: generatorAttribute(["user1", "user2"]), email: "test@webda.io" }); await user1.refresh(); await user2.refresh(); await user3.refresh(); assert.deepStrictEqual(user1.otherIdents.map(i => ({ email: i.email, uuid: i.uuid })), [ { email: "test@webda.io", uuid: "ident1" } ]); assert.deepStrictEqual(user2.otherIdents.map(i => ({ email: i.email, uuid: i.uuid })), [ { email: "test@webda.io", uuid: "ident1" } ]); assert.deepStrictEqual(user3.otherIdents.length, 0); await ident1.patch({ email: "newtest@webda.io" }); await user1.refresh(); await user2.refresh(); await user3.refresh(); assert.deepStrictEqual(user1.otherIdents.map(i => ({ email: i.email, uuid: i.uuid })), [ { email: "newtest@webda.io", uuid: "ident1" } ]); assert.deepStrictEqual(user2.otherIdents.map(i => ({ email: i.email, uuid: i.uuid })), [ { email: "newtest@webda.io", uuid: "ident1" } ]); assert.deepStrictEqual(user3.otherIdents.length, 0); ident1["test"] = generatorAttribute(["user1", "user3"]); // @ts-ignore await ident1.save("test"); await user1.refresh(); await user2.refresh(); await user3.refresh(); assert.deepStrictEqual(user1.otherIdents.map(i => ({ email: i.email, uuid: i.uuid })), [ { email: "newtest@webda.io", uuid: "ident1" } ]); assert.deepStrictEqual(user2.otherIdents.length, 0); assert.deepStrictEqual(user3.otherIdents.map(i => ({ email: i.email, uuid: i.uuid })), [ { email: "newtest@webda.io", uuid: "ident1" } ]); await ident1.delete(); await user1.refresh(); await user2.refresh(); await user3.refresh(); assert.deepStrictEqual(user1.otherIdents.length, 0); assert.deepStrictEqual(user2.otherIdents.length, 0); assert.deepStrictEqual(user3.otherIdents.length, 0); } }; __decorate([ test ], MapperTest.prototype, "cov", null); __decorate([ test ], MapperTest.prototype, "n_n_mapping_array", null); __decorate([ test ], MapperTest.prototype, "n_n_mapping_object", null); MapperTest = __decorate([ suite ], MapperTest); //# sourceMappingURL=mapper.spec.js.map