-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathupdate-manager-n3patch-insert-test.ts
162 lines (132 loc) · 5.23 KB
/
update-manager-n3patch-insert-test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import * as chai from 'chai'
import { Headers } from 'cross-fetch'
import * as dirtyChai from 'dirty-chai'
import * as sinon from 'sinon'
import * as sinonChai from 'sinon-chai'
import { blankNode, Fetcher, graph, lit, namedNode, st, sym, UpdateManager } from '../../src/index'
import BlankNode from '../../src/blank-node';
const { expect } = chai
chai.use((sinonChai as any).default)
chai.use((dirtyChai as any).default)
chai.should()
describe('n3-patch updates via update manager', () => {
const subject = sym('https://pod.example/test/foo#subject')
const predicate = sym('https://pod.example/test/foo#predicate')
let fetchMock;
let store;
let updater;
beforeEach(() => {
store = graph();
fetchMock = sinon.stub().resolves({
ok: true,
status: 200,
statusText: 'OK',
headers: new Headers({
'accept-patch': 'text/n3'
})
});
store.fetcher = new Fetcher(store, { fetch: fetchMock })
updater = new UpdateManager(store)
});
it('calls PATCH to insert a triple', async () => {
const st1 = st(subject, predicate, lit("literal value"), subject.doc())
await updater.update([], [st1])
expect(getPatchCall().lastArg.body).to.equal(`
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
@prefix ex: <http://www.example.org/terms#>.
_:patch
solid:inserts {
<https://pod.example/test/foo#subject> <https://pod.example/test/foo#predicate> "literal value" .
}; a solid:InsertDeletePatch .
`)
})
it('calls PATCH to insert a triple including line feed', async () => {
const st1 = st(subject, predicate, lit("literal\nvalue"), subject.doc())
await updater.update([], [st1])
expect(getPatchCall().lastArg.body).to.equal(`
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
@prefix ex: <http://www.example.org/terms#>.
_:patch
solid:inserts {
<https://pod.example/test/foo#subject> <https://pod.example/test/foo#predicate> "literal\\nvalue" .
}; a solid:InsertDeletePatch .
`)
})
it('calls PATCH to insert a triple including carriage return line feed', async () => {
const st1 = st(subject, predicate, lit("literal\r\nvalue"), subject.doc())
await updater.update([], [st1])
expect(getPatchCall().lastArg.body).to.equal(`
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
@prefix ex: <http://www.example.org/terms#>.
_:patch
solid:inserts {
<https://pod.example/test/foo#subject> <https://pod.example/test/foo#predicate> "literal\\r\\nvalue" .
}; a solid:InsertDeletePatch .
`)
})
it('does not anonymize triples in INSERT DATA query', async () => {
const bNode = new BlankNode('subj');
const st1 = st(bNode, predicate, subject, subject.doc())
await updater.update([], [st1])
// expect(updater.anonymize).to.not.have.been.called;
expect(getPatchCall().lastArg.body).to.equal(`
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
@prefix ex: <http://www.example.org/terms#>.
_:patch
solid:inserts {
_:subj <https://pod.example/test/foo#predicate> <https://pod.example/test/foo#subject> .
}; a solid:InsertDeletePatch .
`)
});
it('Constructs n3patch document correctly', async () => {
updater.store.add(st(namedNode("ex:s1"), namedNode("ex:p1"), namedNode("ex:o1"), subject.doc()));
await updater.update(
[st(namedNode("ex:s1"), namedNode("ex:p1"), namedNode("ex:o1"), subject.doc())],
[st(namedNode("ex:s2"), namedNode("ex:p2"), namedNode("ex:o2"), subject.doc())]
)
expect(getPatchCall().lastArg.body).to.equal(`
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
@prefix ex: <http://www.example.org/terms#>.
_:patch
solid:deletes {
<ex:s1> <ex:p1> <ex:o1> .
};
solid:inserts {
<ex:s2> <ex:p2> <ex:o2> .
}; a solid:InsertDeletePatch .
`)
});
it('Sets conditions formula to match blank node context', async () => {
[
[namedNode("ex:s1"), namedNode("ex:p1"), blankNode("bn1"), subject.doc()],
[namedNode("ex:s1"), namedNode("ex:p1"), blankNode("bn2"), subject.doc()],
[blankNode("bn1"), namedNode("ex:p1"), namedNode("ex:o1"), subject.doc()],
[namedNode("ex:s2"), namedNode("ex:p2"), blankNode("bn3"), subject.doc()],
].forEach((st) => {
updater.store.add(st[0], st[1], st[2], st[3]);
})
await updater.update(
[st(namedNode("ex:s1"), namedNode("ex:p1"), blankNode("bn1"), subject.doc())],
[st(namedNode("ex:s2"), namedNode("ex:p2"), blankNode("bn3"), subject.doc())]
)
expect(getPatchCall().lastArg.body).to.equal(`
@prefix solid: <http://www.w3.org/ns/solid/terms#>.
@prefix ex: <http://www.example.org/terms#>.
_:patch
solid:where {
<ex:s1> <ex:p1> ?bn1 .
?bn1 <ex:p1> <ex:o1> .
<ex:s2> <ex:p2> ?bn3 .
};
solid:deletes {
<ex:s1> <ex:p1> ?bn1 .
};
solid:inserts {
<ex:s2> <ex:p2> ?bn3 .
}; a solid:InsertDeletePatch .
`)
});
function getPatchCall() {
return fetchMock.getCalls().find(it => it.lastArg.method === 'PATCH');
}
});