-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.spec.js
114 lines (100 loc) · 2.54 KB
/
index.spec.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
const mock = require('mock-require')
const chai = require('chai')
const sinonChai = require('sinon-chai')
const sinon = require('sinon')
const moment = require('moment')
const should = chai.should()
const expect = chai.expect
chai.use(sinonChai)
describe('Influx', function() {
function generateData(size = 100) {
const data = []
const now = moment().subtract(1, 'hour')
for (let i = 0; i < size; i++) {
now.add(1, 'second')
data.push({
measurement: 'measurement',
tags: { foo: 'bar' },
fields: { baz: Math.random() * 100 },
timestamp: now.toISOString(),
})
}
return data
}
// mock influx
const influxStub = {
FieldType: { FLOAT: 1 },
InfluxDB: sinon.stub().returns({
getDatabaseNames: sinon.stub().resolves([]),
createDatabase: sinon.stub().resolves(),
writePoints: sinon.stub().resolves(),
})
}
mock('influx', influxStub)
const influx = require('./index')
it('should initialize', () => {
influx.prepare()
influx.load()
influx.getCapabilities()
influx.start()
})
it('should deinitialize', () => {
influx.stop()
influx.unload()
influx.cleanup()
})
it('should handle lifecycle methods', () => {
influx.start()
influx.suspend()
})
it('should process influx array message', async () => {
const contextStub = {
topic: 'influx',
message: generateData(),
appId: 'abc',
gatewayId: 'def',
}
let output = await influx.process(contextStub)
output.should.be.an('array')
})
it('should process influx object message', async () => {
const contextStub = {
topic: 'influx',
message: generateData()[0],
appId: 'abc',
gatewayId: 'def',
}
let output = await influx.process(contextStub)
output.should.be.an('object')
})
it('should process influx json array message', async () => {
const contextStub = {
topic: 'influx',
message: JSON.stringify(generateData()),
appId: 'abc',
gatewayId: 'def',
}
let output = await influx.process(contextStub)
output.should.be.a('string')
})
it('should process influx json object message', async () => {
const contextStub = {
topic: 'influx',
message: JSON.stringify(generateData()[0]),
appId: 'abc',
gatewayId: 'def',
}
let output = await influx.process(contextStub)
output.should.be.a('string')
})
it('should process influx json buffer message', async () => {
const contextStub = {
topic: 'influx',
message: Buffer.from(JSON.stringify(generateData())),
appId: 'abc',
gatewayId: 'def',
}
let output = await influx.process(contextStub)
output.should.be.instanceof(Buffer)
})
})