cse47122
4 years ago
2 changed files with 154 additions and 0 deletions
@ -0,0 +1,73 @@ |
|||||
|
/* globals describe it */ |
||||
|
|
||||
|
const expect = require('chai').expect |
||||
|
|
||||
|
const FakeDeviceManager = require('../src/fake-device-manager') |
||||
|
const measurements = { |
||||
|
usage: {type: 'integer', min: 0, max: 100}, |
||||
|
voltage: {type: 'integer', min: 0, max: 15}, |
||||
|
temperature: {type: 'integer', min: -20, max: 60} |
||||
|
} |
||||
|
|
||||
|
// create fake influx connection
|
||||
|
// let connectionWriteData = false
|
||||
|
const connection = { |
||||
|
send: (deviceId, data) => { |
||||
|
return new Promise((resolve, reject) => { |
||||
|
// connectionWriteData = {deviceId, data}
|
||||
|
resolve() |
||||
|
}) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
describe('FakeDeviceManager', function () { |
||||
|
let fakeDeviceManager = null |
||||
|
|
||||
|
it('should throw error if no params are passed', function () { |
||||
|
expect(FakeDeviceManager).to.throw(Error) |
||||
|
}) |
||||
|
|
||||
|
it('shold throw missing connection param', function () { |
||||
|
expect(() => FakeDeviceManager({})).to.throw('Missing connection param') |
||||
|
}) |
||||
|
|
||||
|
it('shold throw missing measurements param', function () { |
||||
|
expect(() => FakeDeviceManager({ |
||||
|
connection |
||||
|
})).to.throw('Missing measurements param') |
||||
|
}) |
||||
|
|
||||
|
it('should create instance', function () { |
||||
|
fakeDeviceManager = FakeDeviceManager({ |
||||
|
connection, |
||||
|
measurements, |
||||
|
sendInterval: 500 |
||||
|
}) |
||||
|
expect(fakeDeviceManager).to.have.property('start') |
||||
|
}) |
||||
|
|
||||
|
it('should spawn device instances', function (done) { |
||||
|
fakeDeviceManager.start() |
||||
|
setTimeout(() => { |
||||
|
let devices = fakeDeviceManager.getDevices() |
||||
|
done(!devices.length) |
||||
|
}, 500) |
||||
|
}) |
||||
|
|
||||
|
it('should collect device stats', function (done) { |
||||
|
setTimeout(() => { |
||||
|
let stats = fakeDeviceManager.getStats() |
||||
|
if (!stats) return done('No data') |
||||
|
if ( |
||||
|
stats.devices && |
||||
|
stats.measurements && |
||||
|
stats.writes && |
||||
|
stats.size |
||||
|
) { |
||||
|
done() |
||||
|
} else { |
||||
|
done('Invalid write data format') |
||||
|
} |
||||
|
}, 1000) |
||||
|
}) |
||||
|
}) |
@ -0,0 +1,81 @@ |
|||||
|
/* globals describe it */ |
||||
|
|
||||
|
const expect = require('chai').expect |
||||
|
|
||||
|
const FakeDevice = require('../src/fake-device') |
||||
|
const measurements = { |
||||
|
usage: {type: 'integer', min: 0, max: 100}, |
||||
|
voltage: {type: 'integer', min: 0, max: 15}, |
||||
|
temperature: {type: 'integer', min: -20, max: 60} |
||||
|
} |
||||
|
|
||||
|
// create fake influx connection
|
||||
|
let connectionWriteData = false |
||||
|
const connection = { |
||||
|
send: (deviceId, data) => { |
||||
|
return new Promise((resolve, reject) => { |
||||
|
connectionWriteData = {deviceId, data} |
||||
|
resolve() |
||||
|
}) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
describe('FakeDevice', function () { |
||||
|
let fakeDevice = null |
||||
|
|
||||
|
it('should throw error if no params are passed', function () { |
||||
|
expect(FakeDevice).to.throw(Error) |
||||
|
}) |
||||
|
|
||||
|
it('shold throw missing deviceId param', function () { |
||||
|
expect(() => FakeDevice({})).to.throw('Missing deviceId param') |
||||
|
}) |
||||
|
|
||||
|
it('shold throw missing connection param', function () { |
||||
|
expect(() => FakeDevice({ |
||||
|
deviceId: 1 |
||||
|
})).to.throw('Missing connection param') |
||||
|
}) |
||||
|
|
||||
|
it('shold throw missing measurements param', function () { |
||||
|
expect(() => FakeDevice({ |
||||
|
deviceId: 1, |
||||
|
connection |
||||
|
})).to.throw('Missing measurements param') |
||||
|
}) |
||||
|
|
||||
|
it('should create instance', function () { |
||||
|
fakeDevice = FakeDevice({ |
||||
|
deviceId: 1, |
||||
|
connection, |
||||
|
sendInterval: 1000, |
||||
|
measurements |
||||
|
}) |
||||
|
expect(fakeDevice).to.have.property('start') |
||||
|
}) |
||||
|
|
||||
|
it('should generate data', function (done) { |
||||
|
fakeDevice.start() |
||||
|
setTimeout(() => { |
||||
|
let data = fakeDevice.getData() |
||||
|
done(!data.length) |
||||
|
}, 500) |
||||
|
}) |
||||
|
|
||||
|
it('should send data to influx', function (done) { |
||||
|
setTimeout(() => { |
||||
|
if (!connectionWriteData) return done('No data') |
||||
|
if (!connectionWriteData.deviceId) return done('Invalid write data - missing deviceId') |
||||
|
if ( |
||||
|
connectionWriteData.data[0].usage && |
||||
|
connectionWriteData.data[0].voltage && |
||||
|
connectionWriteData.data[0].temperature && |
||||
|
connectionWriteData.data[0].timestamp |
||||
|
) { |
||||
|
done() |
||||
|
} else { |
||||
|
done('Invalid write data format') |
||||
|
} |
||||
|
}, 1000) |
||||
|
}) |
||||
|
}) |
Loading…
Reference in new issue