Skip to content

Commit

Permalink
Add unit-tests for P1Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgelling committed Jan 17, 2024
1 parent 3968224 commit 08523fc
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 7 deletions.
138 changes: 134 additions & 4 deletions __tests__/P1Parser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,139 @@
import { P1Parser } from '../src';
import * as fs from 'fs';
import { DateTime } from 'luxon';

const parser = new P1Parser({ timezone: 'Europe/Amsterdam', withUnits: false });
describe('Parser validates the given options', () => {
it('throws when the given timezone is invalid', () => {
expect(() => {
new P1Parser({ timezone: 'Moon/DarkSide' });
}).toThrow('Invalid timezone: "Moon/DarkSide"');
});

it('does not throw when the given timezone is valid', () => {
expect(() => {
new P1Parser({ timezone: 'Europe/Amsterdam' });
}).not.toThrow();
});
});

describe('Parser parses the different kinds of telegrams', () => {
const parser = new P1Parser({ timezone: 'Europe/Amsterdam' });
const specifications = [
'dsmr4',
'dsmr5',
'esmr5',
];

for (const specification of specifications) {
it(`parses a ${specification} message correctly`, () => {
const result = parser.parse(
fs.readFileSync(__dirname + `/__fixtures__/p1-data/${specification}.txt`).subarray(1, -7),
);

expect(JSON.parse(JSON.stringify(result))).toEqual(JSON.parse(
fs.readFileSync(__dirname + `/__fixtures__/p1-data/${specification}.json`, 'utf-8'),
));
});
}
});

describe('Parser respects the given option', () => {
it('returns a Luxon DateTime object when `asLuxon` is true', () => {
const parser = new P1Parser({
timezone: 'Europe/Amsterdam',
asLuxon: true,
});

const result = parser.parse(
fs.readFileSync(__dirname + '/__fixtures__/p1-data/dsmr5.txt').subarray(1, -7),
);

expect(result.transmitted_at).toBeInstanceOf(DateTime);
expect(result.transmitted_at).not.toBeInstanceOf(Date);

if (typeof result.gas !== 'undefined') {
expect(result.gas.measured_at).toBeInstanceOf(DateTime);
expect(result.gas.measured_at).not.toBeInstanceOf(Date);
}
});

it('returns a simple JS Date object as default', () => {
const parser = new P1Parser({
timezone: 'Europe/Amsterdam',
});

describe('Thing', () => {
it('does thing', () =>{
expect(true).toBeTruthy();
const result = parser.parse(
fs.readFileSync(__dirname + '/__fixtures__/p1-data/dsmr5.txt').subarray(1, -7),
);

expect(result.transmitted_at).toBeInstanceOf(Date);
expect(result.transmitted_at).not.toBeInstanceOf(DateTime);

if (typeof result.gas !== 'undefined' && typeof result.gas.measured_at !== 'undefined') {
expect(result.gas.measured_at).toBeInstanceOf(Date);
expect(result.gas.measured_at).not.toBeInstanceOf(DateTime);
}
});

it('returns a the value with its unit, if `withUnits` is true', () => {
const parser = new P1Parser({
timezone: 'Europe/Amsterdam',
withUnits: true,
});

const result = parser.parse(
fs.readFileSync(__dirname + '/__fixtures__/p1-data/esmr5.txt').subarray(1, -7),
);

expect(result.electricity.received.tariff1).toEqual({
value: 2107.618,
unit: 'kWh',
});
expect(result.electricity.active.current.line1).toEqual({
value: 1,
unit: 'A',
});
expect(result.electricity.sags.line1).toBe(5);
expect(result.electricity.swells.line1).toBe(0);
});

it('returns a the value without the unit, as default', () => {
const parser = new P1Parser({
timezone: 'Europe/Amsterdam',
});

const result = parser.parse(
fs.readFileSync(__dirname + '/__fixtures__/p1-data/esmr5.txt').subarray(1, -7),
);

expect(result.electricity.received.tariff1).toBe(2107.618);
expect(result.electricity.active.current.line1).toBe(1);
expect(result.electricity.sags.line1).toBe(5);
expect(result.electricity.swells.line1).toBe(0);
});

const provider = [
{ spec: 'esmr5', timezone: 'UTC', expected: '2023-05-07T16:55:13.000Z' },
{ spec: 'esmr5', timezone: 'Europe/Amsterdam', expected: '2023-05-07T14:55:13.000Z' },
{ spec: 'esmr5', timezone: 'Europe/London', expected: '2023-05-07T15:55:13.000Z' },
{ spec: 'esmr5', timezone: 'America/New_York', expected: '2023-05-07T20:55:13.000Z' },
{ spec: 'dsmr5', timezone: 'UTC', expected: '2018-01-08T20:25:37.000Z' },
{ spec: 'dsmr5', timezone: 'Europe/Amsterdam', expected: '2018-01-08T19:25:37.000Z' },
{ spec: 'dsmr5', timezone: 'Europe/London', expected: '2018-01-08T20:25:37.000Z' },
{ spec: 'dsmr5', timezone: 'America/New_York', expected: '2018-01-09T01:25:37.000Z' },
];

for (const d of provider) {
it(`uses the timezone "${d.timezone}" when parsing timestamps`, () => {
const parser = new P1Parser({
timezone: d.timezone,
});

const result = parser.parse(
fs.readFileSync(__dirname + `/__fixtures__/p1-data/${d.spec}.txt`).subarray(1, -7),
);

expect((result.transmitted_at as Date).toISOString()).toEqual(d.expected);
});
}
});
58 changes: 58 additions & 0 deletions __tests__/__fixtures__/p1-data/dsmr4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"vendor_id": "ISk",
"model_id": "2MT382-1000",
"version": "42",
"transmitted_at": "2010-12-09T10:30:20.000Z",
"message": "0123456789:;<=>?0123456789:;<=>?0123456789:;<=>?0123456789:;<=>?0123456789:;<=>?",
"electricity": {
"equipment_id": "K8EG004046395507",
"tariff": 2,
"received": {
"tariff1": 123456.789,
"tariff2": 123456.789,
"active": 1.193
},
"delivered": {
"tariff1": 123456.789,
"tariff2": 123456.789,
"active": 0
},
"active": {
"current": {},
"power": {
"negative": {},
"positive": {}
},
"voltage": {}
},
"failures": {
"count": 4,
"lasting_count": 2,
"log": [
{
"start_time": "2010-12-08T14:24:15.000Z",
"duration": 240
},
{
"start_time": "2010-12-08T14:10:04.000Z",
"duration": 301
}
]
},
"sags": {
"line1": 2,
"line2": 1,
"line3": 0
},
"swells": {
"line1": 0,
"line2": 3,
"line3": 0
}
},
"gas": {
"equipment_id": "2222ABCD123456789",
"measured_at": "2010-12-09T10:00:00.000Z",
"received": 12785.123
}
}
69 changes: 69 additions & 0 deletions __tests__/__fixtures__/p1-data/dsmr5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"vendor_id": "Ene",
"model_id": "T210-D ESMR5.0",
"version": "50",
"transmitted_at": "2018-01-08T19:25:37.000Z",
"electricity": {
"equipment_id": "\u00124Vx",
"tariff": 2,
"received": {
"tariff1": 0.855,
"tariff2": 0.693,
"active": 0.134
},
"delivered": {
"tariff1": 0.084,
"tariff2": 0,
"active": 0
},
"active": {
"current": {
"line1": 0,
"line2": 0,
"line3": 0
},
"power": {
"negative": {
"line1": 0,
"line2": 0,
"line3": 0
},
"positive": {
"line1": 0.094,
"line2": 0.04,
"line3": 0
}
},
"voltage": {
"line1": 229,
"line2": 226,
"line3": 229
}
},
"failures": {
"count": 8,
"lasting_count": 4,
"log": [
{
"start_time": "2017-10-24T18:46:25.000Z",
"duration": 305
}
]
},
"sags": {
"line1": 3,
"line2": 3,
"line3": 2
},
"swells": {
"line1": 0,
"line2": 0,
"line3": 0
}
},
"gas": {
"equipment_id": "\u00124Vx",
"measured_at": "2018-01-08T19:55:00.000Z",
"received": 1.29
}
}
69 changes: 69 additions & 0 deletions __tests__/__fixtures__/p1-data/esmr5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"vendor_id": "Ene",
"model_id": "XS210 ESMR 5.0",
"version": "50",
"transmitted_at": "2023-05-07T14:55:13.000Z",
"electricity": {
"equipment_id": "E0047000021993518",
"tariff": 1,
"received": {
"tariff1": 2107.618,
"tariff2": 2543.819,
"active": 0.195
},
"delivered": {
"tariff1": 0.014,
"tariff2": 0,
"active": 0
},
"active": {
"current": {
"line1": 1
},
"power": {
"negative": {
"line1": 0
},
"positive": {
"line1": 0.195
}
},
"voltage": {
"line1": 229
}
},
"failures": {
"count": 226,
"lasting_count": 5,
"log": [
{
"start_time": "2020-11-06T11:31:17.000Z",
"duration": 8311
},
{
"start_time": "2019-09-04T08:29:56.000Z",
"duration": 3484
},
{
"start_time": "2018-08-11T00:23:08.000Z",
"duration": 4225
},
{
"start_time": "2018-07-16T07:16:35.000Z",
"duration": 2216
}
]
},
"sags": {
"line1": 5
},
"swells": {
"line1": 0
}
},
"gas": {
"equipment_id": "G0053003673865917",
"measured_at": "2023-05-07T14:55:00.000Z",
"received": 5159.638
}
}
2 changes: 1 addition & 1 deletion lib/P1Parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 08523fc

Please sign in to comment.