1+ import os
12from datetime import datetime , timedelta
23from typing import Any , Callable , Coroutine , Dict , List , Optional , TypeVar
34from urllib .parse import quote_plus
67import pytest
78from fastapi import Request
89from httpx import ASGITransport , AsyncClient
10+ from pypgstac .db import PgstacDB
11+ from pypgstac .load import Loader
912from pystac import Collection , Extent , Item , SpatialExtent , TemporalExtent
1013from stac_fastapi .api .app import StacApi
11- from stac_fastapi .api .models import create_post_request_model
14+ from stac_fastapi .api .models import create_get_request_model , create_post_request_model
1215from stac_fastapi .extensions .core import FieldsExtension , TransactionExtension
1316from stac_fastapi .types import stac as stac_types
1417
1720from stac_fastapi .pgstac .transactions import TransactionsClient
1821from stac_fastapi .pgstac .types .search import PgstacSearch
1922
23+ DATA_DIR = os .path .join (os .path .dirname (__file__ ), ".." , "data" )
24+
25+
2026STAC_CORE_ROUTES = [
2127 "GET /" ,
2228 "GET /collections" ,
@@ -669,11 +675,13 @@ async def get_collection(
669675 FieldsExtension (),
670676 ]
671677 post_request_model = create_post_request_model (extensions , base_model = PgstacSearch )
678+ get_request_model = create_get_request_model (extensions )
672679 api = StacApi (
673680 client = Client (post_request_model = post_request_model ),
674681 settings = settings ,
675682 extensions = extensions ,
676683 search_post_request_model = post_request_model ,
684+ search_get_request_model = get_request_model ,
677685 )
678686 app = api .app
679687 await connect_to_db (app )
@@ -695,3 +703,105 @@ async def get_collection(
695703 assert response .status_code == 200
696704 finally :
697705 await close_db_connection (app )
706+
707+
708+ @pytest .mark .asyncio
709+ @pytest .mark .parametrize ("validation" , [True , False ])
710+ @pytest .mark .parametrize ("hydrate" , [True , False ])
711+ async def test_no_extension (
712+ hydrate , validation , load_test_data , database , pgstac
713+ ) -> None :
714+ """test PgSTAC with no extension."""
715+ connection = f"postgresql://{ database .user } :{ database .password } @{ database .host } :{ database .port } /{ database .dbname } "
716+ with PgstacDB (dsn = connection ) as db :
717+ loader = Loader (db = db )
718+ loader .load_collections (os .path .join (DATA_DIR , "test_collection.json" ))
719+ loader .load_items (os .path .join (DATA_DIR , "test_item.json" ))
720+
721+ settings = Settings (
722+ postgres_user = database .user ,
723+ postgres_pass = database .password ,
724+ postgres_host_reader = database .host ,
725+ postgres_host_writer = database .host ,
726+ postgres_port = database .port ,
727+ postgres_dbname = database .dbname ,
728+ testing = True ,
729+ use_api_hydrate = hydrate ,
730+ enable_response_models = validation ,
731+ )
732+ extensions = []
733+ post_request_model = create_post_request_model (extensions , base_model = PgstacSearch )
734+ api = StacApi (
735+ client = CoreCrudClient (post_request_model = post_request_model ),
736+ settings = settings ,
737+ extensions = extensions ,
738+ search_post_request_model = post_request_model ,
739+ )
740+ app = api .app
741+ await connect_to_db (app )
742+ try :
743+ async with AsyncClient (transport = ASGITransport (app = app )) as client :
744+ landing = await client .get ("http://test/" )
745+ assert landing .status_code == 200 , landing .text
746+
747+ collection = await client .get ("http://test/collections/test-collection" )
748+ assert collection .status_code == 200 , collection .text
749+
750+ collections = await client .get ("http://test/collections" )
751+ assert collections .status_code == 200 , collections .text
752+
753+ item = await client .get (
754+ "http://test/collections/test-collection/items/test-item"
755+ )
756+ assert item .status_code == 200 , item .text
757+
758+ item_collection = await client .get (
759+ "http://test/collections/test-collection/items" ,
760+ params = {"limit" : 10 },
761+ )
762+ assert item_collection .status_code == 200 , item_collection .text
763+
764+ get_search = await client .get (
765+ "http://test/search" ,
766+ params = {
767+ "collections" : ["test-collection" ],
768+ },
769+ )
770+ assert get_search .status_code == 200 , get_search .text
771+
772+ post_search = await client .post (
773+ "http://test/search" ,
774+ json = {
775+ "collections" : ["test-collection" ],
776+ },
777+ )
778+ assert post_search .status_code == 200 , post_search .text
779+
780+ get_search = await client .get (
781+ "http://test/search" ,
782+ params = {
783+ "collections" : ["test-collection" ],
784+ "fields" : "properties.datetime" ,
785+ },
786+ )
787+ # fields should be ignored
788+ assert get_search .status_code == 200 , get_search .text
789+ props = get_search .json ()["features" ][0 ]["properties" ]
790+ assert len (props ) > 1
791+
792+ post_search = await client .post (
793+ "http://test/search" ,
794+ json = {
795+ "collections" : ["test-collection" ],
796+ "fields" : {
797+ "include" : ["properties.datetime" ],
798+ },
799+ },
800+ )
801+ # fields should be ignored
802+ assert post_search .status_code == 200 , post_search .text
803+ props = get_search .json ()["features" ][0 ]["properties" ]
804+ assert len (props ) > 1
805+
806+ finally :
807+ await close_db_connection (app )
0 commit comments