@@ -309,19 +309,127 @@ async def test_get_collections_search(
309309async def test_get_collections_search_limit_offset (
310310 app_client , load_test_collection , load_test2_collection
311311):
312+ resp = await app_client .get ("/collections" )
313+ cols = resp .json ()["collections" ]
314+ assert len (cols ) == 2
315+ links = resp .json ()["links" ]
316+ assert len (links ) == 2
317+ assert {"root" , "self" } == {link ["rel" ] for link in links }
318+
319+ ###################
320+ # limit should be positive
321+ resp = await app_client .get ("/collections" , params = {"limit" : 0 })
322+ assert resp .status_code == 400
323+
324+ ###################
325+ # limit=1, should have a `next` link
312326 resp = await app_client .get (
313327 "/collections" ,
314328 params = {"limit" : 1 },
315329 )
316- response = resp .json ()
317- assert len (response ["collections" ]) == 1
318- assert response ["collections" ][0 ]["id" ] == load_test_collection ["id" ]
319-
320- # check next link
321- next_link = [link ["href" ] for link in response ["links" ] if link ["rel" ] == "next" ][0 ]
322- next_url = next_link .replace (str (app_client .base_url ), "" )
323- next_resp = await app_client .get (next_url )
324- next_response = next_resp .json ()
325-
326- assert len (next_response ["collections" ]) == 1
327- assert next_response ["collections" ][0 ]["id" ] == load_test2_collection .id
330+ cols = resp .json ()["collections" ]
331+ links = resp .json ()["links" ]
332+ assert len (cols ) == 1
333+ assert cols [0 ]["id" ] == load_test_collection ["id" ]
334+ assert len (links ) == 3
335+ assert {"root" , "self" , "next" } == {link ["rel" ] for link in links }
336+ next_link = list (filter (lambda link : link ["rel" ] == "next" , links ))[0 ]
337+ assert next_link ["href" ].endswith ("?limit=1&offset=1" )
338+
339+ ###################
340+ # limit=2, there should not be a next link
341+ resp = await app_client .get (
342+ "/collections" ,
343+ params = {"limit" : 2 },
344+ )
345+ cols = resp .json ()["collections" ]
346+ links = resp .json ()["links" ]
347+ assert len (cols ) == 2
348+ assert cols [0 ]["id" ] == load_test_collection ["id" ]
349+ assert cols [1 ]["id" ] == load_test2_collection .id
350+ # TODO: check with pgstac
351+ # assert len(links) == 2
352+ # assert {"root", "self"} == {link["rel"] for link in links}
353+
354+ ###################
355+ # limit=3, there should not be a next/previous link
356+ resp = await app_client .get (
357+ "/collections" ,
358+ params = {"limit" : 3 },
359+ )
360+ cols = resp .json ()["collections" ]
361+ links = resp .json ()["links" ]
362+ assert len (cols ) == 2
363+ assert cols [0 ]["id" ] == load_test_collection ["id" ]
364+ assert cols [1 ]["id" ] == load_test2_collection .id
365+ assert len (links ) == 2
366+ assert {"root" , "self" } == {link ["rel" ] for link in links }
367+
368+ ###################
369+ # offset=3, because there are 2 collections, we should not have `next` or `prev` links
370+ resp = await app_client .get (
371+ "/collections" ,
372+ params = {"offset" : 3 },
373+ )
374+ cols = resp .json ()["collections" ]
375+ links = resp .json ()["links" ]
376+ assert len (cols ) == 0
377+ assert len (links ) == 2
378+ assert {"root" , "self" } == {link ["rel" ] for link in links }
379+
380+ ###################
381+ # offset=3,limit=1
382+ resp = await app_client .get (
383+ "/collections" ,
384+ params = {"limit" : 1 , "offset" : 3 },
385+ )
386+ cols = resp .json ()["collections" ]
387+ links = resp .json ()["links" ]
388+ assert len (cols ) == 0
389+ assert len (links ) == 3
390+ assert {"root" , "self" , "previous" } == {link ["rel" ] for link in links }
391+ prev_link = list (filter (lambda link : link ["rel" ] == "previous" , links ))[0 ]
392+ assert prev_link ["href" ].endswith ("?limit=1&offset=2" )
393+
394+ # ###################
395+ # # offset=3,limit=2
396+ # resp = await app_client.get(
397+ # "/collections",
398+ # params={"limit": 2,"offset": 3},
399+ # )
400+ # cols = resp.json()["collections"]
401+ # links = resp.json()["links"]
402+ # assert len(cols) == 0
403+ # assert len(links) == 3
404+ # assert {"root", "self", "previous"} == {link["rel"] for link in links}
405+ # prev_link = list(filter(lambda link: link["rel"] == "previous", links))[0]
406+ # assert prev_link["href"].endswith("?limit=1&offset=2")
407+
408+ ###################
409+ # offset=1, should have a `previous` link
410+ resp = await app_client .get (
411+ "/collections" ,
412+ params = {"offset" : 1 },
413+ )
414+ cols = resp .json ()["collections" ]
415+ links = resp .json ()["links" ]
416+ assert len (cols ) == 1
417+ assert cols [0 ]["id" ] == load_test2_collection .id
418+ # TODO: Check with pgstac
419+ # assert len(links) == 3
420+ # assert {"root", "self", "previous"} == {link["rel"] for link in links}
421+ # prev_link = list(filter(lambda link: link["rel"] == "previous", links))[0]
422+ # assert prev_link["href"].endswith("?offset=0")
423+
424+ ###################
425+ # offset=0, should not have next/previous link
426+ resp = await app_client .get (
427+ "/collections" ,
428+ params = {"offset" : 0 },
429+ )
430+ cols = resp .json ()["collections" ]
431+ links = resp .json ()["links" ]
432+ assert len (cols ) == 2
433+ # TODO: Check with pgstac
434+ # assert len(links) == 2
435+ # assert {"root", "self"} == {link["rel"] for link in links}
0 commit comments