-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from allenai/favyen/fix-infra-unit-test
Fix marine infrastructure filter unit test
- Loading branch information
Showing
2 changed files
with
54 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,54 @@ | ||
import json | ||
import pathlib | ||
|
||
import pytest | ||
|
||
from rslp.utils.filter import NearInfraFilter | ||
|
||
TEST_INFRA_LON = 1.234 | ||
TEST_INFRA_LAT = 5.678 | ||
|
||
|
||
def test_near_infra_filter() -> None: | ||
# Test case 1: Detection is exactly on infrastructure. | ||
# The coordinates are directly extracted from the geojson file. | ||
infra_lat = 16.613 | ||
infra_lon = 103.381 | ||
class TestNearInfraFilter: | ||
@pytest.fixture | ||
def single_point_infra_filter(self, tmp_path: pathlib.Path) -> NearInfraFilter: | ||
geojson_data = { | ||
"type": "FeatureCollection", | ||
"properties": {}, | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [TEST_INFRA_LON, TEST_INFRA_LAT], | ||
}, | ||
} | ||
], | ||
} | ||
fname = tmp_path / "data.geojson" | ||
with fname.open("w") as f: | ||
json.dump(geojson_data, f) | ||
|
||
filter = NearInfraFilter() | ||
return NearInfraFilter(infra_url=str(fname)) | ||
|
||
# Since this point is exactly an infrastructure point, the filter should discard it (return True) | ||
assert filter.should_filter( | ||
infra_lat, infra_lon | ||
), "Detection should be filtered out as it is located on infrastructure." | ||
def test_exactly_on_infra(self, single_point_infra_filter: NearInfraFilter) -> None: | ||
# Test when detection is exactly on infrastructure. | ||
# The coordinates are directly extracted from the geojson file. | ||
# Since this point is exactly an infrastructure point, the filter should discard it (return True) | ||
assert single_point_infra_filter.should_filter( | ||
TEST_INFRA_LAT, | ||
TEST_INFRA_LON, | ||
), "Detection should be filtered out as it is located on infrastructure." | ||
|
||
# Test case 2: Detection is close to infrastructure. | ||
assert filter.should_filter( | ||
infra_lat + 0.0001, infra_lon + 0.0001 | ||
), "Detection should be filtered out as it is too close to infrastructure." | ||
def test_close_to_infra(self, single_point_infra_filter: NearInfraFilter) -> None: | ||
# Test when detection is close to infrastructure. | ||
assert single_point_infra_filter.should_filter( | ||
TEST_INFRA_LAT + 0.0001, TEST_INFRA_LON + 0.0001 | ||
), "Detection should be filtered out as it is too close to infrastructure." | ||
|
||
# Test case 3: Detection is far from infrastructure. | ||
assert not filter.should_filter( | ||
infra_lat + 0.5, infra_lon + 0.5 | ||
), "Detection should be kept as it is far from infrastructure." | ||
def test_far_from_infra(self, single_point_infra_filter: NearInfraFilter) -> None: | ||
# Test when detection is far from infrastructure. | ||
assert not single_point_infra_filter.should_filter( | ||
TEST_INFRA_LAT + 0.5, TEST_INFRA_LON + 0.5 | ||
), "Detection should be kept as it is far from infrastructure." |