Skip to content

Commit 4122736

Browse files
author
Aries Yuwono
committed
add
1 parent 052fa2c commit 4122736

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright 2014-2015 MarkLogic Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
var should = require('should');
17+
18+
var testconfig = require('../etc/test-config-qa.js');
19+
20+
var marklogic = require('../');
21+
var q = marklogic.queryBuilder;
22+
var db = marklogic.createDatabaseClient(testconfig.restWriterConnection);
23+
24+
describe('Transaction with state test', function() {
25+
26+
var tid = 0;
27+
var tid2 = 0;
28+
var hostId = 0;
29+
30+
it('should do transaction with state', function(done) {
31+
db.transactions.open({transactionName: 'stateTransaction', timeLimit: 30, withState: true})
32+
.result(function(response) {
33+
//console.log(JSON.stringify(response, null, 2));
34+
response.should.have.property('cookies');
35+
var cookie = response.cookies[0];
36+
hostId = cookie.substring(cookie.indexOf('=') + 1);
37+
tid = response.txid;
38+
return db.documents.write({
39+
txid: tid,
40+
uri: '/test/state/transaction/doc1.json',
41+
contentType: 'application/json',
42+
content: {firstname: 'John', lastname: 'Doe', txKey: tid}
43+
}).result();
44+
})
45+
.then(function(response) {
46+
//console.log(JSON.stringify(response, null, 2));
47+
response.documents[0].uri.should.equal('/test/state/transaction/doc1.json');
48+
return db.transactions.read(tid).result();
49+
})
50+
.then(function(response) {
51+
//console.log(JSON.stringify(response, null, 2));
52+
response['transaction-status']['host']['host-id'].should.equal(hostId);
53+
response['transaction-status']['transaction-id'].should.equal(tid);
54+
response['transaction-status']['time-limit'].should.equal('30');
55+
return db.transactions.rollback(tid).result();
56+
})
57+
.then(function(response) {
58+
//console.log(JSON.stringify(response, null, 2));
59+
response.finished.should.equal('rollback');
60+
return db.documents.read({uris: '/test/state/transaction/doc1.json'}).result();
61+
})
62+
.then(function(response) {
63+
//console.log(JSON.stringify(response, null, 2));
64+
response.length.should.equal(0);
65+
return db.transactions.open({transactionName: 'stateTransaction2', timeLimit: 60, withState: true}).result();
66+
})
67+
.then(function(response) {
68+
//console.log(JSON.stringify(response, null, 2));
69+
response.should.have.property('cookies');
70+
var cookie = response.cookies[0];
71+
hostId = cookie.substring(cookie.indexOf('=') + 1);
72+
tid2 = response.txid;
73+
return db.documents.write({
74+
txid: tid2,
75+
uri: '/test/state/transaction/doc1.json',
76+
contentType: 'application/json',
77+
content: {firstname: 'John', lastname: 'Adams', txKey: tid2}
78+
}).result();
79+
})
80+
.then(function(response) {
81+
//console.log(JSON.stringify(response, null, 2));
82+
return db.transactions.read(tid2).result();
83+
})
84+
.then(function(response) {
85+
//console.log(JSON.stringify(response, null, 2));
86+
response['transaction-status']['host']['host-id'].should.equal(hostId);
87+
response['transaction-status']['transaction-name'].should.equal('stateTransaction2');
88+
response['transaction-status']['time-limit'].should.equal('60');
89+
return db.transactions.commit(tid2).result();
90+
})
91+
.then(function(response) {
92+
//console.log(JSON.stringify(response, null, 2));
93+
response.finished.should.equal('commit');
94+
return db.documents.read({uris: '/test/state/transaction/doc1.json'}).result();
95+
})
96+
.then(function(response) {
97+
//console.log(JSON.stringify(response, null, 2));
98+
response[0].content.lastname.should.equal('Adams');
99+
response[0].content.txKey.should.equal(tid2);
100+
return db.documents.remove('/test/state/transaction/doc1.json').result();
101+
})
102+
.then(function(response) {
103+
response.removed.should.equal(true);
104+
//console.log(JSON.stringify(response, null, 2));
105+
done();
106+
}, done);
107+
/*.catch(function(error) {
108+
console.log(error);
109+
done();
110+
}, done);*/
111+
});
112+
});
113+

0 commit comments

Comments
 (0)