Skip to content

Commit f94953b

Browse files
authored
Merge pull request #574 from Jan-PieterBaert/tests/applyPatch
Update applyPatch function and add tests for it
2 parents cff3abd + 67f47cd commit f94953b

File tree

3 files changed

+80
-8
lines changed

3 files changed

+80
-8
lines changed

src/collection.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ export default class Collection<
102102
* @param bindings - The bindings to substitute
103103
*/
104104
substitute (bindings: Bindings) {
105-
const elementsCopy = this.elements.map((ea) => ea.substitute(bindings))
106-
107-
return new Collection(elementsCopy) as Collection<Node | Collection<any> | Literal | Variable>
105+
let collection = new Collection();
106+
this.elements.forEach((ea) => {collection.append(ea.substitute(bindings))})
107+
return collection as Collection<Node | Collection<any> | Literal | Variable>;
108108
}
109109

110110
toNT () {

src/store.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ export default class IndexedFormula extends Formula { // IN future - allow pass
253253
applyPatch(
254254
patch: {
255255
delete?: ReadonlyArray<Statement>,
256-
patch?: ReadonlyArray<Statement>,
256+
insert?: ReadonlyArray<Statement>,
257257
where?: any
258258
},
259259
target: TFNamedNode,
@@ -270,9 +270,9 @@ export default class IndexedFormula extends Formula { // IN future - allow pass
270270
// console.log('ds before substitute: ' + ds)
271271
if (binding) ds = ds.substitute(binding)
272272
// console.log('applyPatch: delete: ' + ds)
273-
ds = ds.statements as Statement[]
273+
274274
var bad: Quad[] = []
275-
var ds2 = ds.map(function (st: Quad) { // Find the actual statements in the store
275+
var ds2 = ds.elements.map(function (st: Quad) { // Find the actual statements in the store
276276
var sts = targetKB.statementsMatching(st.subject, st.predicate, st.object, target)
277277
if (sts.length === 0) {
278278
// log.info("NOT FOUND deletable " + st)
@@ -296,8 +296,8 @@ export default class IndexedFormula extends Formula { // IN future - allow pass
296296
// log.info("doPatch insert "+patch['insert'])
297297
ds = patch['insert']
298298
if (binding) ds = ds.substitute(binding)
299-
ds = ds.statements
300-
ds.map(function (st: Quad) {
299+
300+
ds.elements.map(function (st: Quad) {
301301
st.graph = target
302302
targetKB.add(st.subject, st.predicate, st.object, st.graph)
303303
})
@@ -315,6 +315,7 @@ export default class IndexedFormula extends Formula { // IN future - allow pass
315315
query.sync = true
316316

317317
var bindingsFound: Bindings[] = []
318+
query.pat.initBindings = [];
318319

319320
targetKB.query(
320321
query,

tests/unit/indexed-formula-test.js

+71
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import IndexedFormula from '../../src/store'
77
import NamedNode from '../../src/named-node'
88
import { RDFArrayRemove } from '../../src/utils-js'
99
import DataFactory from '../../src/factories/rdflib-data-factory'
10+
import Collection from "../../src/collection";
11+
import Variable from "../../src/variable";
1012

1113
describe('IndexedFormula', () => {
1214
const g0 = NamedNode.fromValue('https://example.com/graph0')
@@ -340,4 +342,73 @@ describe('IndexedFormula', () => {
340342
expect(store1.holdsStatement(store0)).to.be.true()
341343
})
342344
});
345+
346+
describe('applyPatch', () => {
347+
it('patches insert', function () {
348+
const store = new IndexedFormula()
349+
store.add([triple1, triple2, triple3].map(t=>DataFactory.st(t.subject, t.predicate, t.object, g0)))
350+
let collection = new Collection();
351+
collection.append(DataFactory.st(s1, p2, o3))
352+
353+
store.applyPatch({
354+
insert: collection,
355+
where: {statements: [
356+
DataFactory.st(new Variable("var1"), p1, o1),
357+
DataFactory.st(s2, new Variable("var2"), o2),
358+
DataFactory.st(s3, p3, new Variable("var3"))
359+
], optional: [], constraints: []}
360+
}, g0, (err)=>{expect(err).to.be.empty})
361+
362+
expect(store.statements.length).to.eq(4)
363+
});
364+
it('patches delete', function () {
365+
const store = new IndexedFormula()
366+
store.add([triple1, triple2, triple3, triple4].map(t=>DataFactory.st(t.subject, t.predicate, t.object, g0)))
367+
let collection = new Collection();
368+
collection.append(DataFactory.st(new Variable("var1"), new Variable("var2"), new Variable("var3")))
369+
370+
store.applyPatch({
371+
delete: collection,
372+
where: {statements: [
373+
DataFactory.st(new Variable("var1"), p1, o1),
374+
DataFactory.st(s2, new Variable("var2"), o2),
375+
DataFactory.st(s3, p3, new Variable("var3"))
376+
], optional: [], constraints: []}
377+
}, g0, (err)=>{expect(err).to.be.empty})
378+
379+
expect(store.statements.length).to.eq(3)
380+
});
381+
it("patches nothing when the where clause doesn't match", function () {
382+
const store = new IndexedFormula()
383+
store.add([triple1, triple2, triple3])
384+
let collection = new Collection();
385+
collection.append(DataFactory.st(new Variable("var1"), new Variable("var1"), new Variable("var1")))
386+
387+
store.applyPatch({
388+
insert: collection,
389+
where: {statements: [
390+
DataFactory.st(new Variable("var1"), p1, o1),
391+
DataFactory.st(s2, new Variable("var2"), o2),
392+
DataFactory.st(s3, p3, new Variable("var3"))
393+
], optional: [], constraints: []}
394+
}, g0, (err)=>{expect(err).to.be.empty})
395+
396+
expect(store.statements.length).to.eq(3)
397+
});
398+
it('patches nothing when the where clause matches more than one', function () {
399+
const store = new IndexedFormula()
400+
store.add([triple1, triple2, triple3])
401+
let collection = new Collection();
402+
collection.append(DataFactory.st(s1, p2, o3))
403+
404+
store.applyPatch({
405+
insert: collection,
406+
where: {statements: [
407+
DataFactory.st(new Variable("var1"), new Variable("var2"), new Variable("var3")),
408+
], optional: [], constraints: []}
409+
}, g0, (err)=>{expect(err).to.be.empty})
410+
411+
expect(store.statements.length).to.eq(3)
412+
});
413+
})
343414
})

0 commit comments

Comments
 (0)