@coasys/ad4m 0.13.0-test-5 → 0.13.0-test-6
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/lib/index.cjs +610 -337
- package/lib/index.js +608 -338
- package/lib/src/Ad4mClient.d.ts +1 -1
- package/lib/src/Ad4mClient.js +2 -1
- package/lib/src/agent/AgentClient.d.ts +16 -0
- package/lib/src/agent/AgentClient.js +74 -4
- package/lib/src/cache/PersistentCache.d.ts +40 -0
- package/lib/src/cache/PersistentCache.js +139 -0
- package/lib/src/expression/ExpressionClient.d.ts +8 -0
- package/lib/src/expression/ExpressionClient.js +113 -5
- package/lib/src/index.d.ts +1 -0
- package/lib/src/index.js +1 -0
- package/lib/src/model/Ad4mModel.js +29 -57
- package/lib/src/model/Ad4mModel.test.js +688 -1434
- package/lib/src/model/ModelQueryBuilder.js +0 -9
- package/lib/src/model/Subject.js +6 -6
- package/lib/src/model/query-surreal.d.ts +44 -0
- package/lib/src/model/query-surreal.js +249 -0
- package/lib/src/model/relation-filtering.test.js +15 -15
- package/lib/src/model/surreal-utils.d.ts +42 -0
- package/lib/src/model/surreal-utils.js +128 -0
- package/lib/src/model/types.d.ts +0 -1
- package/lib/src/perspectives/PerspectiveClient.d.ts +0 -1
- package/lib/src/perspectives/PerspectiveClient.js +5 -13
- package/lib/src/perspectives/PerspectiveProxy.js +0 -3
- package/lib/src/perspectives/PerspectiveResolver.d.ts +1 -0
- package/lib/src/perspectives/PerspectiveResolver.js +11 -0
- package/lib/src/runtime/RuntimeResolver.d.ts +1 -28
- package/lib/src/runtime/RuntimeResolver.js +4 -197
- package/lib/src/schema.gql +1 -27
- package/lib/src/shacl/SHACLShape.test.js +0 -82
- package/package.json +1 -1
- package/src/Ad4mClient.test.ts +14 -4
- package/src/Ad4mClient.ts +2 -1
- package/src/agent/AgentClient.ts +84 -2
- package/src/cache/PersistentCache.ts +145 -0
- package/src/cache/cache.test.ts +713 -0
- package/src/expression/ExpressionClient.ts +137 -3
- package/src/index.ts +1 -0
- package/src/model/Ad4mModel.test.ts +1 -61
- package/src/model/Ad4mModel.ts +31 -57
- package/src/model/ModelQueryBuilder.ts +0 -6
- package/src/model/types.ts +1 -1
- package/src/perspectives/PerspectiveClient.ts +7 -17
- package/src/perspectives/PerspectiveProxy.ts +0 -3
- package/lib/src/model/hydration-batch.d.ts +0 -34
- package/lib/src/model/hydration-batch.js +0 -147
- package/lib/src/model/hydration-batching.test.d.ts +0 -7
- package/lib/src/model/hydration-batching.test.js +0 -456
- package/lib/src/model/model-manifest.d.ts +0 -41
- package/lib/src/model/model-manifest.js +0 -11
- package/lib/src/model/query-cache.test.d.ts +0 -1
- package/lib/src/model/query-cache.test.js +0 -43
- package/lib/src/model/query-sparql-batch.d.ts +0 -17
- package/lib/src/model/query-sparql-batch.js +0 -260
- package/lib/src/model/query-sparql.test.d.ts +0 -1
- package/lib/src/model/query-sparql.test.js +0 -312
- package/lib/src/model/subscription-delta.test.d.ts +0 -24
- package/lib/src/model/subscription-delta.test.js +0 -180
- package/lib/src/model/subscription-pool.d.ts +0 -39
- package/lib/src/model/subscription-pool.js +0 -165
- package/lib/src/perspectives/PerspectiveClient.test.d.ts +0 -1
- package/lib/src/perspectives/PerspectiveClient.test.js +0 -224
|
@@ -437,11 +437,14 @@ describe("Ad4mModel.fromJSONSchema() with getModelMetadata()", () => {
|
|
|
437
437
|
expect(shape.destructor_actions).toEqual([]);
|
|
438
438
|
});
|
|
439
439
|
});
|
|
440
|
-
describe("Ad4mModel.
|
|
440
|
+
describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
441
|
+
// Mock perspective proxy (minimal since queryToSurrealQL doesn't actually call it)
|
|
441
442
|
const mockPerspective = {};
|
|
443
|
+
// Helper function to normalize whitespace in queries for easier comparison
|
|
442
444
|
function normalizeQuery(query) {
|
|
443
445
|
return query.replace(/\s+/g, ' ').trim();
|
|
444
446
|
}
|
|
447
|
+
// Test Recipe model — explicit required: true to test required-property query filters
|
|
445
448
|
let Recipe = class Recipe extends Ad4mModel {
|
|
446
449
|
constructor() {
|
|
447
450
|
super(...arguments);
|
|
@@ -465,172 +468,328 @@ describe("Ad4mModel.queryToSPARQL()", () => {
|
|
|
465
468
|
Recipe = __decorate([
|
|
466
469
|
Model({ name: "Recipe" })
|
|
467
470
|
], Recipe);
|
|
468
|
-
it("should generate basic
|
|
469
|
-
const query = await Recipe.
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
expect(
|
|
473
|
-
|
|
474
|
-
expect(
|
|
475
|
-
|
|
471
|
+
it("should generate basic query with no filters", async () => {
|
|
472
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {});
|
|
473
|
+
expect(query).toContain("id AS source");
|
|
474
|
+
expect(query).toContain("uri AS source_uri");
|
|
475
|
+
expect(query).toContain("FROM node");
|
|
476
|
+
expect(query).toContain("->link AS links");
|
|
477
|
+
expect(query).toContain("WHERE");
|
|
478
|
+
// Should have graph traversal filters for required properties
|
|
479
|
+
expect(query).toContain("count(->link[WHERE");
|
|
476
480
|
});
|
|
477
481
|
it("should generate query with simple property filter", async () => {
|
|
478
|
-
const query = await Recipe.
|
|
479
|
-
|
|
480
|
-
expect(
|
|
481
|
-
|
|
482
|
-
expect(
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
const
|
|
501
|
-
//
|
|
502
|
-
expect(
|
|
503
|
-
expect(
|
|
504
|
-
});
|
|
505
|
-
it("should
|
|
506
|
-
const query = await Recipe.
|
|
507
|
-
|
|
508
|
-
expect(
|
|
509
|
-
expect(
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
const
|
|
514
|
-
//
|
|
515
|
-
expect(
|
|
516
|
-
});
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
expect(norm).toContain("SELECT ?source");
|
|
586
|
-
// lt is now handled via JS post-filter
|
|
587
|
-
expect(norm).toContain("wTarget_cmp_rating");
|
|
588
|
-
});
|
|
589
|
-
it("should inject SPARQL FILTER for gte/lte combined", async () => {
|
|
590
|
-
const query = await Recipe.queryToSPARQL(mockPerspective, { where: { rating: { gte: 2, lte: 8 } } });
|
|
591
|
-
const norm = normalizeQuery(query);
|
|
592
|
-
expect(norm).toContain("SELECT ?source");
|
|
593
|
-
// gte/lte are now handled via JS post-filter
|
|
594
|
-
expect(norm).toContain("wTarget_cmp_rating");
|
|
595
|
-
});
|
|
596
|
-
it("should inject SPARQL FILTER for between operator", async () => {
|
|
597
|
-
const query = await Recipe.queryToSPARQL(mockPerspective, { where: { rating: { between: [1, 10] } } });
|
|
598
|
-
const norm = normalizeQuery(query);
|
|
599
|
-
expect(norm).toContain("SELECT ?source");
|
|
600
|
-
// between is now handled via JS post-filter
|
|
601
|
-
expect(norm).toContain("wTarget_cmp_rating");
|
|
602
|
-
});
|
|
603
|
-
it("should inject SPARQL FILTER for contains operator", async () => {
|
|
604
|
-
const query = await Recipe.queryToSPARQL(mockPerspective, { where: { name: { contains: "pasta" } } });
|
|
605
|
-
const norm = normalizeQuery(query);
|
|
606
|
-
expect(norm).toContain("SELECT ?source");
|
|
607
|
-
// contains is now handled via JS post-filter
|
|
608
|
-
expect(norm).toContain("wTarget_cmp_name");
|
|
609
|
-
});
|
|
610
|
-
// ---- Multiple property filters combined ----
|
|
611
|
-
it("should combine multiple property equality filters with &&", async () => {
|
|
612
|
-
const query = await Recipe.queryToSPARQL(mockPerspective, {
|
|
613
|
-
where: { name: "Pasta", rating: ["4", "5"] }
|
|
482
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { name: "Pasta" } });
|
|
483
|
+
// Should have graph traversal filters for required properties and user filter
|
|
484
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://name']) > 0");
|
|
485
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://rating']) > 0");
|
|
486
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) = 'Pasta']) > 0");
|
|
487
|
+
});
|
|
488
|
+
it("should generate query with multiple property filters", async () => {
|
|
489
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { name: "Pasta", rating: 5 } });
|
|
490
|
+
expect(query).toContain("WHERE");
|
|
491
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) = 'Pasta']) > 0");
|
|
492
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://rating' AND fn::parse_literal(out.uri) = 5]) > 0");
|
|
493
|
+
expect(query).toContain("AND");
|
|
494
|
+
});
|
|
495
|
+
it("should handle gt operator (filtered in JavaScript, not SQL)", async () => {
|
|
496
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { rating: { gt: 4 } } });
|
|
497
|
+
// Comparison operators for regular properties are now filtered in JavaScript post-query
|
|
498
|
+
// The SQL just filters on the predicate existing
|
|
499
|
+
expect(query).toContain("FROM node");
|
|
500
|
+
// Should NOT contain comparison operators in SQL for regular properties
|
|
501
|
+
expect(query).not.toContain("target > 4");
|
|
502
|
+
});
|
|
503
|
+
it("should handle lt operator (filtered in JavaScript, not SQL)", async () => {
|
|
504
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { rating: { lt: 3 } } });
|
|
505
|
+
// Comparison operators are filtered in JavaScript post-query
|
|
506
|
+
expect(query).toContain("FROM node");
|
|
507
|
+
expect(query).not.toContain("target < 3");
|
|
508
|
+
});
|
|
509
|
+
it("should handle gte and lte operators (filtered in JavaScript, not SQL)", async () => {
|
|
510
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { rating: { gte: 3, lte: 5 } } });
|
|
511
|
+
// Comparison operators are filtered in JavaScript post-query
|
|
512
|
+
expect(query).toContain("FROM node");
|
|
513
|
+
expect(query).not.toContain("target >= 3");
|
|
514
|
+
expect(query).not.toContain("target <= 5");
|
|
515
|
+
});
|
|
516
|
+
it("should handle not operator with single value", async () => {
|
|
517
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { name: { not: "Salad" } } });
|
|
518
|
+
// Not operator uses graph traversal with count = 0
|
|
519
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) = 'Salad']) = 0");
|
|
520
|
+
});
|
|
521
|
+
it("should handle not operator with array (NOT IN)", async () => {
|
|
522
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { name: { not: ["Salad", "Soup"] } } });
|
|
523
|
+
// Not operator with array uses graph traversal with count = 0
|
|
524
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) IN ['Salad', 'Soup']]) = 0");
|
|
525
|
+
});
|
|
526
|
+
it("should handle between operator (filtered in JavaScript, not SQL)", async () => {
|
|
527
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { rating: { between: [3, 5] } } });
|
|
528
|
+
// Between operator for regular properties is now filtered in JavaScript post-query
|
|
529
|
+
expect(query).toContain("FROM node");
|
|
530
|
+
expect(query).not.toContain("target >= 3");
|
|
531
|
+
expect(query).not.toContain("target <= 5");
|
|
532
|
+
});
|
|
533
|
+
it("should handle contains operator on string property (filtered in JavaScript, not SQL)", async () => {
|
|
534
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { name: { contains: "Past" } } });
|
|
535
|
+
// Contains operator for regular properties is now filtered in JavaScript post-query
|
|
536
|
+
expect(query).toContain("FROM node");
|
|
537
|
+
expect(query).not.toContain("target CONTAINS 'Past'");
|
|
538
|
+
});
|
|
539
|
+
it("should handle contains operator on regular property with substring (filtered in JavaScript, not SQL)", async () => {
|
|
540
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { name: { contains: "Salad" } } });
|
|
541
|
+
// Contains operator is filtered in JavaScript post-query
|
|
542
|
+
expect(query).toContain("FROM node");
|
|
543
|
+
expect(query).not.toContain("target CONTAINS 'Salad'");
|
|
544
|
+
});
|
|
545
|
+
it.skip("should handle contains operator on special field (author)", async () => {
|
|
546
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { author: { contains: "alice" } } });
|
|
547
|
+
expect(query).toContain("WHERE author CONTAINS 'alice'");
|
|
548
|
+
// Should not use a subquery pattern
|
|
549
|
+
expect(query).not.toContain("SELECT source FROM node");
|
|
550
|
+
});
|
|
551
|
+
it.skip("should handle contains operator on special field (id)", async () => {
|
|
552
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { id: { contains: "test" } } });
|
|
553
|
+
expect(query).toContain("WHERE source CONTAINS 'test'");
|
|
554
|
+
});
|
|
555
|
+
it("should handle array values (IN clause)", async () => {
|
|
556
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { name: ["Pasta", "Pizza"] } });
|
|
557
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) IN ['Pasta', 'Pizza']]) > 0");
|
|
558
|
+
});
|
|
559
|
+
it.skip("should handle special fields (author, timestamp) without subqueries", async () => {
|
|
560
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { author: "did:key:alice" } });
|
|
561
|
+
expect(query).toContain("WHERE author = 'did:key:alice'");
|
|
562
|
+
// Ensure it's NOT using a subquery pattern for author in WHERE clause
|
|
563
|
+
expect(normalizeQuery(query)).not.toMatch(/WHERE.*source IN.*SELECT source FROM node.*author/);
|
|
564
|
+
});
|
|
565
|
+
it("should not generate ORDER BY clause in SQL (handled in JavaScript)", async () => {
|
|
566
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { order: { timestamp: "DESC" } });
|
|
567
|
+
// ORDER BY is now handled in JavaScript post-query
|
|
568
|
+
expect(query).toContain("FROM node");
|
|
569
|
+
expect(query).not.toContain("ORDER BY");
|
|
570
|
+
});
|
|
571
|
+
it("should not generate LIMIT clause in SQL (handled in JavaScript)", async () => {
|
|
572
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { limit: 10 });
|
|
573
|
+
// LIMIT is now handled in JavaScript post-query
|
|
574
|
+
expect(query).toContain("FROM node");
|
|
575
|
+
expect(query).not.toContain("LIMIT");
|
|
576
|
+
});
|
|
577
|
+
it("should not generate START clause in SQL (handled in JavaScript)", async () => {
|
|
578
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { offset: 20 });
|
|
579
|
+
// START/offset is now handled in JavaScript post-query
|
|
580
|
+
expect(query).toContain("FROM node");
|
|
581
|
+
expect(query).not.toContain("START");
|
|
582
|
+
});
|
|
583
|
+
it("should generate complete query with all options (ORDER BY, LIMIT, START handled in JS)", async () => {
|
|
584
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
585
|
+
where: { name: "Pasta", rating: { gt: 4 } },
|
|
586
|
+
order: { timestamp: "DESC" },
|
|
587
|
+
limit: 10,
|
|
588
|
+
offset: 20
|
|
614
589
|
});
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
expect(
|
|
618
|
-
|
|
619
|
-
expect(
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
expect(
|
|
630
|
-
|
|
590
|
+
// WHERE clause uses graph traversal filters
|
|
591
|
+
expect(query).toContain("WHERE");
|
|
592
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) = 'Pasta']) > 0");
|
|
593
|
+
// Comparison operators (gt) are filtered in JavaScript, not SQL
|
|
594
|
+
expect(query).not.toContain("out.uri > 4");
|
|
595
|
+
expect(query).not.toContain("target > 4");
|
|
596
|
+
// ORDER BY, LIMIT, START are now handled in JavaScript post-query
|
|
597
|
+
expect(query).not.toContain("ORDER BY");
|
|
598
|
+
expect(query).not.toContain("LIMIT");
|
|
599
|
+
expect(query).not.toContain("START");
|
|
600
|
+
});
|
|
601
|
+
it("should only select requested properties", async () => {
|
|
602
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { properties: ["name"] });
|
|
603
|
+
// With array::group(), all link data is selected, filtering happens in instancesFromSurrealResult
|
|
604
|
+
expect(query).toContain("->link AS links");
|
|
605
|
+
});
|
|
606
|
+
it("should select all link data for relations", async () => {
|
|
607
|
+
let MultiRelationModel = class MultiRelationModel extends Ad4mModel {
|
|
608
|
+
constructor() {
|
|
609
|
+
super(...arguments);
|
|
610
|
+
this.coll1 = [];
|
|
611
|
+
this.coll2 = [];
|
|
612
|
+
}
|
|
613
|
+
};
|
|
614
|
+
__decorate([
|
|
615
|
+
HasMany({ through: "test://coll1" }),
|
|
616
|
+
__metadata("design:type", Array)
|
|
617
|
+
], MultiRelationModel.prototype, "coll1", void 0);
|
|
618
|
+
__decorate([
|
|
619
|
+
HasMany({ through: "test://coll2" }),
|
|
620
|
+
__metadata("design:type", Array)
|
|
621
|
+
], MultiRelationModel.prototype, "coll2", void 0);
|
|
622
|
+
MultiRelationModel = __decorate([
|
|
623
|
+
Model({ name: "MultiRelationModel" })
|
|
624
|
+
], MultiRelationModel);
|
|
625
|
+
const query = await MultiRelationModel.queryToSurrealQL(mockPerspective, {});
|
|
626
|
+
// With array::group(), all link data is selected
|
|
627
|
+
expect(query).toContain("->link AS links");
|
|
628
|
+
});
|
|
629
|
+
it("should escape single quotes in string values", async () => {
|
|
630
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { name: "O'Brien's Recipe" } });
|
|
631
|
+
// Single quotes are escaped with backslash in SurrealDB
|
|
632
|
+
expect(query).toContain("O\\'Brien\\'s Recipe");
|
|
633
|
+
});
|
|
634
|
+
it("should handle numeric values without quotes", async () => {
|
|
635
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { rating: 5 } });
|
|
636
|
+
// Numeric values should not have quotes around them
|
|
637
|
+
expect(query).toContain("fn::parse_literal(out.uri) = 5");
|
|
638
|
+
expect(query).not.toContain("fn::parse_literal(out.uri) = '5'");
|
|
639
|
+
});
|
|
640
|
+
it("should handle complex nested query (comparisons, ORDER BY, LIMIT handled in JS)", async () => {
|
|
641
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
642
|
+
where: {
|
|
643
|
+
name: "Pasta",
|
|
644
|
+
rating: { gte: 4, lte: 5 },
|
|
645
|
+
author: "did:key:alice"
|
|
646
|
+
},
|
|
647
|
+
order: { rating: "DESC" },
|
|
648
|
+
limit: 5
|
|
649
|
+
});
|
|
650
|
+
const normalized = normalizeQuery(query);
|
|
651
|
+
// Verify graph traversal filters are present
|
|
652
|
+
expect(normalized).toContain("count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) = 'Pasta']) > 0");
|
|
653
|
+
// Comparison operators (gte, lte) are filtered in JavaScript, not SQL
|
|
654
|
+
expect(normalized).not.toContain("out.uri >= 4");
|
|
655
|
+
expect(normalized).not.toContain("target >= 4");
|
|
656
|
+
expect(normalized).not.toContain("out.uri <= 5");
|
|
657
|
+
expect(normalized).not.toContain("target <= 5");
|
|
658
|
+
// author and timestamp filtering is done in JavaScript post-query
|
|
659
|
+
expect(normalized).not.toContain("author = 'did:key:alice'");
|
|
660
|
+
// ORDER BY and LIMIT are handled in JavaScript post-query
|
|
661
|
+
expect(normalized).not.toContain("ORDER BY");
|
|
662
|
+
expect(normalized).not.toContain("LIMIT");
|
|
663
|
+
// Verify query structure (FROM node, no GROUP BY)
|
|
664
|
+
expect(normalized).toContain("FROM node");
|
|
665
|
+
});
|
|
666
|
+
it("should handle empty query object", async () => {
|
|
667
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {});
|
|
668
|
+
// Should generate valid query with WHERE clause for required properties (name and rating)
|
|
669
|
+
expect(query).toContain("id AS source");
|
|
670
|
+
expect(query).toContain("uri AS source_uri");
|
|
671
|
+
expect(query).toContain("FROM node");
|
|
672
|
+
// Should have WHERE clause filtering for required properties using graph traversal
|
|
673
|
+
expect(query).toContain("WHERE");
|
|
674
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://name']) > 0");
|
|
675
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://rating']) > 0");
|
|
676
|
+
expect(query).not.toContain("ORDER BY");
|
|
677
|
+
expect(query).not.toContain("START");
|
|
678
|
+
});
|
|
679
|
+
it("should handle id special field", async () => {
|
|
680
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { id: "literal://test" } });
|
|
681
|
+
expect(query).toContain("uri = 'literal://test'");
|
|
682
|
+
});
|
|
683
|
+
it("should handle id special field with array (IN clause)", async () => {
|
|
684
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { id: ["literal://test1", "literal://test2"] } });
|
|
685
|
+
expect(query).toContain("uri IN ['literal://test1', 'literal://test2']");
|
|
686
|
+
});
|
|
687
|
+
it("should handle id special field with not operator", async () => {
|
|
688
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { id: { not: "literal://test" } } });
|
|
689
|
+
expect(query).toContain("uri != 'literal://test'");
|
|
690
|
+
});
|
|
691
|
+
it("should handle id special field with not operator and array (NOT IN)", async () => {
|
|
692
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { id: { not: ["literal://test1", "literal://test2"] } } });
|
|
693
|
+
expect(query).toContain("uri NOT IN ['literal://test1', 'literal://test2']");
|
|
694
|
+
});
|
|
695
|
+
it("should handle id special field with between operator", async () => {
|
|
696
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { id: { between: ["literal://a", "literal://z"] } } });
|
|
697
|
+
const normalized = normalizeQuery(query);
|
|
698
|
+
expect(normalized).toContain("uri >= 'literal://a' AND uri <= 'literal://z'");
|
|
699
|
+
});
|
|
700
|
+
it("should handle id special field with gt operator", async () => {
|
|
701
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { id: { gt: "literal://m" } } });
|
|
702
|
+
expect(query).toContain("uri > 'literal://m'");
|
|
703
|
+
});
|
|
704
|
+
it("should handle id special field with gte and lte operators", async () => {
|
|
705
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { id: { gte: "literal://a", lte: "literal://z" } } });
|
|
706
|
+
const normalized = normalizeQuery(query);
|
|
707
|
+
expect(normalized).toContain("uri >= 'literal://a'");
|
|
708
|
+
expect(normalized).toContain("uri <= 'literal://z'");
|
|
709
|
+
});
|
|
710
|
+
it("should handle timestamp special field with gt operator (filtered in JavaScript)", async () => {
|
|
711
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { timestamp: { gt: 1234567890 } } });
|
|
712
|
+
// timestamp filtering is done in JavaScript post-query
|
|
713
|
+
expect(query).toContain("FROM node");
|
|
714
|
+
expect(query).not.toContain("timestamp > 1234567890");
|
|
715
|
+
});
|
|
716
|
+
it("should handle multiple special fields (filtered in JavaScript)", async () => {
|
|
717
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
718
|
+
where: {
|
|
719
|
+
author: "did:key:alice",
|
|
720
|
+
timestamp: { gt: 1000 }
|
|
721
|
+
}
|
|
722
|
+
});
|
|
723
|
+
// author and timestamp filtering is done in JavaScript post-query
|
|
724
|
+
expect(query).toContain("FROM node");
|
|
725
|
+
expect(query).not.toContain("author = 'did:key:alice'");
|
|
726
|
+
expect(query).not.toContain("timestamp > 1000");
|
|
727
|
+
});
|
|
728
|
+
it("should handle mixed special and regular properties", async () => {
|
|
729
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
730
|
+
where: {
|
|
731
|
+
name: "Pasta",
|
|
732
|
+
author: "did:key:alice",
|
|
733
|
+
rating: { gt: 4 }
|
|
734
|
+
}
|
|
735
|
+
});
|
|
736
|
+
const normalized = normalizeQuery(query);
|
|
737
|
+
// Regular properties use graph traversal filters
|
|
738
|
+
expect(normalized).toContain("count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) = 'Pasta']) > 0");
|
|
739
|
+
// Comparison operators (gt) are filtered in JavaScript, not SQL
|
|
740
|
+
expect(normalized).not.toContain("out.uri > 4");
|
|
741
|
+
expect(normalized).not.toContain("target > 4");
|
|
742
|
+
// author filtering is done in JavaScript post-query
|
|
743
|
+
expect(normalized).not.toContain("author = 'did:key:alice'");
|
|
744
|
+
});
|
|
745
|
+
it("should handle boolean values", async () => {
|
|
746
|
+
let Task = class Task extends Ad4mModel {
|
|
747
|
+
constructor() {
|
|
748
|
+
super(...arguments);
|
|
749
|
+
this.completed = false;
|
|
750
|
+
}
|
|
751
|
+
};
|
|
752
|
+
__decorate([
|
|
753
|
+
Property({ through: "task://completed" }),
|
|
754
|
+
__metadata("design:type", Boolean)
|
|
755
|
+
], Task.prototype, "completed", void 0);
|
|
756
|
+
Task = __decorate([
|
|
757
|
+
Model({ name: "Task" })
|
|
758
|
+
], Task);
|
|
759
|
+
const query = await Task.queryToSurrealQL(mockPerspective, { where: { completed: true } });
|
|
760
|
+
expect(query).toContain("fn::parse_literal(out.uri) = true");
|
|
761
|
+
});
|
|
762
|
+
it("should handle array of numbers", async () => {
|
|
763
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { rating: [4, 5] } });
|
|
764
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://rating' AND fn::parse_literal(out.uri) IN [4, 5]]) > 0");
|
|
765
|
+
});
|
|
766
|
+
it("should skip unknown properties in where clause", async () => {
|
|
767
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { unknownProp: "value" } });
|
|
768
|
+
// Should not throw error, just skip the unknown property
|
|
769
|
+
expect(query).toContain("source");
|
|
770
|
+
// Should not contain any condition for unknownProp
|
|
771
|
+
expect(query).not.toContain("unknownProp");
|
|
772
|
+
});
|
|
773
|
+
it.skip("should skip unknown properties in select clause", async () => {
|
|
774
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { properties: ["name", "unknownProp"] });
|
|
775
|
+
// Should include name using aggregation
|
|
776
|
+
expect(query).toContain("array::first(target[WHERE predicate = 'recipe://name']) AS name");
|
|
777
|
+
// Should not error on unknownProp, just skip it
|
|
778
|
+
expect(query).not.toContain("unknownProp");
|
|
779
|
+
});
|
|
780
|
+
it("should handle order by regular property (handled in JavaScript)", async () => {
|
|
781
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { order: { name: "ASC" } });
|
|
782
|
+
// ORDER BY is now handled in JavaScript post-query
|
|
783
|
+
expect(query).toContain("FROM node");
|
|
784
|
+
expect(query).not.toContain("ORDER BY");
|
|
785
|
+
});
|
|
786
|
+
it("should generate query with only properties specified", async () => {
|
|
787
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { properties: ["name", "rating"] });
|
|
788
|
+
// With array::group(), all link data is selected, filtering happens in instancesFromSurrealResult
|
|
789
|
+
expect(query).toContain("->link AS links");
|
|
631
790
|
});
|
|
632
791
|
});
|
|
633
|
-
describe("Ad4mModel
|
|
792
|
+
describe("Ad4mModel.instancesFromSurrealResult() and SurrealDB integration", () => {
|
|
634
793
|
// Test Recipe model
|
|
635
794
|
let Recipe = class Recipe extends Ad4mModel {
|
|
636
795
|
constructor() {
|
|
@@ -655,10 +814,9 @@ describe("Ad4mModel query methods (modelQuery integration)", () => {
|
|
|
655
814
|
Recipe = __decorate([
|
|
656
815
|
Model({ name: "Recipe" })
|
|
657
816
|
], Recipe);
|
|
658
|
-
// Mock perspective with
|
|
817
|
+
// Mock perspective with both querySurrealDB and infer methods
|
|
659
818
|
const mockPerspective = {
|
|
660
|
-
|
|
661
|
-
modelQuery: jest.fn(),
|
|
819
|
+
querySurrealDB: jest.fn(),
|
|
662
820
|
infer: jest.fn(),
|
|
663
821
|
uuid: 'test-perspective-uuid',
|
|
664
822
|
stringOrTemplateObjectToSubjectClassName: jest.fn().mockResolvedValue('Recipe')
|
|
@@ -666,137 +824,276 @@ describe("Ad4mModel query methods (modelQuery integration)", () => {
|
|
|
666
824
|
beforeEach(() => {
|
|
667
825
|
jest.clearAllMocks();
|
|
668
826
|
});
|
|
669
|
-
it("should
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
827
|
+
it("should convert empty SurrealDB results correctly", async () => {
|
|
828
|
+
const result = await Recipe.instancesFromSurrealResult(mockPerspective, {}, []);
|
|
829
|
+
expect(result.results).toEqual([]);
|
|
830
|
+
expect(result.totalCount).toBe(0);
|
|
831
|
+
});
|
|
832
|
+
it("should convert SurrealDB results to model instances", async () => {
|
|
833
|
+
const surrealResults = [
|
|
834
|
+
{
|
|
835
|
+
source: "node:abc123",
|
|
836
|
+
source_uri: "literal://recipe1",
|
|
837
|
+
links: [
|
|
838
|
+
{ predicate: "recipe://name", target: "Pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
839
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
840
|
+
{ predicate: "recipe://ingredient", target: "pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
841
|
+
{ predicate: "recipe://ingredient", target: "tomato", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
842
|
+
{ predicate: "recipe://ingredient", target: "cheese", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
843
|
+
]
|
|
844
|
+
},
|
|
845
|
+
{
|
|
846
|
+
source: "node:def456",
|
|
847
|
+
source_uri: "literal://recipe2",
|
|
848
|
+
links: [
|
|
849
|
+
{ predicate: "recipe://name", target: "Pizza", author: "did:key:bob", timestamp: "2023-01-02T00:00:00Z" },
|
|
850
|
+
{ predicate: "recipe://rating", target: "4", author: "did:key:bob", timestamp: "2023-01-02T00:00:00Z" },
|
|
851
|
+
{ predicate: "recipe://ingredient", target: "dough", author: "did:key:bob", timestamp: "2023-01-02T00:00:00Z" },
|
|
852
|
+
{ predicate: "recipe://ingredient", target: "cheese", author: "did:key:bob", timestamp: "2023-01-02T00:00:00Z" },
|
|
853
|
+
{ predicate: "recipe://ingredient", target: "tomato", author: "did:key:bob", timestamp: "2023-01-02T00:00:00Z" }
|
|
854
|
+
]
|
|
855
|
+
}
|
|
856
|
+
];
|
|
857
|
+
const result = await Recipe.instancesFromSurrealResult(mockPerspective, {}, surrealResults);
|
|
858
|
+
expect(result.results).toHaveLength(2);
|
|
859
|
+
expect(result.totalCount).toBe(2);
|
|
860
|
+
const recipe1 = result.results[0];
|
|
861
|
+
expect(recipe1).toBeInstanceOf(Recipe);
|
|
862
|
+
expect(recipe1.name).toBe("Pasta");
|
|
863
|
+
expect(recipe1.rating).toBe(5);
|
|
864
|
+
expect(recipe1.ingredients).toEqual(["pasta", "tomato", "cheese"]);
|
|
865
|
+
const recipe2 = result.results[1];
|
|
866
|
+
expect(recipe2).toBeInstanceOf(Recipe);
|
|
867
|
+
expect(recipe2.name).toBe("Pizza");
|
|
868
|
+
expect(recipe2.rating).toBe(4);
|
|
869
|
+
});
|
|
870
|
+
it("should filter properties when query specifies properties", async () => {
|
|
871
|
+
const surrealResults = [
|
|
872
|
+
{
|
|
873
|
+
source: "node:abc123",
|
|
874
|
+
source_uri: "literal://recipe1",
|
|
875
|
+
links: [
|
|
876
|
+
{ predicate: "recipe://name", target: "Pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
877
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
878
|
+
{ predicate: "recipe://ingredient", target: "pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
879
|
+
{ predicate: "recipe://ingredient", target: "tomato", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
880
|
+
]
|
|
881
|
+
}
|
|
882
|
+
];
|
|
883
|
+
const result = await Recipe.instancesFromSurrealResult(mockPerspective, { properties: ["name"] }, surrealResults);
|
|
884
|
+
expect(result.results).toHaveLength(1);
|
|
885
|
+
const recipe = result.results[0];
|
|
886
|
+
expect(recipe.name).toBe("Pasta");
|
|
887
|
+
// rating and ingredients should be removed since only "name" was requested
|
|
888
|
+
expect(recipe.rating).toBeUndefined();
|
|
889
|
+
expect(recipe.ingredients).toBeUndefined();
|
|
890
|
+
// author, createdAt, updatedAt are also stripped unless explicitly requested
|
|
891
|
+
expect(recipe.author).toBeUndefined();
|
|
892
|
+
expect(recipe.timestamp).toBeUndefined();
|
|
893
|
+
});
|
|
894
|
+
it("should filter properties when query specifies properties", async () => {
|
|
895
|
+
const surrealResults = [
|
|
896
|
+
{
|
|
897
|
+
source: "node:abc123",
|
|
898
|
+
source_uri: "literal://recipe1",
|
|
899
|
+
links: [
|
|
900
|
+
{ predicate: "recipe://name", target: "Pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
901
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
902
|
+
{ predicate: "recipe://ingredient", target: "pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
903
|
+
{ predicate: "recipe://ingredient", target: "tomato", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
904
|
+
]
|
|
905
|
+
}
|
|
906
|
+
];
|
|
907
|
+
const result = await Recipe.instancesFromSurrealResult(mockPerspective, { properties: ["name"] }, surrealResults);
|
|
908
|
+
expect(result.results).toHaveLength(1);
|
|
909
|
+
const recipe = result.results[0];
|
|
910
|
+
expect(recipe.name).toBe("Pasta");
|
|
911
|
+
// rating and ingredients should be removed since only "name" was requested
|
|
912
|
+
expect(recipe.rating).toBeUndefined();
|
|
913
|
+
});
|
|
914
|
+
it("should handle results missing base field", async () => {
|
|
915
|
+
const surrealResults = [
|
|
916
|
+
{
|
|
917
|
+
links: [
|
|
918
|
+
{ predicate: "recipe://name", target: "Pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
919
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
920
|
+
]
|
|
921
|
+
// Missing source field
|
|
922
|
+
}
|
|
923
|
+
];
|
|
924
|
+
const result = await Recipe.instancesFromSurrealResult(mockPerspective, {}, surrealResults);
|
|
925
|
+
// Should filter out the invalid result (or handle gracefully)
|
|
926
|
+
expect(result.results).toHaveLength(0);
|
|
927
|
+
expect(result.totalCount).toBe(0);
|
|
928
|
+
});
|
|
929
|
+
it("should use SurrealDB by default in findAll()", async () => {
|
|
930
|
+
const surrealResults = [
|
|
931
|
+
{
|
|
932
|
+
source: "node:abc123",
|
|
933
|
+
source_uri: "literal://recipe1",
|
|
934
|
+
links: [
|
|
935
|
+
{ predicate: "recipe://name", target: "Pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
936
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
937
|
+
{ predicate: "recipe://ingredient", target: "pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
938
|
+
]
|
|
939
|
+
}
|
|
940
|
+
];
|
|
941
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
942
|
+
const results = await Recipe.findAll(mockPerspective, {});
|
|
943
|
+
expect(mockPerspective.querySurrealDB).toHaveBeenCalledTimes(1);
|
|
679
944
|
expect(mockPerspective.infer).not.toHaveBeenCalled();
|
|
680
945
|
expect(results).toHaveLength(1);
|
|
681
946
|
expect(results[0].name).toBe("Pasta");
|
|
682
947
|
});
|
|
683
|
-
it("should
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
948
|
+
it("should use Prolog when useSurrealDB is false in findAll()", async () => {
|
|
949
|
+
const prologResults = [{
|
|
950
|
+
AllInstances: [
|
|
951
|
+
["literal://recipe1", [["name", "Pasta"]], [["ingredients", ["pasta"]]], "2023-01-01T00:00:00Z", "did:key:alice"]
|
|
952
|
+
],
|
|
953
|
+
TotalCount: 1
|
|
954
|
+
}];
|
|
955
|
+
mockPerspective.infer.mockResolvedValue(prologResults);
|
|
690
956
|
const results = await Recipe.findAll(mockPerspective, {}, false);
|
|
691
|
-
expect(mockPerspective.
|
|
692
|
-
expect(mockPerspective.
|
|
957
|
+
expect(mockPerspective.infer).toHaveBeenCalledTimes(1);
|
|
958
|
+
expect(mockPerspective.querySurrealDB).not.toHaveBeenCalled();
|
|
693
959
|
expect(results).toHaveLength(1);
|
|
694
960
|
});
|
|
695
|
-
it("should use
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
961
|
+
it("should use SurrealDB by default in findAllAndCount()", async () => {
|
|
962
|
+
const surrealResults = [
|
|
963
|
+
{
|
|
964
|
+
source: "node:abc123",
|
|
965
|
+
source_uri: "literal://recipe1",
|
|
966
|
+
links: [
|
|
967
|
+
{ predicate: "recipe://name", target: "Pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
968
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
969
|
+
{ predicate: "recipe://ingredient", target: "pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
970
|
+
]
|
|
971
|
+
}
|
|
972
|
+
];
|
|
973
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
702
974
|
const { results, totalCount } = await Recipe.findAllAndCount(mockPerspective, {});
|
|
703
|
-
expect(mockPerspective.
|
|
975
|
+
expect(mockPerspective.querySurrealDB).toHaveBeenCalledTimes(1);
|
|
704
976
|
expect(mockPerspective.infer).not.toHaveBeenCalled();
|
|
705
977
|
expect(results).toHaveLength(1);
|
|
706
978
|
expect(totalCount).toBe(1);
|
|
707
979
|
});
|
|
708
|
-
it("should use
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
980
|
+
it("should use SurrealDB by default in paginate()", async () => {
|
|
981
|
+
const surrealResults = [
|
|
982
|
+
{
|
|
983
|
+
source: "node:abc123",
|
|
984
|
+
source_uri: "literal://recipe1",
|
|
985
|
+
links: [
|
|
986
|
+
{ predicate: "recipe://name", target: "Pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
987
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
988
|
+
{ predicate: "recipe://ingredient", target: "pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
989
|
+
]
|
|
990
|
+
}
|
|
991
|
+
];
|
|
992
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
715
993
|
const page = await Recipe.paginate(mockPerspective, 10, 1, {});
|
|
716
|
-
expect(mockPerspective.
|
|
994
|
+
expect(mockPerspective.querySurrealDB).toHaveBeenCalledTimes(1);
|
|
717
995
|
expect(mockPerspective.infer).not.toHaveBeenCalled();
|
|
718
996
|
expect(page.results).toHaveLength(1);
|
|
719
997
|
expect(page.pageSize).toBe(10);
|
|
720
998
|
expect(page.pageNumber).toBe(1);
|
|
721
|
-
expect(page.totalCount).toBe(1);
|
|
722
999
|
});
|
|
723
|
-
it("should use
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
1000
|
+
it("should use SurrealDB by default in count()", async () => {
|
|
1001
|
+
// Since count() uses result.length and GROUP BY returns one row per source,
|
|
1002
|
+
// mock 5 recipe sources
|
|
1003
|
+
const surrealResults = Array.from({ length: 5 }, (_, i) => ({
|
|
1004
|
+
source: `node:abc${i + 1}`,
|
|
1005
|
+
source_uri: `literal://recipe${i + 1}`,
|
|
1006
|
+
links: [
|
|
1007
|
+
{ predicate: "recipe://name", target: `Recipe ${i + 1}`, author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1008
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
1009
|
+
]
|
|
1010
|
+
}));
|
|
1011
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
728
1012
|
const count = await Recipe.count(mockPerspective, {});
|
|
729
|
-
expect(mockPerspective.
|
|
1013
|
+
expect(mockPerspective.querySurrealDB).toHaveBeenCalledTimes(1);
|
|
730
1014
|
expect(mockPerspective.infer).not.toHaveBeenCalled();
|
|
731
1015
|
expect(count).toBe(5);
|
|
732
1016
|
});
|
|
733
|
-
it("should
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
expect(mockPerspective.modelQuery).toHaveBeenCalledTimes(1);
|
|
740
|
-
expect(mockPerspective.querySparql).not.toHaveBeenCalled();
|
|
1017
|
+
it("should use Prolog when useSurrealDB is false in count()", async () => {
|
|
1018
|
+
const prologResults = [{ TotalCount: 10 }];
|
|
1019
|
+
mockPerspective.infer.mockResolvedValue(prologResults);
|
|
1020
|
+
const count = await Recipe.count(mockPerspective, {}, false);
|
|
1021
|
+
expect(mockPerspective.infer).toHaveBeenCalledTimes(1);
|
|
1022
|
+
expect(mockPerspective.querySurrealDB).not.toHaveBeenCalled();
|
|
741
1023
|
expect(count).toBe(10);
|
|
742
1024
|
});
|
|
743
|
-
it("should use
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
1025
|
+
it("should use SurrealDB by default in ModelQueryBuilder.get()", async () => {
|
|
1026
|
+
const surrealResults = [
|
|
1027
|
+
{
|
|
1028
|
+
source: "node:abc123",
|
|
1029
|
+
source_uri: "literal://recipe1",
|
|
1030
|
+
links: [
|
|
1031
|
+
{ predicate: "recipe://name", target: "Pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1032
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1033
|
+
{ predicate: "recipe://ingredient", target: "pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
1034
|
+
]
|
|
1035
|
+
}
|
|
1036
|
+
];
|
|
1037
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
750
1038
|
const results = await Recipe.query(mockPerspective)
|
|
751
1039
|
.where({ name: "Pasta" })
|
|
752
|
-
.engine('sparql')
|
|
753
1040
|
.get();
|
|
754
|
-
expect(mockPerspective.
|
|
1041
|
+
expect(mockPerspective.querySurrealDB).toHaveBeenCalledTimes(1);
|
|
755
1042
|
expect(mockPerspective.infer).not.toHaveBeenCalled();
|
|
756
1043
|
expect(results).toHaveLength(1);
|
|
757
1044
|
expect(results[0].name).toBe("Pasta");
|
|
758
1045
|
});
|
|
759
|
-
it("should use
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
1046
|
+
it("should use Prolog when useSurrealDB(false) in ModelQueryBuilder.get()", async () => {
|
|
1047
|
+
const prologResults = [{
|
|
1048
|
+
AllInstances: [
|
|
1049
|
+
["literal://recipe1", [["name", "Pasta"]], [["ingredients", ["pasta"]]], "2023-01-01T00:00:00Z", "did:key:alice"]
|
|
1050
|
+
],
|
|
1051
|
+
TotalCount: 1
|
|
1052
|
+
}];
|
|
1053
|
+
mockPerspective.infer.mockResolvedValue(prologResults);
|
|
766
1054
|
const results = await Recipe.query(mockPerspective)
|
|
767
1055
|
.where({ name: "Pasta" })
|
|
768
|
-
.
|
|
1056
|
+
.useSurrealDB(false)
|
|
769
1057
|
.get();
|
|
770
|
-
|
|
771
|
-
expect(mockPerspective.
|
|
772
|
-
expect(mockPerspective.infer).not.toHaveBeenCalled();
|
|
1058
|
+
expect(mockPerspective.infer).toHaveBeenCalledTimes(1);
|
|
1059
|
+
expect(mockPerspective.querySurrealDB).not.toHaveBeenCalled();
|
|
773
1060
|
expect(results).toHaveLength(1);
|
|
774
1061
|
});
|
|
775
|
-
it("should use
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
1062
|
+
it("should use SurrealDB by default in ModelQueryBuilder.count()", async () => {
|
|
1063
|
+
// count() counts the number of rows returned by the query (one row per source)
|
|
1064
|
+
const surrealResults = Array.from({ length: 3 }, (_, i) => ({
|
|
1065
|
+
source: `node:abc${i + 1}`,
|
|
1066
|
+
source_uri: `literal://recipe${i + 1}`,
|
|
1067
|
+
links: [
|
|
1068
|
+
{ predicate: "recipe://name", target: `Recipe ${i + 1}`, author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1069
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
1070
|
+
]
|
|
1071
|
+
}));
|
|
1072
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
780
1073
|
const count = await Recipe.query(mockPerspective)
|
|
781
1074
|
.where({ rating: { gt: 4 } })
|
|
782
|
-
.engine('sparql')
|
|
783
1075
|
.count();
|
|
784
|
-
expect(mockPerspective.
|
|
1076
|
+
expect(mockPerspective.querySurrealDB).toHaveBeenCalledTimes(1);
|
|
785
1077
|
expect(mockPerspective.infer).not.toHaveBeenCalled();
|
|
786
1078
|
expect(count).toBe(3);
|
|
787
1079
|
});
|
|
788
|
-
it("should use
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
1080
|
+
it("should use SurrealDB by default in ModelQueryBuilder.paginate()", async () => {
|
|
1081
|
+
const surrealResults = [
|
|
1082
|
+
{
|
|
1083
|
+
source: "node:abc123",
|
|
1084
|
+
source_uri: "literal://recipe1",
|
|
1085
|
+
links: [
|
|
1086
|
+
{ predicate: "recipe://name", target: "Pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1087
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1088
|
+
{ predicate: "recipe://ingredient", target: "pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
1089
|
+
]
|
|
1090
|
+
}
|
|
1091
|
+
];
|
|
1092
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
795
1093
|
const page = await Recipe.query(mockPerspective)
|
|
796
1094
|
.where({ rating: { gt: 3 } })
|
|
797
|
-
.engine('sparql')
|
|
798
1095
|
.paginate(10, 1);
|
|
799
|
-
expect(mockPerspective.
|
|
1096
|
+
expect(mockPerspective.querySurrealDB).toHaveBeenCalledTimes(1);
|
|
800
1097
|
expect(mockPerspective.infer).not.toHaveBeenCalled();
|
|
801
1098
|
expect(page.results).toHaveLength(1);
|
|
802
1099
|
expect(page.pageSize).toBe(10);
|
|
@@ -830,8 +1127,7 @@ describe("Ad4mModel.count() with advanced where conditions", () => {
|
|
|
830
1127
|
], Recipe);
|
|
831
1128
|
// Mock perspective
|
|
832
1129
|
const mockPerspective = {
|
|
833
|
-
|
|
834
|
-
modelQuery: jest.fn(),
|
|
1130
|
+
querySurrealDB: jest.fn(),
|
|
835
1131
|
infer: jest.fn(),
|
|
836
1132
|
uuid: 'test-perspective-uuid',
|
|
837
1133
|
stringOrTemplateObjectToSubjectClassName: jest.fn().mockResolvedValue('Recipe')
|
|
@@ -839,1217 +1135,175 @@ describe("Ad4mModel.count() with advanced where conditions", () => {
|
|
|
839
1135
|
beforeEach(() => {
|
|
840
1136
|
jest.clearAllMocks();
|
|
841
1137
|
});
|
|
842
|
-
it("should apply filtering for gt operator on properties in
|
|
843
|
-
//
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
1138
|
+
it("should apply JS-level filtering for gt operator on properties in SurrealDB count()", async () => {
|
|
1139
|
+
// Mock SurrealDB results: 5 recipes with ratings 1, 2, 3, 4, 5
|
|
1140
|
+
const surrealResults = Array.from({ length: 5 }, (_, i) => ({
|
|
1141
|
+
source: `node:abc${i + 1}`,
|
|
1142
|
+
source_uri: `literal://recipe${i + 1}`,
|
|
1143
|
+
links: [
|
|
1144
|
+
{ predicate: "recipe://name", target: `Recipe ${i + 1}`, author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1145
|
+
{ predicate: "recipe://rating", target: `${i + 1}`, author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
1146
|
+
]
|
|
1147
|
+
}));
|
|
1148
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
848
1149
|
// Count recipes with rating > 3 (should match 2 recipes: rating 4 and 5)
|
|
849
1150
|
const count = await Recipe.count(mockPerspective, { where: { rating: { gt: 3 } } });
|
|
1151
|
+
// Verify count matches the number of instances that would be returned by findAll
|
|
1152
|
+
const findAllResults = await Recipe.findAll(mockPerspective, { where: { rating: { gt: 3 } } });
|
|
850
1153
|
expect(count).toBe(2);
|
|
851
|
-
expect(
|
|
852
|
-
});
|
|
853
|
-
it("should apply filtering for between operator on properties in
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
1154
|
+
expect(count).toBe(findAllResults.length);
|
|
1155
|
+
});
|
|
1156
|
+
it("should apply JS-level filtering for between operator on properties in SurrealDB count()", async () => {
|
|
1157
|
+
// Mock SurrealDB results: 5 recipes with ratings 1, 2, 3, 4, 5
|
|
1158
|
+
const surrealResults = Array.from({ length: 5 }, (_, i) => ({
|
|
1159
|
+
source: `node:abc${i + 1}`,
|
|
1160
|
+
source_uri: `literal://recipe${i + 1}`,
|
|
1161
|
+
links: [
|
|
1162
|
+
{ predicate: "recipe://name", target: `Recipe ${i + 1}`, author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1163
|
+
{ predicate: "recipe://rating", target: `${i + 1}`, author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
1164
|
+
]
|
|
1165
|
+
}));
|
|
1166
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
858
1167
|
// Count recipes with rating between 2 and 4 (should match 3 recipes: rating 2, 3, 4)
|
|
859
1168
|
const count = await Recipe.count(mockPerspective, { where: { rating: { between: [2, 4] } } });
|
|
1169
|
+
// Verify count matches the number of instances that would be returned by findAll
|
|
1170
|
+
const findAllResults = await Recipe.findAll(mockPerspective, { where: { rating: { between: [2, 4] } } });
|
|
860
1171
|
expect(count).toBe(3);
|
|
861
|
-
expect(
|
|
862
|
-
});
|
|
863
|
-
it("should apply filtering for timestamp gt operator in
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
1172
|
+
expect(count).toBe(findAllResults.length);
|
|
1173
|
+
});
|
|
1174
|
+
it("should apply JS-level filtering for timestamp gt operator in SurrealDB count()", async () => {
|
|
1175
|
+
// Mock SurrealDB results: 5 recipes with different timestamps
|
|
1176
|
+
const surrealResults = Array.from({ length: 5 }, (_, i) => ({
|
|
1177
|
+
source: `node:abc${i + 1}`,
|
|
1178
|
+
source_uri: `literal://recipe${i + 1}`,
|
|
1179
|
+
links: [
|
|
1180
|
+
{ predicate: "recipe://name", target: `Recipe ${i + 1}`, author: "did:key:alice", timestamp: `2023-01-0${i + 1}T00:00:00Z` },
|
|
1181
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: `2023-01-0${i + 1}T00:00:00Z` }
|
|
1182
|
+
]
|
|
1183
|
+
}));
|
|
1184
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1185
|
+
// Count recipes with timestamp > 2023-01-03 (should match 2 recipes: 2023-01-04 and 2023-01-05)
|
|
868
1186
|
const targetTimestamp = new Date("2023-01-03T00:00:00Z").getTime();
|
|
869
1187
|
const count = await Recipe.count(mockPerspective, { where: { timestamp: { gt: targetTimestamp } } });
|
|
1188
|
+
// Verify count matches the number of instances that would be returned by findAll
|
|
1189
|
+
const findAllResults = await Recipe.findAll(mockPerspective, { where: { timestamp: { gt: targetTimestamp } } });
|
|
870
1190
|
expect(count).toBe(2);
|
|
871
|
-
expect(
|
|
872
|
-
});
|
|
873
|
-
it("should apply filtering for timestamp between operator in
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
1191
|
+
expect(count).toBe(findAllResults.length);
|
|
1192
|
+
});
|
|
1193
|
+
it("should apply JS-level filtering for timestamp between operator in SurrealDB count()", async () => {
|
|
1194
|
+
// Mock SurrealDB results: 5 recipes with different timestamps
|
|
1195
|
+
const surrealResults = Array.from({ length: 5 }, (_, i) => ({
|
|
1196
|
+
source: `node:abc${i + 1}`,
|
|
1197
|
+
source_uri: `literal://recipe${i + 1}`,
|
|
1198
|
+
links: [
|
|
1199
|
+
{ predicate: "recipe://name", target: `Recipe ${i + 1}`, author: "did:key:alice", timestamp: `2023-01-0${i + 1}T00:00:00Z` },
|
|
1200
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: `2023-01-0${i + 1}T00:00:00Z` }
|
|
1201
|
+
]
|
|
1202
|
+
}));
|
|
1203
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1204
|
+
// Count recipes with timestamp between 2023-01-02 and 2023-01-04
|
|
878
1205
|
const startTimestamp = new Date("2023-01-02T00:00:00Z").getTime();
|
|
879
1206
|
const endTimestamp = new Date("2023-01-04T00:00:00Z").getTime();
|
|
880
1207
|
const count = await Recipe.count(mockPerspective, {
|
|
881
1208
|
where: { timestamp: { between: [startTimestamp, endTimestamp] } }
|
|
882
1209
|
});
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
it("should apply filtering for author filtering in SPARQL count()", async () => {
|
|
887
|
-
mockPerspective.modelQuery.mockResolvedValue({
|
|
888
|
-
instances: [],
|
|
889
|
-
totalCount: 3
|
|
1210
|
+
// Verify count matches the number of instances that would be returned by findAll
|
|
1211
|
+
const findAllResults = await Recipe.findAll(mockPerspective, {
|
|
1212
|
+
where: { timestamp: { between: [startTimestamp, endTimestamp] } }
|
|
890
1213
|
});
|
|
1214
|
+
expect(count).toBe(3);
|
|
1215
|
+
expect(count).toBe(findAllResults.length);
|
|
1216
|
+
});
|
|
1217
|
+
it("should apply JS-level filtering for author filtering in SurrealDB count()", async () => {
|
|
1218
|
+
// Mock SurrealDB results: 3 recipes by Alice and 2 by Bob
|
|
1219
|
+
const surrealResults = [
|
|
1220
|
+
...Array.from({ length: 3 }, (_, i) => ({
|
|
1221
|
+
source: `node:abc${i + 1}`,
|
|
1222
|
+
source_uri: `literal://recipe${i + 1}`,
|
|
1223
|
+
links: [
|
|
1224
|
+
{ predicate: "recipe://name", target: `Recipe ${i + 1}`, author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1225
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
1226
|
+
]
|
|
1227
|
+
})),
|
|
1228
|
+
...Array.from({ length: 2 }, (_, i) => ({
|
|
1229
|
+
source: `node:def${i + 4}`,
|
|
1230
|
+
source_uri: `literal://recipe${i + 4}`,
|
|
1231
|
+
links: [
|
|
1232
|
+
{ predicate: "recipe://name", target: `Recipe ${i + 4}`, author: "did:key:bob", timestamp: "2023-01-02T00:00:00Z" },
|
|
1233
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:bob", timestamp: "2023-01-02T00:00:00Z" }
|
|
1234
|
+
]
|
|
1235
|
+
}))
|
|
1236
|
+
];
|
|
1237
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
891
1238
|
// Count recipes by Alice (should match 3 recipes)
|
|
892
1239
|
const count = await Recipe.count(mockPerspective, { where: { author: "did:key:alice" } });
|
|
1240
|
+
// Verify count matches the number of instances that would be returned by findAll
|
|
1241
|
+
const findAllResults = await Recipe.findAll(mockPerspective, { where: { author: "did:key:alice" } });
|
|
893
1242
|
expect(count).toBe(3);
|
|
894
|
-
expect(
|
|
895
|
-
});
|
|
896
|
-
it("should apply filtering in ModelQueryBuilder.count() with gt operator", async () => {
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
1243
|
+
expect(count).toBe(findAllResults.length);
|
|
1244
|
+
});
|
|
1245
|
+
it("should apply JS-level filtering in ModelQueryBuilder.count() with gt operator", async () => {
|
|
1246
|
+
// Mock SurrealDB results: 5 recipes with ratings 1, 2, 3, 4, 5
|
|
1247
|
+
const surrealResults = Array.from({ length: 5 }, (_, i) => ({
|
|
1248
|
+
source: `node:abc${i + 1}`,
|
|
1249
|
+
source_uri: `literal://recipe${i + 1}`,
|
|
1250
|
+
links: [
|
|
1251
|
+
{ predicate: "recipe://name", target: `Recipe ${i + 1}`, author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1252
|
+
{ predicate: "recipe://rating", target: `${i + 1}`, author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
1253
|
+
]
|
|
1254
|
+
}));
|
|
1255
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
901
1256
|
// Count recipes with rating > 3 using ModelQueryBuilder
|
|
902
1257
|
const count = await Recipe.query(mockPerspective)
|
|
903
1258
|
.where({ rating: { gt: 3 } })
|
|
904
|
-
.engine('sparql')
|
|
905
1259
|
.count();
|
|
1260
|
+
// Verify count matches the number of instances that would be returned by get()
|
|
1261
|
+
const getResults = await Recipe.query(mockPerspective)
|
|
1262
|
+
.where({ rating: { gt: 3 } })
|
|
1263
|
+
.get();
|
|
906
1264
|
expect(count).toBe(2);
|
|
907
|
-
expect(
|
|
908
|
-
});
|
|
909
|
-
it("should apply filtering in ModelQueryBuilder.count() with timestamp between", async () => {
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
1265
|
+
expect(count).toBe(getResults.length);
|
|
1266
|
+
});
|
|
1267
|
+
it("should apply JS-level filtering in ModelQueryBuilder.count() with timestamp between", async () => {
|
|
1268
|
+
// Mock SurrealDB results: 5 recipes with different timestamps
|
|
1269
|
+
const surrealResults = Array.from({ length: 5 }, (_, i) => ({
|
|
1270
|
+
source: `node:abc${i + 1}`,
|
|
1271
|
+
source_uri: `literal://recipe${i + 1}`,
|
|
1272
|
+
links: [
|
|
1273
|
+
{ predicate: "recipe://name", target: `Recipe ${i + 1}`, author: "did:key:alice", timestamp: `2023-01-0${i + 1}T00:00:00Z` },
|
|
1274
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: `2023-01-0${i + 1}T00:00:00Z` }
|
|
1275
|
+
]
|
|
1276
|
+
}));
|
|
1277
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
914
1278
|
const startTimestamp = new Date("2023-01-02T00:00:00Z").getTime();
|
|
915
1279
|
const endTimestamp = new Date("2023-01-04T00:00:00Z").getTime();
|
|
916
1280
|
// Count using ModelQueryBuilder
|
|
917
1281
|
const count = await Recipe.query(mockPerspective)
|
|
918
1282
|
.where({ timestamp: { between: [startTimestamp, endTimestamp] } })
|
|
919
|
-
.engine('sparql')
|
|
920
1283
|
.count();
|
|
1284
|
+
// Verify count matches the number of instances that would be returned by get()
|
|
1285
|
+
const getResults = await Recipe.query(mockPerspective)
|
|
1286
|
+
.where({ timestamp: { between: [startTimestamp, endTimestamp] } })
|
|
1287
|
+
.get();
|
|
921
1288
|
expect(count).toBe(3);
|
|
922
|
-
expect(
|
|
923
|
-
});
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
], Item.prototype, "price", void 0);
|
|
932
|
-
__decorate([
|
|
933
|
-
Property({ through: "item://name", required: true }),
|
|
934
|
-
__metadata("design:type", String)
|
|
935
|
-
], Item.prototype, "name", void 0);
|
|
936
|
-
Item = __decorate([
|
|
937
|
-
Model({ name: "Item" })
|
|
938
|
-
], Item);
|
|
939
|
-
it("should generate gt filter", async () => {
|
|
940
|
-
const mockPersp = { getLinks: jest.fn().mockResolvedValue([]) };
|
|
941
|
-
const query = await Item.queryToSPARQL(mockPersp, { where: { price: { gt: 10 } } });
|
|
942
|
-
// gt is now JS post-filter — SPARQL just joins the property
|
|
943
|
-
expect(query).toContain("item://price");
|
|
944
|
-
});
|
|
945
|
-
it("should generate between filter", async () => {
|
|
946
|
-
const mockPersp = { getLinks: jest.fn().mockResolvedValue([]) };
|
|
947
|
-
const query = await Item.queryToSPARQL(mockPersp, { where: { price: { between: [5, 20] } } });
|
|
948
|
-
// between is now JS post-filter
|
|
949
|
-
expect(query).toContain("item://price");
|
|
950
|
-
});
|
|
951
|
-
it("should generate contains filter", async () => {
|
|
952
|
-
const mockPersp = { getLinks: jest.fn().mockResolvedValue([]) };
|
|
953
|
-
const query = await Item.queryToSPARQL(mockPersp, { where: { name: { contains: "widget" } } });
|
|
954
|
-
// contains is now JS post-filter
|
|
955
|
-
expect(query).toContain("item://name");
|
|
956
|
-
});
|
|
957
|
-
});
|
|
958
|
-
// ──────────────────────────────────────────────────────────
|
|
959
|
-
// SPARQL Direct Triple Pattern & IRI Tests
|
|
960
|
-
// ──────────────────────────────────────────────────────────
|
|
961
|
-
import { buildSPARQLQuery, formatSPARQLValue } from "./query-sparql";
|
|
962
|
-
describe("SPARQL direct triple pattern generation", () => {
|
|
963
|
-
let Channel = class Channel extends Ad4mModel {
|
|
964
|
-
constructor() {
|
|
965
|
-
super(...arguments);
|
|
966
|
-
this.type = "";
|
|
967
|
-
this.name = "";
|
|
968
|
-
this.description = "";
|
|
969
|
-
this.messages = [];
|
|
970
|
-
}
|
|
971
|
-
};
|
|
972
|
-
__decorate([
|
|
973
|
-
Flag({ through: "flux://entry_type", value: "flux://channel" }),
|
|
974
|
-
__metadata("design:type", String)
|
|
975
|
-
], Channel.prototype, "type", void 0);
|
|
976
|
-
__decorate([
|
|
977
|
-
Property({ through: "flux://name", resolveLanguage: "literal", required: true }),
|
|
978
|
-
__metadata("design:type", String)
|
|
979
|
-
], Channel.prototype, "name", void 0);
|
|
980
|
-
__decorate([
|
|
981
|
-
Optional({ through: "flux://description" }),
|
|
982
|
-
__metadata("design:type", String)
|
|
983
|
-
], Channel.prototype, "description", void 0);
|
|
984
|
-
__decorate([
|
|
985
|
-
HasMany({ through: "flux://has_message" }),
|
|
986
|
-
__metadata("design:type", Array)
|
|
987
|
-
], Channel.prototype, "messages", void 0);
|
|
988
|
-
Channel = __decorate([
|
|
989
|
-
Model({ name: "Channel" })
|
|
990
|
-
], Channel);
|
|
991
|
-
const mockPersp = {};
|
|
992
|
-
it("generates direct triple pattern with ?source ?predicate ?target", async () => {
|
|
993
|
-
const query = await Channel.queryToSPARQL(mockPersp, {});
|
|
994
|
-
// Must use direct triple pattern, not link-node reification
|
|
995
|
-
expect(query).toContain("?source ?predicate ?target");
|
|
996
|
-
expect(query).not.toContain("rdf:type");
|
|
997
|
-
expect(query).not.toContain("ad4m:Link");
|
|
998
|
-
});
|
|
999
|
-
it("generates Flag filter as ?source <predicate> <value>", async () => {
|
|
1000
|
-
const query = await Channel.queryToSPARQL(mockPersp, {});
|
|
1001
|
-
expect(query).toContain("<flux://entry_type>");
|
|
1002
|
-
expect(query).toContain("<flux://channel>");
|
|
1003
|
-
});
|
|
1004
|
-
it("generates Property binding as ?source <predicate> ?varName", async () => {
|
|
1005
|
-
const query = await Channel.queryToSPARQL(mockPersp, {});
|
|
1006
|
-
expect(query).toContain("<flux://name>");
|
|
1007
|
-
expect(query).toMatch(/\?source\s+<flux:\/\/name>\s+\?cfTarget_name/);
|
|
1008
|
-
});
|
|
1009
|
-
it("uses RDF 1.2 reifier pattern for author/timestamp", async () => {
|
|
1010
|
-
const query = await Channel.queryToSPARQL(mockPersp, {});
|
|
1011
|
-
expect(query).toContain("<http://www.w3.org/1999/02/22-rdf-syntax-ns#reifies>");
|
|
1012
|
-
expect(query).toContain("?source ?predicate ?target .");
|
|
1013
|
-
expect(query).toContain("?_reifier <http://www.w3.org/1999/02/22-rdf-syntax-ns#reifies> <<( ?source ?predicate ?target )>>");
|
|
1014
|
-
expect(query).toContain("?_reifier <ad4m://ontology/author> ?author");
|
|
1015
|
-
expect(query).toContain("?_reifier <ad4m://ontology/timestamp> ?timestamp");
|
|
1016
|
-
expect(query).not.toContain("GRAPH");
|
|
1017
|
-
});
|
|
1018
|
-
it("uses FILTER(isIRI(?source)) to exclude non-IRI subjects", async () => {
|
|
1019
|
-
const query = await Channel.queryToSPARQL(mockPersp, {});
|
|
1020
|
-
expect(query).toContain("FILTER(isIRI(?source)");
|
|
1021
|
-
});
|
|
1022
|
-
});
|
|
1023
|
-
describe("formatSPARQLValue", () => {
|
|
1024
|
-
it("wraps strings in double quotes", () => {
|
|
1025
|
-
expect(formatSPARQLValue("hello")).toBe('"hello"');
|
|
1026
|
-
});
|
|
1027
|
-
it("escapes special characters", () => {
|
|
1028
|
-
expect(formatSPARQLValue('say "hi"')).toBe('"say \\"hi\\""');
|
|
1029
|
-
});
|
|
1030
|
-
it("converts numbers to quoted strings", () => {
|
|
1031
|
-
expect(formatSPARQLValue(42)).toBe('"42"');
|
|
1032
|
-
});
|
|
1033
|
-
});
|
|
1034
|
-
// ──────────────────────────────────────────────────────────
|
|
1035
|
-
// Comprehensive SPARQL migration unit tests
|
|
1036
|
-
// ──────────────────────────────────────────────────────────
|
|
1037
|
-
import { matchesCondition } from "./hydration";
|
|
1038
|
-
import { Literal } from "../Literal";
|
|
1039
|
-
// Helper: create literal URLs using JSON encoding (which handles all types including booleans)
|
|
1040
|
-
function literalUrl(value) {
|
|
1041
|
-
if (typeof value === 'string')
|
|
1042
|
-
return Literal.from(value).toUrl();
|
|
1043
|
-
// Use JSON encoding for numbers and booleans since Literal.get() doesn't handle boolean: prefix
|
|
1044
|
-
return `literal:json:${encodeURIComponent(JSON.stringify(value))}`;
|
|
1045
|
-
}
|
|
1046
|
-
describe("matchesCondition()", () => {
|
|
1047
|
-
// Simple equality
|
|
1048
|
-
it("simple string equality — match", () => {
|
|
1049
|
-
expect(matchesCondition("hello", "hello")).toBe(true);
|
|
1050
|
-
});
|
|
1051
|
-
it("simple string equality — mismatch", () => {
|
|
1052
|
-
expect(matchesCondition("hello", "world")).toBe(false);
|
|
1053
|
-
});
|
|
1054
|
-
it("simple boolean equality — true", () => {
|
|
1055
|
-
expect(matchesCondition(true, true)).toBe(true);
|
|
1056
|
-
});
|
|
1057
|
-
it("simple boolean equality — false", () => {
|
|
1058
|
-
expect(matchesCondition(false, false)).toBe(true);
|
|
1059
|
-
});
|
|
1060
|
-
it("boolean mismatch", () => {
|
|
1061
|
-
expect(matchesCondition(true, false)).toBe(false);
|
|
1062
|
-
});
|
|
1063
|
-
it("simple number equality", () => {
|
|
1064
|
-
expect(matchesCondition(42, 42)).toBe(true);
|
|
1065
|
-
});
|
|
1066
|
-
it("number mismatch", () => {
|
|
1067
|
-
expect(matchesCondition(42, 99)).toBe(false);
|
|
1068
|
-
});
|
|
1069
|
-
// Array IN clause
|
|
1070
|
-
it("array IN clause — value present", () => {
|
|
1071
|
-
expect(matchesCondition("a", ["a", "b", "c"])).toBe(true);
|
|
1072
|
-
});
|
|
1073
|
-
it("array IN clause — value absent", () => {
|
|
1074
|
-
expect(matchesCondition("d", ["a", "b", "c"])).toBe(false);
|
|
1075
|
-
});
|
|
1076
|
-
// NOT operator
|
|
1077
|
-
it("not operator with string", () => {
|
|
1078
|
-
expect(matchesCondition("bob", { not: "alice" })).toBe(true);
|
|
1079
|
-
expect(matchesCondition("alice", { not: "alice" })).toBe(false);
|
|
1080
|
-
});
|
|
1081
|
-
it("not operator with array (NOT IN)", () => {
|
|
1082
|
-
expect(matchesCondition("a", { not: ["b", "c"] })).toBe(true);
|
|
1083
|
-
expect(matchesCondition("b", { not: ["b", "c"] })).toBe(false);
|
|
1084
|
-
});
|
|
1085
|
-
// Comparison operators
|
|
1086
|
-
it("gt", () => {
|
|
1087
|
-
expect(matchesCondition(10, { gt: 5 })).toBe(true);
|
|
1088
|
-
expect(matchesCondition(5, { gt: 5 })).toBe(false);
|
|
1089
|
-
});
|
|
1090
|
-
it("gte", () => {
|
|
1091
|
-
expect(matchesCondition(5, { gte: 5 })).toBe(true);
|
|
1092
|
-
expect(matchesCondition(4, { gte: 5 })).toBe(false);
|
|
1093
|
-
});
|
|
1094
|
-
it("lt", () => {
|
|
1095
|
-
expect(matchesCondition(3, { lt: 5 })).toBe(true);
|
|
1096
|
-
expect(matchesCondition(5, { lt: 5 })).toBe(false);
|
|
1097
|
-
});
|
|
1098
|
-
it("lte", () => {
|
|
1099
|
-
expect(matchesCondition(5, { lte: 5 })).toBe(true);
|
|
1100
|
-
expect(matchesCondition(6, { lte: 5 })).toBe(false);
|
|
1101
|
-
});
|
|
1102
|
-
it("combined gt + lt (range)", () => {
|
|
1103
|
-
expect(matchesCondition(5, { gt: 3, lt: 7 })).toBe(true);
|
|
1104
|
-
expect(matchesCondition(3, { gt: 3, lt: 7 })).toBe(false);
|
|
1105
|
-
expect(matchesCondition(7, { gt: 3, lt: 7 })).toBe(false);
|
|
1106
|
-
});
|
|
1107
|
-
// Between
|
|
1108
|
-
it("between — inclusive range", () => {
|
|
1109
|
-
expect(matchesCondition(5, { between: [3, 7] })).toBe(true);
|
|
1110
|
-
expect(matchesCondition(3, { between: [3, 7] })).toBe(true);
|
|
1111
|
-
expect(matchesCondition(7, { between: [3, 7] })).toBe(true);
|
|
1112
|
-
expect(matchesCondition(2, { between: [3, 7] })).toBe(false);
|
|
1113
|
-
expect(matchesCondition(8, { between: [3, 7] })).toBe(false);
|
|
1114
|
-
});
|
|
1115
|
-
// Contains
|
|
1116
|
-
it("contains with string", () => {
|
|
1117
|
-
expect(matchesCondition("hello world", { contains: "world" })).toBe(true);
|
|
1118
|
-
expect(matchesCondition("hello world", { contains: "xyz" })).toBe(false);
|
|
1119
|
-
});
|
|
1120
|
-
it("contains with array", () => {
|
|
1121
|
-
expect(matchesCondition(["a", "b", "c"], { contains: "b" })).toBe(true);
|
|
1122
|
-
expect(matchesCondition(["a", "b", "c"], { contains: "d" })).toBe(false);
|
|
1123
|
-
});
|
|
1124
|
-
// Edge cases
|
|
1125
|
-
it("returns false for undefined value with equality check", () => {
|
|
1126
|
-
expect(matchesCondition(undefined, "hello")).toBe(false);
|
|
1127
|
-
});
|
|
1128
|
-
it("returns false for empty string when condition is non-empty", () => {
|
|
1129
|
-
expect(matchesCondition("", "Recipe 2")).toBe(false);
|
|
1130
|
-
});
|
|
1131
|
-
it("returns true for undefined with not operator", () => {
|
|
1132
|
-
expect(matchesCondition(undefined, { not: "hello" })).toBe(true);
|
|
1133
|
-
});
|
|
1134
|
-
});
|
|
1135
|
-
describe("buildSPARQLQuery edge cases", () => {
|
|
1136
|
-
let FlagModel = class FlagModel extends Ad4mModel {
|
|
1137
|
-
constructor() {
|
|
1138
|
-
super(...arguments);
|
|
1139
|
-
this.type = "";
|
|
1140
|
-
this.name = "";
|
|
1141
|
-
}
|
|
1142
|
-
};
|
|
1143
|
-
__decorate([
|
|
1144
|
-
Flag({ through: "flag://type", value: "flag://FlagModel" }),
|
|
1145
|
-
__metadata("design:type", String)
|
|
1146
|
-
], FlagModel.prototype, "type", void 0);
|
|
1147
|
-
__decorate([
|
|
1148
|
-
Property({ through: "flag://name", resolveLanguage: "literal", required: true }),
|
|
1149
|
-
__metadata("design:type", String)
|
|
1150
|
-
], FlagModel.prototype, "name", void 0);
|
|
1151
|
-
FlagModel = __decorate([
|
|
1152
|
-
Model({ name: "FlagModel" })
|
|
1153
|
-
], FlagModel);
|
|
1154
|
-
let NoFlagModel = class NoFlagModel extends Ad4mModel {
|
|
1155
|
-
constructor() {
|
|
1156
|
-
super(...arguments);
|
|
1157
|
-
this.title = "";
|
|
1158
|
-
}
|
|
1159
|
-
};
|
|
1160
|
-
__decorate([
|
|
1161
|
-
Property({ through: "noflag://title", resolveLanguage: "literal" }),
|
|
1162
|
-
__metadata("design:type", String)
|
|
1163
|
-
], NoFlagModel.prototype, "title", void 0);
|
|
1164
|
-
NoFlagModel = __decorate([
|
|
1165
|
-
Model({ name: "NoFlagModel" })
|
|
1166
|
-
], NoFlagModel);
|
|
1167
|
-
it("adds conformance JOIN for flag + required properties", () => {
|
|
1168
|
-
const metadata = FlagModel.getModelMetadata();
|
|
1169
|
-
const allRelsMeta = {};
|
|
1170
|
-
const sparql = buildSPARQLQuery(metadata, allRelsMeta, {}, FlagModel);
|
|
1171
|
-
// Should contain flag triple pattern
|
|
1172
|
-
expect(sparql).toContain("<flag://type>");
|
|
1173
|
-
expect(sparql).toContain("<flag://FlagModel>");
|
|
1174
|
-
// Should contain required property pattern
|
|
1175
|
-
expect(sparql).toContain("<flag://name>");
|
|
1176
|
-
});
|
|
1177
|
-
it("uses structural subquery when no conformance patterns exist", () => {
|
|
1178
|
-
const metadata = NoFlagModel.getModelMetadata();
|
|
1179
|
-
const allRelsMeta = {};
|
|
1180
|
-
const sparql = buildSPARQLQuery(metadata, allRelsMeta, {}, NoFlagModel);
|
|
1181
|
-
// Should contain structural subquery with DISTINCT
|
|
1182
|
-
expect(sparql).toContain("SELECT DISTINCT ?source");
|
|
1183
|
-
expect(sparql).toContain("<noflag://title>");
|
|
1184
|
-
});
|
|
1185
|
-
it("adds JOIN for where-clause on literal-stored fields", () => {
|
|
1186
|
-
const metadata = FlagModel.getModelMetadata();
|
|
1187
|
-
const allRelsMeta = {};
|
|
1188
|
-
const sparql = buildSPARQLQuery(metadata, allRelsMeta, { where: { name: "test" } }, FlagModel);
|
|
1189
|
-
// Where clause should add a join pattern for name
|
|
1190
|
-
expect(sparql).toContain("flag://name");
|
|
1191
|
-
});
|
|
1192
|
-
it("handles where: {id: 'specific-id'} with FILTER on ?source", () => {
|
|
1193
|
-
const metadata = FlagModel.getModelMetadata();
|
|
1194
|
-
const allRelsMeta = {};
|
|
1195
|
-
const sparql = buildSPARQLQuery(metadata, allRelsMeta, { where: { id: "expr:123" } }, FlagModel);
|
|
1196
|
-
// Should filter by source
|
|
1197
|
-
expect(sparql).toContain("expr:123");
|
|
1198
|
-
});
|
|
1199
|
-
});
|
|
1200
|
-
// ── Batch hydration for reverse relations (3.4) ─────────────────────────────
|
|
1201
|
-
describe("Batch hydration for reverse relations", () => {
|
|
1202
|
-
it("batch hydration should correctly group targets by parent instance ID", () => {
|
|
1203
|
-
// Test the grouping logic used in batch SPARQL hydration
|
|
1204
|
-
const sparqlResults = [
|
|
1205
|
-
{ target: "inst:1", source: "child:a" },
|
|
1206
|
-
{ target: "inst:1", source: "child:b" },
|
|
1207
|
-
{ target: "inst:2", source: "child:c" },
|
|
1208
|
-
{ target: "inst:3", source: "child:d" },
|
|
1209
|
-
{ target: "inst:3", source: "child:e" },
|
|
1210
|
-
{ target: "inst:3", source: "child:f" },
|
|
1211
|
-
];
|
|
1212
|
-
const reverseLinksMap = new Map();
|
|
1213
|
-
for (const row of sparqlResults) {
|
|
1214
|
-
if (!reverseLinksMap.has(row.target))
|
|
1215
|
-
reverseLinksMap.set(row.target, []);
|
|
1216
|
-
reverseLinksMap.get(row.target).push(row.source);
|
|
1217
|
-
}
|
|
1218
|
-
expect(reverseLinksMap.get("inst:1")).toEqual(["child:a", "child:b"]);
|
|
1219
|
-
expect(reverseLinksMap.get("inst:2")).toEqual(["child:c"]);
|
|
1220
|
-
expect(reverseLinksMap.get("inst:3")).toEqual(["child:d", "child:e", "child:f"]);
|
|
1221
|
-
});
|
|
1222
|
-
it("batch hydration should handle empty relation sets without error", () => {
|
|
1223
|
-
const sparqlResults = [];
|
|
1224
|
-
const reverseLinksMap = new Map();
|
|
1225
|
-
for (const row of sparqlResults) {
|
|
1226
|
-
if (!reverseLinksMap.has(row.target))
|
|
1227
|
-
reverseLinksMap.set(row.target, []);
|
|
1228
|
-
reverseLinksMap.get(row.target).push(row.source);
|
|
1229
|
-
}
|
|
1230
|
-
expect(reverseLinksMap.size).toBe(0);
|
|
1231
|
-
expect(reverseLinksMap.get("nonexistent") || []).toEqual([]);
|
|
1232
|
-
});
|
|
1233
|
-
it("batch hydration should return identical results to N+1 hydration (grouping equivalence)", () => {
|
|
1234
|
-
// Simulate N+1: each instance queries separately
|
|
1235
|
-
const instances = [{ id: "inst:1" }, { id: "inst:2" }];
|
|
1236
|
-
const allLinks = [
|
|
1237
|
-
{ data: { source: "child:a", predicate: "rel://has", target: "inst:1" } },
|
|
1238
|
-
{ data: { source: "child:b", predicate: "rel://has", target: "inst:1" } },
|
|
1239
|
-
{ data: { source: "child:c", predicate: "rel://has", target: "inst:2" } },
|
|
1240
|
-
];
|
|
1241
|
-
// N+1 approach
|
|
1242
|
-
const n1Results = new Map();
|
|
1243
|
-
for (const inst of instances) {
|
|
1244
|
-
const links = allLinks.filter(l => l.data.target === inst.id);
|
|
1245
|
-
n1Results.set(inst.id, links.map(l => l.data.source));
|
|
1246
|
-
}
|
|
1247
|
-
// Batch approach
|
|
1248
|
-
const batchResults = new Map();
|
|
1249
|
-
const sparqlRows = allLinks.map(l => ({ target: l.data.target, source: l.data.source }));
|
|
1250
|
-
for (const row of sparqlRows) {
|
|
1251
|
-
if (!batchResults.has(row.target))
|
|
1252
|
-
batchResults.set(row.target, []);
|
|
1253
|
-
batchResults.get(row.target).push(row.source);
|
|
1254
|
-
}
|
|
1255
|
-
// Results should be identical
|
|
1256
|
-
for (const inst of instances) {
|
|
1257
|
-
expect(batchResults.get(inst.id)).toEqual(n1Results.get(inst.id));
|
|
1258
|
-
}
|
|
1259
|
-
});
|
|
1260
|
-
it("batch hydration with nested includes should use batched queries at each level", () => {
|
|
1261
|
-
// This test verifies the structure: nested includes result in multiple
|
|
1262
|
-
// batch operations, one per relation depth level
|
|
1263
|
-
const depth0Rows = [
|
|
1264
|
-
{ target: "root:1", source: "mid:a" },
|
|
1265
|
-
{ target: "root:1", source: "mid:b" },
|
|
1266
|
-
];
|
|
1267
|
-
const depth1Rows = [
|
|
1268
|
-
{ target: "mid:a", source: "leaf:x" },
|
|
1269
|
-
{ target: "mid:b", source: "leaf:y" },
|
|
1270
|
-
];
|
|
1271
|
-
// Group each level
|
|
1272
|
-
const level0Map = new Map();
|
|
1273
|
-
for (const r of depth0Rows) {
|
|
1274
|
-
if (!level0Map.has(r.target))
|
|
1275
|
-
level0Map.set(r.target, []);
|
|
1276
|
-
level0Map.get(r.target).push(r.source);
|
|
1277
|
-
}
|
|
1278
|
-
const level1Map = new Map();
|
|
1279
|
-
for (const r of depth1Rows) {
|
|
1280
|
-
if (!level1Map.has(r.target))
|
|
1281
|
-
level1Map.set(r.target, []);
|
|
1282
|
-
level1Map.get(r.target).push(r.source);
|
|
1283
|
-
}
|
|
1284
|
-
expect(level0Map.get("root:1")).toEqual(["mid:a", "mid:b"]);
|
|
1285
|
-
expect(level1Map.get("mid:a")).toEqual(["leaf:x"]);
|
|
1286
|
-
expect(level1Map.get("mid:b")).toEqual(["leaf:y"]);
|
|
1287
|
-
});
|
|
1288
|
-
});
|
|
1289
|
-
// ── Push-down FILTER for literal equality (3.5) ──────────────────────────────
|
|
1290
|
-
describe("Push-down FILTER for literal equality", () => {
|
|
1291
|
-
const mockPerspective = {};
|
|
1292
|
-
let FilterTest = class FilterTest extends Ad4mModel {
|
|
1293
|
-
constructor() {
|
|
1294
|
-
super(...arguments);
|
|
1295
|
-
this.name = "";
|
|
1296
|
-
this.rating = 0;
|
|
1297
|
-
}
|
|
1298
|
-
};
|
|
1299
|
-
__decorate([
|
|
1300
|
-
Property({ through: "ft://name", required: true }),
|
|
1301
|
-
__metadata("design:type", String)
|
|
1302
|
-
], FilterTest.prototype, "name", void 0);
|
|
1303
|
-
__decorate([
|
|
1304
|
-
Property({ through: "ft://rating", required: true }),
|
|
1305
|
-
__metadata("design:type", Number)
|
|
1306
|
-
], FilterTest.prototype, "rating", void 0);
|
|
1307
|
-
FilterTest = __decorate([
|
|
1308
|
-
Model({ name: "FilterTest" })
|
|
1309
|
-
], FilterTest);
|
|
1310
|
-
const normalizeQuery = (q) => q.replace(/\s+/g, " ").trim();
|
|
1311
|
-
it("SPARQL query for where: { name: 'Alice' } should use <ad4m://fn/parse_literal> FILTER", async () => {
|
|
1312
|
-
const query = await FilterTest.queryToSPARQL(mockPerspective, { where: { name: "Alice" } });
|
|
1313
|
-
const norm = normalizeQuery(query);
|
|
1314
|
-
// JOIN pattern exists
|
|
1315
|
-
expect(norm).toContain("?wTarget_name");
|
|
1316
|
-
// parse_literal push-down FILTER is present (using correct SPARQL IRI syntax)
|
|
1317
|
-
expect(norm).toContain("ad4m://fn/parse_literal");
|
|
1318
|
-
expect(norm).not.toContain("fn::parse_literal");
|
|
1319
|
-
});
|
|
1320
|
-
it("SPARQL query for where: { name: ['Alice', 'Bob'] } should use <ad4m://fn/parse_literal> IN FILTER", async () => {
|
|
1321
|
-
const query = await FilterTest.queryToSPARQL(mockPerspective, { where: { name: ["Alice", "Bob"] } });
|
|
1322
|
-
const norm = normalizeQuery(query);
|
|
1323
|
-
expect(norm).toContain("?wTarget_name");
|
|
1324
|
-
expect(norm).toContain("ad4m://fn/parse_literal");
|
|
1325
|
-
expect(norm).not.toContain("fn::parse_literal");
|
|
1326
|
-
});
|
|
1327
|
-
it("SPARQL query for where: { rating: { gt: 5 } } should NOT include parse_literal FILTER", async () => {
|
|
1328
|
-
const query = await FilterTest.queryToSPARQL(mockPerspective, { where: { rating: { gt: 5 } } });
|
|
1329
|
-
const norm = normalizeQuery(query);
|
|
1330
|
-
expect(norm).not.toContain("parse_literal");
|
|
1331
|
-
});
|
|
1332
|
-
it("literal property where clause adds JOIN for JS post-filter", async () => {
|
|
1333
|
-
const query = await FilterTest.queryToSPARQL(mockPerspective, { where: { name: "Alice" } });
|
|
1334
|
-
const norm = normalizeQuery(query);
|
|
1335
|
-
expect(norm).toContain("?wTarget_name");
|
|
1336
|
-
});
|
|
1337
|
-
});
|
|
1338
|
-
// ── Lightweight fingerprint (3.7) ────────────────────────────────────────────
|
|
1339
|
-
describe("Lightweight fingerprint optimization", () => {
|
|
1340
|
-
// Replicate the buildFingerprint logic for testing
|
|
1341
|
-
const buildFingerprint = (results) => {
|
|
1342
|
-
if (results.length === 0)
|
|
1343
|
-
return '0:';
|
|
1344
|
-
const ids = results.map((r) => r.id || '').sort().join(',');
|
|
1345
|
-
const ts = results.map((r) => r.updatedAt || r.timestamp || '').join(',');
|
|
1346
|
-
return `${results.length}:${ids}:${ts}`;
|
|
1347
|
-
};
|
|
1348
|
-
const base = [
|
|
1349
|
-
{ id: "a", updatedAt: "100" },
|
|
1350
|
-
{ id: "b", updatedAt: "200" },
|
|
1351
|
-
];
|
|
1352
|
-
it("lightweight fingerprint should detect instance addition", () => {
|
|
1353
|
-
const fp1 = buildFingerprint(base);
|
|
1354
|
-
const fp2 = buildFingerprint([...base, { id: "c", updatedAt: "300" }]);
|
|
1355
|
-
expect(fp1).not.toBe(fp2);
|
|
1356
|
-
});
|
|
1357
|
-
it("lightweight fingerprint should detect instance removal", () => {
|
|
1358
|
-
const fp1 = buildFingerprint(base);
|
|
1359
|
-
const fp2 = buildFingerprint([base[0]]);
|
|
1360
|
-
expect(fp1).not.toBe(fp2);
|
|
1361
|
-
});
|
|
1362
|
-
it("lightweight fingerprint should detect timestamp change", () => {
|
|
1363
|
-
const fp1 = buildFingerprint(base);
|
|
1364
|
-
const fp2 = buildFingerprint([{ id: "a", updatedAt: "999" }, base[1]]);
|
|
1365
|
-
expect(fp1).not.toBe(fp2);
|
|
1366
|
-
});
|
|
1367
|
-
it("lightweight fingerprint should NOT false-positive for identical sets", () => {
|
|
1368
|
-
const fp1 = buildFingerprint(base);
|
|
1369
|
-
const fp2 = buildFingerprint([...base]); // same data, new array
|
|
1370
|
-
expect(fp1).toBe(fp2);
|
|
1371
|
-
});
|
|
1372
|
-
});
|
|
1373
|
-
// ── Native SPARQL getter evaluation ──────────────────────────────────────────
|
|
1374
|
-
describe("Native SPARQL getter evaluation", () => {
|
|
1375
|
-
const { evaluateCustomGettersForInstance } = require("./hydration");
|
|
1376
|
-
const mockPerspective = {
|
|
1377
|
-
getLinks: jest.fn().mockResolvedValue([]),
|
|
1378
|
-
querySparql: jest.fn(),
|
|
1379
|
-
get: jest.fn().mockResolvedValue([]),
|
|
1380
|
-
};
|
|
1381
|
-
beforeEach(() => {
|
|
1382
|
-
jest.clearAllMocks();
|
|
1383
|
-
});
|
|
1384
|
-
it("should execute SELECT getter directly via querySparql", async () => {
|
|
1385
|
-
const metadata = {
|
|
1386
|
-
properties: {
|
|
1387
|
-
replyingTo: {
|
|
1388
|
-
predicate: "flux://has_reply",
|
|
1389
|
-
getter: 'SELECT ?target WHERE { ?target <flux://has_reply> ?source . } LIMIT 1',
|
|
1390
|
-
type: "string",
|
|
1391
|
-
}
|
|
1392
|
-
},
|
|
1393
|
-
relations: {},
|
|
1394
|
-
};
|
|
1395
|
-
mockPerspective.querySparql.mockResolvedValue([
|
|
1396
|
-
{ target: { value: "literal:string:msg123" } }
|
|
1397
|
-
]);
|
|
1398
|
-
const instance = { id: "test://msg1", replyingTo: undefined };
|
|
1399
|
-
await evaluateCustomGettersForInstance(instance, mockPerspective, metadata);
|
|
1400
|
-
expect(mockPerspective.querySparql).toHaveBeenCalledWith(expect.stringContaining("SELECT ?target WHERE"));
|
|
1401
|
-
expect(instance.replyingTo).toBe("literal:string:msg123");
|
|
1402
|
-
});
|
|
1403
|
-
it("should execute ASK getter and return boolean", async () => {
|
|
1404
|
-
const metadata = {
|
|
1405
|
-
properties: {
|
|
1406
|
-
isPopular: {
|
|
1407
|
-
predicate: "flux://is_popular",
|
|
1408
|
-
getter: 'ASK WHERE { SELECT (COUNT(DISTINCT ?reactor) AS ?count) WHERE { ?reactor <flux://reaction> ?source . } HAVING(?count > 5) }',
|
|
1409
|
-
type: "boolean",
|
|
1410
|
-
readOnly: true,
|
|
1411
|
-
}
|
|
1412
|
-
},
|
|
1413
|
-
relations: {},
|
|
1414
|
-
};
|
|
1415
|
-
mockPerspective.querySparql.mockResolvedValue(true);
|
|
1416
|
-
const instance = { id: "test://msg1", isPopular: false };
|
|
1417
|
-
await evaluateCustomGettersForInstance(instance, mockPerspective, metadata);
|
|
1418
|
-
expect(mockPerspective.querySparql).toHaveBeenCalledWith(expect.stringContaining("ASK WHERE"));
|
|
1419
|
-
expect(instance.isPopular).toBe(true);
|
|
1420
|
-
});
|
|
1421
|
-
it("should replace ?source with instance ID in SPARQL getter", async () => {
|
|
1422
|
-
const metadata = {
|
|
1423
|
-
properties: {
|
|
1424
|
-
author: {
|
|
1425
|
-
predicate: "test://author",
|
|
1426
|
-
getter: 'SELECT ?target WHERE { ?source <test://author> ?target . }',
|
|
1427
|
-
type: "string",
|
|
1428
|
-
}
|
|
1429
|
-
},
|
|
1430
|
-
relations: {},
|
|
1431
|
-
};
|
|
1432
|
-
mockPerspective.querySparql.mockResolvedValue([
|
|
1433
|
-
{ target: { value: "did:key:abc123" } }
|
|
1434
|
-
]);
|
|
1435
|
-
const instance = { id: "test://post1", author: undefined };
|
|
1436
|
-
await evaluateCustomGettersForInstance(instance, mockPerspective, metadata);
|
|
1437
|
-
expect(mockPerspective.querySparql).toHaveBeenCalledWith(expect.stringContaining("<test://post1>"));
|
|
1438
|
-
expect(mockPerspective.querySparql).not.toHaveBeenCalledWith(expect.stringContaining("?source"));
|
|
1439
|
-
});
|
|
1440
|
-
it("should handle empty SPARQL results gracefully", async () => {
|
|
1441
|
-
const metadata = {
|
|
1442
|
-
properties: {
|
|
1443
|
-
replyingTo: {
|
|
1444
|
-
predicate: "flux://has_reply",
|
|
1445
|
-
getter: 'SELECT ?target WHERE { ?target <flux://has_reply> ?source . } LIMIT 1',
|
|
1446
|
-
type: "string",
|
|
1447
|
-
}
|
|
1448
|
-
},
|
|
1449
|
-
relations: {},
|
|
1450
|
-
};
|
|
1451
|
-
mockPerspective.querySparql.mockResolvedValue([]);
|
|
1452
|
-
const instance = { id: "test://msg1", replyingTo: "original" };
|
|
1453
|
-
await evaluateCustomGettersForInstance(instance, mockPerspective, metadata);
|
|
1454
|
-
// Should not overwrite with undefined
|
|
1455
|
-
expect(instance.replyingTo).toBe("original");
|
|
1456
|
-
});
|
|
1457
|
-
it("should handle SPARQL getter errors without crashing", async () => {
|
|
1458
|
-
const metadata = {
|
|
1459
|
-
properties: {
|
|
1460
|
-
broken: {
|
|
1461
|
-
predicate: "test://broken",
|
|
1462
|
-
getter: 'SELECT ?target WHERE { INVALID SPARQL }',
|
|
1463
|
-
type: "string",
|
|
1464
|
-
}
|
|
1465
|
-
},
|
|
1466
|
-
relations: {},
|
|
1467
|
-
};
|
|
1468
|
-
mockPerspective.querySparql.mockRejectedValue(new Error("SPARQL parse error"));
|
|
1469
|
-
const instance = { id: "test://msg1", broken: undefined };
|
|
1470
|
-
const consoleSpy = jest.spyOn(console, "warn").mockImplementation();
|
|
1471
|
-
await evaluateCustomGettersForInstance(instance, mockPerspective, metadata);
|
|
1472
|
-
consoleSpy.mockRestore();
|
|
1473
|
-
// Should not throw, should warn
|
|
1474
|
-
expect(instance.broken).toBeUndefined();
|
|
1475
|
-
});
|
|
1476
|
-
it("should execute SPARQL getter for relations", async () => {
|
|
1477
|
-
const metadata = {
|
|
1478
|
-
properties: {},
|
|
1479
|
-
relations: {
|
|
1480
|
-
tags: {
|
|
1481
|
-
predicate: "test://has_tag",
|
|
1482
|
-
getter: 'SELECT ?target WHERE { ?source <test://has_tag> ?target . }',
|
|
1483
|
-
direction: "forward",
|
|
1484
|
-
}
|
|
1485
|
-
},
|
|
1486
|
-
};
|
|
1487
|
-
mockPerspective.querySparql.mockResolvedValue([
|
|
1488
|
-
{ target: { value: "tag:a" } },
|
|
1489
|
-
{ target: { value: "tag:b" } },
|
|
1490
|
-
]);
|
|
1491
|
-
const instance = { id: "test://post1", tags: [] };
|
|
1492
|
-
await evaluateCustomGettersForInstance(instance, mockPerspective, metadata);
|
|
1493
|
-
expect(instance.tags).toEqual(["tag:a", "tag:b"]);
|
|
1494
|
-
});
|
|
1495
|
-
it("should warn for legacy SurrealDB-style getter syntax instead of converting", async () => {
|
|
1496
|
-
const metadata = {
|
|
1497
|
-
properties: {
|
|
1498
|
-
legacy: {
|
|
1499
|
-
predicate: "test://legacy",
|
|
1500
|
-
getter: "(<-link[WHERE predicate = 'test://legacy'].in.uri)[0]",
|
|
1501
|
-
type: "string",
|
|
1502
|
-
}
|
|
1503
|
-
},
|
|
1504
|
-
relations: {},
|
|
1505
|
-
};
|
|
1506
|
-
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => { });
|
|
1507
|
-
const instance = { id: "test://msg1", legacy: undefined };
|
|
1508
|
-
await evaluateCustomGettersForInstance(instance, mockPerspective, metadata);
|
|
1509
|
-
// Should NOT call querySparql — legacy getters are no longer converted
|
|
1510
|
-
expect(mockPerspective.querySparql).not.toHaveBeenCalled();
|
|
1511
|
-
// Should warn about unsupported syntax
|
|
1512
|
-
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("Unsupported legacy getter syntax"));
|
|
1513
|
-
// Value should remain undefined
|
|
1514
|
-
expect(instance.legacy).toBeUndefined();
|
|
1515
|
-
warnSpy.mockRestore();
|
|
1516
|
-
});
|
|
1517
|
-
});
|
|
1518
|
-
// ── Subscribe callback timing ──────────────────────────────────────────
|
|
1519
|
-
describe("ModelQueryBuilder subscribe callback timing", () => {
|
|
1520
|
-
it("subscribe should not invoke callback synchronously before Promise resolves", async () => {
|
|
1521
|
-
// This test verifies that subscribe() uses the Rust model subscription endpoint
|
|
1522
|
-
// and properly handles the subscription lifecycle.
|
|
1523
|
-
const mockSubscriptionId = "test-sub-123";
|
|
1524
|
-
let updateCallback = null;
|
|
1525
|
-
const mockClient = {
|
|
1526
|
-
modelSubscribe: jest.fn().mockResolvedValue({
|
|
1527
|
-
subscriptionId: mockSubscriptionId,
|
|
1528
|
-
result: { instances: [], totalCount: 0 },
|
|
1529
|
-
}),
|
|
1530
|
-
subscribeToQueryUpdates: jest.fn().mockImplementation((_id, cb) => {
|
|
1531
|
-
updateCallback = cb;
|
|
1532
|
-
return () => { }; // unsubscribe function
|
|
1533
|
-
}),
|
|
1534
|
-
keepAliveQuery: jest.fn().mockResolvedValue(true),
|
|
1535
|
-
disposeQuerySubscription: jest.fn().mockResolvedValue(true),
|
|
1536
|
-
};
|
|
1537
|
-
const mockPerspective = {
|
|
1538
|
-
uuid: "test-uuid",
|
|
1539
|
-
client: mockClient,
|
|
1540
|
-
modelSubscribe: jest.fn().mockImplementation(async (className, queryJson, shapeJson) => {
|
|
1541
|
-
return mockClient.modelSubscribe("test-uuid", className, queryJson, shapeJson);
|
|
1542
|
-
}),
|
|
1543
|
-
getLinks: jest.fn().mockResolvedValue([]),
|
|
1544
|
-
modelQuery: jest.fn().mockResolvedValue({ instances: [], totalCount: 0 }),
|
|
1545
|
-
};
|
|
1546
|
-
const { Ad4mModel, Model, Property, Flag } = require("./index");
|
|
1547
|
-
let TimingTest = class TimingTest extends Ad4mModel {
|
|
1548
|
-
constructor() {
|
|
1549
|
-
super(...arguments);
|
|
1550
|
-
this.type = "test://timing";
|
|
1551
|
-
this.name = "";
|
|
1552
|
-
}
|
|
1553
|
-
};
|
|
1554
|
-
__decorate([
|
|
1555
|
-
Flag({ through: "test://type", value: "test://timing" }),
|
|
1556
|
-
__metadata("design:type", String)
|
|
1557
|
-
], TimingTest.prototype, "type", void 0);
|
|
1558
|
-
__decorate([
|
|
1559
|
-
Property({ through: "test://name" }),
|
|
1560
|
-
__metadata("design:type", String)
|
|
1561
|
-
], TimingTest.prototype, "name", void 0);
|
|
1562
|
-
TimingTest = __decorate([
|
|
1563
|
-
Model({ name: "TimingTest" })
|
|
1564
|
-
], TimingTest);
|
|
1565
|
-
let callbackInvokedBeforeResolve = false;
|
|
1566
|
-
let promiseResolved = false;
|
|
1567
|
-
const query = TimingTest.query(mockPerspective);
|
|
1568
|
-
const promise = query.subscribe((results) => {
|
|
1569
|
-
if (!promiseResolved) {
|
|
1570
|
-
callbackInvokedBeforeResolve = true;
|
|
1571
|
-
}
|
|
1572
|
-
});
|
|
1573
|
-
// At this point the Promise hasn't resolved yet
|
|
1574
|
-
// The callback should NOT have fired synchronously
|
|
1575
|
-
expect(callbackInvokedBeforeResolve).toBe(false);
|
|
1576
|
-
const initialResults = await promise;
|
|
1577
|
-
promiseResolved = true;
|
|
1578
|
-
expect(initialResults).toEqual([]);
|
|
1579
|
-
expect(mockPerspective.modelSubscribe).toHaveBeenCalled();
|
|
1580
|
-
});
|
|
1581
|
-
});
|
|
1582
|
-
describe("ModelQueryBuilder paginateSubscribe", () => {
|
|
1583
|
-
it("paginateSubscribe should use a single query with count: true", async () => {
|
|
1584
|
-
const mockSubscriptionId = "paginate-sub-123";
|
|
1585
|
-
const mockClient = {
|
|
1586
|
-
modelSubscribe: jest.fn().mockResolvedValue({
|
|
1587
|
-
subscriptionId: mockSubscriptionId,
|
|
1588
|
-
result: { instances: [], totalCount: 0 },
|
|
1589
|
-
}),
|
|
1590
|
-
subscribeToQueryUpdates: jest.fn().mockImplementation((_id, _cb) => {
|
|
1591
|
-
return () => { };
|
|
1592
|
-
}),
|
|
1593
|
-
keepAliveQuery: jest.fn().mockResolvedValue(true),
|
|
1594
|
-
disposeQuerySubscription: jest.fn().mockResolvedValue(true),
|
|
1595
|
-
};
|
|
1596
|
-
const mockPerspective = {
|
|
1597
|
-
uuid: "test-uuid",
|
|
1598
|
-
client: mockClient,
|
|
1599
|
-
modelSubscribe: jest.fn().mockImplementation(async (className, queryJson, shapeJson) => {
|
|
1600
|
-
return mockClient.modelSubscribe("test-uuid", className, queryJson, shapeJson);
|
|
1601
|
-
}),
|
|
1602
|
-
modelQuery: jest.fn().mockResolvedValue({ instances: [{ id: "item1", type: "test://paginate" }], totalCount: 42 }),
|
|
1603
|
-
};
|
|
1604
|
-
const { Ad4mModel, Model, Flag, Property } = require("./index");
|
|
1605
|
-
let PaginateTest = class PaginateTest extends Ad4mModel {
|
|
1606
|
-
constructor() {
|
|
1607
|
-
super(...arguments);
|
|
1608
|
-
this.type = "test://paginate";
|
|
1609
|
-
this.name = "";
|
|
1610
|
-
}
|
|
1611
|
-
};
|
|
1612
|
-
__decorate([
|
|
1613
|
-
Flag({ through: "test://type", value: "test://paginate" }),
|
|
1614
|
-
__metadata("design:type", String)
|
|
1615
|
-
], PaginateTest.prototype, "type", void 0);
|
|
1616
|
-
__decorate([
|
|
1617
|
-
Property({ through: "test://name" }),
|
|
1618
|
-
__metadata("design:type", String)
|
|
1619
|
-
], PaginateTest.prototype, "name", void 0);
|
|
1620
|
-
PaginateTest = __decorate([
|
|
1621
|
-
Model({ name: "PaginateTest" })
|
|
1622
|
-
], PaginateTest);
|
|
1623
|
-
const builder = PaginateTest.query(mockPerspective);
|
|
1624
|
-
const result = await builder.paginateSubscribe(10, 1, () => { });
|
|
1625
|
-
// Should return paginated result structure
|
|
1626
|
-
expect(result).toHaveProperty("results");
|
|
1627
|
-
expect(result).toHaveProperty("totalCount");
|
|
1628
|
-
expect(result).toHaveProperty("pageSize", 10);
|
|
1629
|
-
expect(result).toHaveProperty("pageNumber", 1);
|
|
1630
|
-
// modelQuery should be called exactly once (not twice — no separate count query)
|
|
1631
|
-
expect(mockPerspective.modelQuery).toHaveBeenCalledTimes(1);
|
|
1632
|
-
// The query should have count: true
|
|
1633
|
-
const callArgs = mockPerspective.modelQuery.mock.calls[0];
|
|
1634
|
-
const queryJson = JSON.parse(callArgs[1]);
|
|
1635
|
-
expect(queryJson.count).toBe(true);
|
|
1636
|
-
expect(queryJson.limit).toBe(10);
|
|
1637
|
-
expect(queryJson.offset).toBe(0);
|
|
1638
|
-
});
|
|
1639
|
-
it("paginateSubscribe should skip isInit messages from subscription", async () => {
|
|
1640
|
-
const mockSubscriptionId = "paginate-init-sub";
|
|
1641
|
-
let capturedCallback = null;
|
|
1642
|
-
const mockClient = {
|
|
1643
|
-
modelSubscribe: jest.fn().mockResolvedValue({
|
|
1644
|
-
subscriptionId: mockSubscriptionId,
|
|
1645
|
-
result: { instances: [], totalCount: 0 },
|
|
1646
|
-
}),
|
|
1647
|
-
subscribeToQueryUpdates: jest.fn().mockImplementation((_id, cb) => {
|
|
1648
|
-
capturedCallback = cb;
|
|
1649
|
-
return () => { };
|
|
1650
|
-
}),
|
|
1651
|
-
keepAliveQuery: jest.fn().mockResolvedValue(true),
|
|
1652
|
-
disposeQuerySubscription: jest.fn().mockResolvedValue(true),
|
|
1653
|
-
};
|
|
1654
|
-
let modelQueryCallCount = 0;
|
|
1655
|
-
const mockPerspective = {
|
|
1656
|
-
uuid: "test-uuid",
|
|
1657
|
-
client: mockClient,
|
|
1658
|
-
modelSubscribe: jest.fn().mockImplementation(async (className, queryJson, shapeJson) => {
|
|
1659
|
-
return mockClient.modelSubscribe("test-uuid", className, queryJson, shapeJson);
|
|
1660
|
-
}),
|
|
1661
|
-
modelQuery: jest.fn().mockImplementation(async () => {
|
|
1662
|
-
modelQueryCallCount++;
|
|
1663
|
-
return { instances: [], totalCount: 0 };
|
|
1664
|
-
}),
|
|
1665
|
-
};
|
|
1666
|
-
const { Ad4mModel, Model, Flag, Property } = require("./index");
|
|
1667
|
-
let InitGuardTest = class InitGuardTest extends Ad4mModel {
|
|
1668
|
-
constructor() {
|
|
1669
|
-
super(...arguments);
|
|
1670
|
-
this.type = "test://init";
|
|
1671
|
-
this.name = "";
|
|
1672
|
-
}
|
|
1673
|
-
};
|
|
1674
|
-
__decorate([
|
|
1675
|
-
Flag({ through: "test://type", value: "test://init" }),
|
|
1676
|
-
__metadata("design:type", String)
|
|
1677
|
-
], InitGuardTest.prototype, "type", void 0);
|
|
1678
|
-
__decorate([
|
|
1679
|
-
Property({ through: "test://name" }),
|
|
1680
|
-
__metadata("design:type", String)
|
|
1681
|
-
], InitGuardTest.prototype, "name", void 0);
|
|
1682
|
-
InitGuardTest = __decorate([
|
|
1683
|
-
Model({ name: "InitGuardTest" })
|
|
1684
|
-
], InitGuardTest);
|
|
1685
|
-
const userCallback = jest.fn();
|
|
1686
|
-
const builder = InitGuardTest.query(mockPerspective);
|
|
1687
|
-
await builder.paginateSubscribe(10, 1, userCallback);
|
|
1688
|
-
// Reset the count after initial fetch
|
|
1689
|
-
const initialCallCount = modelQueryCallCount;
|
|
1690
|
-
// Simulate an isInit message from subscription
|
|
1691
|
-
expect(capturedCallback).not.toBeNull();
|
|
1692
|
-
capturedCallback({ isInit: true });
|
|
1693
|
-
// Wait for any async processing
|
|
1694
|
-
await new Promise(r => setTimeout(r, 50));
|
|
1695
|
-
// modelQuery should NOT have been called again (isInit was filtered)
|
|
1696
|
-
expect(modelQueryCallCount).toBe(initialCallCount);
|
|
1697
|
-
// User callback should NOT have been invoked by isInit
|
|
1698
|
-
expect(userCallback).not.toHaveBeenCalled();
|
|
1699
|
-
});
|
|
1700
|
-
it("paginateSubscribe should re-fetch on real subscription updates", async () => {
|
|
1701
|
-
const mockSubscriptionId = "paginate-update-sub";
|
|
1702
|
-
let capturedCallback = null;
|
|
1703
|
-
const mockClient = {
|
|
1704
|
-
modelSubscribe: jest.fn().mockResolvedValue({
|
|
1705
|
-
subscriptionId: mockSubscriptionId,
|
|
1706
|
-
result: { instances: [], totalCount: 0 },
|
|
1707
|
-
}),
|
|
1708
|
-
subscribeToQueryUpdates: jest.fn().mockImplementation((_id, cb) => {
|
|
1709
|
-
capturedCallback = cb;
|
|
1710
|
-
return () => { };
|
|
1711
|
-
}),
|
|
1712
|
-
keepAliveQuery: jest.fn().mockResolvedValue(true),
|
|
1713
|
-
disposeQuerySubscription: jest.fn().mockResolvedValue(true),
|
|
1714
|
-
};
|
|
1715
|
-
let modelQueryCallCount = 0;
|
|
1716
|
-
const mockPerspective = {
|
|
1717
|
-
uuid: "test-uuid",
|
|
1718
|
-
client: mockClient,
|
|
1719
|
-
modelSubscribe: jest.fn().mockImplementation(async (className, queryJson, shapeJson) => {
|
|
1720
|
-
return mockClient.modelSubscribe("test-uuid", className, queryJson, shapeJson);
|
|
1721
|
-
}),
|
|
1722
|
-
modelQuery: jest.fn().mockImplementation(async () => {
|
|
1723
|
-
modelQueryCallCount++;
|
|
1724
|
-
return { instances: [{ id: "new-item", type: "test://update" }], totalCount: 1 };
|
|
1725
|
-
}),
|
|
1726
|
-
};
|
|
1727
|
-
const { Ad4mModel, Model, Flag, Property } = require("./index");
|
|
1728
|
-
let UpdateTest = class UpdateTest extends Ad4mModel {
|
|
1729
|
-
constructor() {
|
|
1730
|
-
super(...arguments);
|
|
1731
|
-
this.type = "test://update";
|
|
1732
|
-
this.name = "";
|
|
1733
|
-
}
|
|
1734
|
-
};
|
|
1735
|
-
__decorate([
|
|
1736
|
-
Flag({ through: "test://type", value: "test://update" }),
|
|
1737
|
-
__metadata("design:type", String)
|
|
1738
|
-
], UpdateTest.prototype, "type", void 0);
|
|
1739
|
-
__decorate([
|
|
1740
|
-
Property({ through: "test://name" }),
|
|
1741
|
-
__metadata("design:type", String)
|
|
1742
|
-
], UpdateTest.prototype, "name", void 0);
|
|
1743
|
-
UpdateTest = __decorate([
|
|
1744
|
-
Model({ name: "UpdateTest" })
|
|
1745
|
-
], UpdateTest);
|
|
1746
|
-
const userCallback = jest.fn();
|
|
1747
|
-
const builder = UpdateTest.query(mockPerspective);
|
|
1748
|
-
await builder.paginateSubscribe(10, 1, userCallback);
|
|
1749
|
-
const initialCallCount = modelQueryCallCount;
|
|
1750
|
-
// Simulate a real subscription update (not isInit)
|
|
1751
|
-
capturedCallback({ instances: [{ id: "new-item" }], totalCount: 1 });
|
|
1752
|
-
// Wait for async processing
|
|
1753
|
-
await new Promise(r => setTimeout(r, 100));
|
|
1754
|
-
// modelQuery should have been called again for the re-fetch
|
|
1755
|
-
expect(modelQueryCallCount).toBeGreaterThan(initialCallCount);
|
|
1756
|
-
// User callback should have been invoked with paginated results
|
|
1757
|
-
expect(userCallback).toHaveBeenCalled();
|
|
1758
|
-
const callArg = userCallback.mock.calls[0][0];
|
|
1759
|
-
expect(callArg).toHaveProperty("totalCount");
|
|
1760
|
-
expect(callArg).toHaveProperty("pageSize", 10);
|
|
1761
|
-
expect(callArg).toHaveProperty("pageNumber", 1);
|
|
1762
|
-
});
|
|
1763
|
-
});
|
|
1764
|
-
// ============================================================================
|
|
1765
|
-
// Performance Optimisation Tests
|
|
1766
|
-
// ============================================================================
|
|
1767
|
-
import { getCachedResult, setCachedResult, clearQueryCache, queryCacheSize } from "./query-cache";
|
|
1768
|
-
import { getPropertiesMetadata, getRelationsMetadata } from "./decorators";
|
|
1769
|
-
describe("QueryCache", () => {
|
|
1770
|
-
beforeEach(() => clearQueryCache());
|
|
1771
|
-
it("should return undefined for cache miss", () => {
|
|
1772
|
-
expect(getCachedResult("uuid1", "SELECT ?s WHERE {}")).toBeUndefined();
|
|
1773
|
-
});
|
|
1774
|
-
it("should return cached result within TTL", () => {
|
|
1775
|
-
const result = [{ s: "test" }];
|
|
1776
|
-
setCachedResult("uuid1", "SELECT ?s WHERE {}", result);
|
|
1777
|
-
expect(getCachedResult("uuid1", "SELECT ?s WHERE {}")).toBe(result);
|
|
1778
|
-
});
|
|
1779
|
-
it("should expire after TTL", async () => {
|
|
1780
|
-
setCachedResult("uuid1", "SELECT ?s WHERE {}", [{ s: "test" }], 50);
|
|
1781
|
-
await new Promise(r => setTimeout(r, 60));
|
|
1782
|
-
expect(getCachedResult("uuid1", "SELECT ?s WHERE {}")).toBeUndefined();
|
|
1783
|
-
});
|
|
1784
|
-
it("should cache independently per perspective", () => {
|
|
1785
|
-
setCachedResult("uuid1", "SELECT ?s WHERE {}", "result1");
|
|
1786
|
-
setCachedResult("uuid2", "SELECT ?s WHERE {}", "result2");
|
|
1787
|
-
expect(getCachedResult("uuid1", "SELECT ?s WHERE {}")).toBe("result1");
|
|
1788
|
-
expect(getCachedResult("uuid2", "SELECT ?s WHERE {}")).toBe("result2");
|
|
1789
|
-
});
|
|
1790
|
-
it("should cache independently per query", () => {
|
|
1791
|
-
setCachedResult("uuid1", "query1", "r1");
|
|
1792
|
-
setCachedResult("uuid1", "query2", "r2");
|
|
1793
|
-
expect(getCachedResult("uuid1", "query1")).toBe("r1");
|
|
1794
|
-
expect(getCachedResult("uuid1", "query2")).toBe("r2");
|
|
1795
|
-
});
|
|
1796
|
-
it("should clear all entries", () => {
|
|
1797
|
-
setCachedResult("uuid1", "q1", "r1");
|
|
1798
|
-
setCachedResult("uuid2", "q2", "r2");
|
|
1799
|
-
expect(queryCacheSize()).toBe(2);
|
|
1800
|
-
clearQueryCache();
|
|
1801
|
-
expect(queryCacheSize()).toBe(0);
|
|
1802
|
-
});
|
|
1803
|
-
});
|
|
1804
|
-
describe("SHACL Memoisation", () => {
|
|
1805
|
-
it("should memoise generateSHACL() per class", () => {
|
|
1806
|
-
let MemoTest1 = class MemoTest1 extends Ad4mModel {
|
|
1807
|
-
constructor() {
|
|
1808
|
-
super(...arguments);
|
|
1809
|
-
this.name = "";
|
|
1810
|
-
}
|
|
1811
|
-
};
|
|
1812
|
-
__decorate([
|
|
1813
|
-
Property({ through: "test://name" }),
|
|
1814
|
-
__metadata("design:type", String)
|
|
1815
|
-
], MemoTest1.prototype, "name", void 0);
|
|
1816
|
-
MemoTest1 = __decorate([
|
|
1817
|
-
Model({ name: "MemoTest1" })
|
|
1818
|
-
], MemoTest1);
|
|
1819
|
-
const result1 = MemoTest1.generateSHACL();
|
|
1820
|
-
const result2 = MemoTest1.generateSHACL();
|
|
1821
|
-
expect(result1).toBe(result2); // Same reference = memoised
|
|
1822
|
-
});
|
|
1823
|
-
it("should memoise getPropertiesMetadata per class", () => {
|
|
1824
|
-
let PropMemoTest = class PropMemoTest extends Ad4mModel {
|
|
1825
|
-
constructor() {
|
|
1826
|
-
super(...arguments);
|
|
1827
|
-
this.x = "";
|
|
1828
|
-
}
|
|
1829
|
-
};
|
|
1830
|
-
__decorate([
|
|
1831
|
-
Property({ through: "test://x" }),
|
|
1832
|
-
__metadata("design:type", String)
|
|
1833
|
-
], PropMemoTest.prototype, "x", void 0);
|
|
1834
|
-
PropMemoTest = __decorate([
|
|
1835
|
-
Model({ name: "PropMemoTest" })
|
|
1836
|
-
], PropMemoTest);
|
|
1837
|
-
const r1 = getPropertiesMetadata(PropMemoTest);
|
|
1838
|
-
const r2 = getPropertiesMetadata(PropMemoTest);
|
|
1839
|
-
expect(r1).toBe(r2); // Same reference
|
|
1840
|
-
});
|
|
1841
|
-
it("should memoise getRelationsMetadata per class", () => {
|
|
1842
|
-
let RelMemoTest = class RelMemoTest extends Ad4mModel {
|
|
1843
|
-
constructor() {
|
|
1844
|
-
super(...arguments);
|
|
1845
|
-
this.items = [];
|
|
1846
|
-
}
|
|
1847
|
-
};
|
|
1848
|
-
__decorate([
|
|
1849
|
-
HasMany({ through: "test://items" }),
|
|
1850
|
-
__metadata("design:type", Array)
|
|
1851
|
-
], RelMemoTest.prototype, "items", void 0);
|
|
1852
|
-
RelMemoTest = __decorate([
|
|
1853
|
-
Model({ name: "RelMemoTest" })
|
|
1854
|
-
], RelMemoTest);
|
|
1855
|
-
const r1 = getRelationsMetadata(RelMemoTest);
|
|
1856
|
-
const r2 = getRelationsMetadata(RelMemoTest);
|
|
1857
|
-
expect(r1).toBe(r2); // Same reference
|
|
1858
|
-
});
|
|
1859
|
-
it("should return different results for different classes", () => {
|
|
1860
|
-
let ClassA = class ClassA extends Ad4mModel {
|
|
1861
|
-
constructor() {
|
|
1862
|
-
super(...arguments);
|
|
1863
|
-
this.a = "";
|
|
1864
|
-
}
|
|
1865
|
-
};
|
|
1866
|
-
__decorate([
|
|
1867
|
-
Property({ through: "test://a" }),
|
|
1868
|
-
__metadata("design:type", String)
|
|
1869
|
-
], ClassA.prototype, "a", void 0);
|
|
1870
|
-
ClassA = __decorate([
|
|
1871
|
-
Model({ name: "ClassA" })
|
|
1872
|
-
], ClassA);
|
|
1873
|
-
let ClassB = class ClassB extends Ad4mModel {
|
|
1874
|
-
constructor() {
|
|
1875
|
-
super(...arguments);
|
|
1876
|
-
this.b = "";
|
|
1877
|
-
}
|
|
1878
|
-
};
|
|
1879
|
-
__decorate([
|
|
1880
|
-
Property({ through: "test://b" }),
|
|
1881
|
-
__metadata("design:type", String)
|
|
1882
|
-
], ClassB.prototype, "b", void 0);
|
|
1883
|
-
ClassB = __decorate([
|
|
1884
|
-
Model({ name: "ClassB" })
|
|
1885
|
-
], ClassB);
|
|
1886
|
-
const propsA = getPropertiesMetadata(ClassA);
|
|
1887
|
-
const propsB = getPropertiesMetadata(ClassB);
|
|
1888
|
-
expect(propsA).not.toBe(propsB);
|
|
1889
|
-
expect(propsA).toHaveProperty("a");
|
|
1890
|
-
expect(propsB).toHaveProperty("b");
|
|
1891
|
-
});
|
|
1892
|
-
});
|
|
1893
|
-
describe("Lazy Conformance Filters", () => {
|
|
1894
|
-
it("should defer conformance filter resolution until property access", () => {
|
|
1895
|
-
let LazyTarget2 = class LazyTarget2 extends Ad4mModel {
|
|
1896
|
-
constructor() {
|
|
1897
|
-
super(...arguments);
|
|
1898
|
-
this.type = "test://lazy";
|
|
1899
|
-
this.name = "";
|
|
1900
|
-
}
|
|
1901
|
-
};
|
|
1902
|
-
__decorate([
|
|
1903
|
-
Flag({ through: "test://type", value: "test://lazy" }),
|
|
1904
|
-
__metadata("design:type", String)
|
|
1905
|
-
], LazyTarget2.prototype, "type", void 0);
|
|
1906
|
-
__decorate([
|
|
1907
|
-
Property({ through: "test://name", required: true }),
|
|
1908
|
-
__metadata("design:type", String)
|
|
1909
|
-
], LazyTarget2.prototype, "name", void 0);
|
|
1910
|
-
LazyTarget2 = __decorate([
|
|
1911
|
-
Model({ name: "LazyTarget2" })
|
|
1912
|
-
], LazyTarget2);
|
|
1913
|
-
let LazyParent2 = class LazyParent2 extends Ad4mModel {
|
|
1914
|
-
constructor() {
|
|
1915
|
-
super(...arguments);
|
|
1916
|
-
this.children = [];
|
|
1917
|
-
}
|
|
1918
|
-
};
|
|
1919
|
-
__decorate([
|
|
1920
|
-
HasMany(() => LazyTarget2, { through: "test://children" }),
|
|
1921
|
-
__metadata("design:type", Array)
|
|
1922
|
-
], LazyParent2.prototype, "children", void 0);
|
|
1923
|
-
LazyParent2 = __decorate([
|
|
1924
|
-
Model({ name: "LazyParent2" })
|
|
1925
|
-
], LazyParent2);
|
|
1926
|
-
// generateSHACL was called during @Model — the shape exists
|
|
1927
|
-
const shacl = LazyParent2.generateSHACL();
|
|
1928
|
-
expect(shacl).toBeDefined();
|
|
1929
|
-
expect(shacl.shape).toBeDefined();
|
|
1930
|
-
expect(shacl.name).toBe("LazyParent2");
|
|
1931
|
-
});
|
|
1932
|
-
});
|
|
1933
|
-
// ──────────────────────────────────────────────────────────
|
|
1934
|
-
// deepQuery opt-in — getter evaluation on collection queries
|
|
1935
|
-
// ──────────────────────────────────────────────────────────
|
|
1936
|
-
describe("deepQuery opt-in — getter evaluation", () => {
|
|
1937
|
-
let DeepQueryTestMessage = class DeepQueryTestMessage extends Ad4mModel {
|
|
1938
|
-
constructor() {
|
|
1939
|
-
super(...arguments);
|
|
1940
|
-
this.type = "";
|
|
1941
|
-
this.body = "";
|
|
1942
|
-
this.isPopular = false;
|
|
1943
|
-
this.reactions = [];
|
|
1944
|
-
}
|
|
1945
|
-
};
|
|
1946
|
-
__decorate([
|
|
1947
|
-
Flag({ through: "flux://entry_type", value: "flux://message" }),
|
|
1948
|
-
__metadata("design:type", String)
|
|
1949
|
-
], DeepQueryTestMessage.prototype, "type", void 0);
|
|
1950
|
-
__decorate([
|
|
1951
|
-
Property({ through: "flux://body", resolveLanguage: "literal" }),
|
|
1952
|
-
__metadata("design:type", String)
|
|
1953
|
-
], DeepQueryTestMessage.prototype, "body", void 0);
|
|
1954
|
-
__decorate([
|
|
1955
|
-
Property({
|
|
1956
|
-
through: "flux://has_reply",
|
|
1957
|
-
getter: `SELECT ?target WHERE { ?source <flux://has_reply> ?target . } LIMIT 1`,
|
|
1958
|
-
}),
|
|
1959
|
-
__metadata("design:type", String)
|
|
1960
|
-
], DeepQueryTestMessage.prototype, "replyingTo", void 0);
|
|
1961
|
-
__decorate([
|
|
1962
|
-
ReadOnly({
|
|
1963
|
-
through: "flux://is_popular",
|
|
1964
|
-
getter: `ASK WHERE { ?source <flux://is_popular> "true" . }`,
|
|
1965
|
-
}),
|
|
1966
|
-
__metadata("design:type", Boolean)
|
|
1967
|
-
], DeepQueryTestMessage.prototype, "isPopular", void 0);
|
|
1968
|
-
__decorate([
|
|
1969
|
-
HasMany({ through: "flux://reaction" }),
|
|
1970
|
-
__metadata("design:type", Array)
|
|
1971
|
-
], DeepQueryTestMessage.prototype, "reactions", void 0);
|
|
1972
|
-
DeepQueryTestMessage = __decorate([
|
|
1973
|
-
Model({ name: "DeepQueryTestMessage" })
|
|
1974
|
-
], DeepQueryTestMessage);
|
|
1975
|
-
let sparqlCalls;
|
|
1976
|
-
const mockPerspective = {
|
|
1977
|
-
uuid: "test-uuid",
|
|
1978
|
-
modelQuery: jest.fn(async (className, queryJson, shapeJson) => {
|
|
1979
|
-
// Return a basic result for the single-instance getData() path
|
|
1980
|
-
const query = JSON.parse(queryJson);
|
|
1981
|
-
const id = query?.where?.id;
|
|
1982
|
-
if (id) {
|
|
1983
|
-
return {
|
|
1984
|
-
instances: [{ id, body: "Hello" }],
|
|
1985
|
-
totalCount: 1,
|
|
1986
|
-
};
|
|
1987
|
-
}
|
|
1988
|
-
return { instances: [], totalCount: 0 };
|
|
1989
|
-
}),
|
|
1990
|
-
querySparql: jest.fn(async (q) => {
|
|
1991
|
-
sparqlCalls.push(q);
|
|
1992
|
-
return [];
|
|
1993
|
-
}),
|
|
1994
|
-
get: jest.fn().mockResolvedValue([]),
|
|
1995
|
-
getExpression: jest.fn().mockResolvedValue(null),
|
|
1996
|
-
stringOrTemplateObjectToSubjectClassName: jest.fn().mockResolvedValue("DeepQueryTestMessage"),
|
|
1997
|
-
};
|
|
1998
|
-
beforeEach(() => {
|
|
1999
|
-
sparqlCalls = [];
|
|
2000
|
-
mockPerspective.querySparql.mockClear();
|
|
2001
|
-
mockPerspective.get.mockClear();
|
|
2002
|
-
});
|
|
2003
|
-
function makeInstances(count) {
|
|
2004
|
-
return Array.from({ length: count }, (_, i) => new DeepQueryTestMessage(mockPerspective, `flux://msg-${i}`));
|
|
2005
|
-
}
|
|
2006
|
-
it("single-instance get() delegates getter evaluation to Rust (no JS-side SPARQL)", async () => {
|
|
2007
|
-
const instance = new DeepQueryTestMessage(mockPerspective, "flux://msg-single");
|
|
2008
|
-
mockPerspective.querySparql.mockImplementation(async (q) => {
|
|
2009
|
-
sparqlCalls.push(q);
|
|
2010
|
-
if (q.includes("flux://msg-single") && !q.includes("flux://has_reply") && !q.includes("flux://is_popular")) {
|
|
2011
|
-
return [
|
|
2012
|
-
{ source: "flux://msg-single", predicate: "flux://entry_type", target: "flux://message", author: "did:key:a", timestamp: "3000" },
|
|
2013
|
-
{ source: "flux://msg-single", predicate: "flux://body", target: "literal:string:Hello", author: "did:key:a", timestamp: "3000" },
|
|
2014
|
-
];
|
|
2015
|
-
}
|
|
2016
|
-
return [];
|
|
2017
|
-
});
|
|
2018
|
-
await instance.get();
|
|
2019
|
-
// Getter evaluation now happens Rust-side via evaluate_getters() — no JS SPARQL calls
|
|
2020
|
-
const getterCalls = sparqlCalls.filter((q) => q.includes("flux://has_reply") || q.includes("flux://is_popular"));
|
|
2021
|
-
expect(getterCalls.length).toBe(0);
|
|
2022
|
-
});
|
|
2023
|
-
describe("Ad4mModel.evaluateGetters()", () => {
|
|
2024
|
-
it("resolves getters for a batch of instances", async () => {
|
|
2025
|
-
const instances = makeInstances(4);
|
|
2026
|
-
sparqlCalls = [];
|
|
2027
|
-
await DeepQueryTestMessage.evaluateGetters(instances.slice(0, 2), mockPerspective, ["replyingTo"]);
|
|
2028
|
-
const replyCalls = sparqlCalls.filter((q) => q.includes("flux://has_reply"));
|
|
2029
|
-
expect(replyCalls.length).toBe(2);
|
|
2030
|
-
const popularCalls = sparqlCalls.filter((q) => q.includes("flux://is_popular"));
|
|
2031
|
-
expect(popularCalls.length).toBe(0);
|
|
2032
|
-
});
|
|
2033
|
-
it("evaluates all getters when propertyNames is omitted", async () => {
|
|
2034
|
-
const instances = makeInstances(2);
|
|
2035
|
-
sparqlCalls = [];
|
|
2036
|
-
await DeepQueryTestMessage.evaluateGetters(instances, mockPerspective);
|
|
2037
|
-
const replyCalls = sparqlCalls.filter((q) => q.includes("flux://has_reply"));
|
|
2038
|
-
const popularCalls = sparqlCalls.filter((q) => q.includes("flux://is_popular"));
|
|
2039
|
-
expect(replyCalls.length).toBe(2);
|
|
2040
|
-
expect(popularCalls.length).toBe(2);
|
|
2041
|
-
});
|
|
2042
|
-
it("handles empty array gracefully", async () => {
|
|
2043
|
-
sparqlCalls = [];
|
|
2044
|
-
await DeepQueryTestMessage.evaluateGetters([], mockPerspective);
|
|
2045
|
-
expect(sparqlCalls.length).toBe(0);
|
|
2046
|
-
});
|
|
1289
|
+
expect(count).toBe(getResults.length);
|
|
1290
|
+
});
|
|
1291
|
+
it("should handle count() with Prolog for gt operator (legacy)", async () => {
|
|
1292
|
+
const prologResults = [{ TotalCount: 2 }];
|
|
1293
|
+
mockPerspective.infer.mockResolvedValue(prologResults);
|
|
1294
|
+
const count = await Recipe.count(mockPerspective, { where: { rating: { gt: 3 } } }, false);
|
|
1295
|
+
expect(mockPerspective.infer).toHaveBeenCalledTimes(1);
|
|
1296
|
+
expect(mockPerspective.querySurrealDB).not.toHaveBeenCalled();
|
|
1297
|
+
expect(count).toBe(2);
|
|
2047
1298
|
});
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
});
|
|
1299
|
+
it("should handle count() with Prolog for timestamp between (legacy)", async () => {
|
|
1300
|
+
const prologResults = [{ TotalCount: 3 }];
|
|
1301
|
+
mockPerspective.infer.mockResolvedValue(prologResults);
|
|
1302
|
+
const startTimestamp = new Date("2023-01-02T00:00:00Z").getTime();
|
|
1303
|
+
const endTimestamp = new Date("2023-01-04T00:00:00Z").getTime();
|
|
1304
|
+
const count = await Recipe.count(mockPerspective, { where: { timestamp: { between: [startTimestamp, endTimestamp] } } }, false);
|
|
1305
|
+
expect(mockPerspective.infer).toHaveBeenCalledTimes(1);
|
|
1306
|
+
expect(mockPerspective.querySurrealDB).not.toHaveBeenCalled();
|
|
1307
|
+
expect(count).toBe(3);
|
|
2054
1308
|
});
|
|
2055
1309
|
});
|