|
| 1 | +import {createGraphDBModel, DeleteType, GraphDBModelConstructor} from "../src"; |
| 2 | +import {expect} from "chai"; |
| 3 | + |
| 4 | +export function emptyInstance(repository: any) { |
| 5 | + return async function () { |
| 6 | + let PersonModel: GraphDBModelConstructor, OrganizationModel: GraphDBModelConstructor; |
| 7 | + |
| 8 | + it('should create models', function () { |
| 9 | + PersonModel = createGraphDBModel({ |
| 10 | + name: {type: String, internalKey: 'cids:hasName'}, |
| 11 | + }, { |
| 12 | + rdfTypes: [':PersonTest'], name: 'personTest' |
| 13 | + }); |
| 14 | + |
| 15 | + OrganizationModel = createGraphDBModel({ |
| 16 | + person: {type: PersonModel, internalKey: ':hasPerson'}, |
| 17 | + }, { |
| 18 | + rdfTypes: [':OrgTest'], name: 'orgTest' |
| 19 | + }); |
| 20 | + }); |
| 21 | + it('should create empty instance', async function () { |
| 22 | + const person = PersonModel({ |
| 23 | + _uri: "http://person_1" |
| 24 | + }); |
| 25 | + await person.save(); |
| 26 | + |
| 27 | + const persons = await PersonModel.find({}); |
| 28 | + expect(persons).length(1); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should create nested empty instance - syntax 1', async function () { |
| 32 | + await PersonModel.findAndDelete({}); |
| 33 | + await OrganizationModel.findAndDelete({}); |
| 34 | + |
| 35 | + // This syntax treat the person as a new person instance with no properties |
| 36 | + const org = OrganizationModel({ |
| 37 | + person: { |
| 38 | + _uri: "http://person_2" |
| 39 | + } |
| 40 | + }); |
| 41 | + await org.save(); |
| 42 | + |
| 43 | + // The person instance should be created by this syntax, i.e. `rdf:type` is created for http://person_2 |
| 44 | + const persons = await PersonModel.find({}); |
| 45 | + expect(persons).length(1); |
| 46 | + |
| 47 | + // But the triple `org` :hasPerson <http://person_2> should be there |
| 48 | + const orgFound: any = await OrganizationModel.findOne({}); |
| 49 | + expect(orgFound.person).eq('http://person_2'); |
| 50 | + |
| 51 | + // Populate should work |
| 52 | + await orgFound.populate("person"); |
| 53 | + expect(orgFound.person._uri).eq('http://person_2'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should create nested empty instance - syntax 2', async function () { |
| 57 | + await PersonModel.findAndDelete({}); |
| 58 | + await OrganizationModel.findAndDelete({}); |
| 59 | + |
| 60 | + // This syntax treat the person as an existing person instance. |
| 61 | + const org = OrganizationModel({ |
| 62 | + person: "http://person_2" |
| 63 | + }); |
| 64 | + await org.save(); |
| 65 | + |
| 66 | + // The person instance should not be created when giving an uri, i.e. there is not `rdf:type` associated to the http://person_2 |
| 67 | + const persons = await PersonModel.find({}); |
| 68 | + expect(persons).length(0); |
| 69 | + |
| 70 | + // But the triple `org` :hasPerson <http://person_2> should be there |
| 71 | + const orgFound: any = await OrganizationModel.findOne({}); |
| 72 | + expect(orgFound.person).eq('http://person_2'); |
| 73 | + |
| 74 | + // Note: Populate should not work in this case |
| 75 | + }); |
| 76 | + } |
| 77 | +} |
0 commit comments