diff --git a/src/tokens/tests/test_views.py b/src/tokens/tests/test_views.py index f29c37439..20fbecef9 100644 --- a/src/tokens/tests/test_views.py +++ b/src/tokens/tests/test_views.py @@ -189,3 +189,17 @@ def test_token_submit_form_view(self) -> None: rows = soup.select("div.alert.alert-warning") matches = [s for s in rows if "This token is not valid yet" in str(s)] self.assertEqual(len(matches), 1, "inactive token find does not return the not valid yet.") + + def test_total_finds_pct_with_no_tokens(self) -> None: + """Test calculating percentage with no tokens.""" + self.client.force_login(self.users[0]) + Token.objects.all().delete() + + response = self.client.get(self.url_dashboard) + result = response.context["widgets"]["total_finds"]["no_js"]["Unique finds"]["pct"] + + assert result == 0.0 + + result = response.context["widgets"]["total_finds"]["no_js"]["Not found"]["pct"] + + assert result == 0.0 diff --git a/src/tokens/views.py b/src/tokens/views.py index 363163968..ba55ac0fc 100644 --- a/src/tokens/views.py +++ b/src/tokens/views.py @@ -139,11 +139,11 @@ def get_total_finds_metrics(self, camp_finds: QuerySet) -> dict: "no_js": { "Unique finds": { "value": total_finds_count, - "pct": (total_finds_count / token_count) * 100 if token_count else 0, + "pct": (total_finds_count / token_count) * 100 if token_count else 0.0, }, "Not found": { "value": (token_count - total_finds_count), - "pct": (token_count - total_finds_count) / token_count * 100 if token_count else 0, + "pct": (token_count - total_finds_count) / token_count * 100 if token_count else 0.0, }, }, "chart": {