Skip to content

Commit

Permalink
expand tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chadsr committed May 27, 2024
1 parent 79ce3af commit f1f4645
Showing 1 changed file with 108 additions and 6 deletions.
114 changes: 108 additions & 6 deletions tests/test_waybar_crypto.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,94 @@
import os
import argparse
from typing import Generator
import pytest
from unittest import mock

from waybar_crypto import (
API_KEY_ENV,
CLASS_NAME,
DEFAULT_DISPLAY_OPTIONS_FORMAT,
DEFAULT_XDG_CONFIG_HOME_PATH,
XDG_CONFIG_HOME_ENV,
Config,
NoApiKeyException,
ResponseQuotesLatest,
WaybarCrypto,
parse_args,
read_config,
)


# Get the absolute path of this script
ABS_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_PATH = f"{ABS_DIR}/../config.ini.example"
TEST_CONFIG_PATH = "/test_path"
TEST_API_KEY = "test_key"


@pytest.fixture()
def waybar_crypto():
yield WaybarCrypto(CONFIG_PATH)
def config() -> Generator[Config]:
yield {
"general": {
"currency": "eur",
"currency_symbol": "€",
"spacer_symbol": "|",
"display_options": [],
"display_options_format": DEFAULT_DISPLAY_OPTIONS_FORMAT,
"api_key": "test_key",
},
"coins": {
"btc": {
"icon": "BTC",
"in_tooltip": False,
"price_precision": 1,
"change_precision": 2,
"volume_precision": 2,
},
"eth": {
"icon": "ETH",
"in_tooltip": False,
"price_precision": 2,
"change_precision": 2,
"volume_precision": 2,
},
"dot": {
"icon": "DOT",
"in_tooltip": True,
"price_precision": 4,
"change_precision": 2,
"volume_precision": 2,
},
"avax": {
"icon": "AVAX",
"in_tooltip": True,
"price_precision": 3,
"change_precision": 2,
"volume_precision": 2,
},
},
}


@pytest.fixture()
def waybar_crypto(config: Config) -> Generator[WaybarCrypto]:
yield WaybarCrypto(config)


@pytest.fixture()
def quotes_latest():
def quotes_latest() -> Generator[ResponseQuotesLatest]:
yield {
"status": {
"timestamp": "2024-05-20T17:29:45.646Z",
"error_code": 0,
"error_message": None,
"error_message": "",
"elapsed": 5,
"credit_count": 1,
},
"data": {
"BTC": {
"id": 1,
"name": "Bitcoin",
"symbol": "BTC",
"quote": {
"EUR": {
"price": 62885.47621569202,
Expand All @@ -45,10 +100,14 @@ def quotes_latest():
"percent_change_30d": 4.71056688,
"percent_change_60d": 3.13017816,
"percent_change_90d": 33.96699196,
"last_updated": "2024-05-27T12:58:04.000Z",
}
},
},
"ETH": {
"id": 1027,
"name": "Ethereum",
"symbol": "ETH",
"quote": {
"EUR": {
"price": 2891.33408409618,
Expand All @@ -60,10 +119,14 @@ def quotes_latest():
"percent_change_30d": 0.04147897,
"percent_change_60d": -10.18412449,
"percent_change_90d": 8.36092599,
"last_updated": "2024-05-27T12:58:04.000Z",
}
},
},
"AVAX": {
"id": 5805,
"name": "Avalanche",
"symbol": "AVAX",
"quote": {
"EUR": {
"price": 34.15081432131667,
Expand All @@ -75,10 +138,14 @@ def quotes_latest():
"percent_change_30d": 3.78312279,
"percent_change_60d": -30.74974196,
"percent_change_90d": -0.83220421,
"last_updated": "2024-05-27T12:58:04.000Z",
}
},
},
"DOT": {
"id": 6636,
"name": "Polkadot",
"symbol": "DOT",
"quote": {
"EUR": {
"price": 6.9338115798384905,
Expand All @@ -90,8 +157,9 @@ def quotes_latest():
"percent_change_30d": 8.73368475,
"percent_change_60d": -19.8413195,
"percent_change_90d": -2.24744556,
"last_updated": "2024-05-27T12:58:04.000Z",
}
}
},
},
},
}
Expand Down Expand Up @@ -125,6 +193,32 @@ def test_parse_args_custom_path(mock: mock.MagicMock):
assert args["config_path"] == TEST_CONFIG_PATH


@mock.patch.dict(os.environ, {API_KEY_ENV: ""})
def test_read_config(mock: mock.MagicMock):
config = read_config(TEST_CONFIG_PATH)

assert "general" in config
general = config["general"]
assert isinstance(general, dict)
assert "currency" in general
assert "currency_symbol" in general
assert "spacer_symbol" in general
assert "display_options" in general
assert "display_options_format" in general
assert "api_key" in general

assert "coins" in config
coins = config["coins"]
assert isinstance(coins, dict)


@mock.patch.dict(os.environ, {API_KEY_ENV: TEST_API_KEY})
def test_read_config_env(mock: mock.MagicMock):
config = read_config(TEST_CONFIG_PATH)

assert config["general"]["api_key"] == TEST_API_KEY


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

Expand Down Expand Up @@ -168,3 +262,11 @@ def test_waybar_output(self, waybar_crypto: WaybarCrypto, quotes_latest: Respons
assert isinstance(output[field], str)

assert output["class"] == CLASS_NAME

@mock.patch.dict(os.environ, {API_KEY_ENV: ""})
def test_no_api_key(self, config: Config):
try:
config["general"]["api_key"] = ""
_ = WaybarCrypto(config)
except Exception as e:
assert isinstance(e, NoApiKeyException)

0 comments on commit f1f4645

Please sign in to comment.