Skip to content

Commit

Permalink
Add create test
Browse files Browse the repository at this point in the history
  • Loading branch information
Yarik-Popov committed Nov 4, 2024
1 parent c67647e commit b124460
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
7 changes: 7 additions & 0 deletions backend/api/models/response_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ class MainCommandListResponse(BaseModel):
"""

data: list[MainCommand]


class CommandSingleResponse(BaseModel):
"""
Single command
"""
data: Command
17 changes: 12 additions & 5 deletions backend/api/resources/command.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from fastapi import APIRouter, Depends
from fastapi import APIRouter, Depends, params
from sqlmodel import Session, select

from backend.api.models.request_model import CommandRequest
from backend.api.models.response_model import CommandListResponse
from backend.api.models.response_model import CommandListResponse, CommandSingleResponse
from backend.data.data_models import Command
from backend.data.engine import get_db

# Prefix: "/commands"
command_router = APIRouter(tags=["Commands"])


Expand All @@ -21,15 +22,21 @@ def get_commands(db: Session = Depends(get_db)):
return {"data": items}


@command_router.post("/", response_model=Command)
def create_command(payload: CommandRequest):
@command_router.post("/", response_model=CommandSingleResponse)
def create_command(payload: CommandRequest, db: Session = Depends(get_db)):
"""
Creates an item with the given payload and returns the payload with some other information
@param payload: The data used to create an item
@return returns the data with some other information
@return returns a json object with field of "data" under which there is the payload with some other information
"""
# TODO: Implement this endpoint
command = Command(command_type=payload.command_type, params=payload.params)
db.add(command)
db.commit()
db.refresh(command)
return {"data":command}



@command_router.delete("/{id}", response_model=CommandListResponse)
Expand Down
34 changes: 30 additions & 4 deletions test/backend/test_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@

def test_get_commands(fastapi_test_client, commands_json):
from fastapi.testclient import TestClient

from backend.api.models.request_model import CommandRequest
from backend.data.enums import CommandStatus


def test_get_commands(fastapi_test_client: TestClient, commands_json):
with fastapi_test_client as client:
response = client.get("/commands/")
assert response.status_code == 200
assert response.json() == {"data": commands_json}
res = client.get("/commands/")
assert res.status_code == 200
assert res.json() == {"data": commands_json}


def test_create_command(fastapi_test_client: TestClient):
command = CommandRequest(command_type=1, params="123456789")
model_dump = command.model_dump()
print(model_dump)
with fastapi_test_client as client:
res = client.post("/commands/", json=model_dump, headers={"Content-Type": "application/json"})
# res = client.post("/commands/", json=command)
res.raise_for_status()
assert res.status_code == 200
result = res.json().get("data")
assert result is not None
assert result.get("id") == 3
assert result.get("command_type") == 1
assert result.get("status") == CommandStatus.PENDING.value
assert result.get("params") == "123456789"
# TODO: Figure out a better way to check the times
assert result.get("created_on")
assert result.get("updated_on")

0 comments on commit b124460

Please sign in to comment.