Skip to content

Commit 51c3052

Browse files
committed
metadata patch unit test
1 parent 826ec82 commit 51c3052

File tree

1 file changed

+77
-1
lines changed

1 file changed

+77
-1
lines changed

test-basic/documents-patch.js

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,81 @@ describe('document patch', function(){
103103
}, done);
104104
});
105105
});
106-
// TODO: metadata patch, patch library
106+
describe('for metadata', function() {
107+
before(function(done){
108+
db.documents.write({
109+
uri: uri1,
110+
contentType: 'application/json',
111+
collections: ['collection1/0', 'collection1/1'],
112+
permissions: [
113+
{'role-name':'app-user', capabilities:['read']}
114+
],
115+
properties: {
116+
property1: 'property value 1',
117+
property2: 'property value 2'
118+
},
119+
quality: 1,
120+
content: {key1: 'value 1'}
121+
}).
122+
result(function(response){done();}, done);
123+
});
124+
it('should insert, replace, and delete', function(done) {
125+
var p = marklogic.patchBuilder;
126+
db.documents.patch({uri:uri1, categories:['metadata'], operations:[
127+
p.insert('array-node("collections")/text()[. eq "collection1/1"]', 'before',
128+
'collection1/INSERTED_BEFORE'),
129+
p.insert('array-node("collections")', 'last-child',
130+
'collection1/INSERTED_LAST'),
131+
p.remove('array-node("collections")/text()[. eq "collection1/0"]'),
132+
p.replace('array-node("permissions")/object-node()[node("role-name") eq "app-user"]',
133+
{'role-name':'app-builder', capabilities:['read', 'update']}
134+
),
135+
p.insert('object-node("properties")/text("property2")', 'before',
136+
{propertyINSERTED_BEFORE2: 'property value INSERTED_BEFORE'}),
137+
p.insert('object-node("properties")', 'last-child',
138+
{propertyINSERTED_LAST: 'property value INSERTED_LAST'}),
139+
p.remove('object-node("properties")/text("property1")'),
140+
p.replace('node("quality")', 2)
141+
]}).
142+
result(function(response){
143+
db.read({uris:uri1, categories:['metadata']}).
144+
result(function(documents) {
145+
var document = documents[0];
146+
document.should.be.ok;
147+
document.collections.length.should.equal(3);
148+
document.collections[0].should.equal('collection1/INSERTED_BEFORE');
149+
document.collections[1].should.equal('collection1/1');
150+
document.collections[2].should.equal('collection1/INSERTED_LAST');
151+
document.should.have.property('permissions');
152+
var foundAppUser = false;
153+
var foundAppBuilder = false;
154+
document.permissions.forEach(function(permission){
155+
switch (permission['role-name']) {
156+
case 'app-user':
157+
foundAppUser = true;
158+
break;
159+
case 'app-builder':
160+
foundAppBuilder = true;
161+
permission.capabilities.length.should.equal(2);
162+
break;
163+
}
164+
});
165+
foundAppUser.should.equal(false);
166+
foundAppBuilder.should.equal(true);
167+
document.should.have.property('properties');
168+
document.properties.should.have.property('propertyINSERTED_BEFORE2');
169+
document.properties.propertyINSERTED_BEFORE2.should.
170+
equal('property value INSERTED_BEFORE');
171+
document.properties.should.have.property('property2');
172+
document.properties.property2.should.equal('property value 2');
173+
document.properties.should.have.property('propertyINSERTED_LAST');
174+
document.properties.propertyINSERTED_LAST.should.
175+
equal('property value INSERTED_LAST');
176+
document.should.have.property('quality');
177+
document.quality.should.equal(2);
178+
done();}, done);
179+
}, done);
180+
});
181+
});
182+
// TODO: patch library
107183
});

0 commit comments

Comments
 (0)