1+ from datetime import date
2+
13import pytest
24import pytest_asyncio
35from httpx import AsyncClient
46from sqlmodel import select
57from sqlmodel .ext .asyncio .session import AsyncSession
68
9+ from app .enums import LibraryTagUpdatesEnum
10+ from app .schemas import LibraryNews
711from app .services .database .models import Community , Library
812
913
@@ -19,24 +23,38 @@ async def community(session: AsyncSession):
1923@pytest .mark .asyncio
2024async def test_insert_libraries (session : AsyncSession , community : Community ):
2125 library = Library (
22- library_name = "Python" ,
23- 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" ,
2641 community_id = community .id ,
2742 )
2843 session .add (library )
2944 await session .commit ()
3045
31- statement = select (Library ).where (Library .library_name == "Python " )
46+ statement = select (Library ).where (Library .library_name == "Flask " )
3247 result = await session .exec (statement )
3348 found = result .first ()
3449
3550 assert found is not None
36- assert found .library_name == "Python"
37- assert found .
user_email == "[email protected] " 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"
4058 assert found .community_id == community .id
4159
4260
@@ -45,9 +63,17 @@ async def test_post_libraries_endpoint(
4563 async_client : AsyncClient , session : AsyncSession
4664):
4765 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+ ],
5071 "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" ,
5177 }
5278
5379 response = await async_client .post (
@@ -66,5 +92,98 @@ async def test_post_libraries_endpoint(
6692 created_library = result .first ()
6793
6894 assert created_library is not None
69- assert created_library .releases_url == body ["releases_url" ]
7095 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