From d319918f0811f524fc60f584c7ea57e653219e4e Mon Sep 17 00:00:00 2001 From: Crozzers Date: Tue, 3 Oct 2023 18:19:57 +0100 Subject: [PATCH] Start adding tests for lower level brightness methods --- pytests/helpers.py | 39 +++++++++++++++++++++++++++++++++++++++ pytests/test_linux.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 pytests/helpers.py create mode 100644 pytests/test_linux.py diff --git a/pytests/helpers.py b/pytests/helpers.py new file mode 100644 index 0000000..96910cd --- /dev/null +++ b/pytests/helpers.py @@ -0,0 +1,39 @@ +from abc import ABC, abstractmethod +from typing import Dict, Optional, Type + +from pytest_mock import MockerFixture + +from screen_brightness_control.helpers import BrightnessMethod + + +class BrightnessMethodTest(ABC): + @abstractmethod + def patch(self, mocker: MockerFixture): + '''Applies patches to get `get_display_info` working''' + ... + + @abstractmethod + def method(self) -> Type[BrightnessMethod]: + '''Returns the brightness method under test''' + ... + + class TestGetDisplayInfo: + '''Some standard tests for the `get_display_info` method''' + def test_returns_list_of_dict(self, method): + info = method.get_display_info() + assert isinstance(info, list) + assert all(isinstance(i, dict) for i in info) + + def test_returned_dicts_contain_required_keys(self, method, extras: Optional[Dict[str, Type]]=None): + info = method.get_display_info() + for display in info: + for prop in ('name', 'path', 'model', 'serial', 'manufacturer', 'manufacturer_id', 'edid'): + assert prop in display + if display[prop] is not None: + assert isinstance(display[prop], str) + assert 'index' in display and isinstance(display['index'], int) + assert 'method' in display and display['method'] is method + if extras is not None: + for prop, prop_type in extras.items(): + assert prop in display + assert isinstance(display[prop], prop_type) diff --git a/pytests/test_linux.py b/pytests/test_linux.py new file mode 100644 index 0000000..d75d4d1 --- /dev/null +++ b/pytests/test_linux.py @@ -0,0 +1,36 @@ +import os +from unittest.mock import Mock + +import pytest +from pytest_mock import MockerFixture + +import screen_brightness_control as sbc +from screen_brightness_control.helpers import BrightnessMethod + +from .helpers import BrightnessMethodTest + + +class TestSysFiles(BrightnessMethodTest): + @pytest.fixture(autouse=True) + def patch(self, mocker: MockerFixture): + '''Mock everything needed to get `SysFiles.get_display_info` to run''' + def listdir(dir: str): + if 'subsystem' in dir: + return ['intel_backlight', 'acpi_video0'] + return ['edp1'] + + def isfile(file: str): + return not file.endswith('device/edid') + + mocker.patch.object(os, 'listdir', Mock(side_effect=listdir)) + mocker.patch.object(os.path, 'isdir', Mock(return_value=True)) + mocker.patch.object(os.path, 'isfile', Mock(side_effect=isfile)) + mocker.patch.object(sbc.linux, 'open', mocker.mock_open(read_data='100')) + + @pytest.fixture + def method(self): + return sbc.linux.SysFiles + + class TestGetDisplayInfo(BrightnessMethodTest.TestGetDisplayInfo): + def test_returned_dicts_contain_required_keys(self, method): + super().test_returned_dicts_contain_required_keys(method, extras={'scale': float})