-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
431 additions
and
95 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
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 |
---|---|---|
@@ -1,2 +1,10 @@ | ||
from .api import APITestCase, APIClient | ||
""" | ||
© Ocado Group | ||
Created on 08/12/2023 at 18:05:20(+00:00). | ||
All test helpers. | ||
""" | ||
|
||
from .api import APIClient, APITestCase | ||
from .cron import CronTestCase, CronTestClient | ||
from .model import ModelTestCase |
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,40 @@ | ||
""" | ||
© Ocado Group | ||
Created on 08/12/2023 at 18:05:47(+00:00). | ||
Test helpers for Django models. | ||
""" | ||
|
||
import typing as t | ||
|
||
from django.db.models import Model | ||
from django.db.utils import IntegrityError | ||
from django.test import TestCase | ||
|
||
AnyModel = t.TypeVar("AnyModel", bound=Model) | ||
|
||
|
||
class ModelTestCase(TestCase, t.Generic[AnyModel]): | ||
"""Base for all model test cases.""" | ||
|
||
@classmethod | ||
def get_model_class(cls) -> t.Type[AnyModel]: | ||
"""Get the model's class. | ||
Returns: | ||
The model's class. | ||
""" | ||
|
||
# pylint: disable-next=no-member | ||
return t.get_args(cls.__orig_bases__[0])[ # type: ignore[attr-defined] | ||
0 | ||
] | ||
|
||
def assert_raises_integrity_error(self, *args, **kwargs): | ||
"""Assert the code block raises an integrity error. | ||
Returns: | ||
Error catcher that will assert if an integrity error is raised. | ||
""" | ||
|
||
return self.assertRaises(IntegrityError, *args, **kwargs) |
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
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
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,107 @@ | ||
""" | ||
© Ocado Group | ||
Created on 08/12/2023 at 15:48:38(+00:00). | ||
""" | ||
|
||
from unittest.mock import patch | ||
|
||
from django.test import TestCase | ||
from django.utils import timezone | ||
|
||
from ...models import User | ||
|
||
|
||
class TestAbstract(TestCase): | ||
""" | ||
Tests the abstract model inherited by other models. | ||
Abstract model path: codeforlife.models | ||
""" | ||
|
||
fixtures = [ | ||
"users", | ||
"teachers", | ||
] | ||
|
||
def setUp(self): | ||
self.john_doe = User.objects.get(pk=1) | ||
self.jane_doe = User.objects.get(pk=2) | ||
|
||
def test_delete(self): | ||
""" | ||
Deleting a model instance sets its deletion schedule. | ||
""" | ||
|
||
now = timezone.now() | ||
with patch.object(timezone, "now", return_value=now) as timezone_now: | ||
self.john_doe.delete() | ||
|
||
assert timezone_now.call_count == 2 | ||
assert self.john_doe.delete_after == now + User.delete_wait | ||
assert self.john_doe.last_saved_at == now | ||
|
||
def test_objects__delete(self): | ||
""" | ||
Deleting a set of models in a query sets their deletion schedule. | ||
""" | ||
|
||
now = timezone.now() | ||
with patch.object(timezone, "now", return_value=now) as timezone_now: | ||
User.objects.filter( | ||
pk__in=[ | ||
self.john_doe.pk, | ||
self.jane_doe.pk, | ||
] | ||
).delete() | ||
|
||
assert timezone_now.call_count == 2 | ||
|
||
self.john_doe.refresh_from_db() | ||
assert self.john_doe.delete_after == now + User.delete_wait | ||
assert self.john_doe.last_saved_at == now | ||
|
||
self.jane_doe.refresh_from_db() | ||
assert self.jane_doe.delete_after == now + User.delete_wait | ||
assert self.jane_doe.last_saved_at == now | ||
|
||
def test_objects__create(self): | ||
""" | ||
Creating a model records when it was first saved. | ||
""" | ||
|
||
now = timezone.now() | ||
with patch.object(timezone, "now", return_value=now) as timezone_now: | ||
user = User.objects.create_user( | ||
password="password", | ||
first_name="first_name", | ||
last_name="last_name", | ||
email="[email protected]", | ||
) | ||
|
||
assert timezone_now.call_count == 1 | ||
assert user.last_saved_at == now | ||
|
||
def test_objects__bulk_create(self): | ||
""" | ||
Bulk creating models records when they were first saved. | ||
""" | ||
|
||
now = timezone.now() | ||
with patch.object(timezone, "now", return_value=now) as timezone_now: | ||
users = User.objects.bulk_create( | ||
[ | ||
User( | ||
first_name="first_name_1", | ||
last_name="last_name_1", | ||
email="[email protected]", | ||
), | ||
User( | ||
first_name="first_name_2", | ||
last_name="last_name_2", | ||
email="[email protected]", | ||
), | ||
] | ||
) | ||
|
||
assert timezone_now.call_count == 2 | ||
assert all(user.last_saved_at == now for user in users) |
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,18 @@ | ||
""" | ||
© Ocado Group | ||
Created on 08/12/2023 at 17:43:11(+00:00). | ||
""" | ||
|
||
from ....tests import ModelTestCase | ||
from ...models import Class | ||
|
||
|
||
class TestClass(ModelTestCase[Class]): | ||
"""Tests the Class model.""" | ||
|
||
def test_id__validators__regex(self): | ||
""" | ||
Check the regex validation of a class' ID. | ||
""" | ||
|
||
raise NotImplementedError() # TODO |
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,18 @@ | ||
""" | ||
© Ocado Group | ||
Created on 08/12/2023 at 17:43:11(+00:00). | ||
""" | ||
|
||
from ....tests import ModelTestCase | ||
from ...models import School | ||
|
||
|
||
class TestSchool(ModelTestCase[School]): | ||
"""Tests the School model.""" | ||
|
||
def test_constraints__no_uk_county_if_country_not_uk(self): | ||
""" | ||
Cannot have set a UK county if the country is not set to UK. | ||
""" | ||
|
||
raise NotImplementedError() # TODO |
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,46 @@ | ||
""" | ||
© Ocado Group | ||
Created on 08/12/2023 at 17:43:11(+00:00). | ||
""" | ||
|
||
from ....tests import ModelTestCase | ||
from ...models import Student | ||
|
||
|
||
class TestStudent(ModelTestCase[Student]): | ||
"""Tests the Student model.""" | ||
|
||
def test_objects__create(self): | ||
""" | ||
Create a student. | ||
""" | ||
|
||
raise NotImplementedError() # TODO | ||
|
||
def test_objects__bulk_create(self): | ||
""" | ||
Bulk create many students. | ||
""" | ||
|
||
raise NotImplementedError() # TODO | ||
|
||
def test_objects__create_user(self): | ||
""" | ||
Create a user with a student profile. | ||
""" | ||
|
||
raise NotImplementedError() # TODO | ||
|
||
def test_objects__bulk_create_users(self): | ||
""" | ||
Bulk create many users with a student profile. | ||
""" | ||
|
||
raise NotImplementedError() # TODO | ||
|
||
def test_teacher(self): | ||
""" | ||
Get student's teacher. | ||
""" | ||
|
||
raise NotImplementedError() # TODO |
Oops, something went wrong.