turbokongen / hass-AMS

Custom component reading AMS through MBus adapter into HomeAssistant
42 stars 10 forks source link

Unit tests for meter parsers #93

Closed joernnilsson closed 1 year ago

joernnilsson commented 1 year ago

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:

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

turbokongen commented 1 year ago

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. :)

Hellowlol commented 1 year ago

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

Hellowlol commented 1 year ago

@joernnilsson Nice work 👍

joernnilsson commented 1 year ago

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 commented 1 year ago

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:

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.

turbokongen commented 1 year ago

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

Hellowlol commented 1 year ago

I think that that is it raw data id good

turbokongen commented 1 year ago

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 commented 1 year ago

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

joernnilsson commented 1 year ago

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).

turbokongen commented 1 year ago

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.