-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start adding tests for lower level brightness methods
- Loading branch information
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}) |