Skip to content

Commit

Permalink
DRY up tests a little: put testing url into shared fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
iliakur committed Feb 20, 2023
1 parent b343839 commit c6f62d9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
from miteclock.activities import validate_shortcuts


@pytest.fixture(scope="session")
def testing_url():
return "https://abc.mite.de"


@pytest.fixture
def shortcuts():
return validate_shortcuts(
Expand Down
6 changes: 3 additions & 3 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,18 +370,18 @@ def test_show(item_type, output, application_context):
"""


def test_missing_config_dir(tmp_path):
def test_missing_config_dir(tmp_path, testing_url):
"""When config is missing we try to re-create it.
If valid input is supplied, we succeed.
"""
result = CliRunner().invoke(
cli.main, obj=tmp_path, input="6d12e0bf974df0e9\nhttps://abc.mite.de\n"
cli.main, obj=tmp_path, input=f"6d12e0bf974df0e9\n{testing_url}\n"
)
assert result.exit_code == 0
assert result.output == (
"Key not found, please enter it: 6d12e0bf974df0e9\n"
+ "Please copy/paste your mite URL: https://abc.mite.de\n"
+ f"Please copy/paste your mite URL: {testing_url}\n"
+ HELP_MSG
)
assert (
Expand Down
39 changes: 17 additions & 22 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,25 @@ def test_load_api_key_invalid(key, errmsg, tmp_path):
assert str(excinfo.value) == errmsg


def test_default_toml_content():
def test_default_toml_content(testing_url):
"""Toml content for the default config."""
url = "https://abc.mite.de"
assert to_toml(Config(url=url)) == (
f'url = "{url}"\n'
assert to_toml(Config(url=testing_url)) == (
f'url = "{testing_url}"\n'
'menu_keys = "asdfjkl;"\n\n'
"[shortcuts] # Add your shortcuts inside this section.\n"
)


def test_load_config_does_not_exist(conf_path, capsys):
def test_load_config_does_not_exist(conf_path, capsys, testing_url):
"""Handle missing config file.
Prompt for mandatory information and use it to instantiate the current config type.
"""
config = load_config(
conf_path,
prompt=prompt_for_testing("https://abc.mite.de"),
)
assert config == Config(
url="https://abc.mite.de", menu_keys="asdfjkl;", shortcuts={}
prompt=prompt_for_testing(testing_url),
)
assert config == Config(url=testing_url, menu_keys="asdfjkl;", shortcuts={})
out, _ = capsys.readouterr()
assert out == "Please copy/paste your mite URL"

Expand Down Expand Up @@ -156,18 +153,17 @@ def test_load_config_toml_parse_error(conf_path):
)


def test_load_valid_config(conf_path):
base_url = "https://abc.mite.de"
def test_load_valid_config(conf_path, testing_url):
conf_path.write_text(
f'url="{base_url}"\n'
f'url="{testing_url}"\n'
'menu_keys="abc"\n\n'
"[shortcuts]\n"
'a="test"\nb="test2"\nc = ["a", "test3"]'
'\nd = {"pattern"= "QA", "match"="strict"}'
)
config = load_config(conf_path)
assert config == Config(
base_url,
testing_url,
menu_keys="abc",
shortcuts={
"a": "test",
Expand All @@ -178,8 +174,7 @@ def test_load_valid_config(conf_path):
)


def test_load_valid_legacy_config(conf_path):
base_url = "https://abc.mite.de"
def test_load_valid_legacy_config(conf_path, testing_url):
conf_path.write_text(
'account="abc"\n'
'menu_keys="abc"\n\n'
Expand All @@ -189,7 +184,7 @@ def test_load_valid_legacy_config(conf_path):
)
config = load_config(conf_path)
assert config == Config(
url=base_url,
url=testing_url,
menu_keys="abc",
shortcuts={
"a": "test",
Expand All @@ -200,7 +195,7 @@ def test_load_valid_legacy_config(conf_path):
)


def test_legacy_config(conf_path):
def test_legacy_config(conf_path, testing_url):
"""When config file has legacy structure, we should still return latest version."""
conf_path.write_text(
"\n".join(
Expand All @@ -214,17 +209,17 @@ def test_legacy_config(conf_path):
)
config = load_config(conf_path)
assert config == Config(
url="https://abc.mite.de",
url=testing_url,
menu_keys="abc",
)


@pytest.mark.parametrize(
"url_path", ["/", "/daily", "/daily/#2021/12/26", "/#2021/12/26"]
)
def test_url_remove_path(url_path):
def test_url_remove_path(url_path, testing_url):
"""If we get any kind of path added to a URL we keep only the base."""
base_url = "https://abc.mite.de"
base_url = testing_url
assert str(Config(url=base_url + url_path).url) == base_url


Expand All @@ -235,8 +230,8 @@ def test_non_mite_url():
assert str(excinfo.value) == "url: Make sure you are using a mite url."


def test_no_menu_keys():
def test_no_menu_keys(testing_url):
"""At least one menu key should be specified, otherwise we crash early."""
with pytest.raises(ValueError) as excinfo:
Config(url="https://abc.mite.de", menu_keys="")
Config(url=testing_url, menu_keys="")
assert str(excinfo.value) == "menu_keys: At least one key must be provided."

0 comments on commit c6f62d9

Please sign in to comment.