Skip to content

Commit

Permalink
test MIN_PRECISION is enforced (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
chadsr committed May 27, 2024
1 parent 9368233 commit 815ef13
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
21 changes: 21 additions & 0 deletions tests/test_waybar_crypto.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import os
import tempfile
import argparse
import pytest
from unittest import mock
import logging
import configparser

from waybar_crypto import (
API_KEY_ENV,
CLASS_NAME,
DEFAULT_DISPLAY_OPTIONS_FORMAT,
DEFAULT_XDG_CONFIG_HOME_PATH,
MIN_PRECISION,
XDG_CONFIG_HOME_ENV,
CoinmarketcapApiException,
Config,
NoApiKeyException,
ResponseQuotesLatest,
WaybarCrypto,
WaybarCryptoException,
parse_args,
read_config,
)
Expand Down Expand Up @@ -260,6 +264,23 @@ def test_read_config_env():
assert config["general"]["api_key"] == TEST_API_KEY


def test_read_config_min_precision():
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("btc", "price_precision", str(MIN_PRECISION - 1))

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

try:
_ = read_config(tmp_config_path)
except Exception as e:
assert isinstance(e, WaybarCryptoException)


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

Expand Down
5 changes: 0 additions & 5 deletions waybar_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,6 @@ def read_config(config_path: str) -> Config:

for coin_precision_option in COIN_PRECISION_OPTIONS:
if coin_precision_option in cfp[coin_name]:
if not cfp[coin_name][coin_precision_option].isdigit():
raise WaybarCryptoException(
f"configured option '{coin_precision_option}' for cryptocurrency '{coin_name}' must be an integer"
)

precision_value = cfp.getint(coin_name, coin_precision_option)
if precision_value < MIN_PRECISION:
raise WaybarCryptoException(
Expand Down

0 comments on commit 815ef13

Please sign in to comment.