1111
1212
1313@pytest_asyncio .fixture
14- async def community (session : AsyncSession ):
14+ async def community (session : AsyncSession ) -> Community :
1515 community = Community (username = "admin" , email = "a@a.com" , password = "123" )
1616 session .add (community )
1717 await session .commit ()
1818 await session .refresh (community )
1919 return community
2020
2121
22+ @pytest_asyncio .fixture
23+ async def news_list (community : Community ) -> list [News ]:
24+ news_list = [
25+ News (
26+ title = "Python 3.12 Lançado!" ,
27+ content = "A nova versão do Python traz melhorias ..." ,
28+ category = "release" ,
29+ user_email = "dev@example.com" ,
30+ source_url = "https://python.org/news" ,
31+ tags = "python, release, programming" ,
32+ social_media_url = "https://linkedin.com/pythonista" ,
33+ community_id = community .id , # Usando o ID da comunidade do fixture
34+ ),
35+ News (
36+ title = "FastAPI 0.100 Lançado!" ,
37+ content = "FastAPI agora suporta novas funcionalidades ..." ,
38+ category = "release" ,
39+ user_email = "example@pynews.com" ,
40+ source_url = "https://fastapi.com/news" ,
41+ tags = "fastapi, release, web" ,
42+ social_media_url = "https://twitter.com/fastapi" ,
43+ likes = 100 ,
44+ ),
45+ ]
46+ return news_list
47+
48+
2249@pytest .mark .asyncio
23- async def test_insert_news (session : AsyncSession , community : Community ):
50+ async def test_insert_news (
51+ session : AsyncSession , community : Community , news_list : list
52+ ):
2453 """
2554 Testa a inserção de uma notícia no banco de dados.
2655 """
27- news = News (
28- title = "Python 3.12 Lançado!" ,
29- content = "A nova versão do Python traz melhorias ..." ,
30- category = "release" ,
31- user_email = "dev@example.com" ,
32- source_url = "https://python.org/news" ,
33- tags = "python, release, programming" ,
34- social_media_url = "https://linkedin.com/pythonista" ,
35- community_id = community .id , # Usando o ID da comunidade do fixture
36- )
37- session .add (news )
56+ session .add (news_list [0 ])
3857 await session .commit ()
3958
4059 statement = select (News ).where (News .title == "Python 3.12 Lançado!" )
@@ -57,23 +76,125 @@ async def test_insert_news(session: AsyncSession, community: Community):
5776 assert found_news .updated_at >= found_news .created_at
5877
5978
60- # ADD like test case for News model
61-
6279@pytest .mark .asyncio
63- async def test_news_endpoint (
80+ async def test_post_news_endpoint (
6481 async_client : AsyncClient , mock_headers : Mapping [str , str ]
6582):
66- """Test the news endpoint returns correct status and version ."""
83+ """Test the news endpoint returns correct status."""
6784 response = await async_client .post ("/api/news" , headers = mock_headers )
6885
6986 assert response .status_code == status .HTTP_200_OK
7087 assert response .json () == {"status" : "News Criada" }
7188
7289
7390@pytest .mark .asyncio
74- async def test_news_endpoint_without_auth (async_client : AsyncClient ):
75- """Test the news endpoint without authentication headers."""
76- response = await async_client .post ("/api/news" )
91+ async def test_get_news_endpoint (
92+ session : AsyncSession ,
93+ async_client : AsyncClient ,
94+ mock_headers : Mapping [str , str ],
95+ news_list : list ,
96+ ):
97+ session .add (news_list [0 ])
98+ session .add (news_list [1 ])
99+ await session .commit ()
100+
101+ """Test the news endpoint returns correct status and version."""
102+ response = await async_client .get (
103+ "/api/news" ,
104+ headers = mock_headers ,
105+ )
77106
78107 assert response .status_code == status .HTTP_200_OK
79- assert response .json () == {"status" : "News Criada" }
108+ assert "news_list" in response .json ()
109+ assert len (response .json ()["news_list" ]) == 2
110+
111+
112+ @pytest .mark .asyncio
113+ async def test_get_news_by_category (
114+ session : AsyncSession ,
115+ async_client : AsyncClient ,
116+ mock_headers : Mapping [str , str ],
117+ news_list : list ,
118+ ):
119+ # Add news to DB
120+ session .add_all (news_list )
121+ await session .commit ()
122+
123+ # Filter by category
124+ response = await async_client .get (
125+ "/api/news" ,
126+ params = {"category" : "release" },
127+ headers = {"Content-Type" : "application/json" },
128+ )
129+ data = response .json ()
130+ assert response .status_code == status .HTTP_200_OK
131+ assert "news_list" in data
132+ assert len (data ["news_list" ]) == 2
133+ titles = [news ["title" ] for news in data ["news_list" ]]
134+ assert "Python 3.12 Lançado!" in titles
135+ assert "FastAPI 0.100 Lançado!" in titles
136+
137+
138+ @pytest .mark .asyncio
139+ async def test_get_news_by_user_email (
140+ session : AsyncSession , async_client : AsyncClient , news_list : list
141+ ):
142+ session .add_all (news_list )
143+ await session .commit ()
144+
145+ response = await async_client .get (
146+ "/api/news" ,
147+ params = {},
148+ headers = {
149+ "Content-Type" : "application/json" ,
150+ "user-email" : "dev@example.com" ,
151+ },
152+ )
153+ data = response .json ()
154+ assert response .status_code == status .HTTP_200_OK
155+ assert len (data ["news_list" ]) == 1
156+ assert data ["news_list" ][0 ]["user_email" ] == "dev@example.com"
157+ assert data ["news_list" ][0 ]["title" ] == "Python 3.12 Lançado!"
158+
159+
160+ @pytest .mark .asyncio
161+ async def test_get_news_by_id (
162+ session : AsyncSession ,
163+ async_client : AsyncClient ,
164+ mock_headers : Mapping [str , str ],
165+ news_list : list ,
166+ ):
167+ session .add_all (news_list )
168+ await session .commit ()
169+ # Get the id from DB
170+ statement = select (News ).where (News .title == "Python 3.12 Lançado!" )
171+ result = await session .exec (statement )
172+ news = result .first ()
173+ response = await async_client .get (
174+ "/api/news" ,
175+ params = {"id" : news .id },
176+ headers = mock_headers ,
177+ )
178+ data = response .json ()
179+ assert response .status_code == status .HTTP_200_OK
180+ assert len (data ["news_list" ]) == 1
181+ assert data ["news_list" ][0 ]["id" ] == news .id
182+ assert data ["news_list" ][0 ]["title" ] == "Python 3.12 Lançado!"
183+
184+
185+ @pytest .mark .asyncio
186+ async def test_get_news_empty_result (
187+ async_client : AsyncClient , mock_headers : Mapping [str , str ]
188+ ):
189+ response = await async_client .get (
190+ "/api/news" ,
191+ params = {"category" : "notfound" },
192+ headers = mock_headers ,
193+ )
194+ data = response .json ()
195+ assert response .status_code == status .HTTP_200_OK
196+ assert "news_list" in data
197+ assert data ["news_list" ] == []
198+
199+
200+ # ADD like test case for News model
0 commit comments