node-eventstore-client/test/appendToStream_test.js

134 lines
5.5 KiB
JavaScript
Raw Permalink Normal View History

var uuid = require('uuid');
var client = require('../src/client');
2017-10-17 22:55:07 +00:00
var Long = require('long');
var ANY_VERSION = Long.fromNumber(client.expectedVersion.any);
var NOSTREAM_VERSION = Long.fromNumber(client.expectedVersion.noStream);
module.exports = {
'Append One Event To Stream Happy Path': function(test) {
test.expect(2);
var event = client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'testEvent');
this.conn.appendToStream(this.testStreamName, ANY_VERSION, event)
.then(function(result) {
2017-10-17 22:55:07 +00:00
test.areEqual("nextExpectedVersion", result.nextExpectedVersion, Long.fromNumber(0));
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) {
test.expect(2);
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, ANY_VERSION, events)
2017-03-22 08:12:53 +00:00
.then(function(result) {
2017-10-17 22:55:07 +00:00
test.areEqual("result.nextExpectedVersion", result.nextExpectedVersion, Long.fromNumber(expectedVersion));
2017-03-22 08:12:53 +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 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'));
else
events.push(client.createJsonEventData(uuid.v4(), {b: Math.random(), a: uuid.v4()}, null, 'otherEvent'));
}
this.conn.appendToStream(this.testStreamName, ANY_VERSION, events)
.then(function(result) {
2017-10-17 22:55:07 +00:00
test.areEqual("result.nextExpectedVersion", result.nextExpectedVersion, Long.fromNumber(expectedVersion));
test.ok(result.logPosition, "No log position in result.");
test.done();
})
.catch(function(err) {
test.done(err);
});
},
2017-03-29 07:59:20 +00:00
'Append Large event': function(test) {
test.expect(2);
const largeData = Buffer.alloc(3 * 1024 *1024, " ");
2017-03-29 07:59:20 +00:00
const event = client.createJsonEventData(uuid.v4(), {a: largeData.toString()}, null, 'largePayloadEvent');
this.conn.appendToStream(this.testStreamName, ANY_VERSION, event)
2017-03-29 07:59:20 +00:00
.then(function(result) {
2017-10-17 22:55:07 +00:00
test.areEqual("result.nextExpectedVersion", result.nextExpectedVersion, Long.fromNumber(0));
2017-03-29 07:59:20 +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) {
test.expect(1);
var event = client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'testEvent');
this.conn.appendToStream(this.testStreamName, Long.fromNumber(10), event)
.then(function(result) {
test.fail("Append succeeded but should have failed.");
test.done();
})
.catch(function(err) {
var isWrongExpectedVersion = err instanceof client.WrongExpectedVersionError;
test.ok(isWrongExpectedVersion, "Expected WrongExpectedVersionError, got " + err.constructor.name);
if (isWrongExpectedVersion) return test.done();
test.done(err);
});
},
'Append To Stream Deleted': function(test) {
test.expect(1);
var self = this;
this.conn.deleteStream(this.testStreamName, NOSTREAM_VERSION, true)
.then(function() {
var event = client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'testEvent');
return self.conn.appendToStream(self.testStreamName, ANY_VERSION, event)
})
.then(function(result) {
test.fail("Append succeeded but should have failed.");
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);
});
},
'Append To Stream Access Denied': function(test) {
test.expect(1);
var self = this;
var metadata = {$acl: {$w: "$admins"}};
this.conn.setStreamMetadataRaw(this.testStreamName, NOSTREAM_VERSION, metadata)
.then(function() {
var event = client.createJsonEventData(uuid.v4(), {a: Math.random(), b: uuid.v4()}, null, 'testEvent');
return self.conn.appendToStream(self.testStreamName, ANY_VERSION, event)
})
.then(function(result) {
test.fail("Append succeeded but should have failed.");
test.done();
})
.catch(function(err) {
var isStreamDeleted = err instanceof client.AccessDeniedError;
test.ok(isStreamDeleted, "Expected AccessDeniedError, got " + err.constructor.name);
if (isStreamDeleted) return test.done();
test.done(err);
});
}
};
require('./common/base_test').init(module.exports);