1
+ from datetime import date
2
+
1
3
import pytest
2
4
import pytest_asyncio
3
5
from httpx import AsyncClient
4
6
from sqlmodel import select
5
7
from sqlmodel .ext .asyncio .session import AsyncSession
6
8
9
+ from app .enums import LibraryTagUpdatesEnum
10
+ from app .schemas import LibraryNews
7
11
from app .services .database .models import Community , Library
8
12
9
13
@@ -19,24 +23,38 @@ async def community(session: AsyncSession):
19
23
@pytest .mark .asyncio
20
24
async def test_insert_libraries (session : AsyncSession , community : Community ):
21
25
library = Library (
22
- library_name = "Python" ,
23
- user_email = "teste@teste.com" ,
24
- releases_url = "http://teste.com" ,
25
- logo = "logo" ,
26
+ library_name = "Flask" ,
27
+ news = [
28
+ LibraryNews (
29
+ tag = LibraryTagUpdatesEnum .UPDATE , description = "Updated"
30
+ ).model_dump (),
31
+ LibraryNews (
32
+ tag = LibraryTagUpdatesEnum .BUG_FIX , description = "Fixed"
33
+ ).model_dump (),
34
+ ],
35
+ logo = "http://teste.com/" ,
36
+ version = "3.11" ,
37
+ release_date = date .today (),
38
+ releases_doc_url = "http://teste.com/" ,
39
+ fixed_release_url = "http://teste.com/" ,
40
+ language = "Python" ,
26
41
community_id = community .id ,
27
42
)
28
43
session .add (library )
29
44
await session .commit ()
30
45
31
- statement = select (Library ).where (Library .library_name == "Python " )
46
+ statement = select (Library ).where (Library .library_name == "Flask " )
32
47
result = await session .exec (statement )
33
48
found = result .first ()
34
49
35
50
assert found is not None
36
- assert found .library_name == "Python"
37
- assert found .user_email == "teste@teste.com"
38
- assert found .releases_url == "http://teste.com"
39
- assert found .logo == "logo"
51
+ assert len (found .news ) == 2
52
+ assert found .logo == "http://teste.com/"
53
+ assert found .version == "3.11"
54
+ assert found .release_date == date .today ()
55
+ assert found .releases_doc_url == "http://teste.com/"
56
+ assert found .fixed_release_url == "http://teste.com/"
57
+ assert found .language == "Python"
40
58
assert found .community_id == community .id
41
59
42
60
@@ -45,9 +63,17 @@ async def test_post_libraries_endpoint(
45
63
async_client : AsyncClient , session : AsyncSession
46
64
):
47
65
body = {
48
- "library_name" : "Python from API" ,
49
- "releases_url" : "http://teste.com/" ,
66
+ "library_name" : "FastAPI" ,
67
+ "news" : [
68
+ {"tag" : "updates" , "description" : "Updated" },
69
+ {"tag" : "bug_fix" , "description" : "Fixed" },
70
+ ],
50
71
"logo" : "http://teste.com/" ,
72
+ "version" : "3.11" ,
73
+ "release_date" : "2025-01-01" ,
74
+ "releases_doc_url" : "http://teste.com/" ,
75
+ "fixed_release_url" : "http://teste.com/" ,
76
+ "language" : "Python" ,
51
77
}
52
78
53
79
response = await async_client .post (
@@ -66,5 +92,98 @@ async def test_post_libraries_endpoint(
66
92
created_library = result .first ()
67
93
68
94
assert created_library is not None
69
- assert created_library .releases_url == body ["releases_url" ]
70
95
assert created_library .logo == body ["logo" ]
96
+ assert created_library .version == body ["version" ]
97
+ assert created_library .release_date == date .fromisoformat (
98
+ body ["release_date" ]
99
+ )
100
+ assert created_library .releases_doc_url == body ["releases_doc_url" ]
101
+ assert created_library .fixed_release_url == body ["fixed_release_url" ]
102
+ assert created_library .language == body ["language" ]
103
+ assert len (created_library .news ) == len (body ["news" ])
104
+
105
+
106
+ @pytest .mark .asyncio
107
+ async def test_get_libraries_by_language (async_client : AsyncClient ):
108
+ body = {
109
+ "library_name" : "FastAPI" ,
110
+ "news" : [
111
+ {"tag" : "updates" , "description" : "Updated" },
112
+ {"tag" : "bug_fix" , "description" : "Fixed" },
113
+ ],
114
+ "logo" : "http://teste.com/" ,
115
+ "version" : "3.11" ,
116
+ "release_date" : "2025-01-01" ,
117
+ "releases_doc_url" : "http://teste.com/" ,
118
+ "fixed_release_url" : "http://teste.com/" ,
119
+ "language" : "Python" ,
120
+ }
121
+
122
+ responsePOST = await async_client .post (
123
+ "/api/libraries" ,
124
+ json = body ,
125
+ headers = {"Content-Type" : "application/json" },
126
+ )
127
+
128
+ assert responsePOST .status_code == 200
129
+ assert responsePOST .json ()["status" ] == "Library created successfully"
130
+
131
+ response = await async_client .get (
132
+ "/api/libraries" ,
133
+ params = {"language" : "Python" },
134
+ )
135
+
136
+ assert response .status_code == 200
137
+
138
+ data = response .json ()
139
+
140
+ assert len (data ) == 1
141
+ assert data [0 ]["library_name" ] == "FastAPI"
142
+ assert len (data [0 ]["news" ]) == 2
143
+ assert data [0 ]["news" ][0 ]["tag" ] == "updates"
144
+ assert data [0 ]["news" ][0 ]["description" ] == "Updated"
145
+ assert data [0 ]["news" ][1 ]["tag" ] == "bug_fix"
146
+ assert data [0 ]["news" ][1 ]["description" ] == "Fixed"
147
+ assert data [0 ]["logo" ] == "http://teste.com/"
148
+ assert data [0 ]["version" ] == "3.11"
149
+ assert data [0 ]["release_date" ] == "2025-01-01"
150
+ assert data [0 ]["releases_doc_url" ] == "http://teste.com/"
151
+ assert data [0 ]["fixed_release_url" ] == "http://teste.com/"
152
+ assert data [0 ]["language" ] == "Python"
153
+
154
+
155
+ @pytest .mark .asyncio
156
+ async def test_get_libraries_by_inexistent_language (async_client : AsyncClient ):
157
+ body = {
158
+ "library_name" : "FastAPI" ,
159
+ "news" : [
160
+ {"tag" : "updates" , "description" : "Updated" },
161
+ {"tag" : "bug_fix" , "description" : "Fixed" },
162
+ ],
163
+ "logo" : "http://teste.com/" ,
164
+ "version" : "3.11" ,
165
+ "release_date" : "2025-01-01" ,
166
+ "releases_doc_url" : "http://teste.com/" ,
167
+ "fixed_release_url" : "http://teste.com/" ,
168
+ "language" : "Python" ,
169
+ }
170
+
171
+ responsePOST = await async_client .post (
172
+ "/api/libraries" ,
173
+ json = body ,
174
+ headers = {"Content-Type" : "application/json" },
175
+ )
176
+
177
+ assert responsePOST .status_code == 200
178
+ assert responsePOST .json ()["status" ] == "Library created successfully"
179
+
180
+ response = await async_client .get (
181
+ "/api/libraries" ,
182
+ params = {"language" : "NodeJS" },
183
+ )
184
+
185
+ assert response .status_code == 200
186
+
187
+ data = response .json ()
188
+
189
+ assert len (data ) == 0
0 commit comments