11
11
12
12
13
13
@pytest_asyncio .fixture
14
- async def community (session : AsyncSession ):
14
+ async def community (session : AsyncSession ) -> Community :
15
15
community = Community (
username = "admin" ,
email = "[email protected] " ,
password = "123" )
16
16
session .add (community )
17
17
await session .commit ()
18
18
await session .refresh (community )
19
19
return community
20
20
21
21
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
+
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
+
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
+
22
49
@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
+ ):
24
53
"""
25
54
Testa a inserção de uma notícia no banco de dados.
26
55
"""
27
- news = News (
28
- title = "Python 3.12 Lançado!" ,
29
- content = "A nova versão do Python traz melhorias ..." ,
30
- category = "release" ,
31
-
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 ])
38
57
await session .commit ()
39
58
40
59
statement = select (News ).where (News .title == "Python 3.12 Lançado!" )
@@ -57,23 +76,125 @@ async def test_insert_news(session: AsyncSession, community: Community):
57
76
assert found_news .updated_at >= found_news .created_at
58
77
59
78
60
- # ADD like test case for News model
61
-
62
79
@pytest .mark .asyncio
63
- async def test_news_endpoint (
80
+ async def test_post_news_endpoint (
64
81
async_client : AsyncClient , mock_headers : Mapping [str , str ]
65
82
):
66
- """Test the news endpoint returns correct status and version ."""
83
+ """Test the news endpoint returns correct status."""
67
84
response = await async_client .post ("/api/news" , headers = mock_headers )
68
85
69
86
assert response .status_code == status .HTTP_200_OK
70
87
assert response .json () == {"status" : "News Criada" }
71
88
72
89
73
90
@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
+ )
77
106
78
107
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" :
"[email protected] " ,
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" ]
== "[email protected] "
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