2016-03-15 00:55:35 +00:00
|
|
|
var uuid = require('uuid');
|
|
|
|
var client = require('../src/client');
|
2017-10-17 22:55:07 +00:00
|
|
|
var Long = require('long');
|
2016-03-15 00:55:35 +00:00
|
|
|
|
|
|
|
const streamSize = 100;
|
|
|
|
|
2017-11-08 20:45:22 +00:00
|
|
|
var NOSTREAM_VERSION = Long.fromNumber(client.expectedVersion.noStream);
|
|
|
|
|
2016-03-15 00:55:35 +00:00
|
|
|
module.exports = {
|
|
|
|
setUp: function(cb) {
|
|
|
|
this.eventsData = [];
|
|
|
|
for(var i = 0; i < streamSize; i++)
|
|
|
|
this.eventsData.push(client.createJsonEventData(uuid.v4(), {a: uuid.v4(), b: Math.random()}, null, 'anEvent'));
|
2017-11-08 20:45:22 +00:00
|
|
|
this.conn.appendToStream(this.testStreamName, NOSTREAM_VERSION, this.eventsData)
|
2016-03-15 00:55:35 +00:00
|
|
|
.then(function() {
|
|
|
|
cb();
|
|
|
|
})
|
|
|
|
.catch(cb);
|
|
|
|
},
|
|
|
|
'Read Stream Events Backward Happy Path': function(test) {
|
2017-01-29 02:04:58 +00:00
|
|
|
test.expect(7 + (streamSize * 6));
|
2016-03-15 00:55:35 +00:00
|
|
|
var self = this;
|
2017-11-08 20:45:22 +00:00
|
|
|
this.conn.readStreamEventsBackward(this.testStreamName, Long.fromNumber(streamSize-1), streamSize)
|
2016-03-15 00:55:35 +00:00
|
|
|
.then(function(slice) {
|
|
|
|
test.areEqual('slice.status', slice.status, client.eventReadStatus.Success);
|
|
|
|
test.areEqual('slice.stream', slice.stream, self.testStreamName);
|
2017-10-17 22:55:07 +00:00
|
|
|
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, Long.fromNumber(streamSize-1));
|
2016-03-15 00:55:35 +00:00
|
|
|
test.areEqual('slice.readDirection', slice.readDirection, 'backward');
|
2017-10-17 22:55:07 +00:00
|
|
|
test.areEqual('slice.nextEventNumber', slice.nextEventNumber, Long.fromNumber(-1));
|
|
|
|
test.areEqual('slice.lastEventNumber', slice.lastEventNumber, Long.fromNumber(streamSize-1));
|
2016-03-15 00:55:35 +00:00
|
|
|
test.areEqual('slice.isEndOfStream', slice.isEndOfStream, true);
|
|
|
|
for(var i = 0; i < streamSize; i++) {
|
|
|
|
var reverseIndex = streamSize - i - 1;
|
|
|
|
test.eventEqualEventData('slice.events[' + i + ']', slice.events[i], self.eventsData[reverseIndex]);
|
2017-10-17 22:55:07 +00:00
|
|
|
test.areEqual('slice.events[' + i + '].originalEventNumber', slice.events[i].originalEventNumber, Long.fromNumber(reverseIndex));
|
2016-03-15 00:55:35 +00:00
|
|
|
}
|
|
|
|
test.done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
test.done(err);
|
|
|
|
})
|
|
|
|
},
|
|
|
|
'Read Stream Events Backward With Non-Existing Stream': function(test) {
|
2017-01-29 02:04:58 +00:00
|
|
|
test.expect(4);
|
2016-03-15 00:55:35 +00:00
|
|
|
var anotherStream = 'test' + uuid.v4();
|
2017-11-08 20:45:22 +00:00
|
|
|
this.conn.readStreamEventsBackward(anotherStream, Long.fromNumber(streamSize-1), streamSize)
|
2016-03-15 00:55:35 +00:00
|
|
|
.then(function(slice) {
|
|
|
|
test.areEqual('slice.status', slice.status, client.sliceReadStatus.StreamNotFound);
|
|
|
|
test.areEqual('slice.stream', slice.stream, anotherStream);
|
2017-10-17 22:55:07 +00:00
|
|
|
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, Long.fromNumber(streamSize-1));
|
2016-03-15 00:55:35 +00:00
|
|
|
test.areEqual('slice.events.length', slice.events.length, 0);
|
|
|
|
test.done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
test.done(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
'Read Stream Events Backward With Deleted Stream': function(test) {
|
2017-01-29 02:04:58 +00:00
|
|
|
test.expect(4);
|
2016-03-15 00:55:35 +00:00
|
|
|
var self = this;
|
2017-11-08 20:45:22 +00:00
|
|
|
this.conn.deleteStream(this.testStreamName, Long.fromNumber(streamSize-1), true)
|
2016-03-15 00:55:35 +00:00
|
|
|
.then(function() {
|
2017-11-08 20:45:22 +00:00
|
|
|
return self.conn.readStreamEventsBackward(self.testStreamName, Long.fromNumber(streamSize-1), streamSize)
|
2016-03-15 00:55:35 +00:00
|
|
|
})
|
|
|
|
.then(function(slice) {
|
|
|
|
test.areEqual('slice.status', slice.status, client.eventReadStatus.StreamDeleted);
|
|
|
|
test.areEqual('slice.stream', slice.stream, self.testStreamName);
|
2017-10-17 22:55:07 +00:00
|
|
|
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, Long.fromNumber(streamSize-1));
|
2016-03-15 00:55:35 +00:00
|
|
|
test.areEqual('slice.events.length', slice.events.length, 0);
|
|
|
|
test.done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
test.done(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
'Read Stream Events Backward With Inexisting Version': function(test) {
|
2017-01-29 02:04:58 +00:00
|
|
|
test.expect(4);
|
2016-03-15 00:55:35 +00:00
|
|
|
var self = this;
|
2017-11-08 20:45:22 +00:00
|
|
|
return self.conn.readStreamEventsBackward(self.testStreamName, Long.fromNumber(streamSize * 2), streamSize)
|
2016-03-15 00:55:35 +00:00
|
|
|
.then(function(slice) {
|
|
|
|
test.areEqual('slice.status', slice.status, client.eventReadStatus.Success);
|
|
|
|
test.areEqual('slice.stream', slice.stream, self.testStreamName);
|
2017-10-17 22:55:07 +00:00
|
|
|
test.areEqual('slice.fromEventNumber', slice.fromEventNumber, Long.fromNumber(streamSize*2));
|
2016-03-15 00:55:35 +00:00
|
|
|
test.areEqual('slice.events.length', slice.events.length, 0);
|
|
|
|
test.done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
test.done(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
'Read Stream Events Backward With No Access': function(test) {
|
2017-01-29 02:04:58 +00:00
|
|
|
test.expect(1);
|
2016-03-15 00:55:35 +00:00
|
|
|
var self = this;
|
|
|
|
var metadata = {$acl: {$r: '$admins'}};
|
2017-11-08 20:45:22 +00:00
|
|
|
this.conn.setStreamMetadataRaw(self.testStreamName, NOSTREAM_VERSION, metadata)
|
2016-03-15 00:55:35 +00:00
|
|
|
.then(function(){
|
2017-11-08 20:45:22 +00:00
|
|
|
return self.conn.readStreamEventsBackward(self.testStreamName, Long.fromNumber(streamSize-1), streamSize);
|
2016-03-15 00:55:35 +00:00
|
|
|
})
|
|
|
|
.then(function(slice) {
|
|
|
|
test.fail("readStreamEventsBackward succeeded but should have failed.");
|
|
|
|
test.done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
var isAccessDenied = err instanceof client.AccessDeniedError;
|
|
|
|
test.ok(isAccessDenied, "readStreamEventsBackward should have failed with AccessDeniedError, got " + err.constructor.name);
|
|
|
|
if (isAccessDenied) return test.done();
|
|
|
|
test.done(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
require('./common/base_test').init(module.exports);
|