-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathnodejs-documents-transaction-withstate.js
More file actions
101 lines (95 loc) · 4.64 KB
/
Copy pathnodejs-documents-transaction-withstate.js
File metadata and controls
101 lines (95 loc) · 4.64 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
/*
* Copyright (c) 2015-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
var should = require('should');
var testconfig = require('../etc/test-config-qa.js');
var marklogic = require('../');
var q = marklogic.queryBuilder;
var db = marklogic.createDatabaseClient(testconfig.restWriterConnection);
describe('Transaction with state test', function () {
var tid = 0;
var tid2 = 0;
var hostId = 0;
it('should do transaction with state', function (done) {
db.transactions.open({ transactionName: 'stateTransaction', timeLimit: 30, withState: true })
.result(function (response) {
//console.log(JSON.stringify(response, null, 2));
response.should.have.property('cookies');
var cookie = response.cookies[0];
hostId = cookie.substring(cookie.indexOf('=') + 1);
tid = response.txid;
return db.documents.write({
txid: tid,
uri: '/test/state/transaction/doc1.json',
contentType: 'application/json',
content: { firstname: 'John', lastname: 'Doe', txKey: tid }
}).result();
})
.then(function (response) {
//console.log(JSON.stringify(response, null, 2));
response.documents[0].uri.should.equal('/test/state/transaction/doc1.json');
return db.transactions.read(tid).result();
})
.then(function (response) {
//console.log(JSON.stringify(response, null, 2));
response['transaction-status']['host']['host-id'].should.equal(hostId);
response['transaction-status']['transaction-id'].should.equal(tid);
parseInt(response['transaction-status']['time-limit']).should.be.above(27);
return db.transactions.rollback(tid).result();
})
.then(function (response) {
//console.log(JSON.stringify(response, null, 2));
response.finished.should.equal('rollback');
return db.documents.read({ uris: '/test/state/transaction/doc1.json' }).result();
})
.then(function (response) {
//console.log(JSON.stringify(response, null, 2));
response.length.should.equal(0);
return db.transactions.open({ transactionName: 'stateTransaction2', timeLimit: 60, withState: true }).result();
})
.then(function (response) {
//console.log(JSON.stringify(response, null, 2));
response.should.have.property('cookies');
var cookie = response.cookies[0];
hostId = cookie.substring(cookie.indexOf('=') + 1);
tid2 = response.txid;
return db.documents.write({
txid: tid2,
uri: '/test/state/transaction/doc1.json',
contentType: 'application/json',
content: { firstname: 'John', lastname: 'Adams', txKey: tid2 }
}).result();
})
.then(function (response) {
//console.log(JSON.stringify(response, null, 2));
return db.transactions.read(tid2).result();
})
.then(function (response) {
//console.log(JSON.stringify(response, null, 2));
response['transaction-status']['host']['host-id'].should.equal(hostId);
response['transaction-status']['transaction-name'].should.equal('stateTransaction2');
response['transaction-status']['time-limit'].should.equal('60');
return db.transactions.commit(tid2).result();
})
.then(function (response) {
//console.log(JSON.stringify(response, null, 2));
response.finished.should.equal('commit');
return db.documents.read({ uris: '/test/state/transaction/doc1.json' }).result();
})
.then(function (response) {
//console.log(JSON.stringify(response, null, 2));
response[0].content.lastname.should.equal('Adams');
response[0].content.txKey.should.equal(tid2);
return db.documents.remove('/test/state/transaction/doc1.json').result();
})
.then(function (response) {
response.removed.should.equal(true);
//console.log(JSON.stringify(response, null, 2));
done();
}, done);
/*.catch(function(error) {
console.log(error);
done();
}, done);*/
});
});