Skip to content

Commit

Permalink
Add tests for normalising incorrect percentages for countries and sec…
Browse files Browse the repository at this point in the history
…tors
  • Loading branch information
leilafarsani committed Dec 3, 2024
1 parent c9f8536 commit f2b1373
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions tests/test_at_activity_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ def test_split_by_country():
] == results


def test_split_by_country_with_incorrect_percentages():
"""Test that country splits work correctly even with percentages that don't sum to 100"""

iati_activity = IATIActivity(
transactions=[IATIActivityTransaction(value=1000)],
recipient_countries=[
IATIActivityRecipientCountry(code="FR", percentage=30), # 30%
IATIActivityRecipientCountry(code="GB", percentage=40), # 40%
],
)

results = iati_activity.get_transactions_split_as_json()

# Check results with ranges
assert len(results) == 2
assert results[0]["recipient_country_code"] == "FR"
assert 428.57 <= results[0]["value"] <= 428.58
assert results[1]["recipient_country_code"] == "GB"
assert 571.42 <= results[1]["value"] <= 571.43
assert all(r["sectors"] == [] for r in results)


def test_no_split_but_sector_set():
"""If a activity only has one sector (per vocab), then no split should happen but the sectors should appear on the transactions"""

Expand Down Expand Up @@ -131,6 +153,42 @@ def test_split_by_sector():
] == results


def test_split_by_sector_with_incorrect_percentages():
"""Test that sector splits work correctly even with percentages that don't sum to 100"""

iati_activity = IATIActivity(
transactions=[IATIActivityTransaction(value=1000)],
sectors=[
IATIActivitySector(vocabulary="cats", code="Henry", percentage=30), # 30%
IATIActivitySector(vocabulary="cats", code="Linda", percentage=40), # 40%
IATIActivitySector(vocabulary="dogs", code="Rover", percentage=100), # 100%
],
)

results = iati_activity.get_transactions_split_as_json()

# Check results with ranges
assert len(results) == 3

# Check Henry (cats)
assert results[0]["sectors"][0]["code"] == "Henry"
assert results[0]["sectors"][0]["vocabulary"] == "cats"
assert 428.57 <= results[0]["value"] <= 428.58
assert results[0]["recipient_country_code"] is None

# Check Linda (cats)
assert results[1]["sectors"][0]["code"] == "Linda"
assert results[1]["sectors"][0]["vocabulary"] == "cats"
assert 571.42 <= results[1]["value"] <= 571.43
assert results[1]["recipient_country_code"] is None

# Check Rover (dogs)
assert results[2]["sectors"][0]["code"] == "Rover"
assert results[2]["sectors"][0]["vocabulary"] == "dogs"
assert results[2]["value"] == 1000
assert results[2]["recipient_country_code"] is None


def test_split_by_everything():
iati_activity = IATIActivity(
transactions=[IATIActivityTransaction(value=1000)],
Expand Down

0 comments on commit f2b1373

Please sign in to comment.