forked from Arkisto-Platform/arkisto-type-definitions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.spec.js
290 lines (267 loc) · 11.6 KB
/
lib.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import { describe, it, test, expect } from "vitest";
import {
readExtension,
stripPrefix,
normaliseEntity,
normaliseEntities,
getRootDataset,
expandIdentifiers,
extractClasses,
extractProperties,
createDescriboMainProfile,
} from "./lib.js";
import { readJSON, pathExists } from "fs-extra/esm";
describe("Testing extension file methods", () => {
it(`should be able to read an extension file`, async () => {
let crate = await readExtension({ name: "ro-crate-additional-schema" });
expect(crate).toBeDefined();
let rootDataset = getRootDataset({ crate });
expect(rootDataset.mentions.length).toEqual(13);
});
it(`should be able to remove prefix's`, () => {
let value = stripPrefix("schema:Person");
expect(value).toEqual("Person");
value = stripPrefix("http://schema.org/Person");
expect(value).toEqual("Person");
value = stripPrefix("https://schema.org/Person");
expect(value).toEqual("Person");
value = stripPrefix("https://pcdm.org/models#Person");
expect(value).toEqual("Person");
});
it("should be able to normalise class definitions", () => {
let entity = {
"@id": "pcdm:Collection",
"@type": "rdfs:Class",
name: "Collection",
"rdfs:comment":
"A Collection is a group of resources. Collections have descriptive metadata, access metadata, and may links to works and/or collections. By default, member works and collections are an",
"rdfs:label": "Collection",
};
expect(() => normaliseEntity(entity)).toThrowError();
entity = {
"@id": "pcdm:Collection",
"@type": "rdfs:Class",
name: "Collection",
"rdfs:comment":
"A Collection is a group of resources. Collections have descriptive metadata, access metadata, and may links to works and/or collections. By default, member works and collections are an",
"rdfs:label": "Collection",
"rdfs:subClassOf": { "@id": "https://schema.org/Thing" },
};
let e = normaliseEntity(entity);
expect(e).toMatchObject({
name: "Collection",
label: "Collection",
domain: ["Thing"],
});
});
it("should be able to normalise property definitions", () => {
let entity = {
"@id": "pcdm:fileOf",
"@type": "rdf:Property",
name: "fileOf",
domainIncludes: { "@id": "https://schema.org/File" },
inverseOf: "",
rangeIncludes: { "@id": "pcdm:Object" },
"rdfs:comment": "Links from a File to its containing Object.",
"rdfs:label": "fileOf",
};
expect(() => normaliseEntity(entity)).toThrowError();
entity = {
"@id": "pcdm:fileOf",
"@type": "rdf:Property",
name: "fileOf",
"schema:domainIncludes": { "@id": "https://schema.org/File" },
inverseOf: "",
"schema:rangeIncludes": { "@id": "pcdm:Object" },
"rdfs:comment": "Links from a File to its containing Object.",
"rdfs:label": "fileOf",
};
let e = normaliseEntity(entity);
expect(e).toMatchObject({
name: "fileOf",
label: "fileOf",
domain: ["File"],
range: ["Object"],
});
});
it("should extract classes and properties from the olac roles extension", async () => {
let crate = await readExtension({ name: "olac-roles" });
crate = normaliseEntities({ crate });
crate = expandIdentifiers({ crate });
let properties = extractProperties({ crate });
expect(properties.length).toEqual(24);
let classes = extractClasses({ crate });
expect(classes.length).toEqual(0);
});
it(`should be able to expand all identifiers in a crate`, async () => {
let crate = {
"@context": {
pcdm: "https://pcdm.org/models#",
rdf: "https://www.w3.org/1999/02/22-rdf-syntax-ns#",
rdfs: "https://www.w3.org/2000/01/rdf-schema#",
schema: "https://schema.org/",
bio: "https://bioschemas.org/",
},
"@graph": [
{
"@id": "ro-crate-metadata.json",
"@type": "CreativeWork",
conformsTo: { "@id": "https://w3id.org/ro/crate/1.1" },
about: { "@id": "./" },
name: "ro-crate-metadata.json",
"@reverse": {},
},
{
"@id": "./",
"@type": "Dataset",
name: "My Research Object Crate",
"@reverse": { about: { "@id": "ro-crate-metadata.json" } },
mentions: [
{ "@id": "pcdm:Collection" },
{ "@id": "pcdm:Object" },
{ "@id": "bio:ComputationalWorkflow" },
{ "@id": "bio:FormalParameter" },
{ "@id": "pcdm:fileOf" },
{ "@id": "pcdm:hasFile" },
{ "@id": "pcdm:hasMember" },
{ "@id": "pcdm:memberOf" },
{ "@id": "schema:resultOf" },
{ "@id": "bio:ComputationalWorkflow#input" },
{ "@id": "bio:ComputationalWorkflow#output" },
],
},
{
"@id": "pcdm:Collection",
"@type": "rdfs:Class",
name: "Collection",
"rdfs:comment":
"A Collection is a group of resources. Collections have descriptive metadata, access metadata, and may links to works and/or collections. By default, member works and collections are an",
"rdfs:label": "Collection",
"rdfs:subClassOf": { "@id": "schema:Thing" },
"@reverse": {
mentions: { "@id": "./" },
domainIncludes: [{ "@id": "pcdm:hasMember" }, { "@id": "pcdm:memberOf" }],
rangeIncludes: [{ "@id": "pcdm:hasMember" }, { "@id": "pcdm:memberOf" }],
},
},
{
"@id": "pcdm:fileOf",
"@type": "rdf:Property",
name: "fileOf",
domainIncludes: { "@id": "schema:File" },
inverseOf: "",
rangeIncludes: { "@id": "pcdm:Object" },
"rdfs:comment": "Links from a File to its containing Object.",
"rdfs:label": "fileOf",
"@reverse": { mentions: { "@id": "./" } },
},
],
};
crate = expandIdentifiers({ crate });
expect(crate["@graph"][2]["@id"]).toEqual("https://pcdm.org/models#Collection");
expect(crate["@graph"][3]["@id"]).toEqual("https://pcdm.org/models#fileOf");
});
it(`should be able to extract all classes and properties from the crate`, async () => {
let crate = {
"@context": {
pcdm: "http://pcdm.org/models#",
rdf: "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
rdfs: "http://www.w3.org/2000/01/rdf-schema#",
schema: "http://schema.org/",
bio: "https://bioschemas.org/",
mentions: "http://schema.org/mentions",
label: "http://www.w3.org/2000/01/rdf-schema#label",
},
"@graph": [
{
"@id": "ro-crate-metadata.json",
"@type": "CreativeWork",
conformsTo: { "@id": "https://w3id.org/ro/crate/1.1" },
about: { "@id": "./" },
name: "ro-crate-metadata.json",
"@reverse": {},
},
{
"@id": "./",
"@type": "Dataset",
name: "My Research Object Crate",
"@reverse": { about: { "@id": "ro-crate-metadata.json" } },
mentions: [
{ "@id": "pcdm:Collection" },
{ "@id": "pcdm:Object" },
{ "@id": "bio:ComputationalWorkflow" },
{ "@id": "bio:FormalParameter" },
{ "@id": "pcdm:fileOf" },
{ "@id": "pcdm:hasFile" },
{ "@id": "pcdm:hasMember" },
{ "@id": "pcdm:memberOf" },
{ "@id": "schema:resultOf" },
{ "@id": "bio:ComputationalWorkflow#input" },
{ "@id": "bio:ComputationalWorkflow#output" },
],
},
{
"@id": "pcdm:Collection",
"@type": "rdfs:Class",
name: "Collection",
"rdfs:comment":
"A Collection is a group of resources. Collections have descriptive metadata, access metadata, and may links to works and/or collections. By default, member works and collections are an",
"rdfs:label": "Collection",
"rdfs:subClassOf": { "@id": "schema:Thing" },
"@reverse": {
mentions: { "@id": "./" },
domainIncludes: [{ "@id": "pcdm:hasMember" }, { "@id": "pcdm:memberOf" }],
rangeIncludes: [{ "@id": "pcdm:hasMember" }, { "@id": "pcdm:memberOf" }],
},
},
{
"@id": "pcdm:fileOf",
"@type": "rdf:Property",
name: "fileOf",
"schema:domainIncludes": { "@id": "schema:File" },
inverseOf: "",
"schema:rangeIncludes": { "@id": "pcdm:Object" },
"rdfs:comment": "Links from a File to its containing Object.",
"rdfs:label": "fileOf",
"@reverse": { mentions: { "@id": "./" } },
},
],
};
// crate = expandIdentifiers({ crate });
const classes = extractClasses({ crate });
expect(classes.length).toEqual(1);
const properties = extractProperties({ crate });
expect(properties.length).toEqual(1);
});
it(`should be able to join the classes defined in the extension file into the schema.org tree`, async () => {
let definitions = await createDescriboMainProfile();
expect(
definitions["CreativeWork"].inputs.filter((i) => i.name === "hasPart")[0]
).toMatchObject({
id: "https://schema.org/hasPart",
name: "hasPart",
type: [
"ANY",
"CreativeWork",
"Dataset",
"File",
"RepositoryCollection",
"RepositoryObject",
],
});
expect(
definitions["CreativeWork"].inputs.filter((i) => i.name === "temporal")[0]
).toMatchObject({
id: "https://schema.org/temporal",
name: "temporal",
type: ["TextArea", "DateTime"],
});
expect(
definitions["Thing"].inputs.filter((i) => i.name === "description")[0]
).toMatchObject({
id: "https://schema.org/description",
name: "description",
type: ["TextArea", "TextObject"],
});
});
});