Skip to content

Commit

Permalink
Start adding tests for lower level brightness methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Crozzers committed Oct 3, 2023
1 parent 89642fc commit d319918
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
39 changes: 39 additions & 0 deletions pytests/helpers.py
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)
36 changes: 36 additions & 0 deletions pytests/test_linux.py
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})

0 comments on commit d319918

Please sign in to comment.