Skip to content

Update applyPatch function and add tests for it #574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ export default class Collection<
* @param bindings - The bindings to substitute
*/
substitute (bindings: Bindings) {
const elementsCopy = this.elements.map((ea) => ea.substitute(bindings))

return new Collection(elementsCopy) as Collection<Node | Collection<any> | Literal | Variable>
let collection = new Collection();
this.elements.forEach((ea) => {collection.append(ea.substitute(bindings))})
return collection as Collection<Node | Collection<any> | Literal | Variable>;
}

toNT () {
Expand Down
11 changes: 6 additions & 5 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export default class IndexedFormula extends Formula { // IN future - allow pass
applyPatch(
patch: {
delete?: ReadonlyArray<Statement>,
patch?: ReadonlyArray<Statement>,
insert?: ReadonlyArray<Statement>,
where?: any
},
target: TFNamedNode,
Expand All @@ -268,9 +268,9 @@ export default class IndexedFormula extends Formula { // IN future - allow pass
// console.log('ds before substitute: ' + ds)
if (binding) ds = ds.substitute(binding)
// console.log('applyPatch: delete: ' + ds)
ds = ds.statements as Statement[]

var bad: Quad[] = []
var ds2 = ds.map(function (st: Quad) { // Find the actual statements in the store
var ds2 = ds.elements.map(function (st: Quad) { // Find the actual statements in the store
var sts = targetKB.statementsMatching(st.subject, st.predicate, st.object, target)
if (sts.length === 0) {
// log.info("NOT FOUND deletable " + st)
Expand All @@ -294,8 +294,8 @@ export default class IndexedFormula extends Formula { // IN future - allow pass
// log.info("doPatch insert "+patch['insert'])
ds = patch['insert']
if (binding) ds = ds.substitute(binding)
ds = ds.statements
ds.map(function (st: Quad) {

ds.elements.map(function (st: Quad) {
st.graph = target
targetKB.add(st.subject, st.predicate, st.object, st.graph)
})
Expand All @@ -313,6 +313,7 @@ export default class IndexedFormula extends Formula { // IN future - allow pass
query.sync = true

var bindingsFound: Bindings[] = []
query.pat.initBindings = [];

targetKB.query(
query,
Expand Down
71 changes: 71 additions & 0 deletions tests/unit/indexed-formula-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import IndexedFormula from '../../src/store'
import NamedNode from '../../src/named-node'
import { RDFArrayRemove } from '../../src/utils-js'
import DataFactory from '../../src/factories/rdflib-data-factory'
import Collection from "../../src/collection";
import Variable from "../../src/variable";

describe('IndexedFormula', () => {
const g0 = NamedNode.fromValue('https://example.com/graph0')
Expand Down Expand Up @@ -340,4 +342,73 @@ describe('IndexedFormula', () => {
expect(store1.holdsStatement(store0)).to.be.true()
})
});

describe('applyPatch', () => {
it('patches insert', function () {
const store = new IndexedFormula()
store.add([triple1, triple2, triple3].map(t=>DataFactory.st(t.subject, t.predicate, t.object, g0)))
let collection = new Collection();
collection.append(DataFactory.st(s1, p2, o3))

store.applyPatch({
insert: collection,
where: {statements: [
DataFactory.st(new Variable("var1"), p1, o1),
DataFactory.st(s2, new Variable("var2"), o2),
DataFactory.st(s3, p3, new Variable("var3"))
], optional: [], constraints: []}
}, g0, (err)=>{expect(err).to.be.empty})

expect(store.statements.length).to.eq(4)
});
it('patches delete', function () {
const store = new IndexedFormula()
store.add([triple1, triple2, triple3, triple4].map(t=>DataFactory.st(t.subject, t.predicate, t.object, g0)))
let collection = new Collection();
collection.append(DataFactory.st(new Variable("var1"), new Variable("var2"), new Variable("var3")))

store.applyPatch({
delete: collection,
where: {statements: [
DataFactory.st(new Variable("var1"), p1, o1),
DataFactory.st(s2, new Variable("var2"), o2),
DataFactory.st(s3, p3, new Variable("var3"))
], optional: [], constraints: []}
}, g0, (err)=>{expect(err).to.be.empty})

expect(store.statements.length).to.eq(3)
});
it("patches nothing when the where clause doesn't match", function () {
const store = new IndexedFormula()
store.add([triple1, triple2, triple3])
let collection = new Collection();
collection.append(DataFactory.st(new Variable("var1"), new Variable("var1"), new Variable("var1")))

store.applyPatch({
insert: collection,
where: {statements: [
DataFactory.st(new Variable("var1"), p1, o1),
DataFactory.st(s2, new Variable("var2"), o2),
DataFactory.st(s3, p3, new Variable("var3"))
], optional: [], constraints: []}
}, g0, (err)=>{expect(err).to.be.empty})

expect(store.statements.length).to.eq(3)
});
it('patches nothing when the where clause matches more than one', function () {
const store = new IndexedFormula()
store.add([triple1, triple2, triple3])
let collection = new Collection();
collection.append(DataFactory.st(s1, p2, o3))

store.applyPatch({
insert: collection,
where: {statements: [
DataFactory.st(new Variable("var1"), new Variable("var2"), new Variable("var3")),
], optional: [], constraints: []}
}, g0, (err)=>{expect(err).to.be.empty})

expect(store.statements.length).to.eq(3)
});
})
})