Browse Source

added unit test for backlight_eval

Fabian Peter Hammerle 3 years ago
parent
commit
43ce2c0d98
2 changed files with 34 additions and 2 deletions
  1. 1 1
      .github/workflows/python.yml
  2. 33 1
      test/test_backlight.py

+ 1 - 1
.github/workflows/python.yml

@@ -48,7 +48,7 @@ jobs:
       env:
         PYTHON_VERSION: ${{ matrix.python-version }}
     - run: pipenv graph
-    - run: pipenv run pytest --cov=acpi_backlight --cov-report=term-missing --cov-fail-under=80
+    - run: pipenv run pytest --cov=acpi_backlight --cov-report=term-missing --cov-fail-under=90
     - run: pipenv run pylint --load-plugins=pylint_import_requirements acpi_backlight
     # https://github.com/PyCQA/pylint/issues/352
     - run: pipenv run pylint tests/*

+ 33 - 1
test/test_backlight.py

@@ -1,6 +1,8 @@
+import unittest.mock
+
 import pytest
 
-from acpi_backlight import Backlight
+from acpi_backlight import Backlight, backlight_eval
 
 # pylint: disable=protected-access
 # pylint: disable=redefined-outer-name; fixture
@@ -57,3 +59,33 @@ def test_brightness_relative_set(
     tmp_path.joinpath("max_brightness").write_text(str(max_brightness))
     backlight.brightness_relative = brightness_relative
     assert tmp_path.joinpath("brightness").read_text() == expected_brightness_abs_str
+
+
+@pytest.mark.parametrize(
+    ("current_brightness_relative", "expr_str", "expected_new_brightness_relative"),
+    (
+        (100, "1", 1),
+        (100, "0", 0),
+        (100, "0.42", 0.42),
+        (100, "1/4", 0.25),
+        (100, "b * 0.5", 50),
+        (100, "b / 2", 50),
+        (100, "b + 21", 121),
+        (100, "b - 21", 79),
+    ),
+)
+def test_backlight_eval(
+    current_brightness_relative, expr_str, expected_new_brightness_relative
+):
+    with unittest.mock.patch(
+        "acpi_backlight.Backlight.brightness_relative",
+        new_callable=unittest.mock.PropertyMock,
+    ) as property_mock:
+        property_mock.return_value = current_brightness_relative
+        backlight_eval(expr_str)
+    # args and kwargs properties were added in python3.8
+    assert all(not call[1] for call in property_mock.call_args_list)
+    setter_calls_args = [call[0] for call in property_mock.call_args_list if call[0]]
+    assert len(setter_calls_args) == 1, setter_calls_args
+    (new_brightness_relative,) = setter_calls_args[0]
+    assert new_brightness_relative == pytest.approx(expected_new_brightness_relative)