2016-03-11 23:55:27 +00:00
|
|
|
var uuid = require('uuid');
|
|
|
|
var client = require('../src/client');
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
'Append One Event To Stream Happy Path': function(test) {
|
2017-01-29 02:04:58 +00:00
|
|
|
test.expect(2);
|
2016-03-11 23:55:27 +00:00
|
|
|
var event = client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'testEvent');
|
|
|
|
this.conn.appendToStream(this.testStreamName, client.expectedVersion.any, event)
|
|
|
|
.then(function(result) {
|
2016-03-14 01:38:42 +00:00
|
|
|
test.areEqual("nextExpectedVersion", result.nextExpectedVersion, 0);
|
2016-03-11 23:55:27 +00:00
|
|
|
test.ok(result.logPosition, "No log position in result.");
|
|
|
|
test.done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
test.done(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
'Append Multiple Events To Stream Happy Path': function(test) {
|
2017-01-29 02:04:58 +00:00
|
|
|
test.expect(2);
|
2016-03-15 00:55:35 +00:00
|
|
|
const expectedVersion = 25;
|
|
|
|
var events = [];
|
|
|
|
for(var i = 0; i <= expectedVersion; i++) {
|
|
|
|
if (i % 2 === 0)
|
|
|
|
events.push(client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'testEvent'));
|
2017-03-22 08:12:53 +00:00
|
|
|
else
|
|
|
|
events.push(client.createJsonEventData(uuid.v4(), {b: Math.random(), a: uuid.v4()}, null, 'otherEvent'));
|
|
|
|
}
|
|
|
|
this.conn.appendToStream(this.testStreamName, client.expectedVersion.any, events)
|
|
|
|
.then(function(result) {
|
|
|
|
test.areEqual("result.nextExpectedVersion", result.nextExpectedVersion, expectedVersion);
|
|
|
|
test.ok(result.logPosition, "No log position in result.");
|
|
|
|
test.done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
test.done(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
'Append Multiple Events To Stream Happy Path Stress Test': function(test) {
|
|
|
|
test.expect(2);
|
|
|
|
const expectedVersion = 1000;
|
|
|
|
var events = [];
|
|
|
|
for(var i = 0; i <= expectedVersion; i++) {
|
|
|
|
if (i % 2 === 0)
|
|
|
|
events.push(client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'testEvent'));
|
2016-03-15 00:55:35 +00:00
|
|
|
else
|
|
|
|
events.push(client.createJsonEventData(uuid.v4(), {b: Math.random(), a: uuid.v4()}, null, 'otherEvent'));
|
|
|
|
}
|
2016-03-11 23:55:27 +00:00
|
|
|
this.conn.appendToStream(this.testStreamName, client.expectedVersion.any, events)
|
|
|
|
.then(function(result) {
|
2016-03-15 00:55:35 +00:00
|
|
|
test.areEqual("result.nextExpectedVersion", result.nextExpectedVersion, expectedVersion);
|
2016-03-11 23:55:27 +00:00
|
|
|
test.ok(result.logPosition, "No log position in result.");
|
|
|
|
test.done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
test.done(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
'Append To Stream Wrong Expected Version': function(test) {
|
2017-01-29 02:04:58 +00:00
|
|
|
test.expect(1);
|
2016-03-11 23:55:27 +00:00
|
|
|
var event = client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'testEvent');
|
|
|
|
this.conn.appendToStream(this.testStreamName, 10, event)
|
|
|
|
.then(function(result) {
|
2016-03-14 01:38:42 +00:00
|
|
|
test.fail("Append succeeded but should have failed.");
|
2016-03-11 23:55:27 +00:00
|
|
|
test.done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
var isWrongExpectedVersion = err instanceof client.WrongExpectedVersionError;
|
|
|
|
test.ok(isWrongExpectedVersion, "Expected WrongExpectedVersionError, got " + err.constructor.name);
|
2016-03-14 01:38:42 +00:00
|
|
|
if (isWrongExpectedVersion) return test.done();
|
|
|
|
test.done(err);
|
2016-03-11 23:55:27 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
'Append To Stream Deleted': function(test) {
|
2017-01-29 02:04:58 +00:00
|
|
|
test.expect(1);
|
2016-03-11 23:55:27 +00:00
|
|
|
var self = this;
|
|
|
|
this.conn.deleteStream(this.testStreamName, client.expectedVersion.noStream, true)
|
|
|
|
.then(function() {
|
|
|
|
var event = client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'testEvent');
|
|
|
|
return self.conn.appendToStream(self.testStreamName, client.expectedVersion.any, event)
|
|
|
|
})
|
|
|
|
.then(function(result) {
|
2016-03-14 01:38:42 +00:00
|
|
|
test.fail("Append succeeded but should have failed.");
|
2016-03-11 23:55:27 +00:00
|
|
|
test.done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
var isStreamDeleted = err instanceof client.StreamDeletedError;
|
|
|
|
test.ok(isStreamDeleted, "Expected StreamDeletedError, got " + err.constructor.name);
|
2016-03-14 01:38:42 +00:00
|
|
|
if (isStreamDeleted) return test.done();
|
|
|
|
test.done(err);
|
2016-03-11 23:55:27 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
'Append To Stream Access Denied': function(test) {
|
2017-01-29 02:04:58 +00:00
|
|
|
test.expect(1);
|
2016-03-11 23:55:27 +00:00
|
|
|
var self = this;
|
|
|
|
var metadata = {$acl: {$w: "$admins"}};
|
|
|
|
this.conn.setStreamMetadataRaw(this.testStreamName, client.expectedVersion.noStream, metadata)
|
|
|
|
.then(function() {
|
|
|
|
var event = client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'testEvent');
|
|
|
|
return self.conn.appendToStream(self.testStreamName, client.expectedVersion.any, event)
|
|
|
|
})
|
|
|
|
.then(function(result) {
|
2016-03-14 01:38:42 +00:00
|
|
|
test.fail("Append succeeded but should have failed.");
|
2016-03-11 23:55:27 +00:00
|
|
|
test.done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
var isStreamDeleted = err instanceof client.AccessDeniedError;
|
|
|
|
test.ok(isStreamDeleted, "Expected AccessDeniedError, got " + err.constructor.name);
|
2016-03-14 01:38:42 +00:00
|
|
|
if (isStreamDeleted) return test.done();
|
|
|
|
test.done(err);
|
2016-03-11 23:55:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2016-03-14 01:38:42 +00:00
|
|
|
|
|
|
|
require('./common/base_test').init(module.exports);
|