Browse Source

added unit tests for Backlight class

Fabian Peter Hammerle 2 weeks ago
parent
commit
3e21c5a774
2 changed files with 60 additions and 1 deletions
  1. 1 1
      .github/workflows/python.yml
  2. 59 0
      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=64
+    - run: pipenv run pytest --cov=acpi_backlight --cov-report=term-missing --cov-fail-under=80
     - run: pipenv run pylint --load-plugins=pylint_import_requirements acpi_backlight
     # https://github.com/PyCQA/pylint/issues/352
     - run: pipenv run pylint tests/*

+ 59 - 0
test/test_backlight.py

@@ -0,0 +1,59 @@
+import pytest
+
+from acpi_backlight import Backlight
+
+# pylint: disable=protected-access
+# pylint: disable=redefined-outer-name; fixture
+
+
+def test_init_default():
+    backlight = Backlight()
+    assert backlight._acpi_dir_path == "/sys/class/backlight/intel_backlight"
+
+
+@pytest.mark.parametrize("name", ["intel_backlight", "other"])
+def test_init(name):
+    backlight = Backlight(name=name)
+    assert backlight._acpi_dir_path == "/sys/class/backlight/{}".format(name)
+
+
+@pytest.mark.parametrize(
+    ("max_brightness", "brightness_absolute_str", "expected_brightness_relative"),
+    (
+        (100, "0", 0),
+        (100, "100", 1),
+        (100, "40", 0.4),
+        (4096, "2048", 0.5),
+        (4096, "3584", 0.875),
+    ),
+)
+def test_brightness_relative_get(
+    tmp_path, max_brightness, brightness_absolute_str, expected_brightness_relative
+):
+    backlight = Backlight()
+    backlight._acpi_dir_path = str(tmp_path)
+    tmp_path.joinpath("brightness").write_text(brightness_absolute_str)
+    tmp_path.joinpath("max_brightness").write_text(str(max_brightness))
+    assert backlight.brightness_relative == pytest.approx(expected_brightness_relative)
+
+
+@pytest.mark.parametrize(
+    ("max_brightness", "brightness_relative", "expected_brightness_abs_str"),
+    (
+        (100, 0, "0"),
+        (100, 1, "100"),
+        (100, 0.4, "40"),
+        (4096, 0.5, "2048"),
+        (4096, 0.8, "3277"),
+        (100, 1.1, "100"),
+        (100, -0.1, "0"),
+    ),
+)
+def test_brightness_relative_set(
+    tmp_path, max_brightness, brightness_relative, expected_brightness_abs_str
+):
+    backlight = Backlight()
+    backlight._acpi_dir_path = str(tmp_path)
+    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