Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: display option edge cases #86

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions tests/test_waybar_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from waybar_crypto import (
API_KEY_ENV,
CLASS_NAME,
DEFAULT_DISPLAY_OPTIONS,
DEFAULT_DISPLAY_OPTIONS_FORMAT,
DEFAULT_XDG_CONFIG_HOME_PATH,
MIN_PRECISION,
Expand Down Expand Up @@ -281,6 +282,58 @@ def test_read_config_min_precision():
assert isinstance(e, WaybarCryptoException)


def test_read_config_display_options_single():
test_display_option = "price"

with open(TEST_CONFIG_PATH, "r", encoding="utf-8") as f:
cfp = configparser.ConfigParser(allow_no_value=True, interpolation=None)
cfp.read_file(f)
cfp.set("general", "display", test_display_option)

with tempfile.NamedTemporaryFile(mode="w") as tmp:
cfp.write(tmp)
tmp.flush()
tmp_config_path = tmp.file.name

config = read_config(tmp_config_path)
display_options = config["general"]["display_options"]
assert display_options == [test_display_option]


def test_read_config_display_options_multiple():
test_display_options = ["price", "percent_change_1h", "percent_change_24h"]

with open(TEST_CONFIG_PATH, "r", encoding="utf-8") as f:
cfp = configparser.ConfigParser(allow_no_value=True, interpolation=None)
cfp.read_file(f)
cfp.set("general", "display", ",".join(test_display_options))

with tempfile.NamedTemporaryFile(mode="w") as tmp:
cfp.write(tmp)
tmp.flush()
tmp_config_path = tmp.file.name

config = read_config(tmp_config_path)
display_options = config["general"]["display_options"]
assert display_options == test_display_options


def test_read_config_default_display_options():
with open(TEST_CONFIG_PATH, "r", encoding="utf-8") as f:
cfp = configparser.ConfigParser(allow_no_value=True, interpolation=None)
cfp.read_file(f)
cfp.set("general", "display", None)

with tempfile.NamedTemporaryFile(mode="w") as tmp:
cfp.write(tmp)
tmp.flush()
tmp_config_path = tmp.file.name

config = read_config(tmp_config_path)
display_options = config["general"]["display_options"]
assert display_options == DEFAULT_DISPLAY_OPTIONS


class TestWaybarCrypto:
"""Tests for the WaybarCrypto."""

Expand Down
5 changes: 4 additions & 1 deletion waybar_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ def read_config(config_path: str) -> Config:
spacer_symbol = cfp.get("general", "spacer_symbol")

# Get a list of the chosen display options
display_options: list[str] = cfp.get("general", "display").split(",")
display_options: list[str] = []
display_options_str = cfp.get("general", "display")
if display_options_str and len(display_options_str) > 0:
display_options = display_options_str.split(",")

if len(display_options) == 0:
display_options = DEFAULT_DISPLAY_OPTIONS
Expand Down