示例#1
0
def test_app_sort_extension(load_test_data, postgres_transactions):
    settings = ApiSettings(stac_api_extensions=["sort"], )
    app = create_app(settings)

    coll = Collection.parse_obj(load_test_data("test_collection.json"))
    postgres_transactions.create_collection(coll, request=MockStarletteRequest)

    first_item = load_test_data("test_item.json")
    item_date = datetime.strptime(first_item["properties"]["datetime"],
                                  "%Y-%m-%dT%H:%M:%SZ")
    postgres_transactions.create_item(Item.parse_obj(first_item),
                                      request=MockStarletteRequest)

    second_item = load_test_data("test_item.json")
    second_item["id"] = "another-item"
    another_item_date = item_date - timedelta(days=1)
    second_item["properties"]["datetime"] = another_item_date.strftime(
        "%Y-%m-%dT%H:%M:%SZ")
    postgres_transactions.create_item(Item.parse_obj(second_item),
                                      request=MockStarletteRequest)

    with TestClient(app) as client:
        params = {
            "collections": [first_item["collection"]],
            "sortby": [{
                "field": "datetime",
                "direction": "desc"
            }],
        }
        resp = client.post("/search", json=params)
        assert resp.status_code == 200
        resp_json = resp.json()
        assert resp_json["features"][0]["id"] == first_item["id"]
        assert resp_json["features"][1]["id"] == second_item["id"]
示例#2
0
def app_client(load_test_data, postgres_transactions):
    app = create_app(settings)
    coll = Collection.parse_obj(load_test_data("test_collection.json"))
    postgres_transactions.create_collection(coll, request=MockStarletteRequest)

    with TestClient(app) as test_app:
        yield test_app
示例#3
0
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
示例#4
0
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)
示例#5
0
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
示例#6
0
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, request=MockStarletteRequest)
    assert item.dict(
        exclude={"links": ..., "properties": {"created", "updated"}}
    ) == resp.dict(exclude={"links": ..., "properties": {"created", "updated"}})
示例#7
0
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)
示例#8
0
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)
示例#9
0
def test_app_fields_extension(load_test_data, postgres_transactions):
    settings = ApiSettings(stac_api_extensions=["fields"])
    app = create_app(settings)

    coll = Collection.parse_obj(load_test_data("test_collection.json"))
    item = Item.parse_obj(load_test_data("test_item.json"))
    postgres_transactions.create_collection(coll, request=MockStarletteRequest)
    postgres_transactions.create_item(item, request=MockStarletteRequest)

    with TestClient(app) as client:
        resp = client.get("/search",
                          params={"collections": ["test-collection"]})
        assert resp.status_code == 200
        resp_json = resp.json()
        assert list(resp_json["features"][0]["properties"]) == ["datetime"]
示例#10
0
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"
示例#11
0
def test_app_context_extension(load_test_data, postgres_transactions):
    settings = ApiSettings(stac_api_extensions=["context"])
    app = create_app(settings)

    coll = Collection.parse_obj(load_test_data("test_collection.json"))
    item = Item.parse_obj(load_test_data("test_item.json"))
    postgres_transactions.create_collection(coll, request=MockStarletteRequest)
    postgres_transactions.create_item(item, request=MockStarletteRequest)

    with TestClient(app) as client:
        resp = client.get("/search",
                          params={"collections": ["test-collection"]})
        assert resp.status_code == 200
        resp_json = resp.json()

        assert "context" in resp_json
        assert resp_json["context"]["returned"] == resp_json["context"][
            "matched"] == 1
示例#12
0
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
示例#13
0
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)
示例#14
0
def test_app_query_extension(load_test_data, postgres_transactions):
    settings = ApiSettings(stac_api_extensions=["query"], )
    app = create_app(settings)

    coll = Collection.parse_obj(load_test_data("test_collection.json"))
    test_item = load_test_data("test_item.json")
    item = Item.parse_obj(test_item)
    postgres_transactions.create_collection(coll, request=MockStarletteRequest)
    postgres_transactions.create_item(item, request=MockStarletteRequest)

    with TestClient(app) as client:
        params = {
            "query": {
                "proj:epsg": {
                    "gt": test_item["properties"]["proj:epsg"] + 1
                }
            }
        }
        resp = client.post("/search", json=params)
        assert resp.status_code == 200
        resp_json = resp.json()
        assert len(resp_json["features"]) == 0