@@ -1540,3 +1540,59 @@ async def test_collection_items_limit_env_variable(
15401540 assert resp .status_code == 200
15411541 resp_json = resp .json ()
15421542 assert int (limit ) == len (resp_json ["features" ])
1543+
1544+
1545+ @pytest .mark .asyncio
1546+ async def test_collection_items_sort_desc (app_client , txn_client , ctx ):
1547+ """Verify GET /collections/{collectionId}/items honors descending sort on properties.datetime."""
1548+ first_item = ctx .item
1549+
1550+ # Create a second item in the same collection with an earlier datetime
1551+ second_item = dict (first_item )
1552+ second_item ["id" ] = "another-item-for-collection-sort-desc"
1553+ another_item_date = datetime .strptime (
1554+ first_item ["properties" ]["datetime" ], "%Y-%m-%dT%H:%M:%SZ"
1555+ ) - timedelta (days = 1 )
1556+ second_item ["properties" ]["datetime" ] = another_item_date .strftime (
1557+ "%Y-%m-%dT%H:%M:%SZ"
1558+ )
1559+
1560+ await create_item (txn_client , second_item )
1561+
1562+ # Descending sort: the original (newer) item should come first
1563+ resp = await app_client .get (
1564+ f"/collections/{ first_item ['collection' ]} /items" ,
1565+ params = [("sortby" , "-properties.datetime" )],
1566+ )
1567+ assert resp .status_code == 200
1568+ resp_json = resp .json ()
1569+ assert resp_json ["features" ][0 ]["id" ] == first_item ["id" ]
1570+ assert resp_json ["features" ][1 ]["id" ] == second_item ["id" ]
1571+
1572+
1573+ @pytest .mark .asyncio
1574+ async def test_collection_items_sort_asc (app_client , txn_client , ctx ):
1575+ """Verify GET /collections/{collectionId}/items honors ascending sort on properties.datetime."""
1576+ first_item = ctx .item
1577+
1578+ # Create a second item in the same collection with an earlier datetime
1579+ second_item = dict (first_item )
1580+ second_item ["id" ] = "another-item-for-collection-sort-asc"
1581+ another_item_date = datetime .strptime (
1582+ first_item ["properties" ]["datetime" ], "%Y-%m-%dT%H:%M:%SZ"
1583+ ) - timedelta (days = 1 )
1584+ second_item ["properties" ]["datetime" ] = another_item_date .strftime (
1585+ "%Y-%m-%dT%H:%M:%SZ"
1586+ )
1587+
1588+ await create_item (txn_client , second_item )
1589+
1590+ # Ascending sort: the older item should come first
1591+ resp = await app_client .get (
1592+ f"/collections/{ first_item ['collection' ]} /items" ,
1593+ params = [("sortby" , "+properties.datetime" )],
1594+ )
1595+ assert resp .status_code == 200
1596+ resp_json = resp .json ()
1597+ assert resp_json ["features" ][0 ]["id" ] == second_item ["id" ]
1598+ assert resp_json ["features" ][1 ]["id" ] == first_item ["id" ]
0 commit comments