|
| 1 | +/* |
| 2 | +* Copyright (c) 2015-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. |
| 3 | +*/ |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +const should = require('should'); |
| 7 | + |
| 8 | +const marklogic = require('../'); |
| 9 | +const p = marklogic.planBuilder; |
| 10 | + |
| 11 | +const pbb = require('./plan-builder-base'); |
| 12 | +const assert = require('assert'); |
| 13 | +const testlib = require('../etc/test-lib'); |
| 14 | +const { restWriterConnection } = require('../etc/test-config'); |
| 15 | +let serverConfiguration = {}; |
| 16 | +const tcGraph = p.graphCol('http://test.optic.tc#'); |
| 17 | +const tcLabel = p.sem.iri('http://test.optic.tc#label'); |
| 18 | +const person = p.col('person'); |
| 19 | +const parent = p.col('parent'); |
| 20 | +const ancestor = p.col('ancestor'); |
| 21 | +const parentProp = p.sem.iri('http://marklogic.com/transitiveClosure/parent'); |
| 22 | +const execPlan = pbb.execPlan; |
| 23 | + |
| 24 | +describe('tests for server-side transitive-closure method.', function () { |
| 25 | + before(function (done) { |
| 26 | + this.timeout(6000); |
| 27 | + try { |
| 28 | + testlib.findServerConfiguration(serverConfiguration); |
| 29 | + setTimeout(() => { |
| 30 | + if (serverConfiguration.serverVersion < 12) { |
| 31 | + this.skip(); |
| 32 | + } |
| 33 | + done(); |
| 34 | + }, 3000); |
| 35 | + } catch (error) { |
| 36 | + done(error); |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + it('with simple pattern full transitive closure', function (done) { |
| 41 | + execPlan( |
| 42 | + p.fromTriples([ |
| 43 | + p.pattern(person, parentProp, ancestor, tcGraph) |
| 44 | + ] |
| 45 | + ).transitiveClosure(person, ancestor) |
| 46 | + .orderBy([ancestor, person]) |
| 47 | + ) |
| 48 | + .then(function (response) { |
| 49 | + const rows = response.rows; |
| 50 | + rows.length.should.equal(21); |
| 51 | + rows[0].should.have.property('person'); |
| 52 | + rows[0].should.have.property('ancestor'); |
| 53 | + done(); |
| 54 | + }) |
| 55 | + .catch(done); |
| 56 | + }); |
| 57 | + |
| 58 | + it('with simple pattern minLength=2, transitive closure grandparents and up', function (done) { |
| 59 | + execPlan( |
| 60 | + p.fromTriples([ |
| 61 | + p.pattern(person, parentProp, ancestor, tcGraph) |
| 62 | + ] |
| 63 | + ).transitiveClosure(person, ancestor, {'min-length': 2}) |
| 64 | + ) |
| 65 | + .then(function (response) { |
| 66 | + const rows = response.rows; |
| 67 | + // 2 steps or more excludes direct parent-child relationships |
| 68 | + rows.length.should.equal(12); |
| 69 | + rows[0].should.have.property('person'); |
| 70 | + rows[0].should.have.property('ancestor'); |
| 71 | + done(); |
| 72 | + }) |
| 73 | + .catch(done); |
| 74 | + }); |
| 75 | + |
| 76 | + it('with simple pattern minLength=2, maxLength=2, transitive closure grandparents only', function (done) { |
| 77 | + execPlan( |
| 78 | + p.fromTriples([ |
| 79 | + p.pattern(person, parentProp, ancestor, tcGraph) |
| 80 | + ] |
| 81 | + ).transitiveClosure(person, ancestor, {minLength: 2, maxLength: 2}) |
| 82 | + ) |
| 83 | + .then(function (response) { |
| 84 | + const rows = response.rows; |
| 85 | + // 2 steps only is grandparent relationships only |
| 86 | + rows.length.should.equal(6); |
| 87 | + rows[0].should.have.property('person'); |
| 88 | + rows[0].should.have.property('ancestor'); |
| 89 | + done(); |
| 90 | + }) |
| 91 | + .catch(done); |
| 92 | + }); |
| 93 | + |
| 94 | + it('with simple pattern transitive closure with parent column as ancestor', function (done) { |
| 95 | + execPlan( |
| 96 | + p.fromTriples([ |
| 97 | + p.pattern(person, parentProp, parent, tcGraph) |
| 98 | + ] |
| 99 | + ).transitiveClosure(person, p.as("ancestor", parent)) |
| 100 | + ) |
| 101 | + .then(function (response) { |
| 102 | + const rows = response.rows; |
| 103 | + rows.length.should.equal(21); |
| 104 | + rows[0].should.have.property('person'); |
| 105 | + rows[0].should.have.property('ancestor'); |
| 106 | + done(); |
| 107 | + }) |
| 108 | + .catch(done); |
| 109 | + }); |
| 110 | + |
| 111 | + it('with simple pattern transitive closure join to get labels', function (done) { |
| 112 | + this.timeout(5000); |
| 113 | + execPlan( |
| 114 | + p.fromTriples([ |
| 115 | + p.pattern(person, parentProp, ancestor, tcGraph) |
| 116 | + ]) |
| 117 | + .transitiveClosure(person, ancestor) |
| 118 | + .joinLeftOuter( |
| 119 | + p.fromTriples([ |
| 120 | + p.pattern(person, tcLabel, p.col('person_name')) |
| 121 | + ]) |
| 122 | + ) |
| 123 | + .joinLeftOuter( |
| 124 | + p.fromTriples([ |
| 125 | + p.pattern(ancestor, tcLabel, p.col('ancestor_name')) |
| 126 | + ]) |
| 127 | + ) |
| 128 | + ) |
| 129 | + .then(function (response) { |
| 130 | + const rows = response.rows; |
| 131 | + rows.length.should.equal(21); |
| 132 | + rows[0].should.have.property('person'); |
| 133 | + rows[0].should.have.property('ancestor'); |
| 134 | + rows[0].should.have.property('person_name'); |
| 135 | + rows[0].should.have.property('ancestor_name'); |
| 136 | + done(); |
| 137 | + }) |
| 138 | + .catch(done); |
| 139 | + }); |
| 140 | + |
| 141 | +}); |
0 commit comments