This repository has been archived by the owner on Sep 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
tests.js
137 lines (120 loc) · 4.32 KB
/
tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var _ = require("lodash");
var expect = require('chai').expect;
var firehoser = require('./firehoser');
class MockAWSFirehose {
constructor(firehoseErrors,failedPut){
this.firehoseErrors = firehoseErrors || false;
this.failedPut = failedPut || false;
this.attempt = 0;
}
putRecordBatch(batch, cb){
const err = this.firehoseErrors?new Error('OH NOES! Firehose Error!'):null;
this.attempt++;
// console.log('attempt!:',this.attempt);
let reqresp = batch.Records.slice();
let failedCount = 0;
if (this.failedPut && this.attempt<2){
reqresp = _.map(reqresp,(r)=>{ return {ErrorMessage : 'Life is terrible', ErrorCode : 403 }; });
failedCount = reqresp.length;
}
cb(err, {
FailedPutCount : failedCount,
RequestResponses : reqresp
});
}
}
describe("DeliveryStream", function(){
describe("instantiation", function(){
it("should only require 1 argument", function(){
let ds = new firehoser.DeliveryStream("the_ds");
expect(ds).to.be.an.instanceof(firehoser.DeliveryStream);
});
});
describe("DeliveryStream", function(){
let ds = new firehoser.DeliveryStream("the_ds", null, null, 3000, new MockAWSFirehose());
let jds = new firehoser.JSONDeliveryStream(
"the_json_ds",
null,
{
"type": "object",
"properties": {
"firstName": {
"type": "string",
"maxLength": 3
},
},
"required": ["firstName"]
},
3000,
new MockAWSFirehose()
);
it("should accept one record", function(){
return ds.putRecord("this is the record");
});
it("should accept multiple records", function(){
return ds.putRecords(["record1", "record2"]);
});
it("should accept more than 500 records", function(){
return ds.putRecords(_.map(_.range(600), (i) => {`record${i}`}));
});
it("should throw an error when a record doesn't match the schema", function(){
return jds.putRecord({
"firstName": 3
}).catch((errors)=>{
let err = errors[0];
expect(err.type).to.equal("schema");
expect(err.details).to.exist;
expect(err.originalRecord).to.exist;
})
});
it("should not throw an error when the record matches the schema", function(){
return jds.putRecord({
"firstName": "Don"
}).catch((errors)=>{
expect(errors).to.be.undefined;
expect(errors).to.be.empty;
})
});
it("should handle firehose errors", function(){
let fherr = new firehoser.DeliveryStream("the_ds", null, null, 1000, new MockAWSFirehose(true));
return fherr.putRecord({ "firstName": "Don" }).catch((errors)=>{
expect(errors).to.not.be.empty;
expect(errors.length).to.equal(1);
});
});
it("should retry failed PUTS", function(){
let puterr = new firehoser.DeliveryStream("the_ds", null, null, 100, new MockAWSFirehose(false,true));
return puterr.putRecord({ "firstName": "Don" });
});
});
})
describe('pickData', function(){
let leftovers = [{
type: "firehose",
description: "out of service",
details: {
ErrorCode: 500,
ErrorMessage: "AWS is currently down",
},
originalRecord: {
Data: {log: true, data: { id: 1}}
}
},
{
type: "firehose",
description: "out of service",
details: {
ErrorCode: 500,
ErrorMessage: "AWS is currently down",
},
originalRecord: {
Data: {log: true, data: { id: 2}}
}
}];
it("should return the object with Data attributes", function(){
let resolvedLeftoverData = _.map(leftovers, firehoser.pickData);
expect(resolvedLeftoverData.length).to.equal(2);
expect(resolvedLeftoverData[0]).to.have.property('Data');
expect(resolvedLeftoverData[1]).to.have.property('Data');
});
})