-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
130 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,22 @@ | ||
import json | ||
import os | ||
from pathlib import Path | ||
from typing import Any | ||
|
||
import aiofiles | ||
from fastapi import Request | ||
from pydantic import Json | ||
|
||
from df_designer.settings import path_to_save | ||
|
||
|
||
async def save_data(request: Request): | ||
async def save_data(path: Path, data: dict[str, str]): | ||
"""Save the json config.""" | ||
result = await request.json() | ||
async with aiofiles.open(path_to_save, "w", encoding="utf-8") as file: | ||
await file.write(json.dumps(result)) | ||
async with aiofiles.open(path, "w", encoding="utf-8") as file: | ||
await file.write(json.dumps(data)) | ||
|
||
|
||
async def get_data() -> Json[Any]: | ||
async def get_data(path: Path) -> Json[Any]: | ||
"""Get the json config.""" | ||
if os.path.exists(path_to_save): | ||
async with aiofiles.open(path_to_save, "r", encoding="utf-8") as file: | ||
if os.path.exists(path): | ||
async with aiofiles.open(path, "r", encoding="utf-8") as file: | ||
return json.loads(await file.read()) | ||
else: | ||
return {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
# @pytest.fixture(scope="session", autouse=True) | ||
# def test_folder(): | ||
# """create a folder""" | ||
# print("]---starting test---[") | ||
# yield | ||
# print("]---ending test---[") | ||
|
||
|
||
@pytest.fixture | ||
def test_file_path(): | ||
return Path("/tmp").joinpath("test.json") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import json | ||
from pathlib import Path | ||
|
||
import aiofiles | ||
import pytest | ||
|
||
from df_designer.logic import get_data, save_data | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_logic_save_data(test_file_path: Path): | ||
"""Test save json.""" | ||
await save_data(test_file_path, data={"status": "ok"}) | ||
|
||
async with aiofiles.open(test_file_path, "r", encoding="utf-8") as file: | ||
assert json.loads(await file.read()) == {"status": "ok"} | ||
|
||
Path(test_file_path).unlink() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_logic_get_data(test_file_path: Path): | ||
"""Test read json.""" | ||
async with aiofiles.open(test_file_path, "w", encoding="utf-8") as file: | ||
await file.write(json.dumps({"status": "ok"})) | ||
|
||
assert await get_data(test_file_path) == {"status": "ok"} | ||
|
||
Path(test_file_path).unlink() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_logic_get_data_empty(test_file_path: Path): | ||
"""Test return if no file.""" | ||
assert await get_data(test_file_path) == {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters