Compare commits
2 Commits
dc107ebdb3
...
253c85985b
Author | SHA1 | Date | |
---|---|---|---|
253c85985b
|
|||
11d49fd476
|
@@ -35,6 +35,8 @@ async def json_write(data: Any, path: Union[str, Path]) -> None:
|
||||
async with aiofiles.open(str(path), mode="w", encoding="utf-8") as f:
|
||||
await f.write(
|
||||
dumps(data, ensure_ascii=False, escape_forward_slashes=False, indent=4)
|
||||
if hasattr(dumps, "escape_forward_slashes")
|
||||
else dumps(data, ensure_ascii=False, indent=4)
|
||||
)
|
||||
|
||||
|
||||
|
@@ -29,7 +29,11 @@ def json_write(data: Any, path: Union[str, Path]) -> None:
|
||||
* path (`Union[str, Path]`): Path-like object or path as a string of a destination
|
||||
"""
|
||||
with open(str(path), mode="w", encoding="utf-8") as f:
|
||||
f.write(dumps(data, ensure_ascii=False, escape_forward_slashes=False, indent=4))
|
||||
f.write(
|
||||
dumps(data, ensure_ascii=False, escape_forward_slashes=False, indent=4)
|
||||
if hasattr(dumps, "escape_forward_slashes")
|
||||
else dumps(data, ensure_ascii=False, indent=4)
|
||||
)
|
||||
|
||||
|
||||
def nested_set(target: dict, value: Any, *path: str, create_missing=True) -> dict:
|
||||
|
6
tests/config.json
Normal file
6
tests/config.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"locale": "en",
|
||||
"bot": {
|
||||
"bot_token": "sample_token"
|
||||
}
|
||||
}
|
1
tests/data/empty.json
Normal file
1
tests/data/empty.json
Normal file
@@ -0,0 +1 @@
|
||||
{}
|
3
tests/data/invalid.json
Normal file
3
tests/data/invalid.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"foo": 'bar'
|
||||
}
|
15
tests/data/test.json
Normal file
15
tests/data/test.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"foo": "bar",
|
||||
"abcdefg": [
|
||||
"higklmnop",
|
||||
{
|
||||
"lol": {
|
||||
"kek": [
|
||||
1.0000035,
|
||||
0.2542,
|
||||
1337
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
42
tests/test_config_async.py
Normal file
42
tests/test_config_async.py
Normal 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
39
tests/test_config_sync.py
Normal 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
60
tests/test_json_async.py
Normal 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
57
tests/test_json_sync.py
Normal 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)
|
Reference in New Issue
Block a user