diff options
author | BrainDoctor <github.account@chrigel.net> | 2017-07-18 16:16:11 +0200 |
---|---|---|
committer | BrainDoctor <github.account@chrigel.net> | 2017-07-18 21:14:52 +0200 |
commit | 1b54db67ba71dd2c65dd31d49cb8798ac4cc4d89 (patch) | |
tree | c6febfe64b0b26aec440a160fe2712666cd979d2 /tests | |
parent | 64f6191801edbdd2cf3d15178a8fdef879cd574c (diff) |
more unit tests for fronius plugin
Diffstat (limited to 'tests')
-rw-r--r-- | tests/node.d/fronius.chart.spec.js | 102 | ||||
-rw-r--r-- | tests/node.d/fronius.parse.spec.js | 67 | ||||
-rw-r--r-- | tests/node.d/fronius.spec.js | 58 | ||||
-rw-r--r-- | tests/node.d/fronius.validation.spec.js | 158 |
4 files changed, 327 insertions, 58 deletions
diff --git a/tests/node.d/fronius.chart.spec.js b/tests/node.d/fronius.chart.spec.js new file mode 100644 index 0000000000..c9c75f9c3c --- /dev/null +++ b/tests/node.d/fronius.chart.spec.js @@ -0,0 +1,102 @@ +"use strict"; + +var netdata = require("../../node.d/node_modules/netdata"); +// remember: subject will be a singleton! +var subject = require("../../node.d/fronius.node"); + +var service = netdata.service({ + name: "fronius", + module: this +}); + +describe("fronius chart creation", function () { + + beforeAll(function () { + // change this to enable debug log + netdata.options.DEBUG = false; + }); + + it("should return a basic chart dimension", function () { + var result = subject.createBasicDimension("id", "name", 2); + + expect(result.divisor).toBe(2); + expect(result.id).toBe("id"); + expect(result.algorithm).toEqual(netdata.chartAlgorithms.absolute); + expect(result.multiplier).toBe(1); + }); + + it("should return the power chart definition", function () { + var id = "power"; + var result = subject.getSitePowerChart(service, id); + + expect(result.id).toBe(id); + expect(result.units).toBe("W"); + expect(result.type).toBe(netdata.chartTypes.area); + expect(result.family).toBe("power"); + expect(result.context).toBe("fronius.power"); + expect(result.dimensions[subject.powerGridId].name).toBe("Grid"); + expect(result.dimensions[subject.powerPvId].name).toBe("Photovoltaics"); + expect(result.dimensions[subject.powerAccuId].name).toBe("Accumulator"); + expect(Object.keys(result.dimensions).length).toBe(3); + }); + + it("should return the consumption chart definition", function () { + var id = "Load"; + var result = subject.getSiteConsumptionChart(service, id); + + expect(result.id).toBe(id); + expect(result.units).toBe("W"); + expect(result.type).toBe(netdata.chartTypes.area); + expect(result.family).toBe("consumption"); + expect(result.context).toBe("fronius.consumption"); + expect(Object.keys(result.dimensions).length).toBe(1); + expect(result.dimensions[subject.consumptionLoadId].name).toBe("Load"); + }); + + it("should return the autonomy chart definition", function () { + var id = "Autonomy"; + var result = subject.getSiteAutonomyChart(service, id); + + expect(result.id).toBe(id); + expect(result.units).toBe("%"); + expect(result.type).toBe(netdata.chartTypes.area); + expect(result.family).toBe("autonomy"); + expect(result.context).toBe("fronius.autonomy"); + expect(Object.keys(result.dimensions).length).toBe(2); + expect(result.dimensions[subject.autonomyId].name).toBe("Autonomy"); + expect(result.dimensions[subject.consumptionSelfId].name).toBe("Self Consumption"); + }); + + it("should return the energy today chart definition", function () { + var id = "Energy today"; + var result = subject.getSiteEnergyTodayChart(service, id); + + expect(result.id).toBe(id); + expect(result.units).toBe("kWh"); + expect(result.type).toBe(netdata.chartTypes.area); + expect(result.family).toBe("energy"); + expect(result.context).toBe("fronius.energy.today"); + expect(Object.keys(result.dimensions).length).toBe(1); + expect(result.dimensions[subject.energyTodayId].name).toBe("Today"); + }); + + it("should return the energy year chart definition", function () { + var id = "Energy year"; + var result = subject.getSiteEnergyYearChart(service, id); + + expect(result.id).toBe(id); + expect(result.units).toBe("kWh"); + expect(result.type).toBe(netdata.chartTypes.area); + expect(result.family).toBe("energy"); + expect(result.context).toBe("fronius.energy.year"); + expect(Object.keys(result.dimensions).length).toBe(1); + expect(result.dimensions[subject.energyYearId].name).toBe("Year"); + }); + + it("should return the same chart definition on second call for lazy loading", function () { + var first = subject.getSitePowerChart(service, "id"); + var second = subject.getSitePowerChart(service, "id"); + + expect(first).toBe(second); + }); +});
\ No newline at end of file diff --git a/tests/node.d/fronius.parse.spec.js b/tests/node.d/fronius.parse.spec.js new file mode 100644 index 0000000000..a6f1cee305 --- /dev/null +++ b/tests/node.d/fronius.parse.spec.js @@ -0,0 +1,67 @@ +"use strict"; + +var netdata = require("../../node.d/node_modules/netdata"); +// remember: subject will be a singleton! +var subject = require("../../node.d/fronius.node"); + +var service = netdata.service({ + name: "fronius", + module: this +}); + +describe("fronius data parsing", function () { + + var fakeResponse = { + "Head" : { + "RequestArguments" : {}, + "Status" : { + "Code" : 0, + "Reason" : "", + "UserMessage" : "" + }, + "Timestamp" : "2017-07-17T16:01:04+02:00" + }, + "Body" : { + "Data" : { + "Site" : { + "Mode" : "meter", + "P_Grid" : -3430.729923, + "P_Load" : -910.270077, + "P_Akku" : null, + "P_PV" : 4341, + "rel_SelfConsumption" : 20.969133, + "rel_Autonomy" : 100, + "E_Day" : 57230, + "E_Year" : 6425915.5, + "E_Total" : 15388710, + "Meter_Location" : "grid" + }, + "Inverters" : { + "1" : { + "DT" : 123, + "P" : 4341, + "E_Day" : 57230, + "E_Year" : 6425915.5, + "E_Total" : 15388710 + } + } + } + } + }; + + beforeAll(function () { + // change this to enable debug log + netdata.options.DEBUG = false; + }); + + it("should return a parsed value", function () { + // arrange + netdata.send = jasmine.createSpy("send"); + // act + subject.processResponse(service, fakeResponse); + var result = netdata.send.calls.argsFor(0)[0]; + // assert + expect(result).toContain("SET p_grid = -3431"); + }); + +}); diff --git a/tests/node.d/fronius.spec.js b/tests/node.d/fronius.spec.js deleted file mode 100644 index 11581552a0..0000000000 --- a/tests/node.d/fronius.spec.js +++ /dev/null @@ -1,58 +0,0 @@ -"use strict"; -// delete these comments if not needed anymore. - -var netdata = require("../../node.d/node_modules/netdata"); -var fronius = require("../../node.d/fronius.node"); - -describe("fronius chart creation", function () { - - beforeAll(function () { - // change this to enable debug log - netdata.options.DEBUG = false; - }); - - it("should return a basic chart definition", function () { - // act - var result = fronius.createBasicDimension("id", "name", 2); - // assert - expect(result.divisor).toBe(2); - expect(result.id).toBe("id"); - expect(result.algorithm).toEqual("absolute"); - expect(result.multiplier).toBe(1); - }); - - it("will fail", function () { - netdata.debug("test"); - - throw new Error("demonstrate failure of unit test runner"); - }); - -}); - -describe("fronius data parsing", function () { - - var service = netdata.service({ - name: "fronius", - module: this - }); - - // this is a faked JSON response from the server. - // Used with freeformatter.com/json-escape.html to escape the json and turn it into a string. - var fakeResponse = "{\r\n\t\"Head\" : {\r\n\t\t\"RequestArguments\" : {},\r\n\t\t\"Status\" : {\r\n\t\t\t\"Code\" : 0,\r\n\t\t\t\"Reason\" : \"\",\r\n\t\t\t\"UserMessage\" : \"\"\r\n\t\t},\r\n\t\t\"Timestamp\" : \"2017-07-17T16:01:04+02:00\"\r\n\t},\r\n\t\"Body\" : {\r\n\t\t\"Data\" : {\r\n\t\t\t\"Site\" : {\r\n\t\t\t\t\"Mode\" : \"meter\",\r\n\t\t\t\t\"P_Grid\" : -3430.729923,\r\n\t\t\t\t\"P_Load\" : -910.270077,\r\n\t\t\t\t\"P_Akku\" : null,\r\n\t\t\t\t\"P_PV\" : 4341,\r\n\t\t\t\t\"rel_SelfConsumption\" : 20.969133,\r\n\t\t\t\t\"rel_Autonomy\" : 100,\r\n\t\t\t\t\"E_Day\" : 57230,\r\n\t\t\t\t\"E_Year\" : 6425915.5,\r\n\t\t\t\t\"E_Total\" : 15388710,\r\n\t\t\t\t\"Meter_Location\" : \"grid\"\r\n\t\t\t},\r\n\t\t\t\"Inverters\" : {\r\n\t\t\t\t\"1\" : {\r\n\t\t\t\t\t\"DT\" : 123,\r\n\t\t\t\t\t\"P\" : 4341,\r\n\t\t\t\t\t\"E_Day\" : 57230,\r\n\t\t\t\t\t\"E_Year\" : 6425915.5,\r\n\t\t\t\t\t\"E_Total\" : 15388710\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}" - - beforeAll(function () { - // change this to enable debug log - netdata.options.DEBUG = false; - }); - - it("should return a parsed value", function () { - // arrange - netdata.send = jasmine.createSpy("send"); - // act - fronius.processResponse(service, fakeResponse); - var result = netdata.send.calls.argsFor(0)[0]; - // assert - expect(result).toContain("SET p_grid = -3431"); - }); - -}); diff --git a/tests/node.d/fronius.validation.spec.js b/tests/node.d/fronius.validation.spec.js new file mode 100644 index 0000000000..06241f3ceb --- /dev/null +++ b/tests/node.d/fronius.validation.spec.js @@ -0,0 +1,158 @@ +"use strict"; + +var netdata = require("../../node.d/node_modules/netdata"); +// remember: subject will be a singleton! +var subject = require("../../node.d/fronius.node"); + +var service = netdata.service({ + name: "fronius", + module: this +}); + +describe("fronius response validation", function () { + + // this is a faked JSON response from the server. + // Used with freeformatter.com/json-escape.html to escape the json and turn it into a string. + var fakeResponse = "{\r\n\t\"Head\" : {\r\n\t\t\"RequestArguments\" : {},\r\n\t\t\"Status\" " + + ": {\r\n\t\t\t\"Code\" : 0,\r\n\t\t\t\"Reason\" : \"\",\r\n\t\t\t\"UserMessage\" : " + + "\"\"\r\n\t\t},\r\n\t\t\"Timestamp\" : \"2017-07-17T16:01:04+02:00\"\r\n\t},\r\n\t\"Body\" : " + + "{\r\n\t\t\"Data\" : {\r\n\t\t\t\"Site\" : {\r\n\t\t\t\t\"Mode\" : \"meter\",\r\n\t\t\t\t\"P_Grid\" " + + ": -3430.729923,\r\n\t\t\t\t\"P_Load\" : -910.270077,\r\n\t\t\t\t\"P_Akku\" : " + + "null,\r\n\t\t\t\t\"P_PV\" : 4341,\r\n\t\t\t\t\"rel_SelfConsumption\" : " + + "20.969133,\r\n\t\t\t\t\"rel_Autonomy\" : 100,\r\n\t\t\t\t\"E_Day\" : 57230,\r\n\t\t\t\t\"E_Year\" " + + ": 6425915.5,\r\n\t\t\t\t\"E_Total\" : 15388710,\r\n\t\t\t\t\"Meter_Location\" : " + + "\"grid\"\r\n\t\t\t},\r\n\t\t\t\"Inverters\" : {\r\n\t\t\t\t\"1\" : {\r\n\t\t\t\t\t\"DT\" : " + + "123,\r\n\t\t\t\t\t\"P\" : 4341,\r\n\t\t\t\t\t\"E_Day\" : 57230,\r\n\t\t\t\t\t\"E_Year\" : " + + "6425915.5,\r\n\t\t\t\t\t\"E_Total\" : 15388710\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}"; + + it("should do nothing if response is null", function () { + netdata.send = jasmine.createSpy("send"); + + subject.processResponse(service, null); + var result = netdata.send.calls.count(); + + expect(result).toBe(0); + }); + + it("should return null if response is null", function () { + var result = subject.parseResponse(null); + + expect(result).toBeNull(); + }); + + it("should return true if response is valid", function () { + var result = subject.isResponseValid({ + "Body": { + "Data": { + "Site": { + "Mode": "meter" + }, + "Inverters": { + "1": {} + } + } + } + }); + + expect(result).toBeTruthy(); + }); + + it("should return false if response is missing data", function () { + var result = subject.isResponseValid({ + "Body": {} + }); + + expect(result).toBeFalsy(); + }); + + it("should return false if response is missing inverter", function () { + var result = subject.isResponseValid({ + "Body": { + "Data": { + "Site": {} + } + } + }); + + expect(result).toBeFalsy(); + }); + + it("should return false if response is missing inverter", function () { + var result = subject.isResponseValid({ + "Body": { + "Data": { + "Inverters": {} + } + } + }); + + expect(result).toBeFalsy(); + }); + +}); + +describe("fronius configuration validation", function () { + + it("should return 0 if there are no servers configured", function () { + var result = subject.configure({}); + + expect(result).toBe(0); + }); + + it("should return 0 if the servers array is empty", function () { + var result = subject.configure({ + "servers": [] + }); + + expect(result).toBe(0); + }); + + it("should return 0 if there is one server configured incorrectly", function () { + var result = subject.configure({ + "servers": [{}] + }); + + expect(result).toBe(0); + }); + + it("should return 1 if there is one server configured", function () { + subject.serviceExecute = jasmine.createSpy("serviceExecute"); + var name = "solar1"; + var result = subject.configure({ + "servers": [{ + "name": name, + "api_path": "/api/", + "hostname": "solar1.local" + }] + }); + + expect(result).toBe(1); + expect(subject.serviceExecute).toHaveBeenCalledWith(name, "solar1.local/api/", 5); + }); + + it("should return 2 if there are two servers configured", function () { + subject.serviceExecute = jasmine.createSpy("serviceExecute"); + var name1 = "solar 1"; + var name2 = "solar 2"; + var result = subject.configure({ + "servers": [ + { + "name": name1, + "api_path": "/", + "hostname": "solar1.local" + }, + { + "name": name2, + "api_path": "/", + "hostname": "solar2.local", + "update_every": 3 + } + ] + }); + + expect(result).toBe(2); + expect(subject.serviceExecute).toHaveBeenCalledWith(name1, "solar1.local/", 5); + expect(subject.serviceExecute).toHaveBeenCalledWith(name2, "solar2.local/", 3); + }); + +});
\ No newline at end of file |