Skip to content

Commit

Permalink
Minimally annotate the tests
Browse files Browse the repository at this point in the history
This gets the tests passing type checking without requiring that
they're fully annotated.
  • Loading branch information
PeterJCLaw committed Jan 8, 2024
1 parent cc3a5fb commit 824915d
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ jobs:
- name: Type-check
run: |
poetry run mypy src
poetry run mypy src tests
validate-dependencies:
name: Check dependency locks
Expand Down
Empty file added src/devdata/py.typed
Empty file.
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


@pytest.fixture()
def test_data_dir():
def test_data_dir() -> Path:
return Path(__file__).parent / "test-data-tmp"


Expand Down
6 changes: 5 additions & 1 deletion tests/test_circular_fk.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from __future__ import annotations

from typing import Any

from test_infrastructure import DevdataTestBase
from turtles.models import Turtle, World


class TestCircularFK(DevdataTestBase):
def get_original_data(self):
def get_original_data(self) -> list[dict[str, Any]]:
return [
{
"model": "turtles.Turtle",
Expand Down
6 changes: 5 additions & 1 deletion tests/test_fk_restriction.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from __future__ import annotations

from typing import Any

from django.contrib.auth.models import User
from test_infrastructure import DevdataTestBase, make_photo_data, make_user_data


class TestFKRestriction(DevdataTestBase):
def get_original_data(self):
def get_original_data(self) -> list[dict[str, Any]]:
return [
# Internal user (included)
make_user_data(101, "internal", is_superuser=True),
Expand Down
21 changes: 15 additions & 6 deletions tests/test_infrastructure/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import collections
import itertools
import json
from typing import Any, Dict, Iterable, Set
from pathlib import Path
from typing import Any, Dict, Iterable

import pytest
from django.core import serializers
Expand All @@ -21,7 +22,7 @@
class DevdataTestBase:
# Public API for tests

def get_original_data(self):
def get_original_data(self) -> list[TestObject]:
raise NotImplementedError

def assert_on_exported_data(self, exported_data):
Expand All @@ -48,12 +49,16 @@ def exported_pks(self, exported_data, model, strategy=None):
return set(x["pk"] for x in exported)

def _filter_exported(
self, lookup: Dict[str, Set], objects: Iterable[TestObject]
self,
lookup: dict[str, set[Any]],
objects: list[TestObject],
) -> Iterable[TestObject]:
obj = objects[0] # Same model so we just use the first for structure
model_name = obj["model"]
model = to_model(model_name)

assert model, model_name

fk_fields = [
(x.attname, to_app_model_label(x.related_model))
for x in model._meta.fields
Expand All @@ -71,13 +76,17 @@ def _filter_exported(
):
yield obj

def dump_data_for_import(self, original_data, test_data_dir):
def dump_data_for_import(
self,
original_data: list[TestObject],
test_data_dir: Path,
) -> None:
# Write out data to the filesystem as if it had been exported
data: dict[str, dict[str, TestObject]]
data: dict[str, dict[str, list[TestObject]]]
data = collections.defaultdict(
lambda: collections.defaultdict(list),
)
exported_pks: dict[str, set] = collections.defaultdict(set)
exported_pks: dict[str, set[Any]] = collections.defaultdict(set)
for obj in original_data:
if obj["strategy"] is None:
continue
Expand Down
9 changes: 6 additions & 3 deletions tests/test_infrastructure/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


def make_photo_data(
pk: int, strategy: Optional[str], **fields
pk: int, strategy: Optional[str], **fields: Any
) -> Dict[str, Any]:
return {
"model": "photofeed.Photo",
Expand All @@ -24,11 +24,14 @@ def make_photo_data(


def make_user_data(
pk: int, strategy: Optional[str], **fields
pk: int, strategy: Optional[str], **fields: Any
) -> Dict[str, Any]:
return {
"model": "auth.User",
"strategy": strategy,
"pk": pk,
"fields": {"username": fake.user_name(), **fields},
"fields": {
"username": fake.user_name(), # type: ignore[attr-defined]
**fields,
},
}
8 changes: 5 additions & 3 deletions tests/test_infrastructure/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import os
import subprocess

Expand All @@ -17,9 +19,9 @@ def run_command(*command, **kwargs):
# the pytest environment.
"TEST_DATABASE_NAME": settings.DATABASES["default"]["NAME"],
},
**kwargs
**kwargs,
)


def assert_ran_successfully(process: subprocess.Popen):
assert process.returncode == 0, process.stderr.decode("utf-8")
def assert_ran_successfully(process: subprocess.Popen[bytes]) -> None:
assert process.returncode == 0, process.stderr.decode("utf-8") # type: ignore[union-attr]
11 changes: 8 additions & 3 deletions tests/testsite/testsite/settings/devdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@
devdata settings for tests.
"""

from __future__ import annotations

from typing import Any

from devdata.strategies import (
ExactQuerySetStrategy,
LatestSampleQuerySetStrategy,
QuerySetStrategy,
RandomSampleQuerySetStrategy,
)
from devdata.types import Anonymiser

from ..custom_strategies import InternalUsersStrategy

DEVDATA_FIELD_ANONYMISERS = {}
DEVDATA_MODEL_ANONYMISERS = {}
DEVDATA_FIELD_ANONYMISERS: dict[str, Anonymiser] = {}
DEVDATA_MODEL_ANONYMISERS: dict[str, dict[str, Anonymiser]] = {}

DEVDATA_DEFAULT_STRATEGY = QuerySetStrategy(name="default")

Expand Down Expand Up @@ -77,6 +82,6 @@
],
}

DEVDATA_EXTRA_STRATEGIES = [
DEVDATA_EXTRA_STRATEGIES: list[tuple[str, dict[str, Any]]] = [
("devdata.extras.PostgresSequences", {}),
]
4 changes: 3 additions & 1 deletion tests/testsite/testsite/settings/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Django default settings, customised as necessary for testing.
"""

from __future__ import annotations

import os
from pathlib import Path

Expand All @@ -11,7 +13,7 @@

DEBUG = True

ALLOWED_HOSTS = []
ALLOWED_HOSTS: list[str] = []

INSTALLED_APPS = [
"django.contrib.admin",
Expand Down

0 comments on commit 824915d

Please sign in to comment.