diff --git a/df_designer/logic.py b/df_designer/logic.py index 59ff9442..f06d92ba 100644 --- a/df_designer/logic.py +++ b/df_designer/logic.py @@ -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 {} diff --git a/df_designer/main.py b/df_designer/main.py index eac031e9..db77f596 100644 --- a/df_designer/main.py +++ b/df_designer/main.py @@ -4,6 +4,7 @@ from fastapi.staticfiles import StaticFiles from df_designer.logic import get_data, save_data +from df_designer.settings import path_to_save app = FastAPI() app.mount("/static", StaticFiles(directory="static"), name="static") @@ -29,15 +30,16 @@ async def alive() -> dict[str, str]: @app.post("/save") async def save(request: Request): """Save data.""" - await save_data(request) + data = await request.json() + await save_data(path=path_to_save, data=data) return {"status": "ok"} @app.get("/get") async def get(): """Get data.""" - result = await get_data() - return result + result = await get_data(path_to_save) + return {"status": "ok", "data": result} ################################################################ @@ -45,39 +47,39 @@ async def get(): ################################################################ -# /projects -@app.get("/projects") -async def projects_get() -> dict[str, str]: - """(get projects from db) - returns JSON of all saved projects""" +# /flows +@app.get("/flows") +async def flows_get() -> dict[str, str]: + """(get flows from db) - returns JSON of all saved flows""" return {"status": "ok"} -@app.post("/projects") -async def projects_post() -> dict[str, str]: - """(add new project) - receives JSON of new project""" +@app.post("/flows") +async def flows_post() -> dict[str, str]: + """(add new flows) - receives JSON of new flows""" return {"status": "ok"} -@app.patch("/projects") -async def projects_patch() -> dict[str, str]: - """(edit all projects list) - receives JSON of edited projects""" +@app.patch("/flows") +async def flows_patch() -> dict[str, str]: + """(edit all flows list) - receives JSON of edited flows""" return {"status": "ok"} -@app.delete("/projects") -async def projects_delete() -> dict[str, str]: - """@delete (delete project) - receives projectID by query param""" +@app.delete("/flows") +async def flows_delete() -> dict[str, str]: + """@delete (delete flows) - receives flowsID by query param""" return {"status": "ok"} -@app.post("/projects/upload") -async def projects_upload_post() -> dict[str, str]: +@app.post("/flows/upload") +async def flows_upload_post() -> dict[str, str]: """upload""" return {"status": "ok"} -@app.get("/projects/download") -async def projects_download_get() -> dict[str, str]: +@app.get("/flows/download") +async def flows_download_get() -> dict[str, str]: """upload""" return {"status": "ok"} @@ -89,6 +91,8 @@ async def service_health_get() -> dict[str, str]: return {"status": "ok"} +# TODO: rename meta :do +# TODO: команды для Максима @app.get("/service/version") async def service_version_get() -> dict[str, str]: """(get dff curr version)""" diff --git a/df_designer/settings.py b/df_designer/settings.py index 0b15923f..d880bdb1 100644 --- a/df_designer/settings.py +++ b/df_designer/settings.py @@ -9,4 +9,4 @@ # app settings -path_to_save = Path().home().joinpath("data.json") +path_to_save = Path().home().joinpath("flows.json") diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 00000000..edf1916d --- /dev/null +++ b/tests/conftest.py @@ -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") diff --git a/tests/test_logic.py b/tests/test_logic.py new file mode 100644 index 00000000..13eea200 --- /dev/null +++ b/tests/test_logic.py @@ -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) == {} diff --git a/tests/test_main.py b/tests/test_main.py index e5298146..7f37c8fb 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -31,33 +31,68 @@ def test_main_get(): # assert response.json() == {"status": "true"} # make mock request -def test_projects_get(): - response = client.get("/projects") +def test_flows_get(): + response = client.get("/flows") assert response.status_code == 200 -def test_projects_post(): - response = client.post("/projects") +def test_flows_post(): + response = client.post("/flows") assert response.status_code == 200 -def test_projects_patch(): - response = client.patch("/projects") +def test_flows_patch(): + response = client.patch("/flows") assert response.status_code == 200 -def test_projects_delete(): - response = client.delete("/projects") +def test_flows_delete(): + response = client.delete("/flows") assert response.status_code == 200 -def test_projects_upload_post(): - response = client.post("/projects/upload") +def test_flows_upload_post(): + response = client.post("/flows/upload") assert response.status_code == 200 -def test_projects_download_get(): - response = client.get("/projects/download") +def test_flows_download_get(): + response = client.get("/flows/download") + assert response.status_code == 200 + + +def test_service_health_get(): + response = client.get("/service/health") + assert response.status_code == 200 + + +def test_service_version_get(): + response = client.get("/service/version") + assert response.status_code == 200 + + +def test_library_functions_get(): + response = client.get("/library/functions") + assert response.status_code == 200 + + +def test_library_llms_get(): + response = client.get("/library/llms") + assert response.status_code == 200 + + +def test_dff_tests_prompt_get(): + response = client.post("/dff/tests/prompt") + assert response.status_code == 200 + + +def test_dff_tests_condition_get(): + response = client.post("/dff/tests/condition") + assert response.status_code == 200 + + +def test_build_get(): + response = client.post("/build") assert response.status_code == 200 @@ -67,4 +102,4 @@ def test_settings(): assert settings.port == 8000 assert settings.log_level == "info" assert settings.reload is True - assert settings.path_to_save == Path().home().joinpath("data.json") + assert settings.path_to_save == Path().home().joinpath("flows.json")