From effb5a6fce9a12806d362a3d99d3133ae4c5e636 Mon Sep 17 00:00:00 2001 From: Cliff Hill Date: Thu, 30 Sep 2021 11:59:32 -0400 Subject: [PATCH] Got tests for settings. Signed-off-by: Cliff Hill --- src/playlist/data/settings.py | 15 +++++++-------- tests/data/test_settings.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 tests/data/test_settings.py diff --git a/src/playlist/data/settings.py b/src/playlist/data/settings.py index edfd0e8..a4b6963 100644 --- a/src/playlist/data/settings.py +++ b/src/playlist/data/settings.py @@ -4,6 +4,7 @@ from __future__ import annotations import dataclasses import datetime import functools +import getpass import pathlib import yaml @@ -22,9 +23,7 @@ class CredentialSettings(base.BaseData["CredentialSettings"]): token: str @classmethod - def create(cls: type[CredentialSettings]) -> CredentialSettings: - import getpass - + def create(cls: type[CredentialSettings]) -> CredentialSettings: # pragma: no cover from playlist.plex import server username = input("Plex Username: ") @@ -43,7 +42,7 @@ class DownloadSettings(base.BaseData["DownloadSettings"]): process_delay: float @classmethod - def create(cls: type[DownloadSettings]) -> DownloadSettings: + def create(cls: type[DownloadSettings]) -> DownloadSettings: # pragma: no cover return cls( batch_size=const.DEFAULTS.BATCH_SIZE, process_delay=const.DEFAULTS.PROCESS_DELAY, @@ -57,7 +56,7 @@ class TrackSettings(base.BaseData["TrackSettings"]): duration: float @classmethod - def create(cls: type[TrackSettings]) -> TrackSettings: + def create(cls: type[TrackSettings]) -> TrackSettings: # pragma: no cover return cls(duration=const.DEFAULTS.DURATION) @@ -69,7 +68,7 @@ class PlaylistSettings(base.BaseData["PlaylistSettings"]): max_tracks: int @classmethod - def create(cls: type[PlaylistSettings]) -> PlaylistSettings: + def create(cls: type[PlaylistSettings]) -> PlaylistSettings: # pragma: no cover return cls( playtime=const.DEFAULTS.PLAYTIME, max_tracks=const.DEFAULTS.MAX_TRACKS, @@ -119,7 +118,7 @@ class Settings(base.BaseData["Settings"]): @functools.cache -def get() -> Settings: +def get() -> Settings: # pragma: no cover """Get the Settings object instance.""" instance: Settings filepath = const.PATHS.CONFIG / const.SETTINGS_FILENAME @@ -132,7 +131,7 @@ def get() -> Settings: return instance -def write() -> None: +def write() -> None: # pragma: no cover """Write the Settings object instance.""" filepath = const.PATHS.CONFIG / const.SETTINGS_FILENAME get().yaml_write(filepath) diff --git a/tests/data/test_settings.py b/tests/data/test_settings.py new file mode 100644 index 0000000..3dce91a --- /dev/null +++ b/tests/data/test_settings.py @@ -0,0 +1,30 @@ +"""Tests for validating the Settings object.""" +import pathlib + +import pytest # type: ignore [import] + +from playlist.data import settings # type: ignore [import] + + +@pytest.fixture # type: ignore [misc] +def example_settings() -> settings.Settings: + """Make testable Settings object.""" + return settings.Settings( + creds=settings.CredentialSettings(baseurl="http://nowhere.huh", token="12345"), + ) + + +@pytest.fixture # type: ignore [misc] +def settings_filepath(tmp_path: pathlib.Path) -> pathlib.Path: + """Get the path to use for the settings file.""" + return tmp_path / "fake_settings.yaml" + + +def test_yaml( + example_settings: settings.Settings, + settings_filepath: pathlib.Path, +) -> None: + """Test the YAML read/write operations.""" + example_settings.yaml_write(settings_filepath) + result = example_settings.yaml_read(settings_filepath) + assert result == example_settings