def test_temporal_search_two_tailed(): # Test two tailed utcnow = datetime.utcnow().replace(microsecond=0, tzinfo=timezone.utc) utcnow_str = utcnow.strftime(DATETIME_RFC339) search = Search(collections=["collection1"], datetime=f"{utcnow_str}/{utcnow_str}") assert search.start_date == search.end_date == utcnow search = Search(collections=["collection1"], datetime=f"{utcnow_str}/..") assert search.start_date == utcnow assert search.end_date == None
def test_spatial_search(): # Search with bbox Search(collections=["collection1", "collection2"], bbox=[-180, -90, 180, 90]) # Search with geojson search = Search( collections=["collection1", "collection2"], intersects={ "type": "Point", "coordinates": [0, 0] }, ) shape(search.intersects)
def test_invalid_temporal_search(): # Not RFC339 utcnow = datetime.utcnow().strftime("%Y-%m-%d") with pytest.raises(ValidationError): search = Search(collections=["collection1"], datetime=utcnow) # End date is before start date start = datetime.utcnow() time.sleep(2) end = datetime.utcnow() with pytest.raises(ValidationError): search = Search( collections=["collection1"], datetime= f"{end.strftime(DATETIME_RFC339)}/{start.strftime(DATETIME_RFC339)}", )
def test_temporal_search_single_tailed(): # Test single tailed utcnow = datetime.utcnow().replace(microsecond=0, tzinfo=timezone.utc) utcnow_str = utcnow.strftime(DATETIME_RFC339) search = Search(collections=["collection1"], datetime=utcnow_str) assert search.start_date == None assert search.end_date == utcnow
def test_api_fields_extension(): Search( collections=["collection1"], fields={ "includes": {"field1", "field2"}, "excludes": {"field3", "field4"} }, )
def test_api_query_extension_invalid(): # Invalid operator with pytest.raises(ValidationError): Search( collections=["collection1", "collection2"], query={"field": { "greater_than": 100 }}, )
def test_api_sort_extension_invalid(): # Invalid sort direction with pytest.raises(ValidationError): Search( collections=["collection1", "collection2"], sortby=[{ "field": "field1", "direction": "ascending" }], )
def test_api_query_extension(): # One field Search(collections=["collection1", "collection2"], query={"field": { "lt": 100 }}) # Many fields Search( collections=["collection1", "collection2"], query={ "field": { "lt": 100 }, "field1": { "gt": 200 } }, )
def test_temporal_search(): # Test single tailed utcnow = datetime.utcnow().strftime(DATETIME_RFC339) search = Search(collections=["collection1"], datetime=utcnow) assert len(search.datetime) == 2 assert search.datetime == ["..", utcnow] # Test two tailed search = Search(collections=["collection1"], datetime=f"{utcnow}/{utcnow}") assert len(search.datetime) == 2 assert search.datetime == [utcnow, utcnow] search = Search(collections=["collection1"], datetime=f"{utcnow}/..") assert len(search.datetime) == 2 assert search.datetime == [utcnow, ".."] # Test open date range search = Search(collections=["collection1"], datetime=f"../..") assert len(search.datetime) == 2 assert search.datetime == ["..", ".."]
def test_invalid_spatial_search(): # bbox and intersects are mutually exclusive with pytest.raises(ValidationError): Search( collections=["collection1", "collection2"], intersects={ "type": "Point", "coordinates": [0, 0] }, bbox=[-180, -90, 180, 90], ) # Invalid geojson with pytest.raises(ValidationError): Search( collections=["collection1", "collection2"], intersects={ "type": "Polygon", "coordinates": [0] }, )
def test_api_sort_extension(): Search( collections=["collection1", "collection2"], sortby=[ { "field": "field1", "direction": "asc" }, { "field": "field2", "direction": "desc" }, ], )
def test_temporal_search_open(): # Test open date range search = Search(collections=["collection1"], datetime="../..") assert search.start_date == search.end_date == None
def test_search_by_id(): Search(collections=["collection1", "collection2"], ids=["id1", "id2"])
def test_search(): Search(collections=["collection1", "collection2"])
def test_search_invalid_bbox(bbox): with pytest.raises(ValidationError): Search(collections=["foo"], bbox=bbox)
def test_search_geometry_bbox(): search = Search(collections=["foo", "bar"], bbox=[0, 0, 1, 1]) geom1 = shape(search.spatial_filter) geom2 = Polygon.from_bounds(*search.bbox) assert (geom1.intersection(geom2).area / geom1.union(geom2).area) == 1.0