| @@ 22-120 (lines=99) @@ | ||
| 19 | from ..tasks import BatchDeleteAnimals |
|
| 20 | ||
| 21 | ||
| 22 | class BatchDeleteAnimalsTest( |
|
| 23 | AnimalFeaturesMixin, WebSocketMixin, TestCase): |
|
| 24 | ||
| 25 | def setUp(self): |
|
| 26 | # calling base methods |
|
| 27 | super().setUp() |
|
| 28 | ||
| 29 | # get a submission object |
|
| 30 | self.submission = Submission.objects.get(pk=1) |
|
| 31 | self.submission_id = self.submission.id |
|
| 32 | ||
| 33 | # setting animals to delete |
|
| 34 | self.animal_ids = [ |
|
| 35 | 'ANIMAL:::ID:::132713', |
|
| 36 | 'ANIMAL:::ID:::son', |
|
| 37 | 'ANIMAL:::ID:::mother'] |
|
| 38 | ||
| 39 | # setting tasks |
|
| 40 | self.my_task = BatchDeleteAnimals() |
|
| 41 | ||
| 42 | def test_on_failure(self): |
|
| 43 | """Testing on failure methods""" |
|
| 44 | ||
| 45 | exc = Exception("Test") |
|
| 46 | task_id = "test_task_id" |
|
| 47 | args = [] |
|
| 48 | kwargs = { |
|
| 49 | 'submission_id': self.submission_id, |
|
| 50 | 'animal_ids': self.animal_ids} |
|
| 51 | einfo = ExceptionInfo |
|
| 52 | ||
| 53 | # call on_failure method |
|
| 54 | self.my_task.on_failure(exc, task_id, args, kwargs, einfo) |
|
| 55 | ||
| 56 | # check submission status and message |
|
| 57 | self.submission.refresh_from_db() |
|
| 58 | ||
| 59 | # check submission.state changed |
|
| 60 | self.assertEqual(self.submission.status, ERROR) |
|
| 61 | self.assertEqual( |
|
| 62 | self.submission.message, |
|
| 63 | "Error in animal batch delete: Test") |
|
| 64 | ||
| 65 | # test email sent |
|
| 66 | self.assertGreater(len(mail.outbox), 0) |
|
| 67 | ||
| 68 | # read email |
|
| 69 | email = mail.outbox[0] |
|
| 70 | ||
| 71 | self.assertEqual( |
|
| 72 | "Error in animal batch delete for submission: 1", |
|
| 73 | email.subject) |
|
| 74 | ||
| 75 | self.check_message( |
|
| 76 | message='Error', |
|
| 77 | notification_message='Error in animal batch delete: Test') |
|
| 78 | ||
| 79 | def test_delete_animal(self): |
|
| 80 | # calling task and delete a animal |
|
| 81 | res = self.my_task.run( |
|
| 82 | submission_id=self.submission_id, |
|
| 83 | animal_ids=self.animal_ids) |
|
| 84 | ||
| 85 | self.assertEqual(res, "success") |
|
| 86 | ||
| 87 | # no animals |
|
| 88 | n_animals = Animal.objects.count() |
|
| 89 | self.assertEqual(n_animals, 0) |
|
| 90 | ||
| 91 | # updating validation messages |
|
| 92 | ||
| 93 | # calling a WebSocketMixin method |
|
| 94 | # no validation message since no data in validation table |
|
| 95 | self.check_message( |
|
| 96 | message=STATUSES.get_value_display(NEED_REVISION), |
|
| 97 | notification_message=( |
|
| 98 | "You've removed %s " |
|
| 99 | "animals. Rerun validation please!" % len(self.animal_ids))) |
|
| 100 | ||
| 101 | def test_delete_animal_not_exists(self): |
|
| 102 | # calling task and delete a animal |
|
| 103 | res = self.my_task.run( |
|
| 104 | submission_id=self.submission_id, |
|
| 105 | animal_ids=["meow"]) |
|
| 106 | ||
| 107 | self.assertEqual(res, "success") |
|
| 108 | ||
| 109 | # all animals remain |
|
| 110 | n_animals = Animal.objects.count() |
|
| 111 | self.assertEqual(n_animals, 3) |
|
| 112 | ||
| 113 | # updating validation messages |
|
| 114 | ||
| 115 | # calling a WebSocketMixin method |
|
| 116 | # no validation message since no data in validation table |
|
| 117 | self.check_message( |
|
| 118 | message=STATUSES.get_value_display(NEED_REVISION), |
|
| 119 | notification_message=( |
|
| 120 | "You've removed 0 animals. It wasn't possible to find records " |
|
| 121 | "with these ids: meow. Rerun validation please!") |
|
| 122 | ) |
|
| 123 | ||
| @@ 22-117 (lines=96) @@ | ||
| 19 | from ..tasks import BatchDeleteSamples |
|
| 20 | ||
| 21 | ||
| 22 | class BatchDeleteSamplesTest( |
|
| 23 | SampleFeaturesMixin, WebSocketMixin, TestCase): |
|
| 24 | ||
| 25 | def setUp(self): |
|
| 26 | # calling base methods |
|
| 27 | super().setUp() |
|
| 28 | ||
| 29 | # get a submission object |
|
| 30 | self.submission = Submission.objects.get(pk=1) |
|
| 31 | self.submission_id = self.submission.id |
|
| 32 | ||
| 33 | # setting samples to delete |
|
| 34 | self.sample_ids = ['Siems_0722_393449'] |
|
| 35 | ||
| 36 | # setting tasks |
|
| 37 | self.my_task = BatchDeleteSamples() |
|
| 38 | ||
| 39 | def test_on_failure(self): |
|
| 40 | """Testing on failure methods""" |
|
| 41 | ||
| 42 | exc = Exception("Test") |
|
| 43 | task_id = "test_task_id" |
|
| 44 | args = [] |
|
| 45 | kwargs = { |
|
| 46 | 'submission_id': self.submission_id, |
|
| 47 | 'animal_ids': self.sample_ids} |
|
| 48 | einfo = ExceptionInfo |
|
| 49 | ||
| 50 | # call on_failure method |
|
| 51 | self.my_task.on_failure(exc, task_id, args, kwargs, einfo) |
|
| 52 | ||
| 53 | # check submission status and message |
|
| 54 | self.submission.refresh_from_db() |
|
| 55 | ||
| 56 | # check submission.state changed |
|
| 57 | self.assertEqual(self.submission.status, ERROR) |
|
| 58 | self.assertEqual( |
|
| 59 | self.submission.message, |
|
| 60 | "Error in sample batch delete: Test") |
|
| 61 | ||
| 62 | # test email sent |
|
| 63 | self.assertGreater(len(mail.outbox), 0) |
|
| 64 | ||
| 65 | # read email |
|
| 66 | email = mail.outbox[0] |
|
| 67 | ||
| 68 | self.assertEqual( |
|
| 69 | "Error in sample batch delete for submission: 1", |
|
| 70 | email.subject) |
|
| 71 | ||
| 72 | self.check_message( |
|
| 73 | message='Error', |
|
| 74 | notification_message='Error in sample batch delete: Test') |
|
| 75 | ||
| 76 | def test_delete_sample(self): |
|
| 77 | # calling task and delete a sample |
|
| 78 | res = self.my_task.run( |
|
| 79 | submission_id=self.submission.id, |
|
| 80 | sample_ids=self.sample_ids) |
|
| 81 | ||
| 82 | self.assertEqual(res, "success") |
|
| 83 | ||
| 84 | # no samples |
|
| 85 | n_samples = Sample.objects.count() |
|
| 86 | self.assertEqual(n_samples, 0) |
|
| 87 | ||
| 88 | # updating validation messages |
|
| 89 | ||
| 90 | # calling a WebSocketMixin method |
|
| 91 | # no validation message since no data in validation table |
|
| 92 | self.check_message( |
|
| 93 | message=STATUSES.get_value_display(NEED_REVISION), |
|
| 94 | notification_message=( |
|
| 95 | "You've removed %s " |
|
| 96 | "samples. Rerun validation please!" % len(self.sample_ids))) |
|
| 97 | ||
| 98 | def test_delete_samples_not_exists(self): |
|
| 99 | # calling task and delete a animal |
|
| 100 | res = self.my_task.run( |
|
| 101 | submission_id=self.submission_id, |
|
| 102 | sample_ids=["meow"]) |
|
| 103 | ||
| 104 | self.assertEqual(res, "success") |
|
| 105 | ||
| 106 | # all samples remain |
|
| 107 | n_samples = Sample.objects.count() |
|
| 108 | self.assertEqual(n_samples, 1) |
|
| 109 | ||
| 110 | # updating validation messages |
|
| 111 | ||
| 112 | # calling a WebSocketMixin method |
|
| 113 | # no validation message since no data in validation table |
|
| 114 | self.check_message( |
|
| 115 | message=STATUSES.get_value_display(NEED_REVISION), |
|
| 116 | notification_message=( |
|
| 117 | "You've removed 0 samples. It wasn't possible to find records " |
|
| 118 | "with these ids: meow. Rerun validation please!") |
|
| 119 | ) |
|
| 120 | ||