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 bf37700 commit a3b892f
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 13 deletions.
15 changes: 5 additions & 10 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,19 @@ jobs:
--health-timeout 5s
--health-retries 5
strategy:
matrix:
python: ["3.11"]
django: ["42"]

env:
TOXENV: postgres
# env:
# TOXENV: pg-only

steps:
- name: Check out the repository
uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python }}
- name: Set up Python 3.11
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python }}
python-version: "3.11"

- name: Install and run tox
run: |
pip install tox
tox
tox -e pg-only
12 changes: 10 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@
@pytest.fixture
def user() -> User:
return User.objects.create_user(
username="testuser1", first_name="fred", last_name="flintstone"
username="testuser1",
first_name="fred",
last_name="flintstone",
location="London",
biography="I am a test user",
)


@pytest.fixture
def user2() -> User:
return User.objects.create_user(
username="testuser2", first_name="ginger", last_name="rogers"
username="testuser2",
first_name="ginger",
last_name="rogers",
location="New York",
biography="I am another test user",
)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 4.2.5 on 2023-09-18 12:12

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("tests", "0002_user_uuid"),
]

operations = [
migrations.AddField(
model_name="user",
name="biography",
field=models.TextField(blank=True),
),
migrations.AddField(
model_name="user",
name="date_of_birth",
field=models.DateField(blank=True, null=True),
),
migrations.AddField(
model_name="user",
name="location",
field=models.CharField(blank=True, max_length=255),
),
]
3 changes: 3 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@

class User(AbstractUser):
uuid = models.UUIDField(unique=True, default=uuid4, editable=False)
location = models.CharField(max_length=255, blank=True)
biography = models.TextField(blank=True)
date_of_birth = models.DateField(blank=True, null=True)
3 changes: 3 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def test_model_fields_summary(user_anonymiser: UserAnonymiser) -> None:
FieldSummaryData(User._meta.get_field("is_active"), False),
FieldSummaryData(User._meta.get_field("date_joined"), False),
FieldSummaryData(User._meta.get_field("uuid"), False),
FieldSummaryData(User._meta.get_field("location"), False),
FieldSummaryData(User._meta.get_field("biography"), False),
FieldSummaryData(User._meta.get_field("date_of_birth"), False),
FieldSummaryData(User._meta.get_field("groups"), False),
FieldSummaryData(User._meta.get_field("user_permissions"), False),
]
Expand Down
31 changes: 31 additions & 0 deletions tests/test_models_pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,34 @@ def test_redact_queryset_two(
user2.refresh_from_db()
# confirm that we haven't reused the same uuid for all objects
assert user.uuid != user2.uuid

@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

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"
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ deps =
commands =
pytest --cov=anonymiser --verbose --ds={env:DJANGO_SETTINGS_MODULE} tests/

[testenv:postgres]
[testenv:pg-only]
deps =
pytest
pytest-django
Expand Down

0 comments on commit a3b892f

Please sign in to comment.