hass-AMS icon indicating copy to clipboard operation
hass-AMS copied to clipboard

Draft: unit tests for meter parsers

Open joernnilsson opened this issue 2 years ago • 11 comments

This may serve as a staring point for a unit test framework.

At the moment it's a quick and dirty proof of concept only. Before I put any more time into it, I'd like to hear from the maintainer(s) whether this is a good idea or not.

Requires pystest. not already installed: pip3 install pytest

To try it out, simply run: py.test test_parser_kaifa.py. To enable debug logging: py.test --log-cli-level DEBUG test_parser_kaifa.py.

The data values are copied from parser_test.py. Currently there are 2/7 tests that fail:

  • test_kaifa_MA304H4D_long
  • test_kaifa_MA304H4_long

They both fail on Kaifa.test_valid_data(), having a length > 157.

joernnilsson avatar Nov 09 '22 10:11 joernnilsson

Very good contribution. 🥇 This is something I have wanted to make since the very start, but I do not understand the coding around this. I will have a look at it and see if I can understand the concept. :)

turbokongen avatar Nov 09 '22 18:11 turbokongen

I highly recommend switching to pytest, its a much better and easier test framework.

Hellowlol avatar Nov 09 '22 19:11 Hellowlol

@joernnilsson Nice work 👍

Hellowlol avatar Nov 09 '22 19:11 Hellowlol

Switched to pytest as suggested.

I haven't done anything towards streamlining the process or increased coverage. There are so my things that could be done regarding tooling and ci/cd, but I think it may be overkill for now.

joernnilsson avatar Nov 10 '22 15:11 joernnilsson

~~Does there exist any test data from the other meter types? I could build test for them as well~~

Edit: I found some packages in older issues:

  • Aidon SE (included one long and one short): https://github.com/turbokongen/hass-AMS/issues/44
  • Kamstrup: https://github.com/turbokongen/hass-AMS/issues/48

For the Aidon tests, the long is good while the short package fails to validate For the Kamstrup test, everything looks fine

To run the tests: py.test test_parser_kaifa.py (5/7 pass) py.test test_parser_aidon.py (1/2 pass) py.test test_parser_kamstrup.py (1/1 pass)

add --log-cli-level DEBUG for more parser output.

joernnilsson avatar Nov 11 '22 07:11 joernnilsson

Here is all the data I have received from people. It is mostly not in decimal, but hex, so it will need to be converted.

aidon packages.txt Aidon_se.txt Han2-decoded.txt Han-decoded.txt Kaifa SE decimal.txt Kaifa SE hex.txt kaifa_1.txt Kaifa_2.txt kaifa-packages.txt New_kaifa_se.txt

turbokongen avatar Nov 11 '22 18:11 turbokongen

I think that that is it raw data id good

Hellowlol avatar Nov 11 '22 19:11 Hellowlol

I have corrected some of the data, as it was wrong.

The buildup of package types are not intiutive between Norway and sweden:

There are three type of packages: Mini (Only containing active power data) Short (Containing all power measurements, voltages and currents) Long (Short package plus energy)

The manufacturers distributes this differently: Aidon: Short and Long Aidon_se: Long Kaifa: Mini, Short and Long Kaifa_se: Long Kamstrup: Short and Long

Hope this information helps

turbokongen avatar Nov 12 '22 07:11 turbokongen

I added in automatic run of the test, and flake8 checking on a push to the repository. This should prevent stuff from breaking.

turbokongen avatar Nov 12 '22 16:11 turbokongen

Nice!

This looks very good, and I like the added workflow :+1: I guess the things remaining is adding the test data and getting the failing tests green.

I wanted to add a test for the meter detection code as well, but it complicated things since _find_parser is in AmsHub (which drags along all the HA dependencies).

joernnilsson avatar Nov 14 '22 13:11 joernnilsson

The failing test have been fixed, I will have a look at extending the coverage. Btw I had to add homeassistant into the pytest enviroment to get everything to run smooth together with the workflow, so it might not be too complicated to add the _find_parser test.

turbokongen avatar Nov 14 '22 17:11 turbokongen