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"]
async def test_create_collection_conflict(app_client, load_test_data: Callable): in_json = load_test_data("test_collection.json") Collection.parse_obj(in_json) resp = await app_client.post( "/collections", json=in_json, ) assert resp.status_code == 200 Collection.parse_obj(resp.json()) resp = await app_client.post( "/collections", json=in_json, ) assert resp.status_code == 409
async def test_create_collection(app_client, load_test_data: Callable): in_json = load_test_data("test_collection.json") in_coll = Collection.parse_obj(in_json) resp = await app_client.post( "/collections", json=in_json, ) assert resp.status_code == 200 post_coll = Collection.parse_obj(resp.json()) assert in_coll.dict(exclude={"links"}) == post_coll.dict(exclude={"links"}) resp = await app_client.get(f"/collections/{post_coll.id}") assert resp.status_code == 200 get_coll = Collection.parse_obj(resp.json()) assert post_coll.dict(exclude={"links"}) == get_coll.dict(exclude={"links"})
async def get_collection(self, id: str, **kwargs) -> ORJSONResponse: """Get collection by id. Called with `GET /collections/{collectionId}`. Args: id: Id of the collection. Returns: Collection. """ request = kwargs["request"] pool = kwargs["request"].app.state.readpool async with pool.acquire() as conn: q, p = render( """ SELECT * FROM get_collection(:id::text); """, id=id, ) collection = await conn.fetchval(q, *p) if collection is None: raise NotFoundError links = await CollectionLinks(collection_id=id, request=request).get_links() collection["links"] = links return ORJSONResponse( Collection.construct(**collection).dict(exclude_none=True))
async def load_test_collection(app_client, load_test_data): data = load_test_data("test_collection.json") resp = await app_client.post( "/collections", json=data, ) assert resp.status_code == 200 return Collection.parse_obj(resp.json())
def test_create_collection_already_exists( 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) with pytest.raises(ConflictError): postgres_transactions.create_collection(data, request=MockStarletteRequest)
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_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_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
async def test_update_collection(app_client, load_test_collection): in_coll = load_test_collection in_coll.keywords.append("newkeyword") resp = await app_client.put("/collections", json=in_coll.dict()) assert resp.status_code == 200 resp = await app_client.get(f"/collections/{in_coll.id}") assert resp.status_code == 200 get_coll = Collection.parse_obj(resp.json()) assert in_coll.dict(exclude={"links"}) == get_coll.dict(exclude={"links"}) assert "newkeyword" in get_coll.keywords
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 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 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"
async def _all_collections_func(self, **kwargs) -> List[Dict]: """Read all collections from the database.""" request = kwargs["request"] pool = request.app.state.readpool async with pool.acquire() as conn: collections = await conn.fetchval(""" SELECT * FROM all_collections(); """) linked_collections = [] if collections is not None and len(collections) > 0: for c in collections: coll = Collection.construct(**c) coll.links = await CollectionLinks( collection_id=coll.id, request=request).get_links() linked_collections.append(coll) return linked_collections
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 tiles_extension_app(postgres_core, postgres_transactions, load_test_data): # Ingest test data for testing 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) settings = SqlalchemySettings() api = StacApi( settings=settings, client=postgres_core, extensions=[TilesExtension(TilesClient(postgres_core))], ) with TestClient(api.app) as test_app: yield test_app # Cleanup test data postgres_transactions.delete_item(item.id, request=MockStarletteRequest) postgres_transactions.delete_collection(coll.id, request=MockStarletteRequest)
def test_bulk_item_insert( postgres_transactions: TransactionsClient, postgres_bulk_transactions: BulkTransactionsClient, 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")) items = [] for _ in range(10): _item = item.dict() _item["id"] = str(uuid.uuid4()) items.append(_item) postgres_bulk_transactions.bulk_item_insert(Items(items=items)) for item in items: postgres_transactions.delete_item(item["id"], request=MockStarletteRequest)
def app_client(api_client, load_test_data, postgres_transactions): coll = Collection.parse_obj(load_test_data("test_collection.json")) postgres_transactions.create_collection(coll, request=MockStarletteRequest) with TestClient(api_client.app) as test_app: yield test_app
def test_collection_assets(): test_coll = request(ASSET_EXTENSION) # The example is missing links test_coll["links"] = [{"href": "mocked", "rel": "items"}] valid_coll = Collection(**test_coll).to_dict() assert test_coll["assets"] == valid_coll["assets"]
def test_commons_extension_collection(): test_coll = request(COMMONS_EXTENSION) valid_coll = Collection(**test_coll).to_dict() dict_match(test_coll, valid_coll)
def test_assets_extension_validation_error(): test_collection = request(ASSET_EXTENSION) del test_collection["assets"] with pytest.raises(ValidationError): Collection(**test_collection)
def test_commons_extension_validation_error(): test_collection = request(COMMONS_EXTENSION) del test_collection["properties"] with pytest.raises(ValidationError): Collection(**test_collection)
def test_item_assets_extension(): test_coll = request(ITEM_ASSET_EXTENSION) valid_coll = Collection(**test_coll).to_dict() dict_match(test_coll, valid_coll)
def test_version_extension_collection(): test_coll = request(VERSION_EXTENSION_COLLECTION) valid_coll = Collection(**test_coll).to_dict() dict_match(test_coll, valid_coll)