generated from yunojuno/poetry-template
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
b84bdf0
commit 2a3efbb
Showing
4 changed files
with
93 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import pytest | ||
from django.conf import settings | ||
|
||
from tests.anon import UserAnonymiser | ||
from tests.models import User | ||
|
||
IS_POSTGRES = ( | ||
not settings.DATABASES["default"]["ENGINE"].endswith("postgresql"), | ||
"PostgreSQL only", | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def user() -> User: | ||
return User.objects.create_user( | ||
username="testuser1", first_name="fred", last_name="flintstone" | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def user2() -> User: | ||
return User.objects.create_user( | ||
username="testuser2", first_name="ginger", last_name="rogers" | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def user_anonymiser() -> UserAnonymiser: | ||
return UserAnonymiser() |
This file was deleted.
Oops, something went wrong.
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,61 @@ | ||
from unittest import SkipTest, mock | ||
|
||
import pytest | ||
from django.conf import settings | ||
from django.db import connection | ||
from django.db.backends.utils import CursorWrapper | ||
|
||
from anonymiser.db.expressions import GenerateUuid4 | ||
|
||
from .anon import UserAnonymiser | ||
from .models import User | ||
|
||
# skip if we're using SQLite as it doesn't support UUIDs | ||
if settings.DATABASES["default"]["ENGINE"] == "django.db.backends.sqlite3": | ||
# this will skip all tests in this file | ||
raise SkipTest("Skipping Postgres tets as SQLite is being used.") | ||
|
||
|
||
@pytest.mark.django_db | ||
@mock.patch.object(CursorWrapper, "execute") | ||
def test_generate_uuid4(mock_execute: mock.MagicMock) -> None: | ||
User.objects.update(uuid=GenerateUuid4()) | ||
assert ( | ||
mock_execute.call_args[0][0] | ||
== 'UPDATE "tests_user" SET "uuid" = uuid_generate_v4()' | ||
) | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestPostgresRedaction: | ||
@pytest.fixture(autouse=True) | ||
def activate_postgresql_uuid(self) -> None: | ||
"""Activate the uuid-ossp extension in the test database.""" | ||
with connection.cursor() as cursor: | ||
cursor.execute('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";') | ||
|
||
def test_redact_queryset_none( | ||
self, user: User, user_anonymiser: UserAnonymiser | ||
) -> None: | ||
assert user_anonymiser.redact_queryset(User.objects.none()) == 0 | ||
|
||
def test_redact_queryset_one( | ||
self, user: User, user_anonymiser: UserAnonymiser | ||
) -> None: | ||
uuid = user.uuid | ||
assert user_anonymiser.redact_queryset(User.objects.all()) == 1 | ||
user.refresh_from_db() | ||
assert user.first_name == user_anonymiser.field_redactions["first_name"] | ||
assert user.uuid != uuid | ||
|
||
def test_redact_queryset_two( | ||
self, | ||
user: User, | ||
user2: User, | ||
user_anonymiser: UserAnonymiser, | ||
) -> None: | ||
assert user_anonymiser.redact_queryset(User.objects.all()) == 2 | ||
user.refresh_from_db() | ||
user2.refresh_from_db() | ||
# confirm that we haven't reused the same uuid for all objects | ||
assert user.uuid != user2.uuid |