From 15bd9932483d09eacfb5097d2694e36521751757 Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Fri, 13 Dec 2019 11:32:02 +0000 Subject: [PATCH 1/2] Test for error when trying to remove dynamic path relationship --- test/relationship-spec.ts | 60 ++++++++++++++++++-------- test/testing/dynamicPaths/PersonPay.ts | 9 ++++ 2 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 test/testing/dynamicPaths/PersonPay.ts diff --git a/test/relationship-spec.ts b/test/relationship-spec.ts index 3ac44cbb..bb7d2a5f 100644 --- a/test/relationship-spec.ts +++ b/test/relationship-spec.ts @@ -1,5 +1,5 @@ // tslint:disable:no-implicit-dependencies -import { Record, IFmWatchEvent } from "../src"; +import { Record, IFmWatchEvent, List, IFmLocalEvent, Watch } from "../src"; import { DB } from "abstracted-admin"; import * as chai from "chai"; const expect = chai.expect; @@ -9,6 +9,7 @@ import { FancyPerson } from "./testing/FancyPerson"; import { FmEvents } from "../src/state-mgmt"; import { Company } from "./testing/Company"; import { Pay } from "./testing/Pay"; +import { PersonPay } from "./testing/dynamicPaths/PersonPay"; import { extractFksFromPaths } from "../src/record/extractFksFromPaths"; import { Car } from "./testing/Car"; import OffsetCar from "./testing/dynamicPaths/Car"; @@ -288,20 +289,45 @@ describe("Relationship > ", () => { ); } }); - // it.skip("using addToRelationship() on a hasMany relationship with an inverse of hasOne", async () => { - // const person = await Record.add(Person, { - // name: "Bob", - // age: 23 - // }); - // expect(person.id).to.exist.and.to.be.a("string"); - // const lastUpdated = person.data.lastUpdated; - // const events: IFmRecordEvent[] = []; - // Record.dispatch = (evt: IFmRecordEvent) => events.push(evt); - // await person.addToRelationship("concerts", "12345"); - // expect((person.data.concerts as any)["12345"]).to.equal(true); - // expect(events).to.have.lengthOf(2); - // const eventTypes = new Set(events.map(e => e.type)); - // expect(eventTypes.has(FMEvents.RELATIONSHIP_ADDED)).to.equal(true); - // expect(eventTypes.has(FMEvents.RELATIONSHIP_ADDED_LOCALLY)).to.equal(true); - // }); + + it.only("testing removing relationships with remove()", async () => { + const person = await Record.add(Person, { + id: "p1", + name: "Joe Bloggs", + age: 22, + gender: "male" + }); + + const events: Array> = []; + const cb = async (event: IFmLocalEvent) => { + events.push(event); + }; + const pList = await Watch.list(PersonPay, { person: person.id }) + .all() + .dispatch(cb) + .start(); + + const pay = await Record.add(PersonPay, { + amount: "2400.00", + person: person.id + }); + + // const pay1 = await Record.add(PersonPay, { + // amount: "2400.00", + // person: person.id + // }); + console.log(pay.dbPath); + await pay.remove(); + + // expect(events).to.have.lengthOf(10); + // const eventTypes = new Set(events.map(e => e.type)); + // expect(eventTypes.has(FmEvents.RECORD_REMOVED_LOCALLY)).is.equal(true); + // expect(eventTypes.has(FmEvents.RECORD_REMOVED_CONFIRMATION)).is.equal(true); + + // const payList = await List.all(PersonPay, { offsets: { person: person.id }}); + + // expect(payList).to.have.lengthOf(1); + // const ids = payList.map(p => p.id); + // expect(ids.includes(pay.id)).to.equal(false); + }); }).timeout(4000); diff --git a/test/testing/dynamicPaths/PersonPay.ts b/test/testing/dynamicPaths/PersonPay.ts new file mode 100644 index 00000000..bad8615c --- /dev/null +++ b/test/testing/dynamicPaths/PersonPay.ts @@ -0,0 +1,9 @@ +import { model, property, Model, belongsTo, IFmHasOne } from "../../../src"; +import { Person } from "../Person"; + +@model({ dbOffset: ":person", audit: true }) +export class PersonPay extends Model { + // prettier-ignore + @belongsTo(() => Person) public person: IFmHasOne; + @property public amount?: string; +} From ea62d6eef103169674014d24288b3998f1ce0300 Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Fri, 13 Dec 2019 11:32:39 +0000 Subject: [PATCH 2/2] Uncomment tests --- test/relationship-spec.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/test/relationship-spec.ts b/test/relationship-spec.ts index bb7d2a5f..0757730b 100644 --- a/test/relationship-spec.ts +++ b/test/relationship-spec.ts @@ -312,22 +312,22 @@ describe("Relationship > ", () => { person: person.id }); - // const pay1 = await Record.add(PersonPay, { - // amount: "2400.00", - // person: person.id - // }); + const pay1 = await Record.add(PersonPay, { + amount: "2400.00", + person: person.id + }); console.log(pay.dbPath); await pay.remove(); - // expect(events).to.have.lengthOf(10); - // const eventTypes = new Set(events.map(e => e.type)); - // expect(eventTypes.has(FmEvents.RECORD_REMOVED_LOCALLY)).is.equal(true); - // expect(eventTypes.has(FmEvents.RECORD_REMOVED_CONFIRMATION)).is.equal(true); + expect(events).to.have.lengthOf(10); + const eventTypes = new Set(events.map(e => e.type)); + expect(eventTypes.has(FmEvents.RECORD_REMOVED_LOCALLY)).is.equal(true); + expect(eventTypes.has(FmEvents.RECORD_REMOVED_CONFIRMATION)).is.equal(true); - // const payList = await List.all(PersonPay, { offsets: { person: person.id }}); + const payList = await List.all(PersonPay, { offsets: { person: person.id }}); - // expect(payList).to.have.lengthOf(1); - // const ids = payList.map(p => p.id); - // expect(ids.includes(pay.id)).to.equal(false); + expect(payList).to.have.lengthOf(1); + const ids = payList.map(p => p.id); + expect(ids.includes(pay.id)).to.equal(false); }); }).timeout(4000);