@coasys/ad4m 0.12.0-rc1-dev → 0.12.0-rc1-dev.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/jest.config.js +4 -4
- package/lib/index.cjs +5999 -6044
- package/lib/index.js +5977 -6038
- package/lib/src/Ad4mClient.d.ts +16 -9
- package/lib/src/Ad4mClient.js +25 -46
- package/lib/src/Ad4mClient.test.d.ts +1 -0
- package/lib/src/Ad4mClient.test.js +1204 -0
- package/lib/src/Literal.d.ts +2 -1
- package/lib/src/Literal.js +35 -38
- package/lib/src/Literal.test.d.ts +1 -0
- package/lib/src/Literal.test.js +41 -0
- package/lib/src/SmartLiteral.d.ts +2 -1
- package/lib/src/SmartLiteral.js +15 -30
- package/lib/src/agent/AgentClient.d.ts +4 -1
- package/lib/src/agent/AgentClient.js +99 -79
- package/lib/src/ai/AIClient.d.ts +2 -1
- package/lib/src/ai/AIClient.js +220 -224
- package/lib/src/expression/ExpressionClient.d.ts +1 -1
- package/lib/src/expression/ExpressionClient.js +86 -73
- package/lib/src/index.d.ts +1 -1
- package/lib/src/index.js +1 -1
- package/lib/src/language/LanguageClient.d.ts +1 -1
- package/lib/src/language/LanguageClient.js +52 -52
- package/lib/src/model/Ad4mModel.d.ts +157 -937
- package/lib/src/model/Ad4mModel.js +212 -2797
- package/lib/src/model/Ad4mModel.test.js +1002 -561
- package/lib/src/model/decorators.d.ts +94 -426
- package/lib/src/model/decorators.js +333 -741
- package/lib/src/model/mutation.d.ts +81 -0
- package/lib/src/model/mutation.js +317 -0
- package/lib/src/model/parentUtils.d.ts +32 -0
- package/lib/src/model/parentUtils.js +64 -0
- package/lib/src/model/prolog/generatePrologFacts.d.ts +36 -0
- package/lib/src/model/prolog/generatePrologFacts.js +139 -0
- package/lib/src/model/prolog/index.d.ts +1 -0
- package/lib/src/model/prolog/index.js +1 -0
- package/lib/src/model/query/ModelQueryBuilder.d.ts +82 -0
- package/lib/src/model/query/ModelQueryBuilder.js +115 -0
- package/lib/src/model/query/QueryBuilder.d.ts +101 -0
- package/lib/src/model/query/QueryBuilder.js +193 -0
- package/lib/src/model/query/SurrealQueryBuilder.d.ts +48 -0
- package/lib/src/model/query/SurrealQueryBuilder.js +371 -0
- package/lib/src/model/query/fetchInstance.d.ts +24 -0
- package/lib/src/model/query/fetchInstance.js +144 -0
- package/lib/src/model/query/hydration.d.ts +54 -0
- package/lib/src/model/query/hydration.js +214 -0
- package/lib/src/model/query/operations.d.ts +59 -0
- package/lib/src/model/query/operations.js +384 -0
- package/lib/src/model/query/snapshot.d.ts +59 -0
- package/lib/src/model/query/snapshot.js +106 -0
- package/lib/src/model/query/surrealCompiler.d.ts +42 -0
- package/lib/src/model/query/surrealCompiler.js +304 -0
- package/lib/src/model/schema/fromJSONSchema.d.ts +67 -0
- package/lib/src/model/schema/fromJSONSchema.js +264 -0
- package/lib/src/model/schema/metadata.d.ts +15 -0
- package/lib/src/model/schema/metadata.js +112 -0
- package/lib/src/model/subscription.d.ts +47 -0
- package/lib/src/model/subscription.js +231 -0
- package/lib/src/model/transaction.d.ts +27 -0
- package/lib/src/model/transaction.js +32 -0
- package/lib/src/model/types.d.ts +195 -0
- package/lib/src/model/types.js +8 -0
- package/lib/src/model/util.js +7 -7
- package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +5 -1
- package/lib/src/neighbourhood/NeighbourhoodClient.js +263 -181
- package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +2 -1
- package/lib/src/neighbourhood/NeighbourhoodProxy.js +13 -28
- package/lib/src/neighbourhood/NeighbourhoodProxy.test.js +1 -5
- package/lib/src/perspectives/PerspectiveClient.d.ts +9 -9
- package/lib/src/perspectives/PerspectiveClient.js +316 -240
- package/lib/src/perspectives/PerspectiveProxy.d.ts +54 -92
- package/lib/src/perspectives/PerspectiveProxy.js +803 -444
- package/lib/src/runtime/RuntimeClient.d.ts +5 -1
- package/lib/src/runtime/RuntimeClient.js +412 -238
- package/lib/src/shacl/SHACLFlow.test.js +164 -105
- package/lib/src/shacl/SHACLShape.d.ts +17 -3
- package/lib/src/shacl/SHACLShape.js +127 -90
- package/lib/src/shacl/SHACLShape.test.js +244 -176
- package/lib/src/utils.d.ts +8 -0
- package/lib/src/utils.js +21 -7
- package/package.json +8 -13
- package/src/Ad4mClient.test.ts +1564 -1315
- package/src/Ad4mClient.ts +55 -54
- package/src/Literal.test.ts +45 -26
- package/src/Literal.ts +82 -66
- package/src/SmartLiteral.ts +74 -58
- package/src/agent/AgentClient.ts +144 -99
- package/src/ai/AIClient.ts +409 -359
- package/src/expression/ExpressionClient.ts +142 -96
- package/src/index.ts +1 -1
- package/src/language/LanguageClient.ts +137 -113
- package/src/model/Ad4mModel.test.ts +1128 -647
- package/src/model/Ad4mModel.ts +485 -3247
- package/src/model/IMPROVEMENTS.md +482 -0
- package/src/model/PREDICATE_COLLISION_FIX.md +651 -0
- package/src/model/README.md +317 -0
- package/src/model/SHACL_MUTATION_DECOUPLING.md +356 -0
- package/src/model/SUBSCRIPTION_STRATEGY.md +317 -0
- package/src/model/TC39_STAGE_3_DECORATORS.md +609 -0
- package/src/model/decorators.ts +689 -1046
- package/src/model/mutation.ts +461 -0
- package/src/model/parentUtils.ts +91 -0
- package/src/model/prolog/generatePrologFacts.ts +176 -0
- package/src/model/prolog/index.ts +1 -0
- package/src/model/query/ModelQueryBuilder.ts +173 -0
- package/src/model/query/fetchInstance.ts +181 -0
- package/src/model/query/hydration.ts +273 -0
- package/src/model/query/operations.ts +510 -0
- package/src/model/query/snapshot.ts +126 -0
- package/src/model/query/surrealCompiler.ts +376 -0
- package/src/model/schema/fromJSONSchema.ts +411 -0
- package/src/model/schema/metadata.ts +148 -0
- package/src/model/subscription.ts +307 -0
- package/src/model/transaction.ts +60 -0
- package/src/model/types.ts +207 -0
- package/src/model/util.ts +47 -48
- package/src/neighbourhood/NeighbourhoodClient.ts +393 -251
- package/src/neighbourhood/NeighbourhoodProxy.test.ts +3 -7
- package/src/neighbourhood/NeighbourhoodProxy.ts +58 -40
- package/src/perspectives/PerspectiveClient.ts +779 -540
- package/src/perspectives/PerspectiveProxy.ts +2860 -2053
- package/src/runtime/RuntimeClient.ts +775 -476
- package/src/shacl/SHACLFlow.test.ts +196 -129
- package/src/shacl/SHACLShape.test.ts +284 -190
- package/src/shacl/SHACLShape.ts +225 -172
- package/src/utils.ts +31 -17
- package/tsconfig.json +9 -10
- package/src/model/Subject.ts +0 -159
|
@@ -1,203 +1,223 @@
|
|
|
1
1
|
import { Ad4mModel } from "./Ad4mModel";
|
|
2
|
-
import {
|
|
2
|
+
import { Model, Property, HasMany, Flag } from "./decorators";
|
|
3
|
+
import { Literal } from "../Literal";
|
|
4
|
+
import { resolveParentPredicate, normalizeParentQuery } from "./parentUtils";
|
|
5
|
+
|
|
6
|
+
// ── Shared test fixtures ──────────────────────────────────────────────────────
|
|
7
|
+
// Defined once at module level — used by queryToSurrealQL,
|
|
8
|
+
// instancesFromSurrealResult, and hydration describe blocks alike.
|
|
9
|
+
|
|
10
|
+
@Model({ name: "Recipe" })
|
|
11
|
+
class Recipe extends Ad4mModel {
|
|
12
|
+
@Property({ through: "recipe://name", required: true })
|
|
13
|
+
name: string = "";
|
|
14
|
+
|
|
15
|
+
@Property({ through: "recipe://rating", required: true })
|
|
16
|
+
rating: number = 0;
|
|
17
|
+
|
|
18
|
+
@HasMany({ through: "recipe://ingredient" })
|
|
19
|
+
ingredients: string[] = [];
|
|
20
|
+
}
|
|
3
21
|
|
|
4
22
|
describe("Ad4mModel.getModelMetadata()", () => {
|
|
5
23
|
it("should extract basic model metadata with className", () => {
|
|
6
|
-
@
|
|
24
|
+
@Model({ name: "SimpleModel" })
|
|
7
25
|
class SimpleModel extends Ad4mModel {}
|
|
8
26
|
|
|
9
27
|
const metadata = SimpleModel.getModelMetadata();
|
|
10
|
-
|
|
28
|
+
|
|
11
29
|
expect(metadata.className).toBe("SimpleModel");
|
|
12
30
|
expect(metadata.properties).toEqual({});
|
|
13
|
-
expect(metadata.
|
|
31
|
+
expect(metadata.relations).toEqual({});
|
|
14
32
|
});
|
|
15
33
|
|
|
16
34
|
it("should extract property metadata with all fields", () => {
|
|
17
|
-
@
|
|
35
|
+
@Model({ name: "PropertyModel" })
|
|
18
36
|
class PropertyModel extends Ad4mModel {
|
|
19
|
-
@Property({
|
|
37
|
+
@Property({
|
|
38
|
+
through: "test://name",
|
|
39
|
+
resolveLanguage: "literal",
|
|
40
|
+
required: true,
|
|
41
|
+
})
|
|
20
42
|
name: string = "";
|
|
21
|
-
|
|
22
|
-
@
|
|
43
|
+
|
|
44
|
+
@Property({ through: "test://optional" })
|
|
23
45
|
optional: string = "";
|
|
24
|
-
|
|
25
|
-
@
|
|
46
|
+
|
|
47
|
+
@Property({ through: "test://readonly", readOnly: true })
|
|
26
48
|
readonly: string = "";
|
|
27
|
-
|
|
49
|
+
|
|
28
50
|
@Flag({ through: "test://type", value: "test://flag" })
|
|
29
51
|
type: string = "";
|
|
30
52
|
}
|
|
31
53
|
|
|
32
54
|
const metadata = PropertyModel.getModelMetadata();
|
|
33
|
-
|
|
55
|
+
|
|
34
56
|
// Should have 4 properties
|
|
35
57
|
expect(Object.keys(metadata.properties)).toHaveLength(4);
|
|
36
|
-
|
|
58
|
+
|
|
37
59
|
// Verify "name" property
|
|
38
60
|
expect(metadata.properties.name.predicate).toBe("test://name");
|
|
39
61
|
expect(metadata.properties.name.required).toBe(true);
|
|
40
|
-
expect(metadata.properties.name.
|
|
62
|
+
expect(metadata.properties.name.readOnly).toBeFalsy();
|
|
41
63
|
expect(metadata.properties.name.resolveLanguage).toBe("literal");
|
|
42
|
-
|
|
64
|
+
|
|
43
65
|
// Verify "optional" property
|
|
44
66
|
expect(metadata.properties.optional.predicate).toBe("test://optional");
|
|
45
|
-
expect(metadata.properties.optional.
|
|
46
|
-
|
|
67
|
+
expect(metadata.properties.optional.readOnly).toBeFalsy();
|
|
68
|
+
|
|
47
69
|
// Verify "readonly" property
|
|
48
70
|
expect(metadata.properties.readonly.predicate).toBe("test://readonly");
|
|
49
|
-
expect(metadata.properties.readonly.
|
|
50
|
-
|
|
51
|
-
|
|
71
|
+
expect(metadata.properties.readonly.readOnly).toBe(true);
|
|
72
|
+
|
|
52
73
|
// Verify "type" property (flag)
|
|
53
74
|
expect(metadata.properties.type.predicate).toBe("test://type");
|
|
54
75
|
expect(metadata.properties.type.flag).toBe(true);
|
|
55
76
|
expect(metadata.properties.type.initial).toBe("test://flag");
|
|
56
77
|
});
|
|
57
78
|
|
|
58
|
-
it("should extract collection metadata
|
|
59
|
-
@
|
|
79
|
+
it("should extract collection metadata", () => {
|
|
80
|
+
@Model({ name: "CollectionModel" })
|
|
60
81
|
class CollectionModel extends Ad4mModel {
|
|
61
|
-
@
|
|
82
|
+
@HasMany({ through: "test://items" })
|
|
62
83
|
items: string[] = [];
|
|
63
|
-
|
|
64
|
-
@
|
|
65
|
-
through: "test://filtered",
|
|
66
|
-
where: { condition: "triple(Target, 'test://active', 'true')" }
|
|
67
|
-
})
|
|
68
|
-
filtered: string[] = [];
|
|
69
|
-
|
|
70
|
-
@Collection({ through: "test://local", local: true })
|
|
84
|
+
|
|
85
|
+
@HasMany({ through: "test://local", local: true })
|
|
71
86
|
local: string[] = [];
|
|
72
87
|
}
|
|
73
88
|
|
|
74
89
|
const metadata = CollectionModel.getModelMetadata();
|
|
75
|
-
|
|
76
|
-
// Should have
|
|
77
|
-
expect(Object.keys(metadata.
|
|
78
|
-
|
|
90
|
+
|
|
91
|
+
// Should have 2 collections
|
|
92
|
+
expect(Object.keys(metadata.relations)).toHaveLength(2);
|
|
93
|
+
|
|
79
94
|
// Verify "items" collection
|
|
80
|
-
expect(metadata.
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
// Verify "filtered" collection
|
|
84
|
-
expect(metadata.collections.filtered.predicate).toBe("test://filtered");
|
|
85
|
-
expect(metadata.collections.filtered.where?.condition).toBe("triple(Target, 'test://active', 'true')");
|
|
86
|
-
|
|
95
|
+
expect(metadata.relations.items.predicate).toBe("test://items");
|
|
96
|
+
|
|
87
97
|
// Verify "local" collection
|
|
88
|
-
expect(metadata.
|
|
89
|
-
expect(metadata.
|
|
98
|
+
expect(metadata.relations.local.predicate).toBe("test://local");
|
|
99
|
+
expect(metadata.relations.local.local).toBe(true);
|
|
90
100
|
});
|
|
91
101
|
|
|
92
102
|
it("should extract transform function from property metadata", () => {
|
|
93
|
-
@
|
|
103
|
+
@Model({ name: "TransformModel" })
|
|
94
104
|
class TransformModel extends Ad4mModel {
|
|
95
|
-
@
|
|
105
|
+
@Property({
|
|
96
106
|
through: "test://data",
|
|
97
|
-
transform: (value: string) => value.toUpperCase()
|
|
107
|
+
transform: (value: string) => value.toUpperCase(),
|
|
98
108
|
})
|
|
99
109
|
data: string = "";
|
|
100
110
|
}
|
|
101
111
|
|
|
102
112
|
const metadata = TransformModel.getModelMetadata();
|
|
103
|
-
|
|
113
|
+
|
|
104
114
|
// Assert transform is a function
|
|
105
115
|
expect(typeof metadata.properties.data.transform).toBe("function");
|
|
106
|
-
|
|
116
|
+
|
|
107
117
|
// Test the transform function
|
|
108
118
|
const transformed = metadata.properties.data.transform!("test");
|
|
109
119
|
expect(transformed).toBe("TEST");
|
|
110
120
|
});
|
|
111
121
|
|
|
112
|
-
it("should extract custom getter
|
|
113
|
-
@
|
|
122
|
+
it("should extract custom SurrealQL getter from property metadata", () => {
|
|
123
|
+
@Model({ name: "CustomModel" })
|
|
114
124
|
class CustomModel extends Ad4mModel {
|
|
115
|
-
@
|
|
125
|
+
@Property({
|
|
116
126
|
through: "test://computed",
|
|
117
|
-
|
|
118
|
-
prologSetter: "Value is V / 2, Actions = [{action: 'setSingleTarget', source: 'this', predicate: 'test://value', target: Value}]"
|
|
127
|
+
getter: "(<-link[WHERE predicate = 'test://value'].in.uri)[0]",
|
|
119
128
|
})
|
|
120
|
-
computed:
|
|
129
|
+
computed: string = "";
|
|
121
130
|
}
|
|
122
131
|
|
|
123
132
|
const metadata = CustomModel.getModelMetadata();
|
|
124
|
-
|
|
125
|
-
// Assert
|
|
126
|
-
expect(metadata.properties.computed.
|
|
127
|
-
expect(metadata.properties.computed.prologSetter).toContain("Value is V / 2");
|
|
128
|
-
expect(metadata.properties.computed.prologSetter).toContain("setSingleTarget");
|
|
133
|
+
|
|
134
|
+
// Assert getter contains the custom SurrealQL code
|
|
135
|
+
expect(metadata.properties.computed.getter).toContain("test://value");
|
|
129
136
|
});
|
|
130
137
|
|
|
131
|
-
it("should
|
|
132
|
-
@
|
|
138
|
+
it("should register relatedModel factory for typed @HasMany", () => {
|
|
139
|
+
@Model({ name: "Comment" })
|
|
133
140
|
class Comment extends Ad4mModel {}
|
|
134
|
-
|
|
135
|
-
@
|
|
141
|
+
|
|
142
|
+
@Model({ name: "Post" })
|
|
136
143
|
class Post extends Ad4mModel {
|
|
137
|
-
@
|
|
138
|
-
|
|
139
|
-
where: { isInstance: Comment }
|
|
140
|
-
})
|
|
141
|
-
comments: string[] = [];
|
|
144
|
+
@HasMany(() => Comment, { through: "post://comment" })
|
|
145
|
+
comments: Comment[] = [];
|
|
142
146
|
}
|
|
143
147
|
|
|
144
148
|
const metadata = Post.getModelMetadata();
|
|
145
|
-
|
|
146
|
-
//
|
|
147
|
-
expect(metadata.
|
|
149
|
+
|
|
150
|
+
// relatedModel factory should be registered and return Comment
|
|
151
|
+
expect(metadata.relations.comments.relatedModel).toBeDefined();
|
|
152
|
+
expect(metadata.relations.comments.relatedModel!()).toBe(Comment);
|
|
148
153
|
});
|
|
149
154
|
|
|
150
155
|
it("should throw error for class without @ModelOptions decorator", () => {
|
|
151
156
|
class NoDecoratorModel extends Ad4mModel {}
|
|
152
157
|
|
|
153
158
|
// Assert that calling getModelMetadata throws an error
|
|
154
|
-
expect(() => NoDecoratorModel.getModelMetadata()).toThrow(
|
|
159
|
+
expect(() => NoDecoratorModel.getModelMetadata()).toThrow(
|
|
160
|
+
"Model class must be decorated with @Model",
|
|
161
|
+
);
|
|
155
162
|
});
|
|
156
163
|
|
|
157
164
|
it("should handle complex model with mixed property and collection types", () => {
|
|
158
|
-
@
|
|
165
|
+
@Model({ name: "Recipe" })
|
|
159
166
|
class Recipe extends Ad4mModel {
|
|
160
|
-
@Property({
|
|
167
|
+
@Property({
|
|
168
|
+
through: "recipe://name",
|
|
169
|
+
required: true,
|
|
170
|
+
})
|
|
161
171
|
name: string = "";
|
|
162
|
-
|
|
163
|
-
@
|
|
172
|
+
|
|
173
|
+
@Property({ through: "recipe://description" })
|
|
164
174
|
description: string = "";
|
|
165
|
-
|
|
166
|
-
@
|
|
175
|
+
|
|
176
|
+
@Property({
|
|
177
|
+
through: "recipe://rating",
|
|
178
|
+
getter: "avg_rating_surreal(Base, Value)",
|
|
179
|
+
readOnly: true,
|
|
180
|
+
})
|
|
167
181
|
rating: number = 0;
|
|
168
|
-
|
|
169
|
-
@
|
|
182
|
+
|
|
183
|
+
@HasMany({ through: "recipe://ingredient" })
|
|
170
184
|
ingredients: string[] = [];
|
|
171
|
-
|
|
172
|
-
@
|
|
185
|
+
|
|
186
|
+
@HasMany({ through: "recipe://step", local: true })
|
|
173
187
|
steps: string[] = [];
|
|
174
188
|
}
|
|
175
189
|
|
|
176
190
|
const metadata = Recipe.getModelMetadata();
|
|
177
|
-
|
|
191
|
+
|
|
178
192
|
// Assert className
|
|
179
193
|
expect(metadata.className).toBe("Recipe");
|
|
180
|
-
|
|
194
|
+
|
|
181
195
|
// Assert properties has 3 entries
|
|
182
196
|
expect(Object.keys(metadata.properties)).toHaveLength(3);
|
|
183
197
|
expect(metadata.properties.name).toBeDefined();
|
|
184
198
|
expect(metadata.properties.description).toBeDefined();
|
|
185
199
|
expect(metadata.properties.rating).toBeDefined();
|
|
186
|
-
|
|
200
|
+
|
|
187
201
|
// Assert collections has 2 entries
|
|
188
|
-
expect(Object.keys(metadata.
|
|
189
|
-
expect(metadata.
|
|
190
|
-
expect(metadata.
|
|
191
|
-
|
|
202
|
+
expect(Object.keys(metadata.relations)).toHaveLength(2);
|
|
203
|
+
expect(metadata.relations.ingredients).toBeDefined();
|
|
204
|
+
expect(metadata.relations.steps).toBeDefined();
|
|
205
|
+
|
|
192
206
|
// Verify all metadata fields are correctly extracted
|
|
193
207
|
expect(metadata.properties.name.predicate).toBe("recipe://name");
|
|
194
|
-
expect(metadata.properties.name.resolveLanguage).
|
|
195
|
-
expect(metadata.properties.description.predicate).toBe(
|
|
208
|
+
expect(metadata.properties.name.resolveLanguage).toBeUndefined(); // "literal" is now the implicit default — no need to store it
|
|
209
|
+
expect(metadata.properties.description.predicate).toBe(
|
|
210
|
+
"recipe://description",
|
|
211
|
+
);
|
|
196
212
|
expect(metadata.properties.rating.predicate).toBe("recipe://rating");
|
|
197
|
-
expect(metadata.properties.rating.
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
expect(metadata.
|
|
213
|
+
expect(metadata.properties.rating.getter).toBe(
|
|
214
|
+
"avg_rating_surreal(Base, Value)",
|
|
215
|
+
);
|
|
216
|
+
expect(metadata.relations.ingredients.predicate).toBe(
|
|
217
|
+
"recipe://ingredient",
|
|
218
|
+
);
|
|
219
|
+
expect(metadata.relations.steps.predicate).toBe("recipe://step");
|
|
220
|
+
expect(metadata.relations.steps.local).toBe(true);
|
|
201
221
|
});
|
|
202
222
|
});
|
|
203
223
|
|
|
@@ -209,15 +229,14 @@ describe("Ad4mModel.fromJSONSchema() with getModelMetadata()", () => {
|
|
|
209
229
|
properties: {
|
|
210
230
|
name: { type: "string" },
|
|
211
231
|
price: { type: "number" },
|
|
212
|
-
description: { type: "string" }
|
|
232
|
+
description: { type: "string" },
|
|
213
233
|
},
|
|
214
|
-
required: ["name", "price"]
|
|
234
|
+
required: ["name", "price"],
|
|
215
235
|
};
|
|
216
236
|
|
|
217
237
|
const ProductClass = Ad4mModel.fromJSONSchema(schema, {
|
|
218
238
|
name: "Product",
|
|
219
239
|
namespace: "product://",
|
|
220
|
-
resolveLanguage: "literal"
|
|
221
240
|
});
|
|
222
241
|
|
|
223
242
|
const metadata = ProductClass.getModelMetadata();
|
|
@@ -230,16 +249,18 @@ describe("Ad4mModel.fromJSONSchema() with getModelMetadata()", () => {
|
|
|
230
249
|
expect(metadata.properties.name).toBeDefined();
|
|
231
250
|
expect(metadata.properties.name.predicate).toBe("product://name");
|
|
232
251
|
expect(metadata.properties.name.required).toBe(true);
|
|
233
|
-
expect(metadata.properties.name.
|
|
234
|
-
expect(metadata.properties.name.resolveLanguage).
|
|
252
|
+
expect(metadata.properties.name.readOnly).toBeFalsy();
|
|
253
|
+
expect(metadata.properties.name.resolveLanguage).toBeUndefined(); // implicit literal default
|
|
235
254
|
|
|
236
255
|
expect(metadata.properties.price).toBeDefined();
|
|
237
256
|
expect(metadata.properties.price.predicate).toBe("product://price");
|
|
238
257
|
expect(metadata.properties.price.required).toBe(true);
|
|
239
|
-
expect(metadata.properties.price.resolveLanguage).
|
|
258
|
+
expect(metadata.properties.price.resolveLanguage).toBeUndefined(); // implicit literal default
|
|
240
259
|
|
|
241
260
|
expect(metadata.properties.description).toBeDefined();
|
|
242
|
-
expect(metadata.properties.description.predicate).toBe(
|
|
261
|
+
expect(metadata.properties.description.predicate).toBe(
|
|
262
|
+
"product://description",
|
|
263
|
+
);
|
|
243
264
|
expect(metadata.properties.description.required).toBe(false);
|
|
244
265
|
});
|
|
245
266
|
|
|
@@ -251,19 +272,19 @@ describe("Ad4mModel.fromJSONSchema() with getModelMetadata()", () => {
|
|
|
251
272
|
title: { type: "string" },
|
|
252
273
|
tags: {
|
|
253
274
|
type: "array",
|
|
254
|
-
items: { type: "string" }
|
|
275
|
+
items: { type: "string" },
|
|
255
276
|
},
|
|
256
277
|
comments: {
|
|
257
278
|
type: "array",
|
|
258
|
-
items: { type: "string" }
|
|
259
|
-
}
|
|
279
|
+
items: { type: "string" },
|
|
280
|
+
},
|
|
260
281
|
},
|
|
261
|
-
required: ["title"]
|
|
282
|
+
required: ["title"],
|
|
262
283
|
};
|
|
263
284
|
|
|
264
285
|
const PostClass = Ad4mModel.fromJSONSchema(schema, {
|
|
265
286
|
name: "Post",
|
|
266
|
-
namespace: "post://"
|
|
287
|
+
namespace: "post://",
|
|
267
288
|
});
|
|
268
289
|
|
|
269
290
|
const metadata = PostClass.getModelMetadata();
|
|
@@ -271,13 +292,13 @@ describe("Ad4mModel.fromJSONSchema() with getModelMetadata()", () => {
|
|
|
271
292
|
// Verify className
|
|
272
293
|
expect(metadata.className).toBe("Post");
|
|
273
294
|
|
|
274
|
-
// Verify
|
|
275
|
-
expect(Object.keys(metadata.
|
|
276
|
-
expect(metadata.
|
|
277
|
-
expect(metadata.
|
|
295
|
+
// Verify relations are extracted
|
|
296
|
+
expect(Object.keys(metadata.relations).length).toBeGreaterThan(0);
|
|
297
|
+
expect(metadata.relations.tags).toBeDefined();
|
|
298
|
+
expect(metadata.relations.tags.predicate).toBe("post://tags");
|
|
278
299
|
|
|
279
|
-
expect(metadata.
|
|
280
|
-
expect(metadata.
|
|
300
|
+
expect(metadata.relations.comments).toBeDefined();
|
|
301
|
+
expect(metadata.relations.comments.predicate).toBe("post://comments");
|
|
281
302
|
|
|
282
303
|
// Verify properties (should include at least title)
|
|
283
304
|
expect(metadata.properties.title).toBeDefined();
|
|
@@ -289,7 +310,7 @@ describe("Ad4mModel.fromJSONSchema() with getModelMetadata()", () => {
|
|
|
289
310
|
const schema = {
|
|
290
311
|
title: "Contact",
|
|
291
312
|
"x-ad4m": {
|
|
292
|
-
namespace: "contact://"
|
|
313
|
+
namespace: "contact://",
|
|
293
314
|
},
|
|
294
315
|
type: "object",
|
|
295
316
|
properties: {
|
|
@@ -297,31 +318,29 @@ describe("Ad4mModel.fromJSONSchema() with getModelMetadata()", () => {
|
|
|
297
318
|
type: "string",
|
|
298
319
|
"x-ad4m": {
|
|
299
320
|
through: "foaf://name",
|
|
300
|
-
|
|
301
|
-
writable: true
|
|
302
|
-
}
|
|
321
|
+
},
|
|
303
322
|
},
|
|
304
323
|
email: {
|
|
305
324
|
type: "string",
|
|
306
325
|
"x-ad4m": {
|
|
307
326
|
through: "foaf://mbox",
|
|
308
|
-
local: true
|
|
309
|
-
}
|
|
310
|
-
}
|
|
327
|
+
local: true,
|
|
328
|
+
},
|
|
329
|
+
},
|
|
311
330
|
},
|
|
312
|
-
required: ["name"]
|
|
331
|
+
required: ["name"],
|
|
313
332
|
};
|
|
314
333
|
|
|
315
334
|
const ContactClass = Ad4mModel.fromJSONSchema(schema, {
|
|
316
|
-
name: "Contact"
|
|
335
|
+
name: "Contact",
|
|
317
336
|
});
|
|
318
337
|
|
|
319
338
|
const metadata = ContactClass.getModelMetadata();
|
|
320
339
|
|
|
321
340
|
// Verify x-ad4m metadata is respected
|
|
322
341
|
expect(metadata.properties.name.predicate).toBe("foaf://name");
|
|
323
|
-
expect(metadata.properties.name.resolveLanguage).
|
|
324
|
-
expect(metadata.properties.name.
|
|
342
|
+
expect(metadata.properties.name.resolveLanguage).toBeUndefined(); // implicit literal default
|
|
343
|
+
expect(metadata.properties.name.readOnly).toBeFalsy();
|
|
325
344
|
expect(metadata.properties.name.required).toBe(true);
|
|
326
345
|
|
|
327
346
|
expect(metadata.properties.email.predicate).toBe("foaf://mbox");
|
|
@@ -334,9 +353,9 @@ describe("Ad4mModel.fromJSONSchema() with getModelMetadata()", () => {
|
|
|
334
353
|
type: "object",
|
|
335
354
|
properties: {
|
|
336
355
|
username: { type: "string" },
|
|
337
|
-
fullName: { type: "string" }
|
|
356
|
+
fullName: { type: "string" },
|
|
338
357
|
},
|
|
339
|
-
required: ["username"]
|
|
358
|
+
required: ["username"],
|
|
340
359
|
};
|
|
341
360
|
|
|
342
361
|
const UserClass = Ad4mModel.fromJSONSchema(schema, {
|
|
@@ -344,8 +363,8 @@ describe("Ad4mModel.fromJSONSchema() with getModelMetadata()", () => {
|
|
|
344
363
|
namespace: "user://",
|
|
345
364
|
propertyMapping: {
|
|
346
365
|
username: "custom://identifier",
|
|
347
|
-
fullName: "custom://name"
|
|
348
|
-
}
|
|
366
|
+
fullName: "custom://name",
|
|
367
|
+
},
|
|
349
368
|
});
|
|
350
369
|
|
|
351
370
|
const metadata = UserClass.getModelMetadata();
|
|
@@ -365,23 +384,22 @@ describe("Ad4mModel.fromJSONSchema() with getModelMetadata()", () => {
|
|
|
365
384
|
published: { type: "boolean" },
|
|
366
385
|
authors: {
|
|
367
386
|
type: "array",
|
|
368
|
-
items: { type: "string" }
|
|
387
|
+
items: { type: "string" },
|
|
369
388
|
},
|
|
370
389
|
tags: {
|
|
371
390
|
type: "array",
|
|
372
391
|
items: { type: "string" },
|
|
373
392
|
"x-ad4m": {
|
|
374
|
-
local: true
|
|
375
|
-
}
|
|
376
|
-
}
|
|
393
|
+
local: true,
|
|
394
|
+
},
|
|
395
|
+
},
|
|
377
396
|
},
|
|
378
|
-
required: ["title", "published"]
|
|
397
|
+
required: ["title", "published"],
|
|
379
398
|
};
|
|
380
399
|
|
|
381
400
|
const ArticleClass = Ad4mModel.fromJSONSchema(schema, {
|
|
382
401
|
name: "Article",
|
|
383
402
|
namespace: "article://",
|
|
384
|
-
resolveLanguage: "literal"
|
|
385
403
|
});
|
|
386
404
|
|
|
387
405
|
const metadata = ArticleClass.getModelMetadata();
|
|
@@ -393,35 +411,37 @@ describe("Ad4mModel.fromJSONSchema() with getModelMetadata()", () => {
|
|
|
393
411
|
expect(metadata.properties.title).toBeDefined();
|
|
394
412
|
expect(metadata.properties.title.predicate).toBe("article://title");
|
|
395
413
|
expect(metadata.properties.title.required).toBe(true);
|
|
396
|
-
expect(metadata.properties.title.resolveLanguage).
|
|
414
|
+
expect(metadata.properties.title.resolveLanguage).toBeUndefined(); // implicit literal default
|
|
397
415
|
|
|
398
416
|
expect(metadata.properties.views).toBeDefined();
|
|
399
417
|
expect(metadata.properties.views.predicate).toBe("article://views");
|
|
400
|
-
expect(metadata.properties.views.resolveLanguage).
|
|
418
|
+
expect(metadata.properties.views.resolveLanguage).toBeUndefined(); // implicit literal default
|
|
401
419
|
|
|
402
420
|
expect(metadata.properties.published).toBeDefined();
|
|
403
421
|
expect(metadata.properties.published.predicate).toBe("article://published");
|
|
404
422
|
expect(metadata.properties.published.required).toBe(true);
|
|
405
423
|
|
|
406
|
-
// Verify
|
|
407
|
-
expect(metadata.
|
|
408
|
-
expect(metadata.
|
|
424
|
+
// Verify relations
|
|
425
|
+
expect(metadata.relations.authors).toBeDefined();
|
|
426
|
+
expect(metadata.relations.authors.predicate).toBe("article://authors");
|
|
409
427
|
|
|
410
|
-
expect(metadata.
|
|
411
|
-
expect(metadata.
|
|
412
|
-
expect(metadata.
|
|
428
|
+
expect(metadata.relations.tags).toBeDefined();
|
|
429
|
+
expect(metadata.relations.tags.predicate).toBe("article://tags");
|
|
430
|
+
expect(metadata.relations.tags.local).toBe(true);
|
|
413
431
|
});
|
|
414
432
|
|
|
415
433
|
it("should handle models with only an auto-generated type flag", () => {
|
|
434
|
+
const warnSpy = jest.spyOn(console, "warn").mockImplementation(() => {});
|
|
435
|
+
|
|
416
436
|
const schema = {
|
|
417
437
|
title: "EmptyModel",
|
|
418
438
|
type: "object",
|
|
419
|
-
properties: {}
|
|
439
|
+
properties: {},
|
|
420
440
|
};
|
|
421
441
|
|
|
422
442
|
const EmptyModelClass = Ad4mModel.fromJSONSchema(schema, {
|
|
423
443
|
name: "EmptyModel",
|
|
424
|
-
namespace: "empty://"
|
|
444
|
+
namespace: "empty://",
|
|
425
445
|
});
|
|
426
446
|
|
|
427
447
|
const metadata = EmptyModelClass.getModelMetadata();
|
|
@@ -434,6 +454,12 @@ describe("Ad4mModel.fromJSONSchema() with getModelMetadata()", () => {
|
|
|
434
454
|
expect(metadata.properties.__ad4m_type.predicate).toBe("ad4m://type");
|
|
435
455
|
expect(metadata.properties.__ad4m_type.initial).toBe("empty://instance");
|
|
436
456
|
expect(metadata.properties.__ad4m_type.flag).toBe(true);
|
|
457
|
+
|
|
458
|
+
// The warning is the expected fallback signal for empty schemas
|
|
459
|
+
expect(warnSpy).toHaveBeenCalledWith(
|
|
460
|
+
expect.stringContaining("No properties with initial values found"),
|
|
461
|
+
);
|
|
462
|
+
warnSpy.mockRestore();
|
|
437
463
|
});
|
|
438
464
|
});
|
|
439
465
|
|
|
@@ -443,20 +469,7 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
443
469
|
|
|
444
470
|
// Helper function to normalize whitespace in queries for easier comparison
|
|
445
471
|
function normalizeQuery(query: string): string {
|
|
446
|
-
return query.replace(/\s+/g,
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
// Test Recipe model
|
|
450
|
-
@ModelOptions({ name: "Recipe" })
|
|
451
|
-
class Recipe extends Ad4mModel {
|
|
452
|
-
@Property({ through: "recipe://name" })
|
|
453
|
-
name: string = "";
|
|
454
|
-
|
|
455
|
-
@Property({ through: "recipe://rating" })
|
|
456
|
-
rating: number = 0;
|
|
457
|
-
|
|
458
|
-
@Collection({ through: "recipe://ingredient" })
|
|
459
|
-
ingredients: string[] = [];
|
|
472
|
+
return query.replace(/\s+/g, " ").trim();
|
|
460
473
|
}
|
|
461
474
|
|
|
462
475
|
it("should generate basic query with no filters", async () => {
|
|
@@ -465,43 +478,63 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
465
478
|
expect(query).toContain("id AS source");
|
|
466
479
|
expect(query).toContain("uri AS source_uri");
|
|
467
480
|
expect(query).toContain("FROM node");
|
|
468
|
-
expect(query).toContain(
|
|
481
|
+
expect(query).toContain(
|
|
482
|
+
"(SELECT predicate, out.uri AS target, author, timestamp FROM link WHERE in = $parent.id ORDER BY timestamp ASC) AS links",
|
|
483
|
+
);
|
|
469
484
|
expect(query).toContain("WHERE");
|
|
470
485
|
// Should have graph traversal filters for required properties
|
|
471
486
|
expect(query).toContain("count(->link[WHERE");
|
|
472
487
|
});
|
|
473
488
|
|
|
474
489
|
it("should generate query with simple property filter", async () => {
|
|
475
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
476
|
-
|
|
490
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
491
|
+
where: { name: "Pasta" },
|
|
492
|
+
});
|
|
493
|
+
|
|
477
494
|
// Should have graph traversal filters for required properties and user filter
|
|
478
|
-
expect(query).toContain(
|
|
479
|
-
|
|
480
|
-
|
|
495
|
+
expect(query).toContain(
|
|
496
|
+
"count(->link[WHERE predicate = 'recipe://name']) > 0",
|
|
497
|
+
);
|
|
498
|
+
expect(query).toContain(
|
|
499
|
+
"count(->link[WHERE predicate = 'recipe://rating']) > 0",
|
|
500
|
+
);
|
|
501
|
+
expect(query).toContain(
|
|
502
|
+
"count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) = 'Pasta']) > 0",
|
|
503
|
+
);
|
|
481
504
|
});
|
|
482
505
|
|
|
483
506
|
it("should generate query with multiple property filters", async () => {
|
|
484
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
485
|
-
|
|
507
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
508
|
+
where: { name: "Pasta", rating: 5 },
|
|
509
|
+
});
|
|
510
|
+
|
|
486
511
|
expect(query).toContain("WHERE");
|
|
487
|
-
expect(query).toContain(
|
|
488
|
-
|
|
512
|
+
expect(query).toContain(
|
|
513
|
+
"count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) = 'Pasta']) > 0",
|
|
514
|
+
);
|
|
515
|
+
expect(query).toContain(
|
|
516
|
+
"count(->link[WHERE predicate = 'recipe://rating' AND fn::parse_literal(out.uri) = 5]) > 0",
|
|
517
|
+
);
|
|
489
518
|
expect(query).toContain("AND");
|
|
490
519
|
});
|
|
491
520
|
|
|
492
521
|
it("should handle gt operator (filtered in JavaScript, not SQL)", async () => {
|
|
493
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
522
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
523
|
+
where: { rating: { gt: 4 } },
|
|
524
|
+
});
|
|
494
525
|
|
|
495
526
|
// Comparison operators for regular properties are now filtered in JavaScript post-query
|
|
496
527
|
// The SQL just filters on the predicate existing
|
|
497
528
|
expect(query).toContain("FROM node");
|
|
498
|
-
|
|
529
|
+
|
|
499
530
|
// Should NOT contain comparison operators in SQL for regular properties
|
|
500
531
|
expect(query).not.toContain("target > 4");
|
|
501
532
|
});
|
|
502
533
|
|
|
503
534
|
it("should handle lt operator (filtered in JavaScript, not SQL)", async () => {
|
|
504
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
535
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
536
|
+
where: { rating: { lt: 3 } },
|
|
537
|
+
});
|
|
505
538
|
|
|
506
539
|
// Comparison operators are filtered in JavaScript post-query
|
|
507
540
|
expect(query).toContain("FROM node");
|
|
@@ -509,7 +542,9 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
509
542
|
});
|
|
510
543
|
|
|
511
544
|
it("should handle gte and lte operators (filtered in JavaScript, not SQL)", async () => {
|
|
512
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
545
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
546
|
+
where: { rating: { gte: 3, lte: 5 } },
|
|
547
|
+
});
|
|
513
548
|
|
|
514
549
|
// Comparison operators are filtered in JavaScript post-query
|
|
515
550
|
expect(query).toContain("FROM node");
|
|
@@ -518,21 +553,31 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
518
553
|
});
|
|
519
554
|
|
|
520
555
|
it("should handle not operator with single value", async () => {
|
|
521
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
556
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
557
|
+
where: { name: { not: "Salad" } },
|
|
558
|
+
});
|
|
522
559
|
|
|
523
560
|
// Not operator uses graph traversal with count = 0
|
|
524
|
-
expect(query).toContain(
|
|
561
|
+
expect(query).toContain(
|
|
562
|
+
"count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) = 'Salad']) = 0",
|
|
563
|
+
);
|
|
525
564
|
});
|
|
526
565
|
|
|
527
566
|
it("should handle not operator with array (NOT IN)", async () => {
|
|
528
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
567
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
568
|
+
where: { name: { not: ["Salad", "Soup"] } },
|
|
569
|
+
});
|
|
529
570
|
|
|
530
571
|
// Not operator with array uses graph traversal with count = 0
|
|
531
|
-
expect(query).toContain(
|
|
572
|
+
expect(query).toContain(
|
|
573
|
+
"count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) IN ['Salad', 'Soup']]) = 0",
|
|
574
|
+
);
|
|
532
575
|
});
|
|
533
576
|
|
|
534
577
|
it("should handle between operator (filtered in JavaScript, not SQL)", async () => {
|
|
535
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
578
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
579
|
+
where: { rating: { between: [3, 5] } },
|
|
580
|
+
});
|
|
536
581
|
|
|
537
582
|
// Between operator for regular properties is now filtered in JavaScript post-query
|
|
538
583
|
expect(query).toContain("FROM node");
|
|
@@ -541,7 +586,9 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
541
586
|
});
|
|
542
587
|
|
|
543
588
|
it("should handle contains operator on string property (filtered in JavaScript, not SQL)", async () => {
|
|
544
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
589
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
590
|
+
where: { name: { contains: "Past" } },
|
|
591
|
+
});
|
|
545
592
|
|
|
546
593
|
// Contains operator for regular properties is now filtered in JavaScript post-query
|
|
547
594
|
expect(query).toContain("FROM node");
|
|
@@ -549,47 +596,34 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
549
596
|
});
|
|
550
597
|
|
|
551
598
|
it("should handle contains operator on regular property with substring (filtered in JavaScript, not SQL)", async () => {
|
|
552
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
599
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
600
|
+
where: { name: { contains: "Salad" } },
|
|
601
|
+
});
|
|
553
602
|
|
|
554
603
|
// Contains operator is filtered in JavaScript post-query
|
|
555
604
|
expect(query).toContain("FROM node");
|
|
556
605
|
expect(query).not.toContain("target CONTAINS 'Salad'");
|
|
557
606
|
});
|
|
558
607
|
|
|
559
|
-
it.skip("should handle contains operator on special field (author)", async () => {
|
|
560
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { author: { contains: "alice" } } });
|
|
561
|
-
|
|
562
|
-
expect(query).toContain("WHERE author CONTAINS 'alice'");
|
|
563
|
-
// Should not use a subquery pattern
|
|
564
|
-
expect(query).not.toContain("SELECT source FROM node");
|
|
565
|
-
});
|
|
566
|
-
|
|
567
|
-
it.skip("should handle contains operator on special field (base)", async () => {
|
|
568
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { base: { contains: "test" } } });
|
|
569
|
-
|
|
570
|
-
expect(query).toContain("WHERE source CONTAINS 'test'");
|
|
571
|
-
});
|
|
572
|
-
|
|
573
608
|
it("should handle array values (IN clause)", async () => {
|
|
574
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
});
|
|
609
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
610
|
+
where: { name: ["Pasta", "Pizza"] },
|
|
611
|
+
});
|
|
578
612
|
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
expect(query).toContain("WHERE author = 'did:key:alice'");
|
|
583
|
-
// Ensure it's NOT using a subquery pattern for author in WHERE clause
|
|
584
|
-
expect(normalizeQuery(query)).not.toMatch(/WHERE.*source IN.*SELECT source FROM node.*author/);
|
|
613
|
+
expect(query).toContain(
|
|
614
|
+
"count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) IN ['Pasta', 'Pizza']]) > 0",
|
|
615
|
+
);
|
|
585
616
|
});
|
|
586
617
|
|
|
587
618
|
it("should not generate ORDER BY clause in SQL (handled in JavaScript)", async () => {
|
|
588
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
619
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
620
|
+
order: { timestamp: "DESC" },
|
|
621
|
+
});
|
|
589
622
|
|
|
590
623
|
// ORDER BY is now handled in JavaScript post-query
|
|
624
|
+
// Note: the links correlated subquery uses ORDER BY timestamp ASC internally for link ordering
|
|
591
625
|
expect(query).toContain("FROM node");
|
|
592
|
-
expect(query).not.toContain("ORDER BY");
|
|
626
|
+
expect(query).not.toContain("ORDER BY timestamp DESC");
|
|
593
627
|
});
|
|
594
628
|
|
|
595
629
|
it("should not generate LIMIT clause in SQL (handled in JavaScript)", async () => {
|
|
@@ -601,7 +635,9 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
601
635
|
});
|
|
602
636
|
|
|
603
637
|
it("should not generate START clause in SQL (handled in JavaScript)", async () => {
|
|
604
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
638
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
639
|
+
offset: 20,
|
|
640
|
+
});
|
|
605
641
|
|
|
606
642
|
// START/offset is now handled in JavaScript post-query
|
|
607
643
|
expect(query).toContain("FROM node");
|
|
@@ -613,58 +649,72 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
613
649
|
where: { name: "Pasta", rating: { gt: 4 } },
|
|
614
650
|
order: { timestamp: "DESC" },
|
|
615
651
|
limit: 10,
|
|
616
|
-
offset: 20
|
|
652
|
+
offset: 20,
|
|
617
653
|
});
|
|
618
654
|
|
|
619
655
|
// WHERE clause uses graph traversal filters
|
|
620
656
|
expect(query).toContain("WHERE");
|
|
621
|
-
expect(query).toContain(
|
|
657
|
+
expect(query).toContain(
|
|
658
|
+
"count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) = 'Pasta']) > 0",
|
|
659
|
+
);
|
|
622
660
|
// Comparison operators (gt) are filtered in JavaScript, not SQL
|
|
623
661
|
expect(query).not.toContain("out.uri > 4");
|
|
624
662
|
expect(query).not.toContain("target > 4");
|
|
625
663
|
// ORDER BY, LIMIT, START are now handled in JavaScript post-query
|
|
626
|
-
|
|
664
|
+
// Note: the links subquery uses ORDER BY timestamp ASC internally
|
|
665
|
+
expect(query).not.toContain("ORDER BY timestamp DESC");
|
|
627
666
|
expect(query).not.toContain("LIMIT");
|
|
628
667
|
expect(query).not.toContain("START");
|
|
629
668
|
});
|
|
630
669
|
|
|
631
|
-
it("should only select requested
|
|
632
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
670
|
+
it("should only select requested fields", async () => {
|
|
671
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
672
|
+
properties: ["name"],
|
|
673
|
+
});
|
|
633
674
|
|
|
634
675
|
// With array::group(), all link data is selected, filtering happens in instancesFromSurrealResult
|
|
635
|
-
expect(query).toContain(
|
|
636
|
-
|
|
676
|
+
expect(query).toContain(
|
|
677
|
+
"(SELECT predicate, out.uri AS target, author, timestamp FROM link WHERE in = $parent.id ORDER BY timestamp ASC) AS links",
|
|
678
|
+
);
|
|
637
679
|
});
|
|
638
680
|
|
|
639
|
-
it("should only select requested
|
|
640
|
-
@
|
|
681
|
+
it("should only select requested relations", async () => {
|
|
682
|
+
@Model({ name: "MultiCollectionModel" })
|
|
641
683
|
class MultiCollectionModel extends Ad4mModel {
|
|
642
|
-
@
|
|
684
|
+
@HasMany({ through: "test://coll1" })
|
|
643
685
|
coll1: string[] = [];
|
|
644
686
|
|
|
645
|
-
@
|
|
687
|
+
@HasMany({ through: "test://coll2" })
|
|
646
688
|
coll2: string[] = [];
|
|
647
689
|
}
|
|
648
690
|
|
|
649
|
-
const query = await MultiCollectionModel.queryToSurrealQL(
|
|
691
|
+
const query = await MultiCollectionModel.queryToSurrealQL(
|
|
692
|
+
mockPerspective,
|
|
693
|
+
{},
|
|
694
|
+
);
|
|
650
695
|
|
|
651
696
|
// With array::group(), all link data is selected, filtering happens in instancesFromSurrealResult
|
|
652
|
-
expect(query).toContain(
|
|
653
|
-
|
|
697
|
+
expect(query).toContain(
|
|
698
|
+
"(SELECT predicate, out.uri AS target, author, timestamp FROM link WHERE in = $parent.id ORDER BY timestamp ASC) AS links",
|
|
699
|
+
);
|
|
654
700
|
});
|
|
655
701
|
|
|
656
702
|
it("should escape single quotes in string values", async () => {
|
|
657
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
703
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
704
|
+
where: { name: "O'Brien's Recipe" },
|
|
705
|
+
});
|
|
658
706
|
|
|
659
707
|
// Single quotes are escaped with backslash in SurrealDB
|
|
660
708
|
expect(query).toContain("O\\'Brien\\'s Recipe");
|
|
661
709
|
});
|
|
662
710
|
|
|
663
711
|
it("should handle numeric values without quotes", async () => {
|
|
664
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
665
|
-
|
|
712
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
713
|
+
where: { rating: 5 },
|
|
714
|
+
});
|
|
715
|
+
|
|
666
716
|
// Numeric values should not have quotes around them
|
|
667
|
-
expect(query).toContain("out.uri = 5");
|
|
717
|
+
expect(query).toContain("fn::parse_literal(out.uri) = 5");
|
|
668
718
|
expect(query).not.toContain("out.uri = '5'");
|
|
669
719
|
});
|
|
670
720
|
|
|
@@ -673,16 +723,18 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
673
723
|
where: {
|
|
674
724
|
name: "Pasta",
|
|
675
725
|
rating: { gte: 4, lte: 5 },
|
|
676
|
-
author: "did:key:alice"
|
|
726
|
+
author: "did:key:alice",
|
|
677
727
|
},
|
|
678
728
|
order: { rating: "DESC" },
|
|
679
|
-
limit: 5
|
|
729
|
+
limit: 5,
|
|
680
730
|
});
|
|
681
731
|
|
|
682
732
|
const normalized = normalizeQuery(query);
|
|
683
733
|
|
|
684
734
|
// Verify graph traversal filters are present
|
|
685
|
-
expect(normalized).toContain(
|
|
735
|
+
expect(normalized).toContain(
|
|
736
|
+
"count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) = 'Pasta']) > 0",
|
|
737
|
+
);
|
|
686
738
|
// Comparison operators (gte, lte) are filtered in JavaScript, not SQL
|
|
687
739
|
expect(normalized).not.toContain("out.uri >= 4");
|
|
688
740
|
expect(normalized).not.toContain("target >= 4");
|
|
@@ -692,7 +744,8 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
692
744
|
expect(normalized).not.toContain("author = 'did:key:alice'");
|
|
693
745
|
|
|
694
746
|
// ORDER BY and LIMIT are handled in JavaScript post-query
|
|
695
|
-
|
|
747
|
+
// Note: the links correlated subquery uses ORDER BY timestamp ASC internally
|
|
748
|
+
expect(normalized).not.toContain("ORDER BY rating");
|
|
696
749
|
expect(normalized).not.toContain("LIMIT");
|
|
697
750
|
|
|
698
751
|
// Verify query structure (FROM node, no GROUP BY)
|
|
@@ -701,67 +754,91 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
701
754
|
|
|
702
755
|
it("should handle empty query object", async () => {
|
|
703
756
|
const query = await Recipe.queryToSurrealQL(mockPerspective, {});
|
|
704
|
-
|
|
757
|
+
|
|
705
758
|
// Should generate valid query with WHERE clause for required properties (name and rating)
|
|
706
759
|
expect(query).toContain("id AS source");
|
|
707
760
|
expect(query).toContain("uri AS source_uri");
|
|
708
761
|
expect(query).toContain("FROM node");
|
|
709
|
-
|
|
762
|
+
|
|
710
763
|
// Should have WHERE clause filtering for required properties using graph traversal
|
|
711
764
|
expect(query).toContain("WHERE");
|
|
712
|
-
expect(query).toContain(
|
|
713
|
-
|
|
714
|
-
|
|
765
|
+
expect(query).toContain(
|
|
766
|
+
"count(->link[WHERE predicate = 'recipe://name']) > 0",
|
|
767
|
+
);
|
|
768
|
+
expect(query).toContain(
|
|
769
|
+
"count(->link[WHERE predicate = 'recipe://rating']) > 0",
|
|
770
|
+
);
|
|
771
|
+
// No user-requested ORDER BY or START should appear at the top level
|
|
715
772
|
expect(query).not.toContain("START");
|
|
716
773
|
});
|
|
717
774
|
|
|
718
775
|
it("should handle base special field", async () => {
|
|
719
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
720
|
-
|
|
776
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
777
|
+
where: { base: "literal://test" },
|
|
778
|
+
});
|
|
779
|
+
|
|
721
780
|
expect(query).toContain("uri = 'literal://test'");
|
|
722
781
|
});
|
|
723
782
|
|
|
724
783
|
it("should handle base special field with array (IN clause)", async () => {
|
|
725
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
726
|
-
|
|
784
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
785
|
+
where: { base: ["literal://test1", "literal://test2"] },
|
|
786
|
+
});
|
|
787
|
+
|
|
727
788
|
expect(query).toContain("uri IN ['literal://test1', 'literal://test2']");
|
|
728
789
|
});
|
|
729
790
|
|
|
730
791
|
it("should handle base special field with not operator", async () => {
|
|
731
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
732
|
-
|
|
792
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
793
|
+
where: { base: { not: "literal://test" } },
|
|
794
|
+
});
|
|
795
|
+
|
|
733
796
|
expect(query).toContain("uri != 'literal://test'");
|
|
734
797
|
});
|
|
735
798
|
|
|
736
799
|
it("should handle base special field with not operator and array (NOT IN)", async () => {
|
|
737
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
738
|
-
|
|
739
|
-
|
|
800
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
801
|
+
where: { base: { not: ["literal://test1", "literal://test2"] } },
|
|
802
|
+
});
|
|
803
|
+
|
|
804
|
+
expect(query).toContain(
|
|
805
|
+
"uri NOT IN ['literal://test1', 'literal://test2']",
|
|
806
|
+
);
|
|
740
807
|
});
|
|
741
808
|
|
|
742
809
|
it("should handle base special field with between operator", async () => {
|
|
743
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
744
|
-
|
|
810
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
811
|
+
where: { base: { between: ["literal://a", "literal://z"] } },
|
|
812
|
+
} as any);
|
|
813
|
+
|
|
745
814
|
const normalized = normalizeQuery(query);
|
|
746
|
-
expect(normalized).toContain(
|
|
815
|
+
expect(normalized).toContain(
|
|
816
|
+
"uri >= 'literal://a' AND uri <= 'literal://z'",
|
|
817
|
+
);
|
|
747
818
|
});
|
|
748
819
|
|
|
749
820
|
it("should handle base special field with gt operator", async () => {
|
|
750
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
751
|
-
|
|
821
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
822
|
+
where: { base: { gt: "literal://m" } },
|
|
823
|
+
} as any);
|
|
824
|
+
|
|
752
825
|
expect(query).toContain("uri > 'literal://m'");
|
|
753
826
|
});
|
|
754
827
|
|
|
755
828
|
it("should handle base special field with gte and lte operators", async () => {
|
|
756
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
757
|
-
|
|
829
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
830
|
+
where: { base: { gte: "literal://a", lte: "literal://z" } },
|
|
831
|
+
} as any);
|
|
832
|
+
|
|
758
833
|
const normalized = normalizeQuery(query);
|
|
759
834
|
expect(normalized).toContain("uri >= 'literal://a'");
|
|
760
835
|
expect(normalized).toContain("uri <= 'literal://z'");
|
|
761
836
|
});
|
|
762
837
|
|
|
763
838
|
it("should handle timestamp special field with gt operator (filtered in JavaScript)", async () => {
|
|
764
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
839
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
840
|
+
where: { timestamp: { gt: 1234567890 } },
|
|
841
|
+
});
|
|
765
842
|
|
|
766
843
|
// timestamp filtering is done in JavaScript post-query
|
|
767
844
|
expect(query).toContain("FROM node");
|
|
@@ -772,8 +849,8 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
772
849
|
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
773
850
|
where: {
|
|
774
851
|
author: "did:key:alice",
|
|
775
|
-
timestamp: { gt: 1000 }
|
|
776
|
-
}
|
|
852
|
+
timestamp: { gt: 1000 },
|
|
853
|
+
},
|
|
777
854
|
});
|
|
778
855
|
|
|
779
856
|
// author and timestamp filtering is done in JavaScript post-query
|
|
@@ -787,13 +864,15 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
787
864
|
where: {
|
|
788
865
|
name: "Pasta",
|
|
789
866
|
author: "did:key:alice",
|
|
790
|
-
rating: { gt: 4 }
|
|
791
|
-
}
|
|
867
|
+
rating: { gt: 4 },
|
|
868
|
+
},
|
|
792
869
|
});
|
|
793
870
|
|
|
794
871
|
const normalized = normalizeQuery(query);
|
|
795
872
|
// Regular properties use graph traversal filters
|
|
796
|
-
expect(normalized).toContain(
|
|
873
|
+
expect(normalized).toContain(
|
|
874
|
+
"count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) = 'Pasta']) > 0",
|
|
875
|
+
);
|
|
797
876
|
// Comparison operators (gt) are filtered in JavaScript, not SQL
|
|
798
877
|
expect(normalized).not.toContain("out.uri > 4");
|
|
799
878
|
expect(normalized).not.toContain("target > 4");
|
|
@@ -802,87 +881,116 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
802
881
|
});
|
|
803
882
|
|
|
804
883
|
it("should handle boolean values", async () => {
|
|
805
|
-
@
|
|
884
|
+
@Model({ name: "Task" })
|
|
806
885
|
class Task extends Ad4mModel {
|
|
807
|
-
@Property({
|
|
886
|
+
@Property({
|
|
887
|
+
through: "task://completed",
|
|
888
|
+
required: true,
|
|
889
|
+
})
|
|
808
890
|
completed: boolean = false;
|
|
809
891
|
}
|
|
892
|
+
const query = await Task.queryToSurrealQL(mockPerspective, {
|
|
893
|
+
where: { completed: true },
|
|
894
|
+
});
|
|
810
895
|
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
expect(query).toContain("out.uri = true");
|
|
896
|
+
expect(query).toContain("fn::parse_literal(out.uri) = true");
|
|
814
897
|
});
|
|
815
898
|
|
|
816
899
|
it("should handle array of numbers", async () => {
|
|
817
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
818
|
-
|
|
819
|
-
|
|
900
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
901
|
+
where: { rating: [4, 5] },
|
|
902
|
+
});
|
|
903
|
+
|
|
904
|
+
expect(query).toContain(
|
|
905
|
+
"count(->link[WHERE predicate = 'recipe://rating' AND fn::parse_literal(out.uri) IN [4, 5]]) > 0",
|
|
906
|
+
);
|
|
820
907
|
});
|
|
821
908
|
|
|
822
909
|
it("should skip unknown properties in where clause", async () => {
|
|
823
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
824
|
-
|
|
910
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
911
|
+
where: { unknownProp: "value" } as any,
|
|
912
|
+
});
|
|
913
|
+
|
|
825
914
|
// Should not throw error, just skip the unknown property
|
|
826
915
|
expect(query).toContain("source");
|
|
827
|
-
|
|
828
|
-
// Should not contain any condition for unknownProp
|
|
829
|
-
expect(query).not.toContain("unknownProp");
|
|
830
|
-
});
|
|
831
916
|
|
|
832
|
-
|
|
833
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, { properties: ["name", "unknownProp"] as any });
|
|
834
|
-
|
|
835
|
-
// Should include name using aggregation
|
|
836
|
-
expect(query).toContain("array::first(target[WHERE predicate = 'recipe://name']) AS name");
|
|
837
|
-
// Should not error on unknownProp, just skip it
|
|
917
|
+
// Should not contain any condition for unknownProp
|
|
838
918
|
expect(query).not.toContain("unknownProp");
|
|
839
919
|
});
|
|
840
920
|
|
|
841
921
|
it("should handle order by regular property (handled in JavaScript)", async () => {
|
|
842
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
922
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
923
|
+
order: { name: "ASC" },
|
|
924
|
+
});
|
|
843
925
|
|
|
844
926
|
// ORDER BY is now handled in JavaScript post-query
|
|
927
|
+
// Note: the links correlated subquery uses ORDER BY timestamp ASC internally
|
|
845
928
|
expect(query).toContain("FROM node");
|
|
846
|
-
expect(query).not.toContain("ORDER BY");
|
|
929
|
+
expect(query).not.toContain("ORDER BY name");
|
|
847
930
|
});
|
|
848
931
|
|
|
849
|
-
it("should generate query with only
|
|
850
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
932
|
+
it("should generate query with only fields, no relations", async () => {
|
|
933
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
934
|
+
properties: ["name", "rating"],
|
|
935
|
+
});
|
|
851
936
|
|
|
852
937
|
// With array::group(), all link data is selected, filtering happens in instancesFromSurrealResult
|
|
853
|
-
expect(query).toContain(
|
|
854
|
-
|
|
938
|
+
expect(query).toContain(
|
|
939
|
+
"(SELECT predicate, out.uri AS target, author, timestamp FROM link WHERE in = $parent.id ORDER BY timestamp ASC) AS links",
|
|
940
|
+
);
|
|
855
941
|
});
|
|
856
942
|
|
|
857
|
-
it("should generate query with only
|
|
858
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
943
|
+
it("should generate query with only relations, no fields", async () => {
|
|
944
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
945
|
+
properties: [],
|
|
946
|
+
});
|
|
859
947
|
|
|
860
948
|
// With array::group(), all link data is selected, filtering happens in instancesFromSurrealResult
|
|
861
|
-
expect(query).toContain(
|
|
862
|
-
|
|
949
|
+
expect(query).toContain(
|
|
950
|
+
"(SELECT predicate, out.uri AS target, author, timestamp FROM link WHERE in = $parent.id ORDER BY timestamp ASC) AS links",
|
|
951
|
+
);
|
|
863
952
|
});
|
|
864
|
-
});
|
|
865
953
|
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
954
|
+
it("should include backward link filter when parent is given as raw predicate form", async () => {
|
|
955
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
956
|
+
parent: { id: "some://parent-uri", predicate: "custom://has_recipe" },
|
|
957
|
+
});
|
|
958
|
+
|
|
959
|
+
expect(query).toContain(
|
|
960
|
+
"count(<-link[WHERE predicate = 'custom://has_recipe' AND in.uri = 'some://parent-uri']) > 0",
|
|
961
|
+
);
|
|
962
|
+
});
|
|
963
|
+
|
|
964
|
+
it("should resolve model-backed parent form and include correct backward link filter", async () => {
|
|
965
|
+
@Model({ name: "MenuForQueryTest" })
|
|
966
|
+
class MenuForQueryTest extends Ad4mModel {
|
|
967
|
+
@HasMany(() => Recipe, { through: "menu://has_recipe" })
|
|
968
|
+
recipes: Recipe[] = [];
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
972
|
+
parent: {
|
|
973
|
+
id: "some://menu-uri",
|
|
974
|
+
model: MenuForQueryTest,
|
|
975
|
+
field: "recipes",
|
|
976
|
+
},
|
|
977
|
+
});
|
|
978
|
+
|
|
979
|
+
expect(query).toContain(
|
|
980
|
+
"count(<-link[WHERE predicate = 'menu://has_recipe' AND in.uri = 'some://menu-uri']) > 0",
|
|
981
|
+
);
|
|
982
|
+
});
|
|
983
|
+
});
|
|
879
984
|
|
|
985
|
+
describe("Ad4mModel.instancesFromSurrealResult() — hydration and query dispatch", () => {
|
|
880
986
|
// Mock perspective with both querySurrealDB and infer methods
|
|
881
987
|
const mockPerspective = {
|
|
882
988
|
querySurrealDB: jest.fn(),
|
|
883
989
|
infer: jest.fn(),
|
|
884
|
-
uuid:
|
|
885
|
-
stringOrTemplateObjectToSubjectClassName: jest
|
|
990
|
+
uuid: "test-perspective-uuid",
|
|
991
|
+
stringOrTemplateObjectToSubjectClassName: jest
|
|
992
|
+
.fn()
|
|
993
|
+
.mockResolvedValue("Recipe"),
|
|
886
994
|
} as any;
|
|
887
995
|
|
|
888
996
|
beforeEach(() => {
|
|
@@ -890,8 +998,12 @@ describe("Ad4mModel.instancesFromSurrealResult() and SurrealDB integration", ()
|
|
|
890
998
|
});
|
|
891
999
|
|
|
892
1000
|
it("should convert empty SurrealDB results correctly", async () => {
|
|
893
|
-
const result = await Recipe.instancesFromSurrealResult(
|
|
894
|
-
|
|
1001
|
+
const result = await Recipe.instancesFromSurrealResult(
|
|
1002
|
+
mockPerspective,
|
|
1003
|
+
{},
|
|
1004
|
+
[],
|
|
1005
|
+
);
|
|
1006
|
+
|
|
895
1007
|
expect(result.results).toEqual([]);
|
|
896
1008
|
expect(result.totalCount).toBe(0);
|
|
897
1009
|
});
|
|
@@ -902,27 +1014,81 @@ describe("Ad4mModel.instancesFromSurrealResult() and SurrealDB integration", ()
|
|
|
902
1014
|
source: "node:abc123",
|
|
903
1015
|
source_uri: "literal://recipe1",
|
|
904
1016
|
links: [
|
|
905
|
-
{
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
1017
|
+
{
|
|
1018
|
+
predicate: "recipe://name",
|
|
1019
|
+
target: Literal.from("Pasta").toUrl(),
|
|
1020
|
+
author: "did:key:alice",
|
|
1021
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1022
|
+
},
|
|
1023
|
+
{
|
|
1024
|
+
predicate: "recipe://rating",
|
|
1025
|
+
target: Literal.from(5).toUrl(),
|
|
1026
|
+
author: "did:key:alice",
|
|
1027
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1028
|
+
},
|
|
1029
|
+
{
|
|
1030
|
+
predicate: "recipe://ingredient",
|
|
1031
|
+
target: "pasta",
|
|
1032
|
+
author: "did:key:alice",
|
|
1033
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1034
|
+
},
|
|
1035
|
+
{
|
|
1036
|
+
predicate: "recipe://ingredient",
|
|
1037
|
+
target: "tomato",
|
|
1038
|
+
author: "did:key:alice",
|
|
1039
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1040
|
+
},
|
|
1041
|
+
{
|
|
1042
|
+
predicate: "recipe://ingredient",
|
|
1043
|
+
target: "cheese",
|
|
1044
|
+
author: "did:key:alice",
|
|
1045
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1046
|
+
},
|
|
1047
|
+
],
|
|
911
1048
|
},
|
|
912
1049
|
{
|
|
913
1050
|
source: "node:def456",
|
|
914
1051
|
source_uri: "literal://recipe2",
|
|
915
1052
|
links: [
|
|
916
|
-
{
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
1053
|
+
{
|
|
1054
|
+
predicate: "recipe://name",
|
|
1055
|
+
target: Literal.from("Pizza").toUrl(),
|
|
1056
|
+
author: "did:key:bob",
|
|
1057
|
+
timestamp: "2023-01-02T00:00:00Z",
|
|
1058
|
+
},
|
|
1059
|
+
{
|
|
1060
|
+
predicate: "recipe://rating",
|
|
1061
|
+
target: Literal.from(4).toUrl(),
|
|
1062
|
+
author: "did:key:bob",
|
|
1063
|
+
timestamp: "2023-01-02T00:00:00Z",
|
|
1064
|
+
},
|
|
1065
|
+
{
|
|
1066
|
+
predicate: "recipe://ingredient",
|
|
1067
|
+
target: "dough",
|
|
1068
|
+
author: "did:key:bob",
|
|
1069
|
+
timestamp: "2023-01-02T00:00:00Z",
|
|
1070
|
+
},
|
|
1071
|
+
{
|
|
1072
|
+
predicate: "recipe://ingredient",
|
|
1073
|
+
target: "cheese",
|
|
1074
|
+
author: "did:key:bob",
|
|
1075
|
+
timestamp: "2023-01-02T00:00:00Z",
|
|
1076
|
+
},
|
|
1077
|
+
{
|
|
1078
|
+
predicate: "recipe://ingredient",
|
|
1079
|
+
target: "tomato",
|
|
1080
|
+
author: "did:key:bob",
|
|
1081
|
+
timestamp: "2023-01-02T00:00:00Z",
|
|
1082
|
+
},
|
|
1083
|
+
],
|
|
1084
|
+
},
|
|
923
1085
|
];
|
|
924
1086
|
|
|
925
|
-
const result = await Recipe.instancesFromSurrealResult(
|
|
1087
|
+
const result = await Recipe.instancesFromSurrealResult(
|
|
1088
|
+
mockPerspective,
|
|
1089
|
+
{},
|
|
1090
|
+
surrealResults,
|
|
1091
|
+
);
|
|
926
1092
|
|
|
927
1093
|
expect(result.results).toHaveLength(2);
|
|
928
1094
|
expect(result.totalCount).toBe(2);
|
|
@@ -939,78 +1105,221 @@ describe("Ad4mModel.instancesFromSurrealResult() and SurrealDB integration", ()
|
|
|
939
1105
|
expect(recipe2.rating).toBe(4);
|
|
940
1106
|
});
|
|
941
1107
|
|
|
942
|
-
it("should
|
|
943
|
-
const
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
1108
|
+
it("should set instance id from source_uri", async () => {
|
|
1109
|
+
const result = await Recipe.instancesFromSurrealResult(
|
|
1110
|
+
mockPerspective,
|
|
1111
|
+
{},
|
|
1112
|
+
[
|
|
1113
|
+
{
|
|
1114
|
+
source: "node:abc123",
|
|
1115
|
+
source_uri: "literal://recipe1",
|
|
1116
|
+
links: [
|
|
1117
|
+
{
|
|
1118
|
+
predicate: "recipe://name",
|
|
1119
|
+
target: Literal.from("Pasta").toUrl(),
|
|
1120
|
+
author: "did:key:alice",
|
|
1121
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1122
|
+
},
|
|
1123
|
+
{
|
|
1124
|
+
predicate: "recipe://rating",
|
|
1125
|
+
target: Literal.from(5).toUrl(),
|
|
1126
|
+
author: "did:key:alice",
|
|
1127
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1128
|
+
},
|
|
1129
|
+
],
|
|
1130
|
+
},
|
|
1131
|
+
],
|
|
1132
|
+
);
|
|
955
1133
|
|
|
1134
|
+
expect(result.results).toHaveLength(1);
|
|
1135
|
+
// id is derived from source_uri (the node's URI in the graph)
|
|
1136
|
+
expect(result.results[0].id).toBe("literal://recipe1");
|
|
1137
|
+
});
|
|
1138
|
+
|
|
1139
|
+
it("should use latest-wins semantics when multiple links share a predicate", async () => {
|
|
956
1140
|
const result = await Recipe.instancesFromSurrealResult(
|
|
957
1141
|
mockPerspective,
|
|
958
|
-
{
|
|
959
|
-
|
|
1142
|
+
{},
|
|
1143
|
+
[
|
|
1144
|
+
{
|
|
1145
|
+
source: "node:abc123",
|
|
1146
|
+
source_uri: "literal://recipe1",
|
|
1147
|
+
links: [
|
|
1148
|
+
{
|
|
1149
|
+
predicate: "recipe://name",
|
|
1150
|
+
target: Literal.from("Old Name").toUrl(),
|
|
1151
|
+
author: "did:key:alice",
|
|
1152
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1153
|
+
},
|
|
1154
|
+
{
|
|
1155
|
+
// later timestamp — this value should win
|
|
1156
|
+
predicate: "recipe://name",
|
|
1157
|
+
target: Literal.from("New Name").toUrl(),
|
|
1158
|
+
author: "did:key:alice",
|
|
1159
|
+
timestamp: "2023-01-02T00:00:00Z",
|
|
1160
|
+
},
|
|
1161
|
+
{
|
|
1162
|
+
predicate: "recipe://rating",
|
|
1163
|
+
target: Literal.from(5).toUrl(),
|
|
1164
|
+
author: "did:key:alice",
|
|
1165
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1166
|
+
},
|
|
1167
|
+
],
|
|
1168
|
+
},
|
|
1169
|
+
],
|
|
960
1170
|
);
|
|
961
1171
|
|
|
962
1172
|
expect(result.results).toHaveLength(1);
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
1173
|
+
expect(result.results[0].name).toBe("New Name");
|
|
1174
|
+
});
|
|
1175
|
+
|
|
1176
|
+
it("should apply transform function during hydration", async () => {
|
|
1177
|
+
@Model({ name: "TransformRecipe" })
|
|
1178
|
+
class TransformRecipe extends Ad4mModel {
|
|
1179
|
+
@Property({
|
|
1180
|
+
through: "recipe://name",
|
|
1181
|
+
transform: (v: string) => v.toUpperCase(),
|
|
1182
|
+
})
|
|
1183
|
+
name: string = "";
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
const result = await TransformRecipe.instancesFromSurrealResult(
|
|
1187
|
+
mockPerspective,
|
|
1188
|
+
{},
|
|
1189
|
+
[
|
|
1190
|
+
{
|
|
1191
|
+
source: "node:abc123",
|
|
1192
|
+
source_uri: "literal://tr1",
|
|
1193
|
+
links: [
|
|
1194
|
+
{
|
|
1195
|
+
predicate: "recipe://name",
|
|
1196
|
+
target: Literal.from("pasta").toUrl(),
|
|
1197
|
+
author: "did:key:alice",
|
|
1198
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1199
|
+
},
|
|
1200
|
+
],
|
|
1201
|
+
},
|
|
1202
|
+
],
|
|
1203
|
+
);
|
|
1204
|
+
|
|
1205
|
+
expect(result.results).toHaveLength(1);
|
|
1206
|
+
expect(result.results[0].name).toBe("PASTA");
|
|
1207
|
+
});
|
|
1208
|
+
|
|
1209
|
+
it("should apply JS-level operator filters — smoke test", async () => {
|
|
1210
|
+
// Ratings 1–5 encoded as Literals so the JS filter receives actual numbers
|
|
1211
|
+
const surrealResults = Array.from({ length: 5 }, (_, i) => ({
|
|
1212
|
+
source: `node:abc${i + 1}`,
|
|
1213
|
+
source_uri: `literal://recipe${i + 1}`,
|
|
1214
|
+
links: [
|
|
1215
|
+
{
|
|
1216
|
+
predicate: "recipe://name",
|
|
1217
|
+
target: Literal.from(`Recipe ${i + 1}`).toUrl(),
|
|
1218
|
+
author: "did:key:alice",
|
|
1219
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1220
|
+
},
|
|
1221
|
+
{
|
|
1222
|
+
predicate: "recipe://rating",
|
|
1223
|
+
target: Literal.from(i + 1).toUrl(),
|
|
1224
|
+
author: "did:key:alice",
|
|
1225
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1226
|
+
},
|
|
1227
|
+
],
|
|
1228
|
+
}));
|
|
1229
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1230
|
+
|
|
1231
|
+
// Only ratings 4 and 5 pass the JS-level gt filter
|
|
1232
|
+
const results = await Recipe.findAll(mockPerspective, {
|
|
1233
|
+
where: { rating: { gt: 3 } },
|
|
1234
|
+
});
|
|
1235
|
+
expect(results).toHaveLength(2);
|
|
1236
|
+
|
|
1237
|
+
// count() must be consistent with findAll()
|
|
1238
|
+
const count = await Recipe.count(mockPerspective, {
|
|
1239
|
+
where: { rating: { gt: 3 } },
|
|
1240
|
+
});
|
|
1241
|
+
expect(count).toBe(2);
|
|
972
1242
|
});
|
|
973
1243
|
|
|
974
|
-
it("should filter
|
|
1244
|
+
it("should filter fields when query specifies fields", async () => {
|
|
975
1245
|
const surrealResults = [
|
|
976
1246
|
{
|
|
977
1247
|
source: "node:abc123",
|
|
978
1248
|
source_uri: "literal://recipe1",
|
|
979
1249
|
links: [
|
|
980
|
-
{
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
1250
|
+
{
|
|
1251
|
+
predicate: "recipe://name",
|
|
1252
|
+
target: "Pasta",
|
|
1253
|
+
author: "did:key:alice",
|
|
1254
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1255
|
+
},
|
|
1256
|
+
{
|
|
1257
|
+
predicate: "recipe://rating",
|
|
1258
|
+
target: "5",
|
|
1259
|
+
author: "did:key:alice",
|
|
1260
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1261
|
+
},
|
|
1262
|
+
{
|
|
1263
|
+
predicate: "recipe://ingredient",
|
|
1264
|
+
target: "pasta",
|
|
1265
|
+
author: "did:key:alice",
|
|
1266
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1267
|
+
},
|
|
1268
|
+
{
|
|
1269
|
+
predicate: "recipe://ingredient",
|
|
1270
|
+
target: "tomato",
|
|
1271
|
+
author: "did:key:alice",
|
|
1272
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1273
|
+
},
|
|
1274
|
+
],
|
|
1275
|
+
},
|
|
986
1276
|
];
|
|
987
1277
|
|
|
988
1278
|
const result = await Recipe.instancesFromSurrealResult(
|
|
989
1279
|
mockPerspective,
|
|
990
|
-
{
|
|
991
|
-
surrealResults
|
|
1280
|
+
{ properties: ["name"] },
|
|
1281
|
+
surrealResults,
|
|
992
1282
|
);
|
|
993
1283
|
|
|
994
1284
|
expect(result.results).toHaveLength(1);
|
|
995
1285
|
const recipe = result.results[0];
|
|
996
|
-
expect(recipe.
|
|
997
|
-
//
|
|
998
|
-
expect(recipe.name).toBeUndefined();
|
|
1286
|
+
expect(recipe.name).toBe("Pasta");
|
|
1287
|
+
// rating and ingredients should be removed since only "name" was requested
|
|
999
1288
|
expect(recipe.rating).toBeUndefined();
|
|
1289
|
+
expect(recipe.ingredients).toBeUndefined();
|
|
1290
|
+
// author and timestamp are metadata fields — they are also stripped unless
|
|
1291
|
+
// explicitly listed in `properties`. Integration tests cover the
|
|
1292
|
+
// `properties: ["author", "createdAt"]` case.
|
|
1293
|
+
expect(recipe.author).toBeUndefined();
|
|
1294
|
+
expect(recipe.createdAt).toBeUndefined();
|
|
1000
1295
|
});
|
|
1001
1296
|
|
|
1002
1297
|
it("should handle results missing base field", async () => {
|
|
1003
1298
|
const surrealResults = [
|
|
1004
1299
|
{
|
|
1005
1300
|
links: [
|
|
1006
|
-
{
|
|
1007
|
-
|
|
1008
|
-
|
|
1301
|
+
{
|
|
1302
|
+
predicate: "recipe://name",
|
|
1303
|
+
target: "Pasta",
|
|
1304
|
+
author: "did:key:alice",
|
|
1305
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1306
|
+
},
|
|
1307
|
+
{
|
|
1308
|
+
predicate: "recipe://rating",
|
|
1309
|
+
target: "5",
|
|
1310
|
+
author: "did:key:alice",
|
|
1311
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1312
|
+
},
|
|
1313
|
+
],
|
|
1009
1314
|
// Missing source field
|
|
1010
|
-
} as any
|
|
1315
|
+
} as any,
|
|
1011
1316
|
];
|
|
1012
1317
|
|
|
1013
|
-
const result = await Recipe.instancesFromSurrealResult(
|
|
1318
|
+
const result = await Recipe.instancesFromSurrealResult(
|
|
1319
|
+
mockPerspective,
|
|
1320
|
+
{},
|
|
1321
|
+
surrealResults,
|
|
1322
|
+
);
|
|
1014
1323
|
|
|
1015
1324
|
// Should filter out the invalid result (or handle gracefully)
|
|
1016
1325
|
expect(result.results).toHaveLength(0);
|
|
@@ -1023,11 +1332,26 @@ describe("Ad4mModel.instancesFromSurrealResult() and SurrealDB integration", ()
|
|
|
1023
1332
|
source: "node:abc123",
|
|
1024
1333
|
source_uri: "literal://recipe1",
|
|
1025
1334
|
links: [
|
|
1026
|
-
{
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1335
|
+
{
|
|
1336
|
+
predicate: "recipe://name",
|
|
1337
|
+
target: "Pasta",
|
|
1338
|
+
author: "did:key:alice",
|
|
1339
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1340
|
+
},
|
|
1341
|
+
{
|
|
1342
|
+
predicate: "recipe://rating",
|
|
1343
|
+
target: "5",
|
|
1344
|
+
author: "did:key:alice",
|
|
1345
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1346
|
+
},
|
|
1347
|
+
{
|
|
1348
|
+
predicate: "recipe://ingredient",
|
|
1349
|
+
target: "pasta",
|
|
1350
|
+
author: "did:key:alice",
|
|
1351
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1352
|
+
},
|
|
1353
|
+
],
|
|
1354
|
+
},
|
|
1031
1355
|
];
|
|
1032
1356
|
|
|
1033
1357
|
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
@@ -1040,39 +1364,40 @@ describe("Ad4mModel.instancesFromSurrealResult() and SurrealDB integration", ()
|
|
|
1040
1364
|
expect(results[0].name).toBe("Pasta");
|
|
1041
1365
|
});
|
|
1042
1366
|
|
|
1043
|
-
it("should use Prolog when useSurrealDB is false in findAll()", async () => {
|
|
1044
|
-
const prologResults = [{
|
|
1045
|
-
AllInstances: [
|
|
1046
|
-
["literal://recipe1", [["name", "Pasta"]], [["ingredients", ["pasta"]]], "2023-01-01T00:00:00Z", "did:key:alice"]
|
|
1047
|
-
],
|
|
1048
|
-
TotalCount: 1
|
|
1049
|
-
}];
|
|
1050
|
-
|
|
1051
|
-
mockPerspective.infer.mockResolvedValue(prologResults);
|
|
1052
|
-
|
|
1053
|
-
const results = await Recipe.findAll(mockPerspective, {}, false);
|
|
1054
|
-
|
|
1055
|
-
expect(mockPerspective.infer).toHaveBeenCalledTimes(1);
|
|
1056
|
-
expect(mockPerspective.querySurrealDB).not.toHaveBeenCalled();
|
|
1057
|
-
expect(results).toHaveLength(1);
|
|
1058
|
-
});
|
|
1059
|
-
|
|
1060
1367
|
it("should use SurrealDB by default in findAllAndCount()", async () => {
|
|
1061
1368
|
const surrealResults = [
|
|
1062
1369
|
{
|
|
1063
1370
|
source: "node:abc123",
|
|
1064
1371
|
source_uri: "literal://recipe1",
|
|
1065
1372
|
links: [
|
|
1066
|
-
{
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1373
|
+
{
|
|
1374
|
+
predicate: "recipe://name",
|
|
1375
|
+
target: "Pasta",
|
|
1376
|
+
author: "did:key:alice",
|
|
1377
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1378
|
+
},
|
|
1379
|
+
{
|
|
1380
|
+
predicate: "recipe://rating",
|
|
1381
|
+
target: "5",
|
|
1382
|
+
author: "did:key:alice",
|
|
1383
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1384
|
+
},
|
|
1385
|
+
{
|
|
1386
|
+
predicate: "recipe://ingredient",
|
|
1387
|
+
target: "pasta",
|
|
1388
|
+
author: "did:key:alice",
|
|
1389
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1390
|
+
},
|
|
1391
|
+
],
|
|
1392
|
+
},
|
|
1071
1393
|
];
|
|
1072
1394
|
|
|
1073
1395
|
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1074
1396
|
|
|
1075
|
-
const { results, totalCount } = await Recipe.findAllAndCount(
|
|
1397
|
+
const { results, totalCount } = await Recipe.findAllAndCount(
|
|
1398
|
+
mockPerspective,
|
|
1399
|
+
{},
|
|
1400
|
+
);
|
|
1076
1401
|
|
|
1077
1402
|
expect(mockPerspective.querySurrealDB).toHaveBeenCalledTimes(1);
|
|
1078
1403
|
expect(mockPerspective.infer).not.toHaveBeenCalled();
|
|
@@ -1086,11 +1411,26 @@ describe("Ad4mModel.instancesFromSurrealResult() and SurrealDB integration", ()
|
|
|
1086
1411
|
source: "node:abc123",
|
|
1087
1412
|
source_uri: "literal://recipe1",
|
|
1088
1413
|
links: [
|
|
1089
|
-
{
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1414
|
+
{
|
|
1415
|
+
predicate: "recipe://name",
|
|
1416
|
+
target: "Pasta",
|
|
1417
|
+
author: "did:key:alice",
|
|
1418
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1419
|
+
},
|
|
1420
|
+
{
|
|
1421
|
+
predicate: "recipe://rating",
|
|
1422
|
+
target: "5",
|
|
1423
|
+
author: "did:key:alice",
|
|
1424
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1425
|
+
},
|
|
1426
|
+
{
|
|
1427
|
+
predicate: "recipe://ingredient",
|
|
1428
|
+
target: "pasta",
|
|
1429
|
+
author: "did:key:alice",
|
|
1430
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1431
|
+
},
|
|
1432
|
+
],
|
|
1433
|
+
},
|
|
1094
1434
|
];
|
|
1095
1435
|
|
|
1096
1436
|
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
@@ -1108,12 +1448,22 @@ describe("Ad4mModel.instancesFromSurrealResult() and SurrealDB integration", ()
|
|
|
1108
1448
|
// Since count() uses result.length and GROUP BY returns one row per source,
|
|
1109
1449
|
// mock 5 recipe sources
|
|
1110
1450
|
const surrealResults = Array.from({ length: 5 }, (_, i) => ({
|
|
1111
|
-
source: `node:abc${i+1}`,
|
|
1112
|
-
source_uri: `literal://recipe${i+1}`,
|
|
1451
|
+
source: `node:abc${i + 1}`,
|
|
1452
|
+
source_uri: `literal://recipe${i + 1}`,
|
|
1113
1453
|
links: [
|
|
1114
|
-
{
|
|
1115
|
-
|
|
1116
|
-
|
|
1454
|
+
{
|
|
1455
|
+
predicate: "recipe://name",
|
|
1456
|
+
target: `Recipe ${i + 1}`,
|
|
1457
|
+
author: "did:key:alice",
|
|
1458
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1459
|
+
},
|
|
1460
|
+
{
|
|
1461
|
+
predicate: "recipe://rating",
|
|
1462
|
+
target: "5",
|
|
1463
|
+
author: "did:key:alice",
|
|
1464
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1465
|
+
},
|
|
1466
|
+
],
|
|
1117
1467
|
}));
|
|
1118
1468
|
|
|
1119
1469
|
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
@@ -1125,28 +1475,32 @@ describe("Ad4mModel.instancesFromSurrealResult() and SurrealDB integration", ()
|
|
|
1125
1475
|
expect(count).toBe(5);
|
|
1126
1476
|
});
|
|
1127
1477
|
|
|
1128
|
-
it("should use Prolog when useSurrealDB is false in count()", async () => {
|
|
1129
|
-
const prologResults = [{ TotalCount: 10 }];
|
|
1130
|
-
mockPerspective.infer.mockResolvedValue(prologResults);
|
|
1131
|
-
|
|
1132
|
-
const count = await Recipe.count(mockPerspective, {}, false);
|
|
1133
|
-
|
|
1134
|
-
expect(mockPerspective.infer).toHaveBeenCalledTimes(1);
|
|
1135
|
-
expect(mockPerspective.querySurrealDB).not.toHaveBeenCalled();
|
|
1136
|
-
expect(count).toBe(10);
|
|
1137
|
-
});
|
|
1138
|
-
|
|
1139
1478
|
it("should use SurrealDB by default in ModelQueryBuilder.get()", async () => {
|
|
1140
1479
|
const surrealResults = [
|
|
1141
1480
|
{
|
|
1142
1481
|
source: "node:abc123",
|
|
1143
1482
|
source_uri: "literal://recipe1",
|
|
1144
1483
|
links: [
|
|
1145
|
-
{
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1484
|
+
{
|
|
1485
|
+
predicate: "recipe://name",
|
|
1486
|
+
target: "Pasta",
|
|
1487
|
+
author: "did:key:alice",
|
|
1488
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1489
|
+
},
|
|
1490
|
+
{
|
|
1491
|
+
predicate: "recipe://rating",
|
|
1492
|
+
target: "5",
|
|
1493
|
+
author: "did:key:alice",
|
|
1494
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1495
|
+
},
|
|
1496
|
+
{
|
|
1497
|
+
predicate: "recipe://ingredient",
|
|
1498
|
+
target: "pasta",
|
|
1499
|
+
author: "did:key:alice",
|
|
1500
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1501
|
+
},
|
|
1502
|
+
],
|
|
1503
|
+
},
|
|
1150
1504
|
];
|
|
1151
1505
|
|
|
1152
1506
|
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
@@ -1161,35 +1515,25 @@ describe("Ad4mModel.instancesFromSurrealResult() and SurrealDB integration", ()
|
|
|
1161
1515
|
expect(results[0].name).toBe("Pasta");
|
|
1162
1516
|
});
|
|
1163
1517
|
|
|
1164
|
-
it("should use Prolog when useSurrealDB(false) in ModelQueryBuilder.get()", async () => {
|
|
1165
|
-
const prologResults = [{
|
|
1166
|
-
AllInstances: [
|
|
1167
|
-
["literal://recipe1", [["name", "Pasta"]], [["ingredients", ["pasta"]]], "2023-01-01T00:00:00Z", "did:key:alice"]
|
|
1168
|
-
],
|
|
1169
|
-
TotalCount: 1
|
|
1170
|
-
}];
|
|
1171
|
-
|
|
1172
|
-
mockPerspective.infer.mockResolvedValue(prologResults);
|
|
1173
|
-
|
|
1174
|
-
const results = await Recipe.query(mockPerspective)
|
|
1175
|
-
.where({ name: "Pasta" })
|
|
1176
|
-
.useSurrealDB(false)
|
|
1177
|
-
.get();
|
|
1178
|
-
|
|
1179
|
-
expect(mockPerspective.infer).toHaveBeenCalledTimes(1);
|
|
1180
|
-
expect(mockPerspective.querySurrealDB).not.toHaveBeenCalled();
|
|
1181
|
-
expect(results).toHaveLength(1);
|
|
1182
|
-
});
|
|
1183
|
-
|
|
1184
1518
|
it("should use SurrealDB by default in ModelQueryBuilder.count()", async () => {
|
|
1185
1519
|
// count() counts the number of rows returned by the query (one row per source)
|
|
1186
1520
|
const surrealResults = Array.from({ length: 3 }, (_, i) => ({
|
|
1187
|
-
source: `node:abc${i+1}`,
|
|
1188
|
-
source_uri: `literal://recipe${i+1}`,
|
|
1521
|
+
source: `node:abc${i + 1}`,
|
|
1522
|
+
source_uri: `literal://recipe${i + 1}`,
|
|
1189
1523
|
links: [
|
|
1190
|
-
{
|
|
1191
|
-
|
|
1192
|
-
|
|
1524
|
+
{
|
|
1525
|
+
predicate: "recipe://name",
|
|
1526
|
+
target: `Recipe ${i + 1}`,
|
|
1527
|
+
author: "did:key:alice",
|
|
1528
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1529
|
+
},
|
|
1530
|
+
{
|
|
1531
|
+
predicate: "recipe://rating",
|
|
1532
|
+
target: "5",
|
|
1533
|
+
author: "did:key:alice",
|
|
1534
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1535
|
+
},
|
|
1536
|
+
],
|
|
1193
1537
|
}));
|
|
1194
1538
|
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1195
1539
|
|
|
@@ -1208,11 +1552,26 @@ describe("Ad4mModel.instancesFromSurrealResult() and SurrealDB integration", ()
|
|
|
1208
1552
|
source: "node:abc123",
|
|
1209
1553
|
source_uri: "literal://recipe1",
|
|
1210
1554
|
links: [
|
|
1211
|
-
{
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1555
|
+
{
|
|
1556
|
+
predicate: "recipe://name",
|
|
1557
|
+
target: "Pasta",
|
|
1558
|
+
author: "did:key:alice",
|
|
1559
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1560
|
+
},
|
|
1561
|
+
{
|
|
1562
|
+
predicate: "recipe://rating",
|
|
1563
|
+
target: "5",
|
|
1564
|
+
author: "did:key:alice",
|
|
1565
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1566
|
+
},
|
|
1567
|
+
{
|
|
1568
|
+
predicate: "recipe://ingredient",
|
|
1569
|
+
target: "pasta",
|
|
1570
|
+
author: "did:key:alice",
|
|
1571
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1572
|
+
},
|
|
1573
|
+
],
|
|
1574
|
+
},
|
|
1216
1575
|
];
|
|
1217
1576
|
|
|
1218
1577
|
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
@@ -1229,248 +1588,370 @@ describe("Ad4mModel.instancesFromSurrealResult() and SurrealDB integration", ()
|
|
|
1229
1588
|
});
|
|
1230
1589
|
});
|
|
1231
1590
|
|
|
1232
|
-
describe("
|
|
1233
|
-
//
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1591
|
+
describe("resolveLanguage implicit literal default", () => {
|
|
1592
|
+
// A plain @Property with no resolveLanguage should behave identically to
|
|
1593
|
+
// resolveLanguage: "literal" — scalar values encoded as literal:// URIs
|
|
1594
|
+
// are unwrapped on read, and no createExpression call is made on write.
|
|
1595
|
+
@Model({ name: "ImplicitLiteralModel" })
|
|
1596
|
+
class ImplicitLiteralModel extends Ad4mModel {
|
|
1597
|
+
@Property({ through: "test://name" })
|
|
1237
1598
|
name: string = "";
|
|
1238
|
-
|
|
1239
|
-
@Property({ through: "
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
@
|
|
1243
|
-
|
|
1599
|
+
|
|
1600
|
+
@Property({ through: "test://count" })
|
|
1601
|
+
count: number = 0;
|
|
1602
|
+
|
|
1603
|
+
@Property({ through: "test://active" })
|
|
1604
|
+
active: boolean = false;
|
|
1244
1605
|
}
|
|
1245
1606
|
|
|
1246
|
-
// Mock perspective
|
|
1247
1607
|
const mockPerspective = {
|
|
1248
1608
|
querySurrealDB: jest.fn(),
|
|
1249
1609
|
infer: jest.fn(),
|
|
1250
|
-
uuid:
|
|
1251
|
-
stringOrTemplateObjectToSubjectClassName: jest
|
|
1610
|
+
uuid: "test-perspective-uuid",
|
|
1611
|
+
stringOrTemplateObjectToSubjectClassName: jest
|
|
1612
|
+
.fn()
|
|
1613
|
+
.mockResolvedValue("ImplicitLiteralModel"),
|
|
1252
1614
|
} as any;
|
|
1253
1615
|
|
|
1254
|
-
beforeEach(() =>
|
|
1255
|
-
|
|
1616
|
+
beforeEach(() => jest.clearAllMocks());
|
|
1617
|
+
|
|
1618
|
+
it('should store undefined resolveLanguage in metadata (not "literal")', () => {
|
|
1619
|
+
const metadata = ImplicitLiteralModel.getModelMetadata();
|
|
1620
|
+
expect(metadata.properties.name.resolveLanguage).toBeUndefined();
|
|
1621
|
+
expect(metadata.properties.count.resolveLanguage).toBeUndefined();
|
|
1622
|
+
expect(metadata.properties.active.resolveLanguage).toBeUndefined();
|
|
1256
1623
|
});
|
|
1257
1624
|
|
|
1258
|
-
it("should
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1625
|
+
it("should hydrate literal:// encoded targets for properties with no resolveLanguage", async () => {
|
|
1626
|
+
const surrealResults = [
|
|
1627
|
+
{
|
|
1628
|
+
source: "node:abc123",
|
|
1629
|
+
source_uri: "literal://base",
|
|
1630
|
+
links: [
|
|
1631
|
+
{
|
|
1632
|
+
predicate: "test://name",
|
|
1633
|
+
target: Literal.from("hello world").toUrl(),
|
|
1634
|
+
author: "did:key:alice",
|
|
1635
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1636
|
+
},
|
|
1637
|
+
{
|
|
1638
|
+
predicate: "test://count",
|
|
1639
|
+
target: Literal.from(42).toUrl(),
|
|
1640
|
+
author: "did:key:alice",
|
|
1641
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1642
|
+
},
|
|
1643
|
+
{
|
|
1644
|
+
predicate: "test://active",
|
|
1645
|
+
target: Literal.from(true).toUrl(),
|
|
1646
|
+
author: "did:key:alice",
|
|
1647
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1648
|
+
},
|
|
1649
|
+
],
|
|
1650
|
+
},
|
|
1651
|
+
];
|
|
1652
|
+
|
|
1269
1653
|
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1270
1654
|
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
expect(
|
|
1278
|
-
|
|
1655
|
+
const result = await ImplicitLiteralModel.instancesFromSurrealResult(
|
|
1656
|
+
mockPerspective,
|
|
1657
|
+
{},
|
|
1658
|
+
surrealResults,
|
|
1659
|
+
);
|
|
1660
|
+
|
|
1661
|
+
expect(result.results).toHaveLength(1);
|
|
1662
|
+
const instance = result.results[0];
|
|
1663
|
+
expect(instance.name).toBe("hello world");
|
|
1664
|
+
expect(instance.count).toBe(42);
|
|
1665
|
+
expect(instance.active).toBe(true);
|
|
1279
1666
|
});
|
|
1280
1667
|
|
|
1281
|
-
it("should
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1668
|
+
it("should NOT call perspective.getExpression for properties with no resolveLanguage", async () => {
|
|
1669
|
+
const surrealResults = [
|
|
1670
|
+
{
|
|
1671
|
+
source: "node:abc123",
|
|
1672
|
+
source_uri: "literal://base",
|
|
1673
|
+
links: [
|
|
1674
|
+
{
|
|
1675
|
+
predicate: "test://name",
|
|
1676
|
+
target: Literal.from("test").toUrl(),
|
|
1677
|
+
author: "did:key:alice",
|
|
1678
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1679
|
+
},
|
|
1680
|
+
],
|
|
1681
|
+
},
|
|
1682
|
+
];
|
|
1683
|
+
|
|
1292
1684
|
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1685
|
+
const getExpression = jest.fn();
|
|
1686
|
+
mockPerspective.getExpression = getExpression;
|
|
1293
1687
|
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
expect(
|
|
1301
|
-
expect(count).toBe(findAllResults.length);
|
|
1688
|
+
await ImplicitLiteralModel.instancesFromSurrealResult(
|
|
1689
|
+
mockPerspective,
|
|
1690
|
+
{},
|
|
1691
|
+
surrealResults,
|
|
1692
|
+
);
|
|
1693
|
+
|
|
1694
|
+
expect(getExpression).not.toHaveBeenCalled();
|
|
1302
1695
|
});
|
|
1696
|
+
});
|
|
1303
1697
|
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
source: `node:abc${i+1}`,
|
|
1308
|
-
source_uri: `literal://recipe${i+1}`,
|
|
1309
|
-
links: [
|
|
1310
|
-
{ predicate: "recipe://name", target: `Recipe ${i+1}`, author: "did:key:alice", timestamp: `2023-01-0${i+1}T00:00:00Z` },
|
|
1311
|
-
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: `2023-01-0${i+1}T00:00:00Z` }
|
|
1312
|
-
]
|
|
1313
|
-
}));
|
|
1314
|
-
|
|
1315
|
-
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1698
|
+
describe("resolveParentPredicate()", () => {
|
|
1699
|
+
@Model({ name: "Comment" })
|
|
1700
|
+
class Comment extends Ad4mModel {}
|
|
1316
1701
|
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1702
|
+
@Model({ name: "Tag" })
|
|
1703
|
+
class Tag extends Ad4mModel {}
|
|
1704
|
+
|
|
1705
|
+
@Model({ name: "Channel" })
|
|
1706
|
+
class Channel extends Ad4mModel {
|
|
1707
|
+
@HasMany(() => Comment, { through: "channel://has_comment" })
|
|
1708
|
+
comments: Comment[] = [];
|
|
1709
|
+
|
|
1710
|
+
@HasMany(() => Tag, { through: "channel://has_tag" })
|
|
1711
|
+
tags: Tag[] = [];
|
|
1712
|
+
}
|
|
1713
|
+
|
|
1714
|
+
@Model({ name: "AmbiguousParent" })
|
|
1715
|
+
class AmbiguousParent extends Ad4mModel {
|
|
1716
|
+
@HasMany(() => Comment, { through: "parent://primary_comments" })
|
|
1717
|
+
primaryComments: Comment[] = [];
|
|
1718
|
+
|
|
1719
|
+
@HasMany(() => Comment, { through: "parent://secondary_comments" })
|
|
1720
|
+
secondaryComments: Comment[] = [];
|
|
1721
|
+
}
|
|
1722
|
+
|
|
1723
|
+
it("resolves predicate by explicit field name", () => {
|
|
1724
|
+
const meta = Channel.getModelMetadata();
|
|
1725
|
+
expect(resolveParentPredicate(meta, Comment, "comments")).toBe(
|
|
1726
|
+
"channel://has_comment",
|
|
1727
|
+
);
|
|
1326
1728
|
});
|
|
1327
1729
|
|
|
1328
|
-
it("
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1730
|
+
it("infers predicate from child constructor when field is omitted", () => {
|
|
1731
|
+
const meta = Channel.getModelMetadata();
|
|
1732
|
+
expect(resolveParentPredicate(meta, Comment)).toBe("channel://has_comment");
|
|
1733
|
+
});
|
|
1734
|
+
|
|
1735
|
+
it("throws when explicit field does not exist on parent", () => {
|
|
1736
|
+
const meta = Channel.getModelMetadata();
|
|
1737
|
+
expect(() => resolveParentPredicate(meta, Comment, "nonExistent")).toThrow(
|
|
1738
|
+
/field "nonExistent" not found/,
|
|
1739
|
+
);
|
|
1740
|
+
});
|
|
1741
|
+
|
|
1742
|
+
it("throws when no relation on parent matches the child type", () => {
|
|
1743
|
+
@Model({ name: "UnrelatedParent" })
|
|
1744
|
+
class UnrelatedParent extends Ad4mModel {}
|
|
1745
|
+
const meta = UnrelatedParent.getModelMetadata();
|
|
1746
|
+
expect(() => resolveParentPredicate(meta, Comment)).toThrow(
|
|
1747
|
+
/no forward relation pointing to "Comment"/,
|
|
1748
|
+
);
|
|
1749
|
+
});
|
|
1340
1750
|
|
|
1341
|
-
|
|
1342
|
-
const
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1751
|
+
it("throws when neither field nor childCtor is provided", () => {
|
|
1752
|
+
const meta = Channel.getModelMetadata();
|
|
1753
|
+
expect(() => resolveParentPredicate(meta, undefined)).toThrow(
|
|
1754
|
+
/either "field" or a child model constructor must be provided/,
|
|
1755
|
+
);
|
|
1756
|
+
});
|
|
1757
|
+
|
|
1758
|
+
it("throws with disambiguation hint when multiple relations point to the same type", () => {
|
|
1759
|
+
const meta = AmbiguousParent.getModelMetadata();
|
|
1760
|
+
expect(() => resolveParentPredicate(meta, Comment)).toThrow(
|
|
1761
|
+
/multiple relations.*provide "field" to disambiguate/i,
|
|
1762
|
+
);
|
|
1763
|
+
});
|
|
1764
|
+
});
|
|
1765
|
+
|
|
1766
|
+
describe("normalizeParentQuery()", () => {
|
|
1767
|
+
@Model({ name: "NpqTag" })
|
|
1768
|
+
class NpqTag extends Ad4mModel {}
|
|
1769
|
+
|
|
1770
|
+
@Model({ name: "NpqComment" })
|
|
1771
|
+
class NpqComment extends Ad4mModel {}
|
|
1772
|
+
|
|
1773
|
+
@Model({ name: "NpqBlogPost" })
|
|
1774
|
+
class NpqBlogPost extends Ad4mModel {
|
|
1775
|
+
@HasMany(() => NpqTag, { through: "blog://has_tag" })
|
|
1776
|
+
tags: NpqTag[] = [];
|
|
1777
|
+
|
|
1778
|
+
@HasMany(() => NpqComment, { through: "blog://has_comment" })
|
|
1779
|
+
comments: NpqComment[] = [];
|
|
1780
|
+
|
|
1781
|
+
@HasMany(() => NpqComment, { through: "blog://has_pinned_comment" })
|
|
1782
|
+
pinnedComments: NpqComment[] = [];
|
|
1783
|
+
}
|
|
1784
|
+
|
|
1785
|
+
it("passes through raw { id, predicate } form unchanged", () => {
|
|
1786
|
+
const raw = { id: "some://uri", predicate: "custom://predicate" };
|
|
1787
|
+
expect(normalizeParentQuery(raw)).toEqual(raw);
|
|
1788
|
+
});
|
|
1789
|
+
|
|
1790
|
+
it("resolves model-backed form with explicit field", () => {
|
|
1791
|
+
const result = normalizeParentQuery({
|
|
1792
|
+
id: "some://uri",
|
|
1793
|
+
model: NpqBlogPost,
|
|
1794
|
+
field: "tags",
|
|
1346
1795
|
});
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1796
|
+
expect(result).toEqual({ id: "some://uri", predicate: "blog://has_tag" });
|
|
1797
|
+
});
|
|
1798
|
+
|
|
1799
|
+
it("infers predicate from childCtor when field is omitted", () => {
|
|
1800
|
+
const result = normalizeParentQuery(
|
|
1801
|
+
{ id: "some://uri", model: NpqBlogPost },
|
|
1802
|
+
NpqTag,
|
|
1803
|
+
);
|
|
1804
|
+
expect(result).toEqual({ id: "some://uri", predicate: "blog://has_tag" });
|
|
1805
|
+
});
|
|
1806
|
+
|
|
1807
|
+
it("throws when model-backed form has no field and multiple relations point to the same child type", () => {
|
|
1808
|
+
expect(() =>
|
|
1809
|
+
normalizeParentQuery(
|
|
1810
|
+
{ id: "some://uri", model: NpqBlogPost },
|
|
1811
|
+
NpqComment,
|
|
1812
|
+
),
|
|
1813
|
+
).toThrow(/multiple relations.*provide "field" to disambiguate/i);
|
|
1814
|
+
});
|
|
1815
|
+
});
|
|
1816
|
+
|
|
1817
|
+
describe("Ad4mModel.update() — static convenience method", () => {
|
|
1818
|
+
@Model({ name: "UpdatableModel" })
|
|
1819
|
+
class UpdatableModel extends Ad4mModel {
|
|
1820
|
+
@Property({ through: "test://title" })
|
|
1821
|
+
title: string = "";
|
|
1822
|
+
@Property({ through: "test://body" })
|
|
1823
|
+
body: string = "";
|
|
1824
|
+
@Property({ through: "test://count" })
|
|
1825
|
+
count: number = 0;
|
|
1826
|
+
}
|
|
1827
|
+
|
|
1828
|
+
it("calls get() then save() after merging data", async () => {
|
|
1829
|
+
const existingTitle = "Original Title";
|
|
1830
|
+
const existingBody = "Original Body";
|
|
1831
|
+
|
|
1832
|
+
const mockPerspective = {
|
|
1833
|
+
querySurrealDB: jest.fn().mockResolvedValue([
|
|
1834
|
+
{
|
|
1835
|
+
source: "literal://string:abc123",
|
|
1836
|
+
source_uri: "literal://string:abc123",
|
|
1837
|
+
links: [
|
|
1838
|
+
{ predicate: "test://title", target: `literal://string:${existingTitle}`, author: "did:key:alice", timestamp: "2024-01-01T00:00:00Z" },
|
|
1839
|
+
{ predicate: "test://body", target: `literal://string:${existingBody}`, author: "did:key:alice", timestamp: "2024-01-01T00:00:00Z" },
|
|
1840
|
+
{ predicate: "test://count", target: "literal://number:5", author: "did:key:alice", timestamp: "2024-01-01T00:00:00Z" },
|
|
1841
|
+
],
|
|
1842
|
+
},
|
|
1843
|
+
]),
|
|
1844
|
+
infer: jest.fn(),
|
|
1845
|
+
uuid: "test-uuid",
|
|
1846
|
+
stringOrTemplateObjectToSubjectClassName: jest.fn().mockResolvedValue("UpdatableModel"),
|
|
1847
|
+
add: jest.fn(),
|
|
1848
|
+
remove: jest.fn(),
|
|
1849
|
+
get: jest.fn().mockResolvedValue([]),
|
|
1850
|
+
removeSubject: jest.fn(),
|
|
1851
|
+
createSubject: jest.fn(),
|
|
1852
|
+
subjectEntities: jest.fn().mockResolvedValue([]),
|
|
1853
|
+
} as any;
|
|
1854
|
+
|
|
1855
|
+
const saveSpy = jest.spyOn(UpdatableModel.prototype, "save").mockResolvedValue();
|
|
1856
|
+
const getSpy = jest.spyOn(UpdatableModel.prototype, "get").mockImplementation(async function (this: any) {
|
|
1857
|
+
this.title = existingTitle;
|
|
1858
|
+
this.body = existingBody;
|
|
1859
|
+
this.count = 5;
|
|
1860
|
+
return this;
|
|
1351
1861
|
});
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1862
|
+
|
|
1863
|
+
const result = await UpdatableModel.update(
|
|
1864
|
+
mockPerspective,
|
|
1865
|
+
"literal://string:abc123",
|
|
1866
|
+
{ title: "New Title" },
|
|
1867
|
+
);
|
|
1868
|
+
|
|
1869
|
+
// get() must be called to fetch the pre-existing state
|
|
1870
|
+
expect(getSpy).toHaveBeenCalledTimes(1);
|
|
1871
|
+
|
|
1872
|
+
// save() must be called after merging
|
|
1873
|
+
expect(saveSpy).toHaveBeenCalledTimes(1);
|
|
1874
|
+
|
|
1875
|
+
// Only the provided field was overwritten
|
|
1876
|
+
expect(result.title).toBe("New Title");
|
|
1877
|
+
|
|
1878
|
+
// Other fields are preserved from the fetched state
|
|
1879
|
+
expect(result.body).toBe(existingBody);
|
|
1880
|
+
expect(result.count).toBe(5);
|
|
1881
|
+
|
|
1882
|
+
getSpy.mockRestore();
|
|
1883
|
+
saveSpy.mockRestore();
|
|
1355
1884
|
});
|
|
1356
1885
|
|
|
1357
|
-
it("
|
|
1358
|
-
|
|
1359
|
-
const
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
{ predicate: "recipe://name", target: `Recipe ${i+1}`, author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1365
|
-
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
1366
|
-
]
|
|
1367
|
-
})),
|
|
1368
|
-
...Array.from({ length: 2 }, (_, i) => ({
|
|
1369
|
-
source: `node:def${i+4}`,
|
|
1370
|
-
source_uri: `literal://recipe${i+4}`,
|
|
1371
|
-
links: [
|
|
1372
|
-
{ predicate: "recipe://name", target: `Recipe ${i+4}`, author: "did:key:bob", timestamp: "2023-01-02T00:00:00Z" },
|
|
1373
|
-
{ predicate: "recipe://rating", target: "5", author: "did:key:bob", timestamp: "2023-01-02T00:00:00Z" }
|
|
1374
|
-
]
|
|
1375
|
-
}))
|
|
1376
|
-
];
|
|
1377
|
-
|
|
1378
|
-
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1886
|
+
it("returns the updated instance", async () => {
|
|
1887
|
+
const saveSpy = jest.spyOn(UpdatableModel.prototype, "save").mockResolvedValue();
|
|
1888
|
+
const getSpy = jest.spyOn(UpdatableModel.prototype, "get").mockImplementation(async function (this: any) {
|
|
1889
|
+
this.title = "Old";
|
|
1890
|
+
this.body = "kept";
|
|
1891
|
+
return this;
|
|
1892
|
+
});
|
|
1379
1893
|
|
|
1380
|
-
|
|
1381
|
-
const
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1894
|
+
const mockPerspective = {} as any;
|
|
1895
|
+
const result = await UpdatableModel.update(mockPerspective, "literal://string:xyz", { title: "New" });
|
|
1896
|
+
|
|
1897
|
+
expect(result).toBeInstanceOf(UpdatableModel);
|
|
1898
|
+
expect(result.id).toBe("literal://string:xyz");
|
|
1899
|
+
|
|
1900
|
+
getSpy.mockRestore();
|
|
1901
|
+
saveSpy.mockRestore();
|
|
1388
1902
|
});
|
|
1389
1903
|
|
|
1390
|
-
it("
|
|
1391
|
-
|
|
1392
|
-
const
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
links: [
|
|
1396
|
-
{ predicate: "recipe://name", target: `Recipe ${i+1}`, author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1397
|
-
{ predicate: "recipe://rating", target: `${i+1}`, author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
1398
|
-
]
|
|
1399
|
-
}));
|
|
1400
|
-
|
|
1401
|
-
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1904
|
+
it("passes batchId through to save()", async () => {
|
|
1905
|
+
const saveSpy = jest.spyOn(UpdatableModel.prototype, "save").mockResolvedValue();
|
|
1906
|
+
const getSpy = jest.spyOn(UpdatableModel.prototype, "get").mockImplementation(async function (this: any) {
|
|
1907
|
+
return this;
|
|
1908
|
+
});
|
|
1402
1909
|
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
// Verify count matches the number of instances that would be returned by get()
|
|
1409
|
-
const getResults = await Recipe.query(mockPerspective)
|
|
1410
|
-
.where({ rating: { gt: 3 } })
|
|
1411
|
-
.get();
|
|
1412
|
-
|
|
1413
|
-
expect(count).toBe(2);
|
|
1414
|
-
expect(count).toBe(getResults.length);
|
|
1910
|
+
await UpdatableModel.update({} as any, "literal://string:id1", {}, "batch-42");
|
|
1911
|
+
expect(saveSpy).toHaveBeenCalledWith("batch-42");
|
|
1912
|
+
|
|
1913
|
+
getSpy.mockRestore();
|
|
1914
|
+
saveSpy.mockRestore();
|
|
1415
1915
|
});
|
|
1916
|
+
});
|
|
1416
1917
|
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
{ predicate: "recipe://name", target: `Recipe ${i+1}`, author: "did:key:alice", timestamp: `2023-01-0${i+1}T00:00:00Z` },
|
|
1424
|
-
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: `2023-01-0${i+1}T00:00:00Z` }
|
|
1425
|
-
]
|
|
1426
|
-
}));
|
|
1427
|
-
|
|
1428
|
-
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1918
|
+
describe("Ad4mModel.delete() — static convenience method", () => {
|
|
1919
|
+
@Model({ name: "DeletableModel" })
|
|
1920
|
+
class DeletableModel extends Ad4mModel {
|
|
1921
|
+
@Property({ through: "test://label" })
|
|
1922
|
+
label: string = "";
|
|
1923
|
+
}
|
|
1429
1924
|
|
|
1430
|
-
|
|
1431
|
-
const
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
const getResults = await Recipe.query(mockPerspective)
|
|
1440
|
-
.where({ timestamp: { between: [startTimestamp, endTimestamp] } })
|
|
1441
|
-
.get();
|
|
1442
|
-
|
|
1443
|
-
expect(count).toBe(3);
|
|
1444
|
-
expect(count).toBe(getResults.length);
|
|
1925
|
+
it("calls instance.delete() with no batchId by default", async () => {
|
|
1926
|
+
const deleteSpy = jest.spyOn(DeletableModel.prototype, "delete").mockResolvedValue();
|
|
1927
|
+
|
|
1928
|
+
await DeletableModel.delete({} as any, "literal://string:del1");
|
|
1929
|
+
|
|
1930
|
+
expect(deleteSpy).toHaveBeenCalledTimes(1);
|
|
1931
|
+
expect(deleteSpy).toHaveBeenCalledWith(undefined);
|
|
1932
|
+
|
|
1933
|
+
deleteSpy.mockRestore();
|
|
1445
1934
|
});
|
|
1446
1935
|
|
|
1447
|
-
it("
|
|
1448
|
-
const
|
|
1449
|
-
mockPerspective.infer.mockResolvedValue(prologResults);
|
|
1936
|
+
it("forwards batchId to instance.delete()", async () => {
|
|
1937
|
+
const deleteSpy = jest.spyOn(DeletableModel.prototype, "delete").mockResolvedValue();
|
|
1450
1938
|
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
expect(
|
|
1454
|
-
|
|
1455
|
-
|
|
1939
|
+
await DeletableModel.delete({} as any, "literal://string:del2", "batch-99");
|
|
1940
|
+
|
|
1941
|
+
expect(deleteSpy).toHaveBeenCalledWith("batch-99");
|
|
1942
|
+
|
|
1943
|
+
deleteSpy.mockRestore();
|
|
1456
1944
|
});
|
|
1457
1945
|
|
|
1458
|
-
it("
|
|
1459
|
-
const
|
|
1460
|
-
mockPerspective.infer.mockResolvedValue(prologResults);
|
|
1946
|
+
it("constructs an instance with the given id", async () => {
|
|
1947
|
+
const deleteSpy = jest.spyOn(DeletableModel.prototype, "delete").mockResolvedValue();
|
|
1461
1948
|
|
|
1462
|
-
const
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
);
|
|
1470
|
-
|
|
1471
|
-
expect(mockPerspective.infer).toHaveBeenCalledTimes(1);
|
|
1472
|
-
expect(mockPerspective.querySurrealDB).not.toHaveBeenCalled();
|
|
1473
|
-
expect(count).toBe(3);
|
|
1949
|
+
const targetId = "literal://string:target-id";
|
|
1950
|
+
await DeletableModel.delete({} as any, targetId);
|
|
1951
|
+
|
|
1952
|
+
// The static method must target the correct id
|
|
1953
|
+
expect((deleteSpy.mock.instances[0] as unknown as DeletableModel).id).toBe(targetId);
|
|
1954
|
+
|
|
1955
|
+
deleteSpy.mockRestore();
|
|
1474
1956
|
});
|
|
1475
1957
|
});
|
|
1476
|
-
|