Skip to content

Commit

Permalink
refactor(core): use python-fake for faking
Browse files Browse the repository at this point in the history
  • Loading branch information
sassanh committed Jul 25, 2024
1 parent 207d010 commit bc5d6b6
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 269 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- feat(core): long-pressing the reset button for 3 seconds or more reboots the device - closes #116
- fix(keypad): keypad becoming unresponsive if a key was pressed while the app was loading - closes #118
- fix(camera): closing the camera viewfinder will close the picamera instance so that it can be used again
- refactor(core): use python-fake for faking

## Version 0.15.4

Expand Down
84 changes: 47 additions & 37 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ priority = "primary"
[tool.poetry.dependencies]
python = "^3.11"
psutil = "^6.0.0"
ubo-gui = "^0.12.2"
ubo-gui = "^0.12.3a1"
headless-kivy = [
{ version = "^0.9.4", markers = "extra=='default'", extras = [
"default",
Expand Down Expand Up @@ -49,6 +49,7 @@ adafruit-circuitpython-pct2075 = "^1.1.21"
adafruit-circuitpython-veml7700 = "^1.1.22"

rpi-lgpio = { version = "^0.6", markers = "platform_machine=='aarch64'" }
python-fake = "^0.1.0"

[tool.poetry.group.dev]
optional = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
},
"lightdm": {
"is_active": true,
"is_enabled": false
"is_enabled": true
},
"main": {
"menu": {
Expand Down Expand Up @@ -599,7 +599,7 @@
},
"ssh": {
"is_active": true,
"is_enabled": false
"is_enabled": true
},
"status_icons": {
"icons": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"is_connected": true
},
"lightdm": {
"is_active": false,
"is_active": true,
"is_enabled": true
},
"main": {
Expand Down Expand Up @@ -350,7 +350,7 @@
1,
1
],
"icon": "[color=#ffff00]󰝦[/color]",
"icon": "[color=#008000]󰪥[/color]",
"is_short": false,
"label": "LightDM",
"opacity": null,
Expand Down Expand Up @@ -385,7 +385,7 @@
1,
1
],
"icon": "[color=#ffff00]󰝦[/color]",
"icon": "[color=#008000]󰪥[/color]",
"is_short": false,
"label": "SSH",
"opacity": null,
Expand Down Expand Up @@ -598,7 +598,7 @@
"playback_volume": 0.5
},
"ssh": {
"is_active": false,
"is_active": true,
"is_enabled": true
},
"status_icons": {
Expand Down
63 changes: 30 additions & 33 deletions tests/monkeypatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
def _monkeypatch_socket(monkeypatch: pytest.MonkeyPatch) -> None:
import socket

from ubo_app.utils.fake import Fake
from fake import Fake

monkeypatch.setattr(socket, 'gethostname', lambda: 'test-hostname')

Expand Down Expand Up @@ -63,7 +63,7 @@ def _monkeypatch_psutil(monkeypatch: pytest.MonkeyPatch) -> None:


def _monkeypatch_docker(monkeypatch: pytest.MonkeyPatch) -> None:
from ubo_app.utils.fake import Fake
from fake import Fake

monkeypatch.setattr(
'docker.from_env',
Expand All @@ -84,7 +84,7 @@ def now(cls: type[DateTime], tz: datetime.tzinfo | None = None) -> DateTime:


def _monkeypatch_uuid(monkeypatch: pytest.MonkeyPatch) -> None:
from ubo_app.utils.fake import Fake
from fake import Fake

counter = 0

Expand Down Expand Up @@ -121,7 +121,7 @@ def debug_uuid4() -> Fake:


def _monkeypatch_rpi_modules() -> None:
from ubo_app.utils.fake import Fake
from fake import Fake

class FakeSensor(Fake):
lux = 0.0
Expand All @@ -137,7 +137,7 @@ class FakeSensorModule(Fake):


def _monkeypatch_aiohttp() -> None:
from ubo_app.utils.fake import Fake
from fake import Fake

class FakeUpdateResponse(Fake):
async def json(self: FakeUpdateResponse) -> dict[str, object]:
Expand Down Expand Up @@ -165,7 +165,7 @@ def _monkeypatch_subprocess(monkeypatch: pytest.MonkeyPatch) -> None:
import subprocess
from pathlib import Path

from ubo_app.utils.fake import Fake
from fake import Fake

original_subprocess_run = subprocess.run

Expand Down Expand Up @@ -202,7 +202,7 @@ def fake_subprocess_run(
def _monkeypatch_asyncio_subprocess(monkeypatch: pytest.MonkeyPatch) -> None:
import asyncio

from ubo_app.utils.fake import Fake
from fake import Fake

class FakeAsyncProcess(Fake):
def __init__(self: FakeAsyncProcess, output: bytes = b'') -> None:
Expand All @@ -212,41 +212,37 @@ async def communicate(self: FakeAsyncProcess) -> tuple[bytes, bytes]:
return cast(bytes, self.output), b''

async def fake_create_subprocess_exec(
command: str,
*args: list[str],
**kwargs: object,
) -> FakeAsyncProcess:
*_args: str,
**kwargs: Any, # noqa: ANN401
) -> object:
_ = kwargs
if command == '/usr/bin/env' and args == ('systemctl', 'is-enabled', 'ssh'):
return FakeAsyncProcess(output=b'enabled')
if command == '/usr/bin/env' and args == ('systemctl', 'is-active', 'ssh'):
return FakeAsyncProcess(output=b'active')
if command == '/usr/bin/env' and args == ('systemctl', 'is-enabled', 'lightdm'):
return FakeAsyncProcess(output=b'enabled')
if command == '/usr/bin/env' and args == ('systemctl', 'is-active', 'lightdm'):
return FakeAsyncProcess(output=b'active')
if command == '/usr/bin/env' and args == ('which', 'docker'):
command = _args[0]
args = _args[1:]

if command == '/usr/bin/env':
command = args[0]
args = args[1:]

if command == 'systemctl':
if args[0] == 'is-enabled':
return FakeAsyncProcess(output=b'enabled')
if args[0] == 'is-active':
return FakeAsyncProcess(output=b'active')
if command == 'which' and args[0] == 'docker':
return FakeAsyncProcess(output=b'/bin/docker')
if command == '/usr/bin/env' and args[0] == 'pulseaudio':
if command == 'pulseaudio':
return FakeAsyncProcess()
msg = (
'Unexpected `asyncio.create_subprocess_exec` command in test '
f'environment: {command} - {args}'
)
raise ValueError(msg)

return await original_asyncio_create_subprocess_exec(*_args, **kwargs)

original_asyncio_create_subprocess_exec = asyncio.create_subprocess_exec
monkeypatch.setattr(asyncio, 'create_subprocess_exec', fake_create_subprocess_exec)
monkeypatch.setattr(
asyncio,
'open_unix_connection',
Fake(_Fake__return_value=Fake(_Fake__await_value=(Fake(), Fake()))),
)


def _monkeypatch_asyncio_socket(monkeypatch: pytest.MonkeyPatch) -> None:
import asyncio

from ubo_app.utils.fake import Fake
from fake import Fake

monkeypatch.setattr(asyncio, 'open_connection', Fake())

Expand All @@ -260,9 +256,10 @@ def _monkeypatch(monkeypatch: pytest.MonkeyPatch) -> None:
import atexit
import importlib.metadata

from fake import Fake

import ubo_app.constants
import ubo_app.utils.serializer
from ubo_app.utils.fake import Fake

tracemalloc.start()

Expand Down
Loading

0 comments on commit bc5d6b6

Please sign in to comment.