-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathlockForUpdate-test.js
More file actions
137 lines (128 loc) · 5.91 KB
/
Copy pathlockForUpdate-test.js
File metadata and controls
137 lines (128 loc) · 5.91 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
/*
* Copyright (c) 2015-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
const marklogic = require('../');
const testconfig = require('../etc/test-config.js');
const db = marklogic.createDatabaseClient(testconfig.restWriterConnection);
const op = marklogic.planBuilder;
const should = require('should');
const testlib = require("../etc/test-lib");
let serverConfiguration = {};
describe('optic-update lockForUpdate tests', function() {
this.timeout(6000);
before(function (done) {
try {
testlib.findServerConfiguration(serverConfiguration);
setTimeout(()=>{done();}, 3000);
} catch(error){
done(error);
}
});
describe('test lockForUpdate', function () {
before(function(done){
if(serverConfiguration.serverVersion < 11){
this.skip();
}
done();
});
it('basic test', function (done) {
const uri = '/test/lockForUpdate/data.json';
const options = serverConfiguration.serverVersion <= 11.1? null : {'update' : true};
db.rows.query(op.fromDocDescriptors({uri, doc: {hello: "world"}}).write(), options)
.then((res) => {
const row = res.rows[0];
try {
row.uri.value.should.containEql("/test/lockForUpdate/data.json");
row.doc.value.should.deepEqual({hello: "world"});
const plan = op.fromDocDescriptors({uri, collections: ['optic']}).lockForUpdate().write();
db.eval(`
declareUpdate();
xdmp.lockForUpdate("/test/lockForUpdate/data.json");
xdmp.sleep(2000);
xdmp.documentSetCollections("/test/lockForUpdate/data.json", ['eval1']);
`);
const start = Date.now();
db.rows.query(plan, options).then((res) => {
const duration = Date.now() - start;
try {
(duration > 1500).should.equal(true);
db.documents.read({uris: [uri], categories: ['metadata']})
.result(function (documents) {
try {
documents.length.should.equal(1);
documents[0].collections.should.deepEqual(['optic']);
done();
} catch (e) {
done(e);
}
});
} catch (e) {
done(e);
}
});
} catch (e) {
done(e);
}
})
.catch(e => done(e));
});
it('test with uri column specified', function (done) {
const options = serverConfiguration.serverVersion <= 11.1? null :
{'update' : true};
db.rows.query(op.fromDocUris('/optic/test/musician1.json').lockForUpdate(op.col('uri')), options).then((res) => {
try {
res.rows.length.should.equal(1);
done();
} catch (e) {
done(e);
}
}).catch(e => done(e));
});
it('test with fromParam with custom uri', function (done) {
const rows = [{myUri: '/optic/test/musician1.json'}];
const outputCols = [{"column": "myUri", "type": "string", "nullable": false}];
const options = serverConfiguration.serverVersion <= 11.1? null :
{'update' : true};
db.rows.query(op.fromParam('bindingParam', null, outputCols).lockForUpdate(op.col('myUri')), options, {bindingParam: rows}).then((res) => {
try {
res.rows.length.should.equal(1);
done();
} catch (e) {
done(e);
}
}).catch(e => done(e));
});
it('test with fromParam with qualified uri column', function (done) {
const rows = [{myUri: '/optic/test/musician1.json'}];
const outputCols = [{"column": "myUri", "type": "string", "nullable": false}];
const options = serverConfiguration.serverVersion <= 11.1? null :
{'update' : true};
db.rows.query(op.fromParam('bindingParam', "myQualifier", outputCols).lockForUpdate(op.viewCol('myQualifier', 'myUri')),
options, {bindingParam: rows}).then((res) => {
try {
res.rows.length.should.equal(1);
done();
} catch (e) {
done(e);
}
}).catch(e => done(e));
});
it('test with fromParam with custom uri and array response', function (done) {
const rows = [{myUri: '/optic/test/musician1.json'}];
const outputCols = [{"column": "myUri", "type": "string", "nullable": false}];
const options = {'structure' : 'array'};
if(serverConfiguration.serverVersion > 11.1){
options.update = true;
}
db.rows.query(op.fromParam('bindingParam', null, outputCols).lockForUpdate(op.col('myUri')), options, {bindingParam: rows}).then((res) => {
try {
res[0][0].name.should.equal('myUri');
res[1][0].value.should.equal('/optic/test/musician1.json');
done();
} catch (e) {
done(e);
}
}).catch(e => done(e));
});
});
});