Added some basic test

This commit is contained in:
Profitroll 2023-08-06 01:25:32 +02:00
parent 11d49fd476
commit 253c85985b
Signed by: profitroll
GPG Key ID: FA35CAB49DACD3B2
8 changed files with 223 additions and 0 deletions

6
tests/config.json Normal file
View File

@ -0,0 +1,6 @@
{
"locale": "en",
"bot": {
"bot_token": "sample_token"
}
}

1
tests/data/empty.json Normal file
View File

@ -0,0 +1 @@
{}

3
tests/data/invalid.json Normal file
View File

@ -0,0 +1,3 @@
{
"foo": 'bar'
}

15
tests/data/test.json Normal file
View File

@ -0,0 +1,15 @@
{
"foo": "bar",
"abcdefg": [
"higklmnop",
{
"lol": {
"kek": [
1.0000035,
0.2542,
1337
]
}
}
]
}

View File

@ -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

39
tests/test_config_sync.py Normal file
View File

@ -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

60
tests/test_json_async.py Normal file
View File

@ -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)

57
tests/test_json_sync.py Normal file
View File

@ -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)