2
2
import pytest
3
3
from fastapi import FastAPI
4
4
from httpx import AsyncClient
5
+ from sqlalchemy .ext .asyncio import AsyncSession
6
+ from sqlalchemy .future import select
5
7
6
8
from api import models
9
+ from api .database import User
7
10
8
11
pytestmark = pytest .mark .asyncio
9
12
12
15
# If the database has entries in it, cleanup was not performed properly!
13
16
async def test_list_codejams_without_db_entries (client : AsyncClient , app : FastAPI ) -> None :
14
17
"""No codejams should be returned when the database is empty."""
15
- response = await client .get ('/codejams' )
18
+ response = await client .get (app . url_path_for ( "get_codejams" ) )
16
19
jams = response .json ()
17
20
18
21
assert response .status_code == 200
19
22
assert not jams
20
23
21
24
22
- async def test_get_nonexistent_code_jam (client : AsyncClient ) -> None :
25
+ async def test_get_nonexistent_code_jam (client : AsyncClient , app : FastAPI ) -> None :
23
26
"""Getting a nonexistent code jam should return a 404."""
24
- response = await client .get ('/codejams/ 41902' )
27
+ response = await client .get (app . url_path_for ( "get_codejam" , codejam_id = 41902 ) )
25
28
assert response .status_code == 404
26
29
27
30
28
- async def test_get_existing_code_jam (client : AsyncClient , created_codejam : models .CodeJamResponse ) -> None :
31
+ async def test_get_existing_code_jam (
32
+ client : AsyncClient ,
33
+ created_codejam : models .CodeJamResponse ,
34
+ app : FastAPI
35
+ ) -> None :
29
36
"""Getting an existing code jam should return 200."""
30
- response = await client .get (f'/codejams/ { created_codejam .id } ' )
37
+ response = await client .get (app . url_path_for ( "get_codejam" , codejam_id = created_codejam .id ) )
31
38
assert response .status_code == 200
32
39
raw = response .json ()
33
40
jam = models .CodeJamResponse (** raw )
34
41
assert jam == created_codejam
35
42
36
43
37
- async def test_list_codejams_with_existing_jam (client : AsyncClient , created_codejam : models .CodeJamResponse ) -> None :
44
+ async def test_list_codejams_with_existing_jam (
45
+ client : AsyncClient ,
46
+ created_codejam : models .CodeJamResponse ,
47
+ app : FastAPI
48
+ ) -> None :
38
49
"""Listing all code jams should return the created jam."""
39
- response = await client .get ('/codejams' )
50
+ response = await client .get (app . url_path_for ( "get_codejams" ) )
40
51
assert response .status_code == 200
41
52
raw = response .json ()
42
53
@@ -47,3 +58,24 @@ async def test_list_codejams_with_existing_jam(client: AsyncClient, created_code
47
58
# Ensure the code jam in the "single jam" endpoint matches the
48
59
# code jam we get returned from the API here.
49
60
assert jam == created_codejam
61
+
62
+
63
+ async def test_create_codejams_rejects_invalid_data (client : AsyncClient , app : FastAPI ) -> None :
64
+ """Posting invalid JSON data should return 422."""
65
+ response = await client .post (app .url_path_for ("create_codejam" ), json = {"name" : "test" })
66
+ assert response .status_code == 422
67
+
68
+
69
+ async def test_create_codejams_accepts_valid_data_and_creates_user (
70
+ client : AsyncClient ,
71
+ app : FastAPI , session : AsyncSession
72
+ ) -> None :
73
+ """Posting a valid JSON data should return 200 and the record should exitst in the DB."""
74
+ response = await client .post (app .url_path_for ("create_codejam" ), json = {
75
+ "name" : "CodeJam Test" ,
76
+ "teams" : [{"name" : "Dramatic Dragonflies" , "users" : [{"user_id" : 1 , "is_leader" : True }]}]
77
+ })
78
+ assert response .status_code == 200
79
+
80
+ # Checks whether the previously added user was actually inserted into the database.
81
+ assert (await session .execute (select (User ).where (User .id == 1 ))).scalars ().unique ().one_or_none ()
0 commit comments