Skip to content

Commit

Permalink
Add test additional dataset for subscription/alert tagging test
Browse files Browse the repository at this point in the history
  • Loading branch information
thenav56 committed Dec 5, 2024
1 parent 3908695 commit a06277e
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 26 deletions.
5 changes: 3 additions & 2 deletions apps/subscription/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ def _cl_name(field):
alert_data
CROSS JOIN {_tb_name(UserAlertSubscription)} AS subscriptions
WHERE
subscriptions.{_cl_name(UserAlertSubscription.filter_alert_country)} = alert_data.country_id AND
(
subscriptions.{_cl_name(UserAlertSubscription.filter_alert_country)} = alert_data.country_id
) AND (
COALESCE(array_length(subscriptions.{_cl_name(UserAlertSubscription.filter_alert_admin1s)}, 1), 0) = 0 OR
-- subscriptions.{_cl_name(UserAlertSubscription.filter_alert_admin1s)} && alert_data.admin1s::integer[]
subscriptions.{_cl_name(UserAlertSubscription.filter_alert_admin1s)} && alert_data.admin1s
) AND (
COALESCE(array_length(subscriptions.{_cl_name(UserAlertSubscription.filter_alert_urgencies)}, 1), 0) = 0 OR
Expand All @@ -76,6 +76,7 @@ def _cl_name(field):
) (
SELECT * FROM tagged_alerts
)
ON CONFLICT DO NOTHING
)
-- Flag processed alerts
UPDATE {_tb_name(Alert)}
Expand Down
132 changes: 108 additions & 24 deletions apps/subscription/tests/test_subscription_alert_tagging.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from collections import defaultdict

from apps.cap_feed.factories import (
Admin1Factory,
AlertFactory,
Expand All @@ -8,11 +10,16 @@
)
from apps.cap_feed.models import Alert, AlertInfo
from apps.subscription.factories import UserAlertSubscriptionFactory
from apps.subscription.models import SubscriptionAlert
from apps.subscription.models import SubscriptionAlert, UserAlertSubscription
from apps.subscription.tasks import process_pending_subscription_alerts
from apps.user.factories import UserFactory
from main.tests import TestCase

Category = AlertInfo.Category
Urgency = AlertInfo.Urgency
Severity = AlertInfo.Severity
Certainty = AlertInfo.Certainty


class TestSubscriptionMutation(TestCase):
def setUp(self):
Expand All @@ -22,35 +29,112 @@ def setUp(self):
self.r_asia = RegionFactory.create(name="Asia")
self.c_nepal = CountryFactory.create(region=self.r_asia)
self.ad_bagmati = Admin1Factory.create(country=self.c_nepal)
self.ad_lalitpur = Admin1Factory.create(country=self.c_nepal)
self.ad_bhaktapur = Admin1Factory.create(country=self.c_nepal)

def test_subscription_alert_tagging(self):
feed1 = FeedFactory.create(country=self.c_nepal)
alert1 = AlertFactory.create(

# Alerts
alert1, alert2, alert3, alert4 = all_alerts = [
AlertFactory.create(
feed=_data[0],
country=_data[1],
admin1s=_data[2],
)
for _data in [
(feed1, self.c_nepal, [self.ad_bagmati]), # 1
(feed1, self.c_nepal, [self.ad_lalitpur]), # 2
(feed1, self.c_nepal, [self.ad_bhaktapur]), # 3
(feed1, self.c_nepal, [self.ad_bagmati, self.ad_bhaktapur]), # 4
]
]

# Processed alerts (Noise data)
AlertFactory.create_batch(
9,
feed=feed1,
country=self.c_nepal,
admin1s=[self.ad_bagmati],
# is_processed_by_subscription=True,
)
AlertInfoFactory.create(
alert=alert1,
category=AlertInfo.Category.HEALTH,
urgency=AlertInfo.Urgency.IMMEDIATE,
severity=AlertInfo.Severity.EXTREME,
certainty=AlertInfo.Certainty.OBSERVED,
admin1s=[],
is_processed_by_subscription=True,
)

UserAlertSubscriptionFactory.create(
user=self.user,
filter_alert_country=self.c_nepal,
filter_alert_admin1s=[self.ad_bagmati.id],
filter_alert_urgencies=[AlertInfo.Urgency.IMMEDIATE],
filter_alert_severities=[],
filter_alert_certainties=[],
filter_alert_categories=[],
)
# AlertInfos
alert_info_dataset = [
(alert1, ((Category.HEALTH, Urgency.IMMEDIATE, Severity.EXTREME, Certainty.OBSERVED),)),
(
alert2,
(
(Category.HEALTH, Urgency.IMMEDIATE, Severity.EXTREME, Certainty.OBSERVED),
(Category.HEALTH, Urgency.FUTURE, Severity.MODERATE, Certainty.OBSERVED),
),
),
(alert3, ((Category.HEALTH, Urgency.FUTURE, Severity.MODERATE, Certainty.OBSERVED),)),
(alert4, ((Category.HEALTH, Urgency.FUTURE, Severity.MINOR, Certainty.OBSERVED),)),
]

assert Alert.objects.filter(is_processed_by_subscription=False).count() == 1
for alert, _infos in alert_info_dataset:
for _data in _infos:
AlertInfoFactory.create(
alert=alert,
category=_data[0],
urgency=_data[1],
severity=_data[2],
certainty=_data[3],
)

subs = [
UserAlertSubscriptionFactory.create(
user=self.user,
filter_alert_country=_data[0],
filter_alert_admin1s=_data[1],
filter_alert_urgencies=_data[2],
filter_alert_severities=_data[3],
filter_alert_certainties=_data[4],
filter_alert_categories=_data[5],
)
for _data in [
(self.c_nepal, [], [], [], [], [], []), # 0
(self.c_nepal, [self.ad_bagmati.pk], [Urgency.IMMEDIATE], [], [], [], []), # 1
(self.c_nepal, [self.ad_bagmati.pk, self.ad_lalitpur.pk], [], [], [], [], []), # 2
(self.c_nepal, [self.ad_lalitpur.pk], [], [], [], [], []), # 3
(self.c_nepal, [self.ad_lalitpur.pk], [], [Severity.MODERATE], [], [], []), # 4
(
self.c_nepal,
[self.ad_bagmati.pk, self.ad_bhaktapur.pk],
[],
[Severity.MODERATE, Severity.MINOR],
[],
[],
[],
), # 5
(self.c_nepal, [self.ad_bagmati.pk], [], [Severity.UNKNOWN], [], [], []), # 6
]
]

assert Alert.objects.filter(is_processed_by_subscription=False).count() == len(all_alerts)
assert UserAlertSubscription.objects.filter(user=self.user).count() == len(subs)
assert SubscriptionAlert.objects.count() == 0
process_pending_subscription_alerts()
assert Alert.objects.filter(is_processed_by_subscription=False).count() == 0
assert SubscriptionAlert.objects.count() != 0

# Run this twice to make sure re-running doesn't break anything
for _ in range(2):
# Make sure all_alerts are tageed as "not processed"
Alert.objects.filter(id__in=[a.pk for a in all_alerts]).update(is_processed_by_subscription=False)
process_pending_subscription_alerts()

subscription_alert_map = defaultdict(set)
for subscription_id, alert_id in SubscriptionAlert.objects.values_list('subscription_id', 'alert_id'):
subscription_alert_map[subscription_id].add(alert_id)

assert Alert.objects.filter(is_processed_by_subscription=False).count() == 0
assert SubscriptionAlert.objects.count() != 0
assert dict(subscription_alert_map) == {
subs[0].pk: {alert1.pk, alert2.pk, alert3.pk, alert4.pk},
subs[1].pk: {alert1.pk},
subs[2].pk: {alert1.pk, alert2.pk, alert4.pk},
subs[3].pk: {alert2.pk},
subs[4].pk: {alert2.pk},
subs[5].pk: {alert3.pk, alert4.pk},
# Empty aren't shown here
# subs[6].pk: {},
}

0 comments on commit a06277e

Please sign in to comment.