Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
anton2yakovlev committed Mar 16, 2024
1 parent 709fbc5 commit 4ceed09
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 44 deletions.
15 changes: 2 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from model import get_word_response, get_words_list_response
from words import words_accordind_host, words_accordind_Dvinyatin, words_accordind_dictionary
import random

app = FastAPI()

def form_correct_format(content):
return JSONResponse(content=content, media_type="application/json; charset=utf-8")

def get_word_response(words_list):
content = {"word": random.choice(words_list)}
return form_correct_format(content)

def get_words_list_response(words_list):
content = {"words": words_list}
return form_correct_format(content)
app = FastAPI()

@app.get("/get-so-word")
def get_random_word():
Expand Down
14 changes: 14 additions & 0 deletions model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from fastapi.responses import JSONResponse
import random


def form_correct_format(content):
return JSONResponse(content=content, media_type="application/json; charset=utf-8")

def get_word_response(words_list):
content = {"word": random.choice(words_list)}
return form_correct_format(content)

def get_words_list_response(words_list):
content = {"words": words_list}
return form_correct_format(content)
47 changes: 16 additions & 31 deletions test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,36 @@
from httpx import ASGITransport, AsyncClient
from main import app

@pytest.mark.asyncio
async def test_get_random_word():

async def template(url: str, plural=False):
json_key = "words" if plural else "word"
expected_type = list if plural else str
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
response = await ac.get("/get-so-word")
response = await ac.get("/" + url)
assert response.status_code == 200
assert "word" in response.json()
assert isinstance(response.json()["word"], str)
assert json_key in response.json()
assert isinstance(response.json()[json_key], expected_type)

@pytest.mark.asyncio
async def test_get_random_word():
await template("get-so-word")

@pytest.mark.asyncio
async def test_get_random_host_word():
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
response = await ac.get("/get-host-so-word")
assert response.status_code == 200
assert "word" in response.json()
assert isinstance(response.json()["word"], str)
await template("get-host-so-word")

@pytest.mark.asyncio
async def test_get_random_dvinyatin_word():
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
response = await ac.get("/get-dvinyatin-so-word")
assert response.status_code == 200
assert "word" in response.json()
assert isinstance(response.json()["word"], str)

await template("get-dvinyatin-so-word")

@pytest.mark.asyncio
async def test_get_all_words():
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
response = await ac.get("/get-all-so-word")
assert response.status_code == 200
assert "words" in response.json()
assert isinstance(response.json()["words"], list)
await template("get-all-so-word", plural=True)

@pytest.mark.asyncio
async def test_get_all_host_words():
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
response = await ac.get("/get-all-host-so-word")
assert response.status_code == 200
assert "words" in response.json()
assert isinstance(response.json()["words"], list)
await template("get-all-host-so-word", plural=True)

@pytest.mark.asyncio
async def test_get_all_dvinyatin_words():
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
response = await ac.get("/get-all-dvinyatin-so-word")
assert response.status_code == 200
assert "words" in response.json()
assert isinstance(response.json()["words"], list)
await template("get-all-dvinyatin-so-word", plural=True)

0 comments on commit 4ceed09

Please sign in to comment.