From 464ee1dd4cfc48268d499370fc64810c0314460f Mon Sep 17 00:00:00 2001 From: cse47122 Date: Fri, 25 Dec 2020 15:44:48 +0000 Subject: [PATCH] Upload files to 'test' --- test/fake-device-manager.js | 73 +++++++++++++++++++++++++++++++++ test/fake-device.js | 81 +++++++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 test/fake-device-manager.js create mode 100644 test/fake-device.js diff --git a/test/fake-device-manager.js b/test/fake-device-manager.js new file mode 100644 index 0000000..e6c3685 --- /dev/null +++ b/test/fake-device-manager.js @@ -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) + }) +}) diff --git a/test/fake-device.js b/test/fake-device.js new file mode 100644 index 0000000..8d01581 --- /dev/null +++ b/test/fake-device.js @@ -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) + }) +})