Skip to content

Commit 5dca238

Browse files
committed
Add test - Empty instance
1 parent 99bc7dd commit 5dca238

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

tests/emptyInstance.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
}

tests/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {RemoveIndicatorFromOrg} from "./removeIndicatorFromOrg";
1616
import {PopulateIssue} from "./populateIssue";
1717
import {doubleRDFTypes} from "./doubleRDFTypes";
1818
import {filterForList} from "./filterForList";
19+
import {emptyInstance} from "./emptyInstance";
1920

2021
dotenv.config({path: `${__dirname}/.env`});
2122

@@ -54,4 +55,5 @@ describe("GraphDB", function () {
5455
describe('PopulateIssue', PopulateIssue(repository))
5556
describe('double RDFTypes', doubleRDFTypes(repository))
5657
describe('filter for lists', filterForList(repository))
58+
describe('Empty instance', emptyInstance(repository))
5759
});

0 commit comments

Comments
 (0)