-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: organizing test in specific folders
- Loading branch information
1 parent
e368691
commit d3c38f0
Showing
11 changed files
with
451 additions
and
349 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
83 changes: 83 additions & 0 deletions
83
sample_project/sample_app/tests/tests_models/tests_pipeline_models.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from unittest.mock import call, patch | ||
|
||
from django.test import TestCase | ||
from django_cloud_tasks.tests import factories | ||
|
||
|
||
class PipelineModelTest(TestCase): | ||
def test_start_pipeline(self): | ||
pipeline = factories.PipelineFactory() | ||
leaf_already_completed = factories.RoutineWithoutSignalFactory(status="completed") | ||
pipeline.routines.add(leaf_already_completed) | ||
|
||
leaf_already_reverted = factories.RoutineWithoutSignalFactory(status="reverted") | ||
pipeline.routines.add(leaf_already_reverted) | ||
|
||
with patch("django_cloud_tasks.tasks.RoutineExecutorTask.asap") as task: | ||
with self.assertNumQueries(1): | ||
pipeline.start() | ||
task.assert_not_called() | ||
|
||
second_routine = factories.RoutineFactory() | ||
pipeline.routines.add(second_routine) | ||
third_routine = factories.RoutineFactory() | ||
pipeline.routines.add(third_routine) | ||
first_routine = factories.RoutineFactory() | ||
pipeline.routines.add(first_routine) | ||
|
||
another_first_routine = factories.RoutineFactory() | ||
pipeline.routines.add(another_first_routine) | ||
|
||
factories.RoutineVertexFactory(routine=second_routine, next_routine=third_routine) | ||
factories.RoutineVertexFactory(routine=first_routine, next_routine=second_routine) | ||
|
||
with patch("django_cloud_tasks.tasks.RoutineExecutorTask.asap") as task: | ||
with self.assertNumQueries(7): | ||
pipeline.start() | ||
calls = [call(routine_id=first_routine.pk), call(routine_id=another_first_routine.pk)] | ||
task.assert_has_calls(calls, any_order=True) | ||
|
||
def test_revert_pipeline(self): | ||
pipeline = factories.PipelineFactory() | ||
|
||
leaf_already_reverted = factories.RoutineWithoutSignalFactory(status="reverted") | ||
pipeline.routines.add(leaf_already_reverted) | ||
|
||
with patch("django_cloud_tasks.tasks.RoutineReverterTask.asap") as task: | ||
with self.assertNumQueries(1): | ||
pipeline.revert() | ||
task.assert_not_called() | ||
|
||
second_routine = factories.RoutineFactory() | ||
pipeline.routines.add(second_routine) | ||
|
||
third_routine = factories.RoutineWithoutSignalFactory(status="completed") | ||
pipeline.routines.add(third_routine) | ||
|
||
first_routine = factories.RoutineFactory() | ||
pipeline.routines.add(first_routine) | ||
|
||
fourth_routine = factories.RoutineWithoutSignalFactory(status="completed") | ||
pipeline.routines.add(fourth_routine) | ||
|
||
factories.RoutineVertexFactory(routine=second_routine, next_routine=third_routine) | ||
factories.RoutineVertexFactory(routine=first_routine, next_routine=second_routine) | ||
|
||
with patch("django_cloud_tasks.tasks.RoutineReverterTask.asap") as task: | ||
with self.assertNumQueries(7): | ||
pipeline.revert() | ||
calls = [ | ||
call(routine_id=fourth_routine.pk), | ||
call(routine_id=third_routine.pk), | ||
] | ||
task.assert_has_calls(calls, any_order=True) | ||
|
||
def test_add_routine(self): | ||
pipeline = factories.PipelineFactory() | ||
expected_routine_1 = { | ||
"task_name": "DummyRoutineTask", | ||
"body": {"spell": "wingardium leviosa"}, | ||
} | ||
routine = pipeline.add_routine(expected_routine_1) | ||
self.assertEqual(expected_routine_1["body"], routine.body) | ||
self.assertEqual(expected_routine_1["task_name"], routine.task_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
sample_project/sample_app/tests/tests_models/tests_routine_state_machine_models.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from typing import List | ||
from unittest.mock import patch | ||
|
||
from django.core.exceptions import ValidationError | ||
from django.test import TestCase | ||
from django_cloud_tasks import models | ||
from django_cloud_tasks.tests import factories | ||
|
||
|
||
class RoutineStateMachineTest(TestCase): | ||
def setUp(self): | ||
super().setUp() | ||
revert_routine_task = patch("django_cloud_tasks.tasks.RoutineReverterTask.asap") | ||
routine_task = patch("django_cloud_tasks.tasks.RoutineExecutorTask.asap") | ||
routine_task.start() | ||
revert_routine_task.start() | ||
self.addCleanup(routine_task.stop) | ||
self.addCleanup(revert_routine_task.stop) | ||
|
||
def _status_list(self, ignore_items: list) -> list: | ||
statuses = models.Routine.Statuses.values | ||
for item in ignore_items: | ||
statuses.remove(item) | ||
return statuses | ||
|
||
def test_dont_allow_initial_status_not_equal_pending(self): | ||
for status in self._status_list(ignore_items=["pending", "failed", "scheduled"]): | ||
msg_error = f"The initial routine's status must be 'pending' not '{status}'" | ||
with self.assertRaises(ValidationError, msg=msg_error): | ||
factories.RoutineFactory(status=status) | ||
|
||
def test_ignore_if_status_was_not_updated(self): | ||
routine = factories.RoutineFactory(status="pending") | ||
routine.status = "pending" | ||
routine.save() | ||
|
||
def test_allow_to_update_status_from_scheduled_to_running_or_failed(self): | ||
self.assert_machine_status(accepted_status=["running", "failed", "reverting"], from_status="scheduled") | ||
|
||
def test_allow_to_update_status_from_running_to_completed(self): | ||
self.assert_machine_status( | ||
accepted_status=["completed", "failed"], | ||
from_status="running", | ||
) | ||
|
||
def test_allow_to_update_status_from_completed_to_failed_or_reverting(self): | ||
self.assert_machine_status(accepted_status=["reverting"], from_status="completed") | ||
|
||
def test_allow_to_update_status_from_reverting_to_reverted(self): | ||
self.assert_machine_status( | ||
accepted_status=["reverted"], | ||
from_status="reverting", | ||
) | ||
|
||
def assert_machine_status(self, from_status: str, accepted_status: List[str]): | ||
for status in accepted_status: | ||
routine = factories.RoutineWithoutSignalFactory(status=from_status) | ||
routine.status = status | ||
routine.save() | ||
|
||
accepted_status.append(from_status) | ||
for status in self._status_list(ignore_items=accepted_status): | ||
msg_error = f"Status update from '{from_status}' to '{status}' is not allowed" | ||
with self.assertRaises(ValidationError, msg=msg_error): | ||
routine = factories.RoutineWithoutSignalFactory(status=from_status) | ||
routine.status = status | ||
routine.save() |
Oops, something went wrong.