-
Notifications
You must be signed in to change notification settings - Fork 474
Do not blindly delete duplicate schedules #389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,13 +7,17 @@ | |
| from django.db.migrations.autodetector import MigrationAutodetector | ||
| from django.db.migrations.loader import MigrationLoader | ||
| from django.db.migrations.questioner import NonInteractiveMigrationQuestioner | ||
| from django.utils import timezone | ||
|
|
||
| import timezone_field | ||
|
|
||
| from django_celery_beat import migrations as beat_migrations | ||
| from django_celery_beat.models import ( | ||
| crontab_schedule_celery_timezone, | ||
| SolarSchedule, | ||
| CrontabSchedule, | ||
| ClockedSchedule, | ||
| IntervalSchedule, | ||
| ) | ||
|
|
||
|
|
||
|
|
@@ -74,6 +78,20 @@ def test_default_timezone_without_settings_config(self): | |
| def test_default_timezone_with_settings_config(self): | ||
| assert crontab_schedule_celery_timezone() == self.FIRST_VALID_TIMEZONE | ||
|
|
||
| def test_duplicate_schedules(self): | ||
| # See: https://github.com/celery/django-celery-beat/issues/322 | ||
| # create 2 duplicates schedules | ||
| sched1 = CrontabSchedule.objects.create(hour="4") | ||
| CrontabSchedule.objects.create(hour="4") | ||
| self.assertEqual(CrontabSchedule.objects.count(), 2) | ||
|
||
| # try to create a duplicate CrontabSchedule from a celery schedule | ||
| schedule = schedules.crontab(hour="4") | ||
| sched3 = CrontabSchedule.from_schedule(schedule) | ||
| # the schedule should be the first of the 2 previous duplicates | ||
| self.assertEqual(sched3, sched1) | ||
| # and the duplicates should not be deleted ! | ||
| self.assertEqual(CrontabSchedule.objects.count(), 2) | ||
|
|
||
|
|
||
| class SolarScheduleTestCase(TestCase): | ||
| EVENT_CHOICES = SolarSchedule._meta.get_field("event").choices | ||
|
|
@@ -99,3 +117,43 @@ def test_celery_solar_schedules_included_as_event_choices(self): | |
|
|
||
| for event_choice in event_choices_values: | ||
| assert event_choice in schedules.solar._all_events | ||
|
|
||
| def test_duplicate_schedules(self): | ||
| # Duplicates cannot be tested for solar schedules because of the | ||
| # unique constraints in the SolarSchedule model | ||
| pass | ||
|
||
|
|
||
|
|
||
| class IntervalScheduleTestCase(TestCase): | ||
|
|
||
| def test_duplicate_schedules(self): | ||
| # See: https://github.com/celery/django-celery-beat/issues/322 | ||
| kwargs = {'every': 1, 'period': IntervalSchedule.SECONDS} | ||
| # create 2 duplicates schedules | ||
| sched1 = IntervalSchedule.objects.create(**kwargs) | ||
| IntervalSchedule.objects.create(**kwargs) | ||
| self.assertEqual(IntervalSchedule.objects.count(), 2) | ||
| # try to create a duplicate IntervalSchedule from a celery schedule | ||
| schedule = schedules.schedule(run_every=1.0) | ||
| sched3 = IntervalSchedule.from_schedule(schedule) | ||
| # the schedule should be the first of the 2 previous duplicates | ||
| self.assertEqual(sched3, sched1) | ||
| # and the duplicates should not be deleted ! | ||
| self.assertEqual(IntervalSchedule.objects.count(), 2) | ||
|
|
||
|
|
||
| class ClockedScheduleTestCase(TestCase): | ||
|
|
||
| def test_duplicate_schedules(self): | ||
| # See: https://github.com/celery/django-celery-beat/issues/322 | ||
| d = timezone.now() | ||
| # create 2 duplicates schedules | ||
| sched1 = ClockedSchedule.objects.create(clocked_time=d) | ||
| sched2 = ClockedSchedule.objects.create(clocked_time=d) | ||
| self.assertEqual(ClockedSchedule.objects.count(), 2) | ||
| # try to create a duplicate ClockedSchedule from a previous schedule | ||
| sched3 = ClockedSchedule.from_schedule(sched2.schedule) | ||
| # the schedule should be the first of the 2 previous duplicates | ||
| self.assertEqual(sched3, sched1) | ||
| # and the duplicates should not be deleted ! | ||
| self.assertEqual(ClockedSchedule.objects.count(), 2) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the interest of avoiding dead code, why not drop these lines altogether?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the constraint is here from the very beginning (ec46367) we can indeed drop the MultipleObjectsReturned block (as well as the test method of course)