Skip to content

Commit

Permalink
prevent that errors are being replicated from signals
Browse files Browse the repository at this point in the history
  • Loading branch information
jefer94 committed Jun 18, 2024
1 parent 2952706 commit d742409
Show file tree
Hide file tree
Showing 113 changed files with 2,884 additions and 2,750 deletions.
42 changes: 21 additions & 21 deletions breathecode/activity/tests/tasks/tests_get_attendancy_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

from django.utils import timezone

from breathecode.activity.tasks import get_attendancy_log
from breathecode.activity import tasks
from breathecode.activity.tasks import get_attendancy_log
from breathecode.utils import NDB

from ...models import StudentActivity
Expand Down Expand Up @@ -45,8 +45,8 @@ class MediaTestSuite(MediaTestCase):
@patch.object(NDB, '__init__', MagicMock(return_value=None))
@patch.object(NDB, 'fetch', MagicMock(return_value=[]))
@patch('breathecode.activity.tasks.get_attendancy_log_per_cohort_user.delay', MagicMock())
@patch('django.db.models.signals.pre_delete.send', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send', MagicMock(return_value=None))
@patch('django.db.models.signals.pre_delete.send_robust', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send_robust', MagicMock(return_value=None))
def test_not_found(self):
get_attendancy_log.delay(1)

Expand All @@ -68,8 +68,8 @@ def test_not_found(self):
@patch.object(NDB, '__init__', MagicMock(return_value=None))
@patch.object(NDB, 'fetch', MagicMock(return_value=[]))
@patch('breathecode.activity.tasks.get_attendancy_log_per_cohort_user.delay', MagicMock())
@patch('django.db.models.signals.pre_delete.send', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send', MagicMock(return_value=None))
@patch('django.db.models.signals.pre_delete.send_robust', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send_robust', MagicMock(return_value=None))
def test_syllabus_not_found(self):
with patch('breathecode.activity.tasks.get_attendancy_log.delay', MagicMock()):
model = self.bc.database.create(cohort=1)
Expand Down Expand Up @@ -100,8 +100,8 @@ def test_syllabus_not_found(self):
@patch.object(NDB, '__init__', MagicMock(return_value=None))
@patch.object(NDB, 'fetch', MagicMock(return_value=[]))
@patch('breathecode.activity.tasks.get_attendancy_log_per_cohort_user.delay', MagicMock())
@patch('django.db.models.signals.pre_delete.send', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send', MagicMock(return_value=None))
@patch('django.db.models.signals.pre_delete.send_robust', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send_robust', MagicMock(return_value=None))
def test_syllabus_version_with_json_with_bad_format(self):
syllabus_versions = [
{
Expand Down Expand Up @@ -186,9 +186,9 @@ def test_syllabus_version_with_json_with_bad_format(self):
@patch.object(NDB, '__init__', MagicMock(return_value=None))
@patch('django.utils.timezone.now', MagicMock(return_value=UTC_NOW))
@patch('breathecode.activity.tasks.get_attendancy_log_per_cohort_user.delay', MagicMock())
@patch('breathecode.admissions.signals.student_edu_status_updated.send', MagicMock())
@patch('django.db.models.signals.pre_delete.send', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send_robust', MagicMock())
@patch('django.db.models.signals.pre_delete.send_robust', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send_robust', MagicMock(return_value=None))
def test_the_student_attended_the_first_day(self):
cases = [
([get_datastore_seed(self.bc.fake.slug(), 1, {'slug': 'classroom_attendance'})], [], [1], []),
Expand Down Expand Up @@ -266,8 +266,8 @@ def test_the_student_attended_the_first_day(self):
@patch.object(NDB, '__init__', MagicMock(return_value=None))
@patch('django.utils.timezone.now', MagicMock(return_value=UTC_NOW))
@patch('breathecode.activity.tasks.get_attendancy_log_per_cohort_user.delay', MagicMock())
@patch('django.db.models.signals.pre_delete.send', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send', MagicMock(return_value=None))
@patch('django.db.models.signals.pre_delete.send_robust', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send_robust', MagicMock(return_value=None))
def test_the_students_attended_all_days(self):
cases = [
([
Expand Down Expand Up @@ -470,8 +470,8 @@ def test_the_students_attended_all_days(self):
@patch('breathecode.utils.ndb.NDB.__init__', MagicMock(return_value=None))
@patch('django.utils.timezone.now', MagicMock(return_value=UTC_NOW))
@patch('breathecode.activity.tasks.get_attendancy_log_per_cohort_user.delay', MagicMock())
@patch('django.db.models.signals.pre_delete.send', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send', MagicMock(return_value=None))
@patch('django.db.models.signals.pre_delete.send_robust', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send_robust', MagicMock(return_value=None))
def test_the_students_attended_all_days__the_days_is_string(self):
cases = [
([
Expand Down Expand Up @@ -676,8 +676,8 @@ def test_the_students_attended_all_days__the_days_is_string(self):
@patch.object(NDB, '__init__', MagicMock(return_value=None))
@patch('django.utils.timezone.now', MagicMock(return_value=UTC_NOW))
@patch('breathecode.activity.tasks.get_attendancy_log_per_cohort_user.delay', MagicMock())
@patch('django.db.models.signals.pre_delete.send', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send', MagicMock(return_value=None))
@patch('django.db.models.signals.pre_delete.send_robust', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send_robust', MagicMock(return_value=None))
def test_the_students_attended_all_days__duration_in_days(self):
cases = [
([
Expand Down Expand Up @@ -1079,8 +1079,8 @@ def test_the_students_attended_all_days__duration_in_days(self):
@patch.object(NDB, '__init__', MagicMock(return_value=None))
@patch('django.utils.timezone.now', MagicMock(return_value=UTC_NOW))
@patch('breathecode.activity.tasks.get_attendancy_log_per_cohort_user.delay', MagicMock())
@patch('django.db.models.signals.pre_delete.send', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send', MagicMock(return_value=None))
@patch('django.db.models.signals.pre_delete.send_robust', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send_robust', MagicMock(return_value=None))
def test_the_students_attended_all_days__duration_in_days__two_cohort_users(self):
cases = [
([
Expand Down Expand Up @@ -1480,9 +1480,9 @@ def test_the_students_attended_all_days__duration_in_days__two_cohort_users(self
@patch.object(NDB, '__init__', MagicMock(return_value=None))
@patch('django.utils.timezone.now', MagicMock(return_value=UTC_NOW))
@patch('breathecode.activity.tasks.get_attendancy_log_per_cohort_user.delay', MagicMock())
@patch('breathecode.admissions.signals.student_edu_status_updated.send', MagicMock())
@patch('django.db.models.signals.pre_delete.send', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send_robust', MagicMock())
@patch('django.db.models.signals.pre_delete.send_robust', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send_robust', MagicMock(return_value=None))
def test_the_students_attended_all_days__duration_in_days__two_cohort_users__they_was_deleted(self):
cases = [
([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
Test /answer
"""
import logging
from datetime import timedelta
import random
from datetime import timedelta
from unittest.mock import MagicMock, call, patch

from django.utils import timezone

from breathecode.activity.tasks import get_attendancy_log_per_cohort_user
from breathecode.activity import tasks
from breathecode.activity.tasks import get_attendancy_log_per_cohort_user
from breathecode.utils import NDB

from ..mixins import MediaTestCase
Expand Down Expand Up @@ -43,8 +43,8 @@ class MediaTestSuite(MediaTestCase):
@patch('logging.Logger.info', MagicMock())
@patch('logging.Logger.error', MagicMock())
@patch('breathecode.activity.tasks.get_attendancy_log.delay', MagicMock())
@patch('django.db.models.signals.pre_delete.send', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send', MagicMock(return_value=None))
@patch('django.db.models.signals.pre_delete.send_robust', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send_robust', MagicMock(return_value=None))
def test_not_found(self):
get_attendancy_log_per_cohort_user.delay(1)

Expand All @@ -62,8 +62,8 @@ def test_not_found(self):
@patch('logging.Logger.info', MagicMock())
@patch('logging.Logger.error', MagicMock())
@patch('breathecode.activity.tasks.get_attendancy_log.delay', MagicMock())
@patch('django.db.models.signals.pre_delete.send', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send', MagicMock(return_value=None))
@patch('django.db.models.signals.pre_delete.send_robust', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send_robust', MagicMock(return_value=None))
def test_with_cohort_user(self):
cohort = {'history_log': random.choice(['', None, {}, []])}
model = self.bc.database.create(cohort=cohort, cohort_user=1)
Expand Down Expand Up @@ -94,8 +94,8 @@ def test_with_cohort_user(self):
@patch('logging.Logger.info', MagicMock())
@patch('logging.Logger.error', MagicMock())
@patch('breathecode.activity.tasks.get_attendancy_log.delay', MagicMock())
@patch('django.db.models.signals.pre_delete.send', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send', MagicMock(return_value=None))
@patch('django.db.models.signals.pre_delete.send_robust', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send_robust', MagicMock(return_value=None))
def test_with_cohort_user__user_do_not_assist(self):
utc_now = timezone.now()
day1 = str(random.randint(1, 9))
Expand Down Expand Up @@ -159,8 +159,8 @@ def test_with_cohort_user__user_do_not_assist(self):
@patch('logging.Logger.info', MagicMock())
@patch('logging.Logger.error', MagicMock())
@patch('breathecode.activity.tasks.get_attendancy_log.delay', MagicMock())
@patch('django.db.models.signals.pre_delete.send', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send', MagicMock(return_value=None))
@patch('django.db.models.signals.pre_delete.send_robust', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send_robust', MagicMock(return_value=None))
def test_with_cohort_user__user_assist(self):
utc_now = timezone.now()
day1 = str(random.randint(1, 9))
Expand Down Expand Up @@ -226,8 +226,8 @@ def test_with_cohort_user__user_assist(self):
@patch('logging.Logger.info', MagicMock())
@patch('logging.Logger.error', MagicMock())
@patch('breathecode.activity.tasks.get_attendancy_log.delay', MagicMock())
@patch('django.db.models.signals.pre_delete.send', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send', MagicMock(return_value=None))
@patch('django.db.models.signals.pre_delete.send_robust', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send_robust', MagicMock(return_value=None))
def test_with_cohort_user__with_bad_user_log(self):
utc_now = timezone.now()
available_days = {str(random.randint(1, 9)) for _ in range(4)}
Expand Down
8 changes: 4 additions & 4 deletions breathecode/admissions/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ def update_asset_on_json(from_slug, to_slug, asset_type, simulate=True):
s.save()

if not simulate and len(findings) > 0:
syllabus_asset_slug_updated.send(sender=update_asset_on_json,
from_slug=from_slug,
to_slug=to_slug,
asset_type=asset_type)
syllabus_asset_slug_updated.send_robust(sender=update_asset_on_json,
from_slug=from_slug,
to_slug=to_slug,
asset_type=asset_type)

return findings

Expand Down
22 changes: 11 additions & 11 deletions breathecode/admissions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def save(self, *args, **kwargs):
if created:
self.__old_slug = self.slug

academy_saved.send(instance=self, sender=self.__class__, created=created)
academy_saved.send_robust(instance=self, sender=self.__class__, created=created)


PARTIME = 'PART-TIME'
Expand Down Expand Up @@ -259,7 +259,7 @@ def save(self, *args, **kwargs):

super().save(*args, **kwargs)

if json_modified: syllabus_version_json_updated.send(instance=self, sender=SyllabusVersion)
if json_modified: syllabus_version_json_updated.send_robust(instance=self, sender=SyllabusVersion)


class SyllabusSchedule(models.Model):
Expand Down Expand Up @@ -390,7 +390,7 @@ def save_history_log(self, *args, **kwargs):

super().save(*args, **kwargs)

signals.cohort_saved.send(instance=self, sender=self.__class__, created=False)
signals.cohort_saved.send_robust(instance=self, sender=self.__class__, created=False)

self._current_history_log = self.history_log

Expand All @@ -406,13 +406,13 @@ def save(self, *args, **kwargs):

super().save(*args, **kwargs)

signals.cohort_saved.send(instance=self, sender=self.__class__, created=created)
signals.cohort_saved.send_robust(instance=self, sender=self.__class__, created=created)

if self.history_log and self.history_log != self._current_history_log:
signals.cohort_log_saved.send(instance=self, sender=self.__class__, created=created)
signals.cohort_log_saved.send_robust(instance=self, sender=self.__class__, created=created)

if stage_updated:
signals.cohort_stage_updated.send(instance=self, sender=self.__class__)
signals.cohort_stage_updated.send_robust(instance=self, sender=self.__class__)

self._current_history_log = self.history_log

Expand Down Expand Up @@ -518,12 +518,12 @@ def save(self, *args, **kwargs):
result = super().save(*args, **kwargs) # Call the "real" save() method.

if edu_status_updated:
signals.student_edu_status_updated.send(instance=self, sender=self.__class__)
signals.student_edu_status_updated.send_robust(instance=self, sender=self.__class__)

signals.cohort_log_saved.send(instance=self, sender=self.__class__)
signals.cohort_log_saved.send_robust(instance=self, sender=self.__class__)

if on_create:
signals.cohort_user_created.send(instance=self, sender=self.__class__)
signals.cohort_user_created.send_robust(instance=self, sender=self.__class__)

self.__old_edu_status = self.educational_status

Expand Down Expand Up @@ -597,7 +597,7 @@ def save(self, *args, **kwargs):
self.full_clean()
super().save(*args, **kwargs)

# signals.timeslot_saved.send(instance=self, sender=self.__class__, created=created)
# signals.timeslot_saved.send_robust(instance=self, sender=self.__class__, created=created)


class CohortTimeSlot(TimeSlot):
Expand All @@ -616,4 +616,4 @@ def save(self, *args, **kwargs):
self.full_clean()
super().save(*args, **kwargs)

signals.timeslot_saved.send(instance=self, sender=self.__class__, created=created)
signals.timeslot_saved.send_robust(instance=self, sender=self.__class__, created=created)
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
Test /cohort/user
"""
from unittest.mock import MagicMock, call, patch
from breathecode.admissions.models import CohortUser
from breathecode.admissions.admin import add_student_tag_to_active_campaign

from django.http.request import HttpRequest

from breathecode.admissions.admin import add_student_tag_to_active_campaign
from breathecode.admissions.models import CohortUser

from ..mixins import AdmissionsTestCase


Expand All @@ -14,8 +17,8 @@ class CohortUserTestSuite(AdmissionsTestCase):
"""

@patch('breathecode.marketing.tasks.add_cohort_task_to_student.delay', MagicMock())
@patch('django.db.models.signals.pre_delete.send', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send', MagicMock(return_value=None))
@patch('django.db.models.signals.pre_delete.send_robust', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send_robust', MagicMock(return_value=None))
def test_add_student_tag_to_active_campaign__zero_cohort_users(self):
from breathecode.marketing.tasks import add_cohort_task_to_student

Expand All @@ -31,8 +34,8 @@ def test_add_student_tag_to_active_campaign__zero_cohort_users(self):
"""

@patch('breathecode.marketing.tasks.add_cohort_task_to_student.delay', MagicMock())
@patch('django.db.models.signals.pre_delete.send', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send', MagicMock(return_value=None))
@patch('django.db.models.signals.pre_delete.send_robust', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send_robust', MagicMock(return_value=None))
def test_add_student_tag_to_active_campaign__one_cohort_user(self):
from breathecode.marketing.tasks import add_cohort_task_to_student

Expand All @@ -52,8 +55,8 @@ def test_add_student_tag_to_active_campaign__one_cohort_user(self):
"""

@patch('breathecode.marketing.tasks.add_cohort_task_to_student.delay', MagicMock())
@patch('django.db.models.signals.pre_delete.send', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send', MagicMock(return_value=None))
@patch('django.db.models.signals.pre_delete.send_robust', MagicMock(return_value=None))
@patch('breathecode.admissions.signals.student_edu_status_updated.send_robust', MagicMock(return_value=None))
def test_add_student_tag_to_active_campaign__two_cohort_users(self):
from breathecode.marketing.tasks import add_cohort_task_to_student

Expand Down
Loading

0 comments on commit d742409

Please sign in to comment.