262 lines
13 KiB
Python
262 lines
13 KiB
Python
from datetime import timedelta
|
|
from decimal import Decimal
|
|
|
|
from django.core.exceptions import ValidationError
|
|
from django.db import IntegrityError, transaction
|
|
from django.test import TestCase, override_settings
|
|
from django.urls import reverse
|
|
from django.utils import timezone
|
|
|
|
from .h3_utils import cell_from_coordinates, h3_resolution
|
|
from .models import ClassificationRule, Event, PendingEvent
|
|
|
|
|
|
class ClassificationRuleApiTests(TestCase):
|
|
def test_whitelist_returns_only_enabled_whitelist_entries_in_value_order(self):
|
|
ClassificationRule.objects.create(value="Explosion", rule_type=ClassificationRule.RuleType.WHITELIST)
|
|
ClassificationRule.objects.create(value="Fireworks", rule_type=ClassificationRule.RuleType.WHITELIST)
|
|
ClassificationRule.objects.create(value="Disabled firework", rule_type=ClassificationRule.RuleType.WHITELIST, enabled=False)
|
|
ClassificationRule.objects.create(value="Speech", rule_type=ClassificationRule.RuleType.BLACKLIST)
|
|
|
|
response = self.client.get(reverse("classification_rules:whitelist"))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertEqual(response.json(), {"classifications": ["Explosion", "Fireworks"]})
|
|
|
|
def test_blacklist_returns_only_enabled_blacklist_entries_in_value_order(self):
|
|
ClassificationRule.objects.create(value="Speech", rule_type=ClassificationRule.RuleType.BLACKLIST)
|
|
ClassificationRule.objects.create(value="Conversation", rule_type=ClassificationRule.RuleType.BLACKLIST)
|
|
ClassificationRule.objects.create(value="Disabled music", rule_type=ClassificationRule.RuleType.BLACKLIST, enabled=False)
|
|
ClassificationRule.objects.create(value="Fireworks", rule_type=ClassificationRule.RuleType.WHITELIST)
|
|
|
|
response = self.client.get(reverse("classification_rules:blacklist"))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertEqual(response.json(), {"classifications": ["Conversation", "Speech"]})
|
|
|
|
def test_classification_rule_is_unique_within_a_rule_type(self):
|
|
ClassificationRule.objects.create(value="Fireworks", rule_type=ClassificationRule.RuleType.WHITELIST)
|
|
|
|
with transaction.atomic():
|
|
with self.assertRaises(IntegrityError):
|
|
ClassificationRule.objects.create(value="Fireworks", rule_type=ClassificationRule.RuleType.WHITELIST)
|
|
|
|
ClassificationRule.objects.create(value="Fireworks", rule_type=ClassificationRule.RuleType.BLACKLIST)
|
|
|
|
|
|
class ManualPendingEventTests(TestCase):
|
|
def test_manual_report_creates_a_timestamped_pending_event(self):
|
|
response = self.client.post(reverse("fireworks_detect:submit_manual_pending"), {
|
|
"occurrences": "4",
|
|
"latitude": "52.1234567",
|
|
"longitude": "5.9876543",
|
|
})
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertContains(response, "Report sent to Pending")
|
|
pending = PendingEvent.objects.get()
|
|
self.assertEqual(pending.original_filename, "Manual fireworks report")
|
|
self.assertEqual(pending.source_type, PendingEvent.SourceType.MANUAL)
|
|
self.assertEqual(pending.occurrences, 4)
|
|
self.assertEqual(pending.latitude, Decimal("52.123457"))
|
|
self.assertEqual(pending.longitude, Decimal("5.987654"))
|
|
self.assertEqual(pending.h3_cell, cell_from_coordinates(pending.latitude, pending.longitude))
|
|
self.assertIsNone(pending.direction_degrees)
|
|
self.assertIsNone(pending.direction_accuracy_degrees)
|
|
self.assertEqual(pending.direction_source, "")
|
|
self.assertFalse(pending.sensor)
|
|
self.assertFalse(pending.audio_file)
|
|
self.assertEqual(pending.classifier_response, {})
|
|
self.assertIsNotNone(pending.created_at)
|
|
|
|
def test_manual_report_rejects_an_invalid_occurrence_count(self):
|
|
response = self.client.post(reverse("fireworks_detect:submit_manual_pending"), {"occurrences": "0"})
|
|
|
|
self.assertEqual(response.status_code, 400)
|
|
self.assertContains(response, "Choose between 1 and 1,000 occurrences.", status_code=400)
|
|
self.assertFalse(PendingEvent.objects.exists())
|
|
|
|
def test_event_details_and_source_type_are_kept_when_a_pending_event_is_approved(self):
|
|
pending = PendingEvent.objects.create(
|
|
original_filename="Manual fireworks report",
|
|
classifier_response={},
|
|
source_type=PendingEvent.SourceType.MANUAL,
|
|
occurrences=3,
|
|
location="Back garden",
|
|
latitude=Decimal("52.123456"),
|
|
longitude=Decimal("5.987654"),
|
|
h3_cell=cell_from_coordinates(Decimal("52.123456"), Decimal("5.987654")),
|
|
direction_degrees=237,
|
|
direction_accuracy_degrees=8,
|
|
direction_source="device_compass",
|
|
visually_confirmed=True,
|
|
)
|
|
|
|
event = pending.approve()
|
|
|
|
self.assertEqual(event.source_type, PendingEvent.SourceType.MANUAL)
|
|
self.assertEqual(event.occurrences, 3)
|
|
self.assertEqual(event.location, "Back garden")
|
|
self.assertEqual(event.latitude, Decimal("52.123456"))
|
|
self.assertEqual(event.longitude, Decimal("5.987654"))
|
|
self.assertEqual(event.h3_cell, pending.h3_cell)
|
|
self.assertEqual(event.direction_degrees, 237)
|
|
self.assertEqual(event.direction_accuracy_degrees, 8)
|
|
self.assertEqual(event.direction_source, "device_compass")
|
|
self.assertTrue(event.visually_confirmed)
|
|
|
|
def test_existing_style_records_default_to_sensor_source_type(self):
|
|
pending = PendingEvent.objects.create(original_filename="recording.wav", classifier_response={})
|
|
|
|
self.assertEqual(pending.source_type, PendingEvent.SourceType.SENSOR)
|
|
|
|
def test_home_page_includes_the_manual_report_form(self):
|
|
response = self.client.get(reverse("hello"))
|
|
|
|
self.assertContains(response, "Report fireworks now")
|
|
self.assertContains(response, "I heard fireworks — send to Pending")
|
|
self.assertContains(response, "Capture direction")
|
|
self.assertContains(response, 'manual-direction-panel" hidden')
|
|
self.assertContains(response, "htmx.min.js")
|
|
self.assertContains(response, "/static/vite/")
|
|
self.assertContains(response, 'type="module"')
|
|
def test_manual_event_appears_in_the_normal_event_list_with_its_details(self):
|
|
event = Event.objects.create(
|
|
original_filename="Manual fireworks report",
|
|
classifier_response={},
|
|
source_type=Event.SourceType.MANUAL,
|
|
occurrences=5,
|
|
location="Back garden",
|
|
latitude=Decimal("52.123456"),
|
|
longitude=Decimal("5.987654"),
|
|
direction_degrees=237,
|
|
direction_accuracy_degrees=8,
|
|
visually_confirmed=True,
|
|
)
|
|
|
|
list_response = self.client.get(reverse("fireworks_detect:events_list"))
|
|
self.assertContains(list_response, "Manual fireworks report")
|
|
self.assertContains(list_response, "5 occurrences")
|
|
self.assertContains(list_response, "Back garden")
|
|
|
|
detail_response = self.client.get(reverse("fireworks_detect:event_detail", args=[event.pk]))
|
|
self.assertContains(detail_response, "Report details")
|
|
self.assertContains(detail_response, "Visually confirmed")
|
|
self.assertContains(detail_response, "Captured location")
|
|
self.assertContains(detail_response, cell_from_coordinates(event.latitude, event.longitude))
|
|
self.assertNotContains(detail_response, "52.123456, 5.987654")
|
|
self.assertContains(detail_response, "237° (SW)")
|
|
self.assertContains(detail_response, "Compass accuracy")
|
|
|
|
@override_settings(FIREWORKS_H3_RESOLUTION=8)
|
|
def test_h3_resolution_is_configurable(self):
|
|
self.assertEqual(h3_resolution(), 8)
|
|
self.assertTrue(cell_from_coordinates(Decimal("52.123456"), Decimal("5.987654")))
|
|
|
|
def test_map_uses_manual_h3_data_and_excludes_sensor_events(self):
|
|
manual_cell = cell_from_coordinates(Decimal("52.123456"), Decimal("5.987654"))
|
|
sensor_cell = cell_from_coordinates(Decimal("53.123456"), Decimal("5.987654"))
|
|
Event.objects.create(
|
|
original_filename="Manual fireworks report",
|
|
classifier_response={},
|
|
source_type=Event.SourceType.MANUAL,
|
|
occurrences=4,
|
|
latitude=Decimal("52.123456"),
|
|
longitude=Decimal("5.987654"),
|
|
h3_cell=manual_cell,
|
|
visually_confirmed=True,
|
|
direction_degrees=237,
|
|
direction_accuracy_degrees=8,
|
|
)
|
|
Event.objects.create(
|
|
original_filename="Sensor recording.wav",
|
|
classifier_response={},
|
|
source_type=Event.SourceType.SENSOR,
|
|
occurrences=9,
|
|
latitude=Decimal("53.123456"),
|
|
longitude=Decimal("5.987654"),
|
|
h3_cell=sensor_cell,
|
|
)
|
|
|
|
response = self.client.get(reverse("fireworks_detect:map"), {"range": "all"})
|
|
|
|
self.assertContains(response, "H3 resolution 9")
|
|
self.assertContains(response, "map-events-data")
|
|
self.assertContains(response, manual_cell)
|
|
self.assertContains(response, '"direction_degrees": 237.0')
|
|
self.assertContains(response, "data-h3-resolution")
|
|
self.assertContains(response, 'type="module"')
|
|
self.assertContains(response, "sensor layer disabled")
|
|
self.assertContains(response, "Sensor events — coming soon")
|
|
self.assertNotContains(response, sensor_cell)
|
|
self.assertNotContains(response, "52.123456")
|
|
self.assertNotContains(response, "H3 demo cell")
|
|
|
|
def test_map_date_range_filters_manual_reports(self):
|
|
recent_cell = cell_from_coordinates(Decimal("52.400000"), Decimal("5.700000"))
|
|
older_cell = cell_from_coordinates(Decimal("53.400000"), Decimal("5.700000"))
|
|
Event.objects.create(
|
|
original_filename="Recent manual report", classifier_response={},
|
|
source_type=Event.SourceType.MANUAL, latitude=Decimal("52.400000"),
|
|
longitude=Decimal("5.700000"), h3_cell=recent_cell,
|
|
)
|
|
older = Event.objects.create(
|
|
original_filename="Older manual report", classifier_response={},
|
|
source_type=Event.SourceType.MANUAL, latitude=Decimal("53.400000"),
|
|
longitude=Decimal("5.700000"), h3_cell=older_cell,
|
|
)
|
|
Event.objects.filter(pk=older.pk).update(created_at=timezone.now() - timedelta(days=8))
|
|
|
|
response = self.client.get(reverse("fireworks_detect:map"), {"range": "7"})
|
|
|
|
self.assertContains(response, recent_cell)
|
|
self.assertNotContains(response, older_cell)
|
|
self.assertContains(response, "Last 7 days")
|
|
|
|
|
|
def test_manual_report_saves_a_valid_compass_direction_and_accuracy(self):
|
|
response = self.client.post(reverse("fireworks_detect:submit_manual_pending"), {
|
|
"occurrences": "2",
|
|
"direction_degrees": "237",
|
|
"direction_accuracy_degrees": "8",
|
|
})
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
pending = PendingEvent.objects.get()
|
|
self.assertEqual(pending.direction_degrees, 237)
|
|
self.assertEqual(pending.direction_accuracy_degrees, 8)
|
|
self.assertEqual(pending.direction_source, "device_compass")
|
|
self.assertEqual(pending.direction_cardinal, "SW")
|
|
|
|
def test_zero_degree_direction_is_valid_and_accuracy_may_be_absent(self):
|
|
response = self.client.post(reverse("fireworks_detect:submit_manual_pending"), {
|
|
"occurrences": "1",
|
|
"direction_degrees": "0",
|
|
})
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
pending = PendingEvent.objects.get()
|
|
self.assertEqual(pending.direction_degrees, 0)
|
|
self.assertIsNone(pending.direction_accuracy_degrees)
|
|
self.assertEqual(pending.direction_cardinal, "N")
|
|
|
|
def test_invalid_directions_are_rejected_by_model_validation_and_not_saved_by_manual_submission(self):
|
|
for invalid_direction in (-1, 360):
|
|
pending = PendingEvent(
|
|
original_filename="Manual fireworks report",
|
|
classifier_response={},
|
|
direction_degrees=invalid_direction,
|
|
)
|
|
with self.assertRaises(ValidationError):
|
|
pending.full_clean()
|
|
|
|
response = self.client.post(reverse("fireworks_detect:submit_manual_pending"), {
|
|
"occurrences": "1",
|
|
"direction_degrees": str(invalid_direction),
|
|
})
|
|
self.assertEqual(response.status_code, 200)
|
|
saved = PendingEvent.objects.latest("created_at")
|
|
self.assertIsNone(saved.direction_degrees)
|
|
self.assertIsNone(saved.direction_accuracy_degrees)
|
|
self.assertEqual(saved.direction_source, "")
|