Repository navigation
Expand file tree
/
Copy pathtest_decks.py
More file actions
140 lines (109 loc) · 4.13 KB
/
Copy pathtest_decks.py
File metadata and controls
140 lines (109 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from typing import TYPE_CHECKING
import pytest
from fastapi import status
from tests import factories
if TYPE_CHECKING:
from httpx import AsyncClient
pytestmark = [pytest.mark.usefixtures("set_async_session_in_base_sqlalchemy_factory")]
async def test_get_decks_empty(client: AsyncClient) -> None:
response = await client.get("/api/decks/")
assert response.status_code == status.HTTP_200_OK
assert len(response.json()["items"]) == 0
async def test_get_decks_not_exist(client: AsyncClient) -> None:
response = await client.get("/api/decks/0/")
assert response.status_code == status.HTTP_404_NOT_FOUND
async def test_get_decks(client: AsyncClient) -> None:
deck = await factories.DeckModelFactory.create_async()
response = await client.get("/api/decks/")
assert response.status_code == status.HTTP_200_OK
data = response.json()
assert len(data["items"]) == 1
for k, v in data["items"][0].items():
assert v == getattr(deck, k)
async def test_list_decks_omits_cards(client: AsyncClient) -> None:
deck = await factories.DeckModelFactory.create_async()
await factories.CardModelFactory.create_async(deck_id=deck.id)
response = await client.get("/api/decks/")
assert response.status_code == status.HTTP_200_OK
item = response.json()["items"][0]
assert "cards" not in item
async def test_get_one_deck(client: AsyncClient) -> None:
deck = await factories.DeckModelFactory.create_async()
card = await factories.CardModelFactory.create_async(deck_id=deck.id)
assert card.id
response = await client.get(f"/api/decks/{deck.id}/")
assert response.status_code == status.HTTP_200_OK
data = response.json()
assert len(data["cards"]) == 1
for k, v in data.items():
if k == "cards":
continue
assert v == getattr(deck, k)
@pytest.mark.parametrize(
("name", "description", "status_code"),
[
(None, None, status.HTTP_422_UNPROCESSABLE_CONTENT),
("test deck", None, status.HTTP_200_OK),
("test deck", "test deck description", status.HTTP_200_OK),
],
)
async def test_post_decks(
client: AsyncClient,
name: str,
description: str,
status_code: int,
) -> None:
# create deck
response = await client.post(
"/api/decks/",
json={
"name": name,
"description": description,
},
)
assert response.status_code == status_code
# get item
if status_code == status.HTTP_200_OK:
item_id = response.json()["id"]
response = await client.get(f"/api/decks/{item_id}/")
assert response.status_code == status.HTTP_200_OK
result = response.json()
assert item_id == result["id"]
assert name == result["name"]
assert description == result["description"]
async def test_put_decks_wrong_body(client: AsyncClient) -> None:
deck = await factories.DeckModelFactory.create_async()
# update deck
response = await client.put(
f"/api/decks/{deck.id}/",
json={"name": None, "description": None},
)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_CONTENT
async def test_put_decks_not_exist(client: AsyncClient) -> None:
response = await client.put(
"/api/decks/999/",
json={"name": "some", "description": "some"},
)
assert response.status_code == status.HTTP_404_NOT_FOUND
@pytest.mark.parametrize(
("name", "description"),
[
("test deck updated", None),
("test deck updated", "test deck description updated"),
],
)
async def test_put_decks(client: AsyncClient, name: str, description: str) -> None:
deck = await factories.DeckModelFactory.create_async()
# update deck
response = await client.put(
f"/api/decks/{deck.id}/",
json={"name": name, "description": description},
)
assert response.status_code == status.HTTP_200_OK
# get item
item_id = response.json()["id"]
response = await client.get(f"/api/decks/{item_id}/")
assert response.status_code == status.HTTP_200_OK
result = response.json()
assert name == result["name"]
assert description == result["description"]