1111
1212_TITLE_SEARCH_TERMS = ["ho" , "oo" , "work" ]
1313_TAG_SEARCH_TERMS = ["art" , "om" ]
14+ _HEADERS = {"Authorization" : f"Bearer { ACCESS_TOKEN } " }
1415
1516
1617@pytest .mark .asyncio
@@ -21,9 +22,7 @@ async def test_create_sql_post(
2122 """POST to /posts creates a post in sql and returns it"""
2223 timestamp = datetime .now ().isoformat ()
2324 with client_with_sql as client :
24- response = client .post (
25- "/posts" , json = post , headers = {"Authorization" : f"Bearer { ACCESS_TOKEN } " }
26- )
25+ response = client .post ("/posts" , json = post , headers = _HEADERS )
2726
2827 got = response .json ()
2928 post_id = got ["id" ]
@@ -61,12 +60,12 @@ async def test_create_redis_post(
6160 client_with_redis : TestClient ,
6261 redis_store : RedisStore ,
6362 post : dict ,
63+ freezer ,
6464):
6565 """POST to /posts creates a post in redis and returns it"""
66+ timestamp = datetime .now ().isoformat ()
6667 with client_with_redis as client :
67- response = client .post (
68- "/posts" , json = post , headers = {"Authorization" : f"Bearer { ACCESS_TOKEN } " }
69- )
68+ response = client .post ("/posts" , json = post , headers = _HEADERS )
7069
7170 got = response .json ()
7271 post_id = got ["id" ]
@@ -75,7 +74,8 @@ async def test_create_redis_post(
7574 expected = {
7675 "id" : post_id ,
7776 "title" : post ["title" ],
78- "content" : post .get ("content" ),
77+ "content" : post .get ("content" , "" ),
78+ "author" : {** got ["author" ], ** AUTHOR },
7979 "pk" : post_id ,
8080 "tags" : [
8181 {
@@ -86,6 +86,8 @@ async def test_create_redis_post(
8686 for raw , resp in zip (raw_tags , resp_tags )
8787 ],
8888 "comments" : [],
89+ "created_at" : timestamp ,
90+ "updated_at" : timestamp ,
8991 }
9092
9193 db_query = {"id" : {"$eq" : post_id }}
@@ -102,25 +104,25 @@ async def test_create_mongo_post(
102104 client_with_mongo : TestClient ,
103105 mongo_store : MongoStore ,
104106 post : dict ,
107+ freezer ,
105108):
106109 """POST to /posts creates a post in redis and returns it"""
110+ timestamp = datetime .now ().isoformat ()
107111 with client_with_mongo as client :
108- response = client .post ("/posts" , json = post )
112+ response = client .post ("/posts" , json = post , headers = _HEADERS )
109113
110114 got = response .json ()
111115 post_id = got ["id" ]
112116 raw_tags = post .get ("tags" , [])
113117 expected = {
114118 "id" : post_id ,
115119 "title" : post ["title" ],
116- "content" : post .get ("content" ),
117- "tags" : [
118- {
119- ** raw ,
120- }
121- for raw in raw_tags
122- ],
120+ "content" : post .get ("content" , "" ),
121+ "author" : {"name" : AUTHOR ["name" ]},
122+ "tags" : raw_tags ,
123123 "comments" : [],
124+ "created_at" : timestamp ,
125+ "updated_at" : timestamp ,
124126 }
125127
126128 db_query = {"_id" : {"$eq" : ObjectId (post_id )}}
@@ -138,22 +140,26 @@ async def test_update_sql_post(
138140 sql_store : SQLStore ,
139141 sql_posts : list [SqlPost ],
140142 index : int ,
143+ freezer ,
141144):
142145 """PUT to /posts/{id} updates the sql post of given id and returns updated version"""
146+ timestamp = datetime .now ().isoformat ()
143147 with client_with_sql as client :
144148 post = sql_posts [index ]
149+ post_dict = post .model_dump (mode = "json" , exclude_none = True , exclude_unset = True )
145150 id_ = post .id
146151 update = {
147- "name" : "some other name" ,
148- "todos" : [
149- * post .tags ,
152+ ** post_dict ,
153+ "title" : "some other title" ,
154+ "tags" : [
155+ * post_dict ["tags" ],
150156 {"title" : "another one" },
151157 {"title" : "another one again" },
152158 ],
153- "comments" : [* post . comments , * COMMENT_LIST [index :]],
159+ "comments" : [* post_dict [ " comments" ] , * COMMENT_LIST [index :]],
154160 }
155161
156- response = client .put (f"/posts/{ id_ } " , json = update )
162+ response = client .put (f"/posts/{ id_ } " , json = update , headers = _HEADERS )
157163
158164 got = response .json ()
159165 expected = {
@@ -164,8 +170,9 @@ async def test_update_sql_post(
164170 ** raw ,
165171 "id" : final ["id" ],
166172 "post_id" : final ["post_id" ],
167- "author" : final ["author" ],
168- "author_id" : final ["author_id" ],
173+ "author_id" : 1 ,
174+ "created_at" : timestamp ,
175+ "updated_at" : timestamp ,
169176 }
170177 for raw , final in zip (update ["comments" ], got ["comments" ])
171178 ],
@@ -192,22 +199,25 @@ async def test_update_redis_post(
192199 redis_store : RedisStore ,
193200 redis_posts : list [RedisPost ],
194201 index : int ,
202+ freezer ,
195203):
196204 """PUT to /posts/{id} updates the redis post of given id and returns updated version"""
205+ timestamp = datetime .now ().isoformat ()
197206 with client_with_redis as client :
198207 post = redis_posts [index ]
208+ post_dict = post .model_dump (mode = "json" , exclude_none = True , exclude_unset = True )
199209 id_ = post .id
200210 update = {
201- "name " : "some other name " ,
202- "todos " : [
203- * post . tags ,
211+ "title " : "some other title " ,
212+ "tags " : [
213+ * post_dict . get ( " tags" , []) ,
204214 {"title" : "another one" },
205215 {"title" : "another one again" },
206216 ],
207- "comments" : [* post . comments , * COMMENT_LIST [index :]],
217+ "comments" : [* post_dict . get ( " comments" , []) , * COMMENT_LIST [index :]],
208218 }
209219
210- response = client .put (f"/posts/{ id_ } " , json = update )
220+ response = client .put (f"/posts/{ id_ } " , json = update , headers = _HEADERS )
211221
212222 got = response .json ()
213223 expected = {
@@ -219,6 +229,8 @@ async def test_update_redis_post(
219229 "id" : final ["id" ],
220230 "author" : final ["author" ],
221231 "pk" : final ["pk" ],
232+ "created_at" : timestamp ,
233+ "updated_at" : timestamp ,
222234 }
223235 for raw , final in zip (update ["comments" ], got ["comments" ])
224236 ],
@@ -265,22 +277,25 @@ async def test_update_mongo_post(
265277 mongo_store : MongoStore ,
266278 mongo_posts : list [MongoPost ],
267279 index : int ,
280+ freezer ,
268281):
269282 """PUT to /posts/{id} updates the mongo post of given id and returns updated version"""
283+ timestamp = datetime .now ().isoformat ()
270284 with client_with_mongo as client :
271285 post = mongo_posts [index ]
286+ post_dict = post .model_dump (mode = "json" , exclude_none = True , exclude_unset = True )
272287 id_ = post .id
273288 update = {
274- "name " : "some other name " ,
275- "todos " : [
276- * post . tags ,
289+ "title " : "some other title " ,
290+ "tags " : [
291+ * post_dict . get ( " tags" , []) ,
277292 {"title" : "another one" },
278293 {"title" : "another one again" },
279294 ],
280- "comments" : [* post . comments , * COMMENT_LIST [index :]],
295+ "comments" : [* post_dict . get ( " comments" , []) , * COMMENT_LIST [index :]],
281296 }
282297
283- response = client .put (f"/posts/{ id_ } " , json = update )
298+ response = client .put (f"/posts/{ id_ } " , json = update , headers = _HEADERS )
284299
285300 got = response .json ()
286301 expected = {
@@ -290,6 +305,8 @@ async def test_update_mongo_post(
290305 {
291306 ** raw ,
292307 "author" : final ["author" ],
308+ "created_at" : timestamp ,
309+ "updated_at" : timestamp ,
293310 }
294311 for raw , final in zip (update ["comments" ], got ["comments" ])
295312 ],
@@ -538,14 +555,14 @@ async def test_search_sql_by_tag(
538555
539556
540557@pytest .mark .asyncio
541- @pytest .mark .parametrize ("q" , _TAG_SEARCH_TERMS )
558+ @pytest .mark .parametrize ("q" , [ "random" , "another one" , "another one again" ] )
542559async def test_search_redis_by_tag (
543560 client_with_redis : TestClient ,
544561 redis_store : RedisStore ,
545562 redis_posts : list [RedisPost ],
546563 q : str ,
547564):
548- """GET /posts?tag={} gets all redis posts with tag containing search item"""
565+ """GET /posts?tag={} gets all redis posts with tag containing search item. Partial searches nit supported. """
549566 with client_with_redis as client :
550567 response = client .get (f"/posts?tag={ q } " )
551568
0 commit comments