diff --git a/tests/config.json b/tests/config.json new file mode 100644 index 0000000..da84a0c --- /dev/null +++ b/tests/config.json @@ -0,0 +1,6 @@ +{ + "locale": "en", + "bot": { + "bot_token": "sample_token" + } +} \ No newline at end of file diff --git a/tests/data/empty.json b/tests/data/empty.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/tests/data/empty.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/tests/data/invalid.json b/tests/data/invalid.json new file mode 100644 index 0000000..47c7005 --- /dev/null +++ b/tests/data/invalid.json @@ -0,0 +1,3 @@ +{ + "foo": 'bar' +} \ No newline at end of file diff --git a/tests/data/test.json b/tests/data/test.json new file mode 100644 index 0000000..f40cf4c --- /dev/null +++ b/tests/data/test.json @@ -0,0 +1,15 @@ +{ + "foo": "bar", + "abcdefg": [ + "higklmnop", + { + "lol": { + "kek": [ + 1.0000035, + 0.2542, + 1337 + ] + } + } + ] +} \ No newline at end of file diff --git a/tests/test_config_async.py b/tests/test_config_async.py new file mode 100644 index 0000000..ffe1ed0 --- /dev/null +++ b/tests/test_config_async.py @@ -0,0 +1,42 @@ +from typing import Any, List + +import pytest + +from libbot import config_get, config_set + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "args, expected", + [ + (["locale"], "en"), + (["bot_token", "bot"], "sample_token"), + ], +) +async def test_config_get_valid(args: List[str], expected: str): + assert await config_get(args[0], *args[1:]) == expected + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "args, expected", + [ + (["bot_stonks", "bot"], pytest.raises(KeyError)), + ], +) +async def test_config_get_invalid(args: List[str], expected: Any): + with expected: + assert await config_get(args[0], *args[1:]) == expected + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "key, path, value", + [ + ("locale", [], "en"), + ("bot_token", ["bot"], "sample_token"), + ], +) +async def test_config_set(key: str, path: List[str], value: Any): + await config_set(key, value, *path) + assert await config_get(key, *path) == value diff --git a/tests/test_config_sync.py b/tests/test_config_sync.py new file mode 100644 index 0000000..b675894 --- /dev/null +++ b/tests/test_config_sync.py @@ -0,0 +1,39 @@ +from typing import Any, List + +import pytest + +from libbot import sync + + +@pytest.mark.parametrize( + "args, expected", + [ + (["locale"], "en"), + (["bot_token", "bot"], "sample_token"), + ], +) +def test_config_get_valid(args: List[str], expected: str): + assert sync.config_get(args[0], *args[1:]) == expected + + +@pytest.mark.parametrize( + "args, expected", + [ + (["bot_stonks", "bot"], pytest.raises(KeyError)), + ], +) +def test_config_get_invalid(args: List[str], expected: Any): + with expected: + assert sync.config_get(args[0], *args[1:]) == expected + + +@pytest.mark.parametrize( + "key, path, value", + [ + ("locale", [], "en"), + ("bot_token", ["bot"], "sample_token"), + ], +) +def test_config_set(key: str, path: List[str], value: Any): + sync.config_set(key, value, *path) + assert sync.config_get(key, *path) == value diff --git a/tests/test_json_async.py b/tests/test_json_async.py new file mode 100644 index 0000000..30dbf58 --- /dev/null +++ b/tests/test_json_async.py @@ -0,0 +1,60 @@ +from json import JSONDecodeError, dumps +from pathlib import Path +from typing import Any, Union + +import pytest + +from libbot import json_read, json_write + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "path, expected", + [ + ( + "data/test.json", + { + "foo": "bar", + "abcdefg": ["higklmnop", {"lol": {"kek": [1.0000035, 0.2542, 1337]}}], + }, + ), + ("data/empty.json", {}), + ], +) +async def test_json_read_valid(path: Union[str, Path], expected: Any): + assert await json_read(path) == expected + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "path, expected", + [ + ("data/invalid.json", pytest.raises(JSONDecodeError)), + ("data/nonexistent.json", pytest.raises(FileNotFoundError)), + ], +) +async def test_json_read_invalid(path: Union[str, Path], expected: Any): + with expected: + assert await json_read(path) == expected + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "data, path", + [ + ( + { + "foo": "bar", + "abcdefg": ["higklmnop", {"lol": {"kek": [1.0000035, 0.2542, 1337]}}], + }, + "data/test.json", + ), + ({}, "data/empty.json"), + ], +) +async def test_json_write(data: Any, path: Union[str, Path]): + await json_write(data, path) + + assert Path(path).is_file() + with open(path, "r", encoding="utf-8") as f: + assert f.read() == dumps(data, ensure_ascii=False, indent=4) diff --git a/tests/test_json_sync.py b/tests/test_json_sync.py new file mode 100644 index 0000000..f230788 --- /dev/null +++ b/tests/test_json_sync.py @@ -0,0 +1,57 @@ +from json import JSONDecodeError, dumps +from pathlib import Path +from typing import Any, Union + +import pytest + +from libbot import sync + + +@pytest.mark.parametrize( + "path, expected", + [ + ( + "data/test.json", + { + "foo": "bar", + "abcdefg": ["higklmnop", {"lol": {"kek": [1.0000035, 0.2542, 1337]}}], + }, + ), + ("data/empty.json", {}), + ], +) +def test_json_read_valid(path: Union[str, Path], expected: Any): + assert sync.json_read(path) == expected + + +@pytest.mark.parametrize( + "path, expected", + [ + ("data/invalid.json", pytest.raises(JSONDecodeError)), + ("data/nonexistent.json", pytest.raises(FileNotFoundError)), + ], +) +def test_json_read_invalid(path: Union[str, Path], expected: Any): + with expected: + assert sync.json_read(path) == expected + + +@pytest.mark.parametrize( + "data, path", + [ + ( + { + "foo": "bar", + "abcdefg": ["higklmnop", {"lol": {"kek": [1.0000035, 0.2542, 1337]}}], + }, + "data/test.json", + ), + ({}, "data/empty.json"), + ], +) +def test_json_write(data: Any, path: Union[str, Path]): + sync.json_write(data, path) + + assert Path(path).is_file() + with open(path, "r", encoding="utf-8") as f: + assert f.read() == dumps(data, ensure_ascii=False, indent=4)