def test_bulk_item_insert( postgres_core: CoreCrudClient, postgres_transactions: TransactionsClient, postgres_bulk_transactions: BulkTransactionsClient, load_test_data: Callable, ): coll = load_test_data("test_collection.json") postgres_transactions.create_collection(coll, request=MockStarletteRequest) item = load_test_data("test_item.json") items = [] for _ in range(10): _item = deepcopy(item) _item["id"] = str(uuid.uuid4()) items.append(_item) fc = postgres_core.item_collection(coll["id"], request=MockStarletteRequest) assert len(fc["features"]) == 0 postgres_bulk_transactions.bulk_item_insert(items=items) fc = postgres_core.item_collection(coll["id"], request=MockStarletteRequest) assert len(fc["features"]) == 10 for item in items: postgres_transactions.delete_item(item["id"], item["collection"], request=MockStarletteRequest)
def test_conformance_classes_configurable(): """Test conformance class configurability""" landing = LandingPageMixin() landing_page = landing._landing_page( base_url="http://test/test", conformance_classes=["this is a test"]) assert landing_page["conformsTo"][0] == "this is a test" # Update environment to avoid key error on client instantiation os.environ["READER_CONN_STRING"] = "testing" os.environ["WRITER_CONN_STRING"] = "testing" client = CoreCrudClient(base_conformance_classes=["this is a test"]) assert client.conformance_classes()[0] == "this is a test"
def test_delete_collection( postgres_core: CoreCrudClient, postgres_transactions: TransactionsClient, load_test_data: Callable, ): data = Collection.parse_obj(load_test_data("test_collection.json")) postgres_transactions.create_collection(data, request=MockStarletteRequest) deleted = postgres_transactions.delete_collection( data.id, request=MockStarletteRequest) with pytest.raises(NotFoundError): postgres_core.get_collection(deleted.id, request=MockStarletteRequest)
def test_delete_item( postgres_core: CoreCrudClient, postgres_transactions: TransactionsClient, load_test_data: Callable, ): coll = Collection.parse_obj(load_test_data("test_collection.json")) postgres_transactions.create_collection(coll, request=MockStarletteRequest) item = Item.parse_obj(load_test_data("test_item.json")) postgres_transactions.create_item(item, request=MockStarletteRequest) postgres_transactions.delete_item(item.id, request=MockStarletteRequest) with pytest.raises(NotFoundError): postgres_core.get_item(item.id, request=MockStarletteRequest)
def postgres_core(db_session): return CoreCrudClient( session=db_session, item_table=database.Item, collection_table=database.Collection, token_table=database.PaginationToken, )
def cleanup(postgres_core: CoreCrudClient, postgres_transactions: TransactionsClient): yield collections = postgres_core.all_collections(request=MockStarletteRequest) for coll in collections: if coll.id.split("-")[0] == "test": # Delete the items items = postgres_core.item_collection( coll.id, limit=100, request=MockStarletteRequest ) for feat in items.features: postgres_transactions.delete_item(feat.id, request=MockStarletteRequest) # Delete the collection postgres_transactions.delete_collection( coll.id, request=MockStarletteRequest )
def test_create_duplicate_item_different_collections( postgres_core: CoreCrudClient, postgres_transactions: TransactionsClient, load_test_data: Callable, ): # create test-collection coll = load_test_data("test_collection.json") postgres_transactions.create_collection(coll, request=MockStarletteRequest) # create test-collection-2 coll["id"] = "test-collection-2" postgres_transactions.create_collection(coll, request=MockStarletteRequest) # add item to test-collection item = load_test_data("test_item.json") postgres_transactions.create_item(item, request=MockStarletteRequest) # get item from test-collection resp = postgres_core.get_item(item["id"], item["collection"], request=MockStarletteRequest) assert Item(**item).dict(exclude={ "links":..., "properties": {"created", "updated"} }) == Item(**resp).dict(exclude={ "links":..., "properties": {"created", "updated"} }) # add item to test-collection-2 item["collection"] = "test-collection-2" postgres_transactions.create_item(item, request=MockStarletteRequest) # get item with same id from test-collection-2 resp = postgres_core.get_item(item["id"], item["collection"], request=MockStarletteRequest) assert Item(**item).dict(exclude={ "links":..., "properties": {"created", "updated"} }) == Item(**resp).dict(exclude={ "links":..., "properties": {"created", "updated"} })
def test_get_collection( postgres_core: CoreCrudClient, postgres_transactions: TransactionsClient, load_test_data: Callable, ): data = Collection.parse_obj(load_test_data("test_collection.json")) postgres_transactions.create_collection(data, request=MockStarletteRequest) coll = postgres_core.get_collection(data.id, request=MockStarletteRequest) assert data.dict(exclude={"links"}) == coll.dict(exclude={"links"}) assert coll.id == data.id
def test_get_collection( postgres_core: CoreCrudClient, postgres_transactions: TransactionsClient, load_test_data: Callable, ): data = load_test_data("test_collection.json") postgres_transactions.create_collection(data, request=MockStarletteRequest) coll = postgres_core.get_collection(data["id"], request=MockStarletteRequest) assert Collection(**data).dict(exclude={"links"}) == Collection( **coll).dict(exclude={"links"}) assert coll["id"] == data["id"]
def api_client(db_session): return StacApi( settings=SqlalchemySettings(), client=CoreCrudClient(session=db_session), extensions=[ TransactionExtension(client=TransactionsClient(session=db_session)), ContextExtension(), SortExtension(), FieldsExtension(), QueryExtension(), ], )
def test_update_collection( postgres_core: CoreCrudClient, postgres_transactions: TransactionsClient, load_test_data: Callable, ): data = Collection.parse_obj(load_test_data("test_collection.json")) postgres_transactions.create_collection(data, request=MockStarletteRequest) data.keywords.append("new keyword") postgres_transactions.update_collection(data, request=MockStarletteRequest) coll = postgres_core.get_collection(data.id, request=MockStarletteRequest) assert "new keyword" in coll.keywords
def test_create_item( postgres_core: CoreCrudClient, postgres_transactions: TransactionsClient, load_test_data: Callable, ): coll = Collection.parse_obj(load_test_data("test_collection.json")) postgres_transactions.create_collection(coll, request=MockStarletteRequest) item = Item.parse_obj(load_test_data("test_item.json")) postgres_transactions.create_item(item, request=MockStarletteRequest) resp = postgres_core.get_item( item.id, item.collection, request=MockStarletteRequest ) assert item.dict( exclude={"links": ..., "properties": {"created", "updated"}} ) == resp.dict(exclude={"links": ..., "properties": {"created", "updated"}})
def api_client(db_session): settings = SqlalchemySettings() return StacApi( settings=settings, client=CoreCrudClient(session=db_session), extensions=[ TransactionExtension(client=TransactionsClient(session=db_session), settings=settings), ContextExtension(), SortExtension(), FieldsExtension(), QueryExtension(), ], search_request_model=SQLAlchemySTACSearch, )
def test_get_item( postgres_core: CoreCrudClient, postgres_transactions: TransactionsClient, load_test_data: Callable, ): collection_data = load_test_data("test_collection.json") postgres_transactions.create_collection(collection_data, request=MockStarletteRequest) data = load_test_data("test_item.json") postgres_transactions.create_item(data, request=MockStarletteRequest) coll = postgres_core.get_item( item_id=data["id"], collection_id=data["collection"], request=MockStarletteRequest, ) assert coll["id"] == data["id"] assert coll["collection"] == data["collection"]
def test_update_item( postgres_core: CoreCrudClient, postgres_transactions: TransactionsClient, load_test_data: Callable, ): coll = Collection.parse_obj(load_test_data("test_collection.json")) postgres_transactions.create_collection(coll, request=MockStarletteRequest) item = Item.parse_obj(load_test_data("test_item.json")) postgres_transactions.create_item(item, request=MockStarletteRequest) item.properties.foo = "bar" postgres_transactions.update_item(item, request=MockStarletteRequest) updated_item = postgres_core.get_item(item.id, request=MockStarletteRequest) assert updated_item.properties.foo == "bar"
def test_landing_page_no_collection_title( postgres_core: CoreCrudClient, postgres_transactions: TransactionsClient, load_test_data: Callable, api_client: StacApi, ): class MockStarletteRequestWithApp(MockStarletteRequest): app = api_client.app coll = load_test_data("test_collection.json") del coll["title"] postgres_transactions.create_collection(coll, request=MockStarletteRequest) landing_page = postgres_core.landing_page( request=MockStarletteRequestWithApp) for link in landing_page["links"]: if link["href"].split("/")[-1] == coll["id"]: assert link["title"]
def test_update_item( postgres_core: CoreCrudClient, postgres_transactions: TransactionsClient, load_test_data: Callable, ): coll = load_test_data("test_collection.json") postgres_transactions.create_collection(coll, request=MockStarletteRequest) item = load_test_data("test_item.json") postgres_transactions.create_item(item, request=MockStarletteRequest) item["properties"]["foo"] = "bar" postgres_transactions.update_item(item, request=MockStarletteRequest) updated_item = postgres_core.get_item(item["id"], item["collection"], request=MockStarletteRequest) assert updated_item["properties"]["foo"] == "bar"
def test_create_item( postgres_core: CoreCrudClient, postgres_transactions: TransactionsClient, load_test_data: Callable, ): coll = load_test_data("test_collection.json") postgres_transactions.create_collection(coll, request=MockStarletteRequest) item = load_test_data("test_item.json") postgres_transactions.create_item(item, request=MockStarletteRequest) resp = postgres_core.get_item(item["id"], item["collection"], request=MockStarletteRequest) assert Item(**item).dict(exclude={ "links":..., "properties": {"created", "updated"} }) == Item(**resp).dict(exclude={ "links":..., "properties": {"created", "updated"} })
def test_get_collection_items( postgres_core: CoreCrudClient, postgres_transactions: TransactionsClient, load_test_data: Callable, ): coll = Collection.parse_obj(load_test_data("test_collection.json")) postgres_transactions.create_collection(coll, request=MockStarletteRequest) item = Item.parse_obj(load_test_data("test_item.json")) for _ in range(5): item.id = str(uuid.uuid4()) postgres_transactions.create_item(item, request=MockStarletteRequest) fc = postgres_core.item_collection(coll.id, request=MockStarletteRequest) assert len(fc.features) == 5 for item in fc.features: assert item.collection == coll.id
def test_update_geometry( postgres_core: CoreCrudClient, postgres_transactions: TransactionsClient, load_test_data: Callable, ): coll = load_test_data("test_collection.json") postgres_transactions.create_collection(coll, request=MockStarletteRequest) item = load_test_data("test_item.json") postgres_transactions.create_item(item, request=MockStarletteRequest) item["geometry"]["coordinates"] = [[[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]] postgres_transactions.update_item(item, request=MockStarletteRequest) updated_item = postgres_core.get_item(item["id"], item["collection"], request=MockStarletteRequest) assert updated_item["geometry"]["coordinates"] == item["geometry"][ "coordinates"]
def test_get_collection_items( postgres_core: CoreCrudClient, postgres_transactions: TransactionsClient, load_test_data: Callable, ): coll = load_test_data("test_collection.json") postgres_transactions.create_collection(coll, request=MockStarletteRequest) item = load_test_data("test_item.json") for _ in range(5): item["id"] = str(uuid.uuid4()) postgres_transactions.create_item(item, request=MockStarletteRequest) fc = postgres_core.item_collection(coll["id"], request=MockStarletteRequest) assert len(fc["features"]) == 5 for item in fc["features"]: assert item["collection"] == coll["id"]
def api_client(db_session): settings = SqlalchemySettings() extensions = [ TransactionExtension( client=TransactionsClient(session=db_session), settings=settings ), ContextExtension(), SortExtension(), FieldsExtension(), QueryExtension(), TokenPaginationExtension(), ] get_request_model = create_request_model( "SearchGetRequest", base_model=BaseSearchGetRequest, extensions=extensions, request_type="GET", ) post_request_model = create_request_model( "SearchPostRequest", base_model=BaseSearchPostRequest, extensions=extensions, request_type="POST", ) return StacApi( settings=settings, client=CoreCrudClient( session=db_session, extensions=extensions, post_request_model=post_request_model, ), extensions=extensions, search_get_request_model=get_request_model, search_post_request_model=post_request_model, )
from stac_fastapi.extensions.core import ( FieldsExtension, QueryExtension, SortExtension, TransactionExtension, ) from stac_fastapi.extensions.third_party import BulkTransactionExtension from stac_fastapi.sqlalchemy.config import SqlalchemySettings from stac_fastapi.sqlalchemy.core import CoreCrudClient from stac_fastapi.sqlalchemy.session import Session from stac_fastapi.sqlalchemy.transactions import ( BulkTransactionsClient, TransactionsClient, ) settings = SqlalchemySettings() session = Session.create_from_settings(settings) api = StacApi( settings=settings, extensions=[ TransactionExtension(client=TransactionsClient(session=session)), BulkTransactionExtension(client=BulkTransactionsClient( session=session)), FieldsExtension(), QueryExtension(), SortExtension(), ], client=CoreCrudClient(session=session), ) app = api.app
TransactionExtension(client=TransactionsClient(session=session), settings=settings), BulkTransactionExtension(client=BulkTransactionsClient(session=session)), FieldsExtension(), QueryExtension(), SortExtension(), TokenPaginationExtension(), ContextExtension(), ] post_request_model = create_post_request_model(extensions) api = StacApi( settings=settings, extensions=extensions, client=CoreCrudClient( session=session, extensions=extensions, post_request_model=post_request_model ), search_get_request_model=create_get_request_model(extensions), search_post_request_model=post_request_model, ) app = api.app def run(): """Run app from command line using uvicorn if available.""" try: import uvicorn uvicorn.run( "stac_fastapi.sqlalchemy.app:app", host=settings.app_host,