Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hugorodgerbrown committed Sep 18, 2023
1 parent 631577a commit 540345b
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 109 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ packages = [{ include = "anonymiser" }]
[tool.poetry.dependencies]
python = "^3.10"
django = "^3.2 || ^4.0 || ^5.0"
# optional - used for testing with Postgres
psycopg2-binary = { version = "*", optional = true }

[tool.poetry.group.dev.dependencies]
dj-database-url = "*"
Expand All @@ -41,8 +43,6 @@ pytest-cov = "*"
pytest-django = "*"
ruff = "*"
tox = "*"
# optional - used for testing with Postgres
psycopg2-binary = { version = "*", optional = true }

[tool.poetry.extras]
postgres = ["psycopg2-binary"]
Expand Down
11 changes: 9 additions & 2 deletions tests/settings.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
from os import path
from os import getenv, path

import dj_database_url

DEBUG = True
TEMPLATE_DEBUG = True
USE_TZ = True

DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": "test.db"}}
DATABASE_URL = getenv("DATABASE_URL", "sqlite:///django_anonymise.db")
DATABASES = {"default": dj_database_url.parse(DATABASE_URL)}
# used to skip non-PG tests
IS_SQLITE = DATABASES["default"]["ENGINE"] == "django.db.backends.sqlite3"
IS_POSTGRES = DATABASES["default"]["ENGINE"] == "django.db.backends.postgresql"


INSTALLED_APPS = (
"django.contrib.admin",
Expand Down
10 changes: 0 additions & 10 deletions tests/settings_postgres.py

This file was deleted.

90 changes: 88 additions & 2 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from unittest import mock
from unittest import mock, skipUnless

import pytest
from django.db import models
from django.conf import settings
from django.db import connection, models
from django.db.backends.utils import CursorWrapper

from anonymiser.db.expressions import GenerateUuid4
from anonymiser.models import FieldSummaryData

from .anon import BadUserAnonymiser, UserAnonymiser
Expand Down Expand Up @@ -119,3 +122,86 @@ def test_auto_redact(
def test_bad_anonymiser() -> None:
with pytest.raises(AttributeError):
BadUserAnonymiser().anonymise_field(User(), "first_name")


@pytest.mark.django_db
@mock.patch.object(CursorWrapper, "execute")
@skipUnless(settings.IS_POSTGRES, "Test requires Postgres.")
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=settings.IS_POSTGRES)
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";')

@skipUnless(settings.IS_POSTGRES, "Test requires Postgres.")
def test_redact_queryset_none(
self, user: User, user_anonymiser: UserAnonymiser
) -> None:
assert user_anonymiser.redact_queryset(User.objects.none()) == 0

@skipUnless(settings.IS_POSTGRES, "Test requires Postgres.")
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 == "FIRST_NAME"
assert user.last_name == "LAST_NAME"
assert user.uuid != uuid

@skipUnless(settings.IS_POSTGRES, "Test requires Postgres.")
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

@skipUnless(settings.IS_POSTGRES, "Test requires Postgres.")
@pytest.mark.parametrize(
"auto_redact,location,biography",
[
(True, 255 * "X", 400 * "X"),
(False, "London", "I am a test user"),
],
)
def test_redact_queryset__auto_redact(
self,
user: User,
user_anonymiser: UserAnonymiser,
auto_redact: bool,
location: str,
biography: str,
) -> None:
user_anonymiser.redact_queryset(User.objects.all(), auto_redact=auto_redact)
user.refresh_from_db()
# auto-redacted fields
assert user.location == location
assert user.biography == biography

@skipUnless(settings.IS_POSTGRES, "Test requires Postgres.")
def test_redact_queryset__field_overrides(
self,
user: User,
user_anonymiser: UserAnonymiser,
) -> None:
user_anonymiser.redact_queryset(User.objects.all(), location="Area 51")
user.refresh_from_db()
# auto-redacted fields
assert user.location == "Area 51"
93 changes: 0 additions & 93 deletions tests/test_models_pg.py

This file was deleted.

0 comments on commit 540345b

Please sign in to comment.