Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
optk2k committed Dec 11, 2023
1 parent 689aecc commit a8033b1
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 44 deletions.
17 changes: 7 additions & 10 deletions df_designer/logic.py
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 {}
44 changes: 24 additions & 20 deletions df_designer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -29,55 +30,56 @@ 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}


################################################################
# new methods
################################################################


# /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"}

Expand All @@ -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)"""
Expand Down
2 changes: 1 addition & 1 deletion df_designer/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@


# app settings
path_to_save = Path().home().joinpath("data.json")
path_to_save = Path().home().joinpath("flows.json")
15 changes: 15 additions & 0 deletions tests/conftest.py
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")
35 changes: 35 additions & 0 deletions tests/test_logic.py
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) == {}
61 changes: 48 additions & 13 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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")

0 comments on commit a8033b1

Please sign in to comment.