Parcourir la source

Added various utility tests

Signed-off-by: Stan Janssen <stan.janssen@elaad.nl>
Stan Janssen il y a 4 ans
Parent
commit
348870d454
2 fichiers modifiés avec 43 ajouts et 2 suppressions
  1. 2 2
      openleadr/utils.py
  2. 41 0
      test/test_utils.py

+ 2 - 2
openleadr/utils.py

@@ -412,10 +412,10 @@ def find_by(dict_or_list, key, value, *args):
             _item = item
         for key, value in search_params:
             if isinstance(value, tuple):
-                if _item[key] not in value:
+                if key not in _item or _item[key] not in value:
                     break
             else:
-                if _item[key] != value:
+                if key not in _item or _item[key] != value:
                     break
         else:
             return item

+ 41 - 0
test/test_utils.py

@@ -231,6 +231,47 @@ def test_validate_report_measurement_dict_invalid_power_attributes(caplog):
     assert str(err.value) == ("The power_attributes of the measurement must contain the "
                               "following keys: 'voltage' (int), 'ac' (bool), 'hertz' (int).")
 
+def test_ungroup_target_by_type_with_single_str():
+    targets_by_type = {'ven_id': 'ven123'}
+    targets = utils.ungroup_targets_by_type(targets_by_type)
+    assert targets == [{'ven_id': 'ven123'}]
+
+def test_find_by_with_dict():
+    search_dict = {'one': {'a': 123, 'b': 456},
+                   'two': {'a': 321, 'b': 654}}
+    result = utils.find_by(search_dict, 'a', 123)
+    assert result == {'a': 123, 'b': 456}
+
+def test_find_by_with_missing_member():
+    search_list = [{'a': 123, 'b': 456},
+                   {'a': 321, 'b': 654, 'c': 1000}]
+    result = utils.find_by(search_list, 'c', 1000)
+    assert result == {'a': 321, 'b': 654, 'c': 1000}
+
+def test_ensure_str():
+    assert utils.ensure_str("Hello") == "Hello"
+    assert utils.ensure_str(b"Hello") == "Hello"
+    assert utils.ensure_str(None) is None
+    with pytest.raises(TypeError) as err:
+        utils.ensure_str(1)
+    assert str(err.value) == "Must be bytes or str"
+
+def test_ensure_bytes():
+    assert utils.ensure_bytes("Hello") == b"Hello"
+    assert utils.ensure_bytes(b"Hello") == b"Hello"
+    assert utils.ensure_bytes(None) is None
+    with pytest.raises(TypeError) as err:
+        utils.ensure_bytes(1)
+    assert str(err.value) == "Must be bytes or str"
+
+def test_booleanformat():
+    assert utils.booleanformat("true") == "true"
+    assert utils.booleanformat("false") == "false"
+    assert utils.booleanformat(True) == "true"
+    assert utils.booleanformat(False) == "false"
+    with pytest.raises(ValueError) as err:
+        assert utils.booleanformat(123)
+    assert str(err.value) == "A boolean value must be provided, not 123."
 
 def test_parse_duration():
     assert utils.parse_duration("PT1M") == timedelta(minutes=1)