Skip to content

Commit

Permalink
Tests: Bring code coverage back to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Apr 19, 2024
1 parent d058e0b commit 682429b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ds18b20_datalogger/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from ds18b20_datalogger.model import Settings

if sys.version_info < (3, 9):
from importlib_resources import files
from importlib_resources import files # pragma: nocover
else:
from importlib.resources import files

Expand Down
9 changes: 5 additions & 4 deletions ds18b20_datalogger/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Device:
"""

name: str
path: Path
path: str


class DeviceMap:
Expand All @@ -34,15 +34,16 @@ def by_name(self, name: str) -> Device:

def by_fullpath(self, path: Path) -> Device:
for device in self.devices:
if device.path == path:
if str(device.path) == str(path):
return device
raise KeyError(f"Device not found: path={path}")

def by_pathname(self, path: Path) -> Device:
needle = Path(path).name
for device in self.devices:
if device.path.name == path.name:
if Path(device.path).name == needle:
return device
raise KeyError(f"Device not found: pathname={path.name}")
raise KeyError(f"Device not found: pathname={needle}")


@dataclasses.dataclass
Expand Down
5 changes: 5 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def test_cli_run_read_no_config():
assert "Program needs a configuration file" in output


def test_cli_run_read_nonexist_config():
exitcode, output = invoke("ds18b20-datalogger run foo.yaml")
assert "Configuration file does not exist: foo.yaml" in output


def test_cli_read_success():
command = shlex.split("ds18b20-datalogger read ds18b20_datalogger/datalogger.yaml")
process = subprocess.run(command, stdout=subprocess.PIPE) # noqa: S603
Expand Down
47 changes: 47 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from pathlib import Path

import pytest

from ds18b20_datalogger.model import Device, Settings


@pytest.fixture
def settings() -> Settings:
configfile = Path("etc") / "mois.yaml"
return Settings.from_file(configfile)


def test_model_devicemap_by_name_success(settings):
assert settings.devicemap.by_name("temp-ir-1-1") == Device(
name="temp-ir-1-1", path="/sys/bus/w1/devices/28-0346d4430b06"
)


def test_model_devicemap_by_name_failure(settings):
with pytest.raises(KeyError) as ex:
settings.devicemap.by_name("foo")
assert ex.match("'Device not found: name=foo'")


def test_model_devicemap_by_fullpath_success(settings):
assert settings.devicemap.by_fullpath("/sys/bus/w1/devices/28-0346d4430b06") == Device(
name="temp-ir-1-1", path="/sys/bus/w1/devices/28-0346d4430b06"
)


def test_model_devicemap_by_fullpath_failure(settings):
with pytest.raises(KeyError) as ex:
settings.devicemap.by_fullpath("foo")
assert ex.match("'Device not found: path=foo'")


def test_model_devicemap_by_pathname_success(settings):
assert settings.devicemap.by_pathname("28-0346d4430b06") == Device(
name="temp-ir-1-1", path="/sys/bus/w1/devices/28-0346d4430b06"
)


def test_model_devicemap_by_pathname_failure(settings):
with pytest.raises(KeyError) as ex:
settings.devicemap.by_pathname("foo")
assert ex.match("'Device not found: pathname=foo'")

0 comments on commit 682429b

Please sign in to comment.