Skip to content
Navigation Menu
{{ message }}
forked from marklogic/node-client-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptic-patchBuilder.js
More file actions
272 lines (254 loc) · 12.1 KB
/
Copy pathoptic-patchBuilder.js
File metadata and controls
272 lines (254 loc) · 12.1 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* Copyright (c) 2024 MarkLogic Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const marklogic = require('../');
let p = marklogic.planBuilder;
var testconfig = require('../etc/test-config.js');
var dbWriter = marklogic.createDatabaseClient(testconfig.restWriterConnection);
const testlib = require("../etc/test-lib");
let uris = ['/test/patch-builder.xml', '/test/optic/patchBuilder.json'];
let serverConfiguration = {};
var assert = require('assert');
describe('optic patchBuilder tests', function() {
this.timeout(5000);
before(function (done) {
try {
testlib.findServerConfiguration(serverConfiguration);
setTimeout(()=>{
if(serverConfiguration.serverVersion < 11.2){
this.skip();
}
done();}, 3000);
} catch(error){
done(error);
}
});
describe('fromDocUris with patchBuilder', function () {
before(function (done) {
let doc = {
uri: uris[0],
contentType: 'application/xml',
content: '<?xml version="1.0" encoding="UTF-8"?>\n' +
'<!-- Copyright (c) 2023 MarkLogic Corporation -->\n' +
'<Citation Status="Completed">\n' +
' <ID>123</ID>\n' +
' <PMID>5717905</PMID>\n' +
' <Article>\n' +
' <Journal>\n' +
' <ISSN>5678</ISSN>\n' +
' <JournalIssue>\n' +
' <Volume>118</Volume>\n' +
' <Issue>49</Issue>\n' +
' <PubDate>\n' +
' <Year>1970</Year>\n' +
' <Month>12</Month>\n' +
' <Day>07</Day>\n' +
' </PubDate>\n' +
' </JournalIssue>\n' +
' </Journal>\n' +
' <ArticleTitle>\n' +
' The Influence of Calcium on Cholesterol in Human Serum\n' +
' </ArticleTitle>\n' +
' <AuthorList>\n' +
' <Author>\n' +
' <LastName>Doe</LastName>\n' +
' <ForeName>John</ForeName>\n' +
' </Author>\n' +
' <Author>\n' +
' <LastName>Smith</LastName>\n' +
' <ForeName>Jane</ForeName>\n' +
' </Author>\n' +
' </AuthorList>\n' +
' </Article>\n' +
'</Citation>',
permissions: [
{'role-name': 'rest-reader', capabilities: ['read']},
{'role-name': 'rest-writer', capabilities: ['read', 'update']}
]
};
dbWriter.documents.write(doc)
.result(()=> {
dbWriter.documents.write({
uri: uris[1],
contentType: 'application/json',
content: '{"person":{"lastName":"lastname-1", "firstName":"firstname-1"}}'
}).result(()=>done());
})
.catch(err => done(err));
});
after(function (done){
dbWriter.documents.remove(uris)
.result(()=> done())
.catch(err=> done(err));
});
it('should insert comment using insertBefore', function (done) {
const plan = p.fromDocUris(p.cts.documentQuery(uris[0]))
.joinDocCols(null, p.fragmentIdCol('fragmentId'))
.patch(p.col('doc'),
p.patchBuilder('/')
.insertBefore('Citation', p.xmlComment('Adding xmlComment'))
);
dbWriter.rows.query(plan)
.then(function (response) {
assert(response.rows[0].doc.value.toString().includes('<!--Adding xmlComment-->\n' +
'<Citation Status="Completed">'), "xmlComment was not inserted");
done();
})
.catch(err => done(err));
});
it('should insert comment using insertAfter', function (done) {
const plan = p.fromDocUris(p.cts.documentQuery(uris[0]))
.joinDocCols(null, p.fragmentIdCol('fragmentId'))
.patch(p.col('doc'),
p.patchBuilder('/')
.insertAfter('Citation', p.xmlComment('Adding xmlComment')));
dbWriter.rows.query(plan)
.then(function (response) {
assert(response.rows[0].doc.value.toString().includes('</Citation><!--Adding xmlComment-->'),
"xmlComment was not inserted");
done();
})
.catch(err => done(err));
});
it('should insert child element using insertChild', function (done) {
const plan = p.fromDocUris(p.cts.documentQuery(uris[0]))
.joinDocCols(null, p.fragmentIdCol('fragmentId'))
.patch(p.col('doc'),
p.patchBuilder('/Citation')
.insertChild('Article', p.xmlComment('Adding insertChild xmlComment'))
);
dbWriter.rows.query(plan)
.then(function (response) {
assert(response.rows[0].doc.value.toString().includes('<!--Adding insertChild xmlComment--></Article>'),
"xmlComment was not inserted");
done();
})
.catch(err => done(err));
});
it('should insert element using insertNamedChild', function (done) {
const plan = p.fromDocUris(p.cts.documentQuery(uris[1]))
.joinDocCols(null, p.fragmentIdCol('fragmentId'))
.patch(p.col('doc'),
p.patchBuilder('/')
.insertNamedChild('person', 'address', '123')
);
dbWriter.rows.query(plan)
.then(function (response) {
assert(response.rows[0].doc.value.person.address === '123', "Address was not added");
done();
})
.catch(err => done(err));
});
it('should delete element using remove', function (done) {
const plan = p.fromDocUris(p.cts.documentQuery(uris[1]))
.joinDocCols(null, p.fragmentIdCol('fragmentId'))
.patch(p.col('doc'),
p.patchBuilder('/')
.remove('person/firstName')
);
dbWriter.rows.query(plan)
.then(function (response) {
assert(response.rows[0].doc.value.person.toString().indexOf('firstName') === -1,
"firstName element was not deleted");
done();
})
.catch(err => done(err));
});
it('should replace element using replace', function (done) {
const plan = p.fromDocUris(p.cts.documentQuery(uris[1]))
.joinDocCols(null, p.fragmentIdCol('fragmentId'))
.patch(p.col('doc'),
p.patchBuilder('/')
.replace('person/firstName', 'test-firstName')
);
dbWriter.rows.query(plan)
.then(function (response) {
assert(response.rows[0].doc.value.person.firstName === 'test-firstName',
'firstName value was not replaced');
done();
})
.catch(err => done(err));
});
it('should replace content using replaceInsertChild', function (done) {
const plan = p.fromDocUris(p.cts.documentQuery(uris[1]))
.joinDocCols(null, p.fragmentIdCol('fragmentId'))
.patch(p.col('doc'),
p.patchBuilder('/')
.replaceInsertChild('.', 'person', 'newPerson')
);
dbWriter.rows.query(plan)
.then(function (response) {
assert(response.rows[0].doc.value.person === 'newPerson','person value was not replaced');
done();
})
.catch(err => done(err));
});
it('should replace key value using replaceValue', function (done) {
const plan = p.fromDocUris(p.cts.documentQuery(uris[1]))
.joinDocCols(null, p.fragmentIdCol('fragmentId'))
.patch(p.col('doc'),
p.patchBuilder('/')
.replaceValue( 'person/lastName', 'newLastName')
);
dbWriter.rows.query(plan)
.then(function (response) {
assert(response.rows[0].doc.value.person.lastName === 'newLastName' , "lastName was not updated.");
done();
})
.catch(err => done(err));
});
it('should work with two different instances of PlanBuilder', function (done) {
let planBuilder1 = marklogic.planBuilder;
let planBuilder2 = marklogic.planBuilder;
const plan = planBuilder1.fromDocUris(p.cts.documentQuery(uris[0]))
.joinDocCols(null, planBuilder1.fragmentIdCol('fragmentId'))
.patch(planBuilder1.col('doc'),
planBuilder1.patchBuilder('/')
.insertBefore('Citation', planBuilder1.xmlComment('Adding xmlComment'))
);
const plan2 = planBuilder2.fromDocUris(planBuilder2.cts.documentQuery(uris[0]))
.joinDocCols(null, planBuilder2.fragmentIdCol('fragmentId'))
.patch(planBuilder2.col('doc'),
planBuilder2.patchBuilder('/')
.insertAfter('Citation', planBuilder2.xmlComment('Adding xmlComment')));
try {
assert(JSON.stringify(plan.export()).indexOf('insert-after') === -1);
assert(JSON.stringify(plan2.export()).indexOf('insert-before') === -1);
done();
} catch(error){
done(error);
}
});
it('should continue with onError as continue during CONFLICTINGUPDATES', function (done) {
const plan = p.fromDocUris(p.cts.documentQuery(uris[1]))
.joinDocCols(null, p.fragmentIdCol('fragmentId'))
.patch(p.col('doc'),
p.patchBuilder('/')
.replaceValue( 'person/lastName', 'newLastName')
.replaceValue( 'person/lastName', 'newLastName-2')
)
.onError("continue", p.col("Error"));
dbWriter.rows.query(plan)
.then(function (response) {
assert(response.rows[0].doc.value.person.lastName === 'lastname-1' , "lastName was updated.");
assert(response.rows[0].Error.value.name === 'XDMP-CONFLICTINGUPDATES' ,
"should receive error column with value as XDMP-CONFLICTINGUPDATES");
done();
})
.catch(err => done(err));
});
});
});
You can’t perform that action at this time.
