Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
czue committed Oct 9, 2024
1 parent e1b9236 commit 8cbe507
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion commcare_connect/reports/tests/test_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
PaymentUnitFactory,
UserVisitFactory,
)
from commcare_connect.reports.views import _get_table_data_for_quarter
from commcare_connect.reports.views import _get_table_data_for_quarter, _results_to_geojson


@pytest.mark.django_db
Expand Down Expand Up @@ -62,3 +62,47 @@ def test_delivery_stats(opportunity: Opportunity):
assert unknown_delivery_type_data[0]["users"] == 0
assert unknown_delivery_type_data[0]["services"] == 0
assert unknown_delivery_type_data[0]["beneficiaries"] == 0


def test_results_to_geojson():
# Test input
results = [
{"gps_location_long": "10.123", "gps_location_lat": "20.456", "status": "approved", "other_field": "value1"},
{"gps_location_long": "30.789", "gps_location_lat": "40.012", "status": "rejected", "other_field": "value2"},
{"gps_location_long": "invalid", "gps_location_lat": "50.678", "status": "unknown", "other_field": "value3"},
{"status": "approved", "other_field": "value4"}, # Case where lat/lon are not present
{ # Case where lat/lon are null
"gps_location_long": None,
"gps_location_lat": None,
"status": "rejected",
"other_field": "value5",
},
]

# Call the function
geojson = _results_to_geojson(results)

# Assertions
assert geojson["type"] == "FeatureCollection"
assert len(geojson["features"]) == 2 # Only the first two results should be included

# Check the first feature
feature1 = geojson["features"][0]
assert feature1["type"] == "Feature"
assert feature1["geometry"]["type"] == "Point"
assert feature1["geometry"]["coordinates"] == [10.123, 20.456]
assert feature1["properties"]["status"] == "approved"
assert feature1["properties"]["other_field"] == "value1"
assert feature1["properties"]["color"] == "#00FF00"

# Check the second feature
feature2 = geojson["features"][1]
assert feature2["type"] == "Feature"
assert feature2["geometry"]["type"] == "Point"
assert feature2["geometry"]["coordinates"] == [30.789, 40.012]
assert feature2["properties"]["status"] == "rejected"
assert feature2["properties"]["other_field"] == "value2"
assert feature2["properties"]["color"] == "#FF0000"

# Check that the other cases are not included
assert all(f["properties"]["other_field"] not in ["value3", "value4", "value5"] for f in geojson["features"])

0 comments on commit 8cbe507

Please sign in to comment.