Adding transaction tests
This commit is contained in:
@ -67,7 +67,7 @@ module.exports = {
|
||||
.catch(function (err) {
|
||||
test.done(err);
|
||||
});
|
||||
},*/
|
||||
},
|
||||
'Test Commit Two Events Using Transaction': function(test) {
|
||||
this.conn.startTransaction(testStreamName, client.expectedVersion.any)
|
||||
.then(function(trx) {
|
||||
@ -90,7 +90,6 @@ module.exports = {
|
||||
test.done(err);
|
||||
});
|
||||
},
|
||||
/*
|
||||
'Test Read One Event': function(test) {
|
||||
this.conn.readEvent(testStreamName, 0)
|
||||
.then(function(result) {
|
||||
@ -231,14 +230,15 @@ module.exports = {
|
||||
function liveProcessingStarted() {
|
||||
liveProcessing = true;
|
||||
var events = [createRandomEvent()];
|
||||
self.conn.appendToStream('test', client.expectedVersion.any, events);
|
||||
self.conn.appendToStream(testStreamName, client.expectedVersion.any, events);
|
||||
}
|
||||
function subscriptionDropped(connection, reason, error) {
|
||||
test.ok(liveEvents.length === 1, "Expecting 1 live event, got " + liveEvents.length);
|
||||
test.ok(catchUpEvents.length > 1, "Expecting at least 1 catchUp event, got " + catchUpEvents.length);
|
||||
test.ok(catchUpEvents.length >= 1, "Expecting at least 1 catchUp event, got " + catchUpEvents.length);
|
||||
test.done(error);
|
||||
}
|
||||
var subscription = this.conn.subscribeToStreamFrom('test', null, false, eventAppeared, liveProcessingStarted, subscriptionDropped);
|
||||
//this.conn.appendToStream()
|
||||
var subscription = this.conn.subscribeToStreamFrom(testStreamName, null, false, eventAppeared, liveProcessingStarted, subscriptionDropped);
|
||||
},
|
||||
'Test Subscribe to All From': function(test) {
|
||||
var self = this;
|
||||
|
185
test/transactions_test.js
Normal file
185
test/transactions_test.js
Normal file
@ -0,0 +1,185 @@
|
||||
var uuid = require('uuid');
|
||||
var Long = require('long');
|
||||
var client = require('../src/client');
|
||||
|
||||
module.exports = {
|
||||
setUp: function(cb) {
|
||||
cb();
|
||||
},
|
||||
'Start A Transaction Happy Path': function(test) {
|
||||
this.conn.startTransaction(this.testStreamName, client.expectedVersion.noStream)
|
||||
.then(function(trx) {
|
||||
test.ok(Long.isLong(trx.transactionId), "trx.transactionId should be a Long.");
|
||||
test.done();
|
||||
})
|
||||
.catch(test.done);
|
||||
},
|
||||
/*
|
||||
'Start A Transaction With Wrong Expected Version': function(test) {
|
||||
this.conn.startTransaction(this.testStreamName, 10)
|
||||
.then(function(trx) {
|
||||
test.fail("Start Transaction with wrong expected version succeeded.");
|
||||
test.done();
|
||||
})
|
||||
.catch(function(err) {
|
||||
var isWrongExpectedVersion = err instanceof client.WrongExpectedVersionError;
|
||||
if (isWrongExpectedVersion) return test.done();
|
||||
test.done(err);
|
||||
});
|
||||
},
|
||||
'Start A Transaction With Deleted Stream': function(test) {
|
||||
var self = this;
|
||||
this.conn.deleteStream(this.testStreamName, client.expectedVersion.emptyStream)
|
||||
.then(function() {
|
||||
return self.conn.startTransaction(self.testStreamName, client.expectedVersion.any);
|
||||
})
|
||||
.then(function(trx) {
|
||||
test.fail("Start Transaction with deleted stream succeeded.");
|
||||
test.done();
|
||||
})
|
||||
.catch(function(err) {
|
||||
var isStreamDeleted = err instanceof client.StreamDeletedError;
|
||||
test.ok(isStreamDeleted, "Expected StreamDeletedError got " + err.constructor.name);
|
||||
if (isStreamDeleted) return test.done();
|
||||
test.done(err);
|
||||
});
|
||||
},
|
||||
*/
|
||||
'Start A Transaction With No Access': function(test) {
|
||||
var self = this;
|
||||
var metadata = {$acl: {$w: "$admins"}};
|
||||
this.conn.setStreamMetadataRaw(this.testStreamName, -1, metadata)
|
||||
.then(function() {
|
||||
return self.conn.startTransaction(self.testStreamName, client.expectedVersion.any);
|
||||
})
|
||||
.then(function(trx) {
|
||||
test.fail("Start Transaction with no access succeeded.");
|
||||
test.done();
|
||||
})
|
||||
.catch(function(err) {
|
||||
var isAccessDenied = err instanceof client.AccessDeniedError;
|
||||
test.ok(isAccessDenied, "Expected AccessDeniedError got " + err.constructor.name);
|
||||
if (isAccessDenied) return test.done();
|
||||
test.done(err);
|
||||
});
|
||||
},
|
||||
'Continue A Transaction Happy Path': function(test) {
|
||||
var self = this;
|
||||
this.conn.startTransaction(this.testStreamName, client.expectedVersion.emptyStream)
|
||||
.then(function(trx) {
|
||||
return trx.write(client.createJsonEventData(uuid.v4(), {a: Math.random()}, null, 'anEvent'))
|
||||
.then(function () {
|
||||
return self.conn.continueTransaction(trx.transactionId);
|
||||
});
|
||||
})
|
||||
.then(function(trx) {
|
||||
return trx.write(client.createJsonEventData(uuid.v4(), {a: Math.random()}, null, 'anEvent'))
|
||||
.then(function() {
|
||||
return trx.commit();
|
||||
})
|
||||
.then(function() {
|
||||
test.done();
|
||||
});
|
||||
})
|
||||
.catch(test.done);
|
||||
},
|
||||
'Write/Commit Transaction Happy Path': function(test) {
|
||||
var self = this;
|
||||
this.conn.startTransaction(this.testStreamName, client.expectedVersion.emptyStream)
|
||||
.then(function(trx) {
|
||||
self.events = [];
|
||||
for(var i = 0; i < 15; i++) {
|
||||
var event = {a: uuid.v4(), b: Math.random()};
|
||||
self.events.push(client.createJsonEventData(uuid.v4(), event, null, 'anEvent'));
|
||||
}
|
||||
return trx.write(self.events)
|
||||
.then(function() {
|
||||
var events = [];
|
||||
for(var j = 0; j < 9; j++) {
|
||||
var event = {a: Math.random(), b: uuid.v4()};
|
||||
events.push(client.createJsonEventData(uuid.v4(), event, null, 'anotherEvent'));
|
||||
}
|
||||
Array.prototype.push.apply(self.events, events);
|
||||
trx.write(events);
|
||||
})
|
||||
.then(function() {
|
||||
return trx.commit();
|
||||
});
|
||||
})
|
||||
.then(function(result) {
|
||||
test.ok(result.logPosition, "Missing result.logPosition");
|
||||
test.areEqual("result.nextExpectedVersion", result.nextExpectedVersion, self.events.length-1);
|
||||
test.done();
|
||||
})
|
||||
.catch(test.done);
|
||||
},
|
||||
'Write/Commit Transaction With Wrong Expected Version': function(test) {
|
||||
this.conn.startTransaction(this.testStreamName, 10)
|
||||
.then(function(trx) {
|
||||
return trx.write(client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'anEvent'))
|
||||
.then(function() {
|
||||
return trx.commit();
|
||||
});
|
||||
})
|
||||
.then(function() {
|
||||
test.fail("Commit on transaction with wrong expected version succeeded.");
|
||||
test.done();
|
||||
})
|
||||
.catch(function(err) {
|
||||
var isWrongExpectedVersion = err instanceof client.WrongExpectedVersionError;
|
||||
test.ok(isWrongExpectedVersion, "Expected WrongExpectedVersionError, but got " + err.constructor.name);
|
||||
if (isWrongExpectedVersion) return test.done();
|
||||
test.done(err);
|
||||
});
|
||||
},
|
||||
'Write/Commit Transaction With Deleted Stream': function(test) {
|
||||
var self = this;
|
||||
this.conn.deleteStream(this.testStreamName, client.expectedVersion.emptyStream, true)
|
||||
.then(function() {
|
||||
return self.conn.startTransaction(self.testStreamName, client.expectedVersion.any);
|
||||
})
|
||||
.then(function(trx) {
|
||||
return trx.write(client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'anEvent'))
|
||||
.then(function() {
|
||||
return trx.commit();
|
||||
});
|
||||
})
|
||||
.then(function() {
|
||||
test.fail("Commit on transaction on deleted stream succeeded.");
|
||||
test.done();
|
||||
})
|
||||
.catch(function(err) {
|
||||
var isStreamDeleted = err instanceof client.StreamDeletedError;
|
||||
test.ok(isStreamDeleted, "Expected StreamDeletedError, but got " + err.constructor.name);
|
||||
if (isStreamDeleted) return test.done();
|
||||
test.done(err);
|
||||
});
|
||||
},
|
||||
'Write/Commit Transaction With No Write Access': function(test) {
|
||||
var self = this;
|
||||
this.conn.startTransaction(this.testStreamName, client.expectedVersion.any)
|
||||
.then(function(trx) {
|
||||
var metadata = {$acl: {$w: "$admins"}};
|
||||
return self.conn.setStreamMetadataRaw(self.testStreamName, -1, metadata)
|
||||
.then(function () {
|
||||
return trx.write(client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'anEvent'))
|
||||
.then(function () {
|
||||
return trx.commit();
|
||||
});
|
||||
})
|
||||
})
|
||||
.then(function() {
|
||||
test.fail("Commit on transaction on deleted stream succeeded.");
|
||||
test.done();
|
||||
})
|
||||
.catch(function(err) {
|
||||
var isAccessDenied = err instanceof client.AccessDeniedError;
|
||||
test.ok(isAccessDenied, "Expected AccessDeniedError, but got " + err.constructor.name);
|
||||
if (isAccessDenied) return test.done();
|
||||
test.done(err);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
require('./common/base_test').init(module.exports);
|
||||
|
Reference in New Issue
Block a user