From b0f4c0023c90b1dff465900462b268102f140b73 Mon Sep 17 00:00:00 2001 From: Juergen Edelbluth Date: Tue, 21 Aug 2018 20:51:22 +0200 Subject: [PATCH] Additional Unit Tests for DOs --- src/test/test_configuration_file_discovery.py | 8 ++-- src/test/test_response.py | 36 ++++++++++++++++ src/test/test_temp_hum.py | 43 +++++++++++++++++++ 3 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 src/test/test_response.py create mode 100644 src/test/test_temp_hum.py diff --git a/src/test/test_configuration_file_discovery.py b/src/test/test_configuration_file_discovery.py index 62bd6ac..48b5f45 100644 --- a/src/test/test_configuration_file_discovery.py +++ b/src/test/test_configuration_file_discovery.py @@ -12,7 +12,7 @@ def test_discovery_file_in_folder(monkeypatch: MonkeyPatch): m.setattr('os.path.isfile', lambda path: True) m.setattr('os.path.exists', lambda path: True) result = discover_config_file_by_name('test.ini', '/foo/bar') - assert result == os.path.join('/foo/bar', 'test.ini') + assert os.path.join('/foo/bar', 'test.ini') == result def test_discovery_via_env_var(monkeypatch: MonkeyPatch): @@ -21,7 +21,7 @@ def test_discovery_via_env_var(monkeypatch: MonkeyPatch): m.setattr('os.path.isfile', lambda path: True) m.setattr('os.path.exists', lambda path: True) result = discover_config_file_by_name('test.ini', script_dir=None) - assert result == os.path.join('/rs500_config_path/here/we/are', 'test.ini') + assert os.path.join('/rs500_config_path/here/we/are', 'test.ini') == result def test_discovery_via_user_home(monkeypatch: MonkeyPatch): @@ -30,7 +30,7 @@ def test_discovery_via_user_home(monkeypatch: MonkeyPatch): m.setattr('os.path.exists', lambda path: True) monkeypatch.setattr(pathlib.Path, 'absolute', lambda x: '/user/home/path/test') result = discover_config_file_by_name('test.ini', script_dir=None) - assert result == os.path.join('/user/home/path/test', '.rs500', 'test.ini') + assert os.path.join('/user/home/path/test', '.rs500', 'test.ini') == result def test_discovery_in_etc(monkeypatch: MonkeyPatch): @@ -38,7 +38,7 @@ def test_discovery_in_etc(monkeypatch: MonkeyPatch): m.setattr('os.path.isfile', lambda path: path.startswith('/etc')) m.setattr('os.path.exists', lambda path: path.startswith('/etc')) result = discover_config_file_by_name('test.ini') - assert result == os.path.join('/etc', 'test.ini') + assert os.path.join('/etc', 'test.ini') == result def test_no_hit(): diff --git a/src/test/test_response.py b/src/test/test_response.py new file mode 100644 index 0000000..168493a --- /dev/null +++ b/src/test/test_response.py @@ -0,0 +1,36 @@ +import pytest + +from rs500reader.do import Response, TempHum + + +def test_response_object(): + r = Response() + r.set_channel_data(1, TempHum(23.4, 52)) + r.set_channel_data(3, TempHum(-1.4, 25)) + r.set_channel_data(6, TempHum(0.1, 78)) + assert 23.4 == r.get_channel_data(1).temperature + assert 52 == r.get_channel_data(1).humidity + assert -1.4 == r.get_channel_data(3).temperature + assert 25 == r.get_channel_data(3).humidity + assert 0.1 == r.get_channel_data(6).temperature + assert 78 == r.get_channel_data(6).humidity + + +def test_invalid_channel_set(): + r = Response() + r.set_channel_data(71, TempHum(0.5, 50)) + assert 0.5 == r.get_channel_data(71).temperature + assert 50 == r.get_channel_data(71).humidity + + +def test_none_fetch(): + r = Response() + with pytest.raises(AttributeError): + dummy = r.get_channel_data(1).humidity + + +def test_get_all(): + th = TempHum(8.8, 76) + r = Response() + r.set_channel_data(2, th) + assert {1: None, 2: th, 3: None, 4: None, 5: None, 6: None, 7: None, 8: None} == r.all diff --git a/src/test/test_temp_hum.py b/src/test/test_temp_hum.py new file mode 100644 index 0000000..3df5cc1 --- /dev/null +++ b/src/test/test_temp_hum.py @@ -0,0 +1,43 @@ +from typing import Sequence + +import pytest + +from rs500reader.do import TempHum + + +@pytest.mark.parametrize('temp_from_protocol,expected_temp', [ + ([0x00, 0x00], 0), + ([0xff, 0xff], -0.1), + ([0xff, 0xee], -1.8), + ([0xff, 0xe7], -2.5), + ([0x00, 0xcb], 20.3), + ([0x00, 0xff], 25.5), + ([0x7f, 0xff], 3276.7), # Protocol Dumbness +]) +def test_temp_parser(temp_from_protocol: Sequence[int], expected_temp: float): + obj = TempHum.from_protocol(temp_from_protocol, 0) + assert expected_temp == obj.temperature + + +@pytest.mark.parametrize('hum_from_protocol,expected_hum', [ + (0x00, 0), + (0xff, 255), # Protocol Dumbness + (0x01, 1), + (0x35, 53), + (0x34, 52), + (0x28, 40), + (0x2b, 43), +]) +def test_hum_parser(hum_from_protocol: int, expected_hum: int): + obj = TempHum.from_protocol([0, 0], hum_from_protocol) + assert expected_hum == obj.humidity + + +def test_setters(): + obj = TempHum.from_protocol([0, 0], 0) + assert 0 == obj.humidity + assert 0 == obj.temperature + obj.temperature = 23.4 + obj.humidity = 53 + assert 23.4 == obj.temperature + assert 53 == obj.humidity