Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tests to pass Mypy strict mode #941

Merged
merged 1 commit into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion tests/testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Any

import django
from django.core import checks
from django.db import connection
from django.db.models import (
CASCADE,
Expand Down Expand Up @@ -119,7 +120,7 @@ class DynamicModel(Model):
)

@classmethod
def check(cls, **kwargs):
def check(cls, **kwargs: Any) -> list[checks.CheckMessage]:
# Disable the checks on MySQL so that checks tests don't fail
if not connection.mysql_is_mariadb:
return []
Expand Down
5 changes: 5 additions & 0 deletions tests/testapp/test_aggregates.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ def test_no_rows(self):


class GroupConcatTests(TestCase):
shakes: Author
jk: Author
grisham: Author
str_tutee_ids: list[str]

@classmethod
def setUpTestData(cls):
super().setUpTestData()
Expand Down
4 changes: 2 additions & 2 deletions tests/testapp/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

class CallCheckTest(TestCase):

databases = ["default", "other"]
databases = {"default", "other"}

def test_check(self):
call_command("check", "--database", "default", "--database", "other")


class VariablesTests(TransactionTestCase):

databases = ["default", "other"]
databases = {"default", "other"}

def test_passes(self):
assert check_variables(app_configs=None, databases=["default", "other"]) == []
Expand Down
76 changes: 44 additions & 32 deletions tests/testapp/test_dynamicfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,28 +118,31 @@ def as_sql(self, compiler, connection):


class QueryTests(DynColTestCase):
objs: list[DynamicModel]

@classmethod
def setUpTestData(cls):
super().setUpTestData()
cls.objs = [
DynamicModel(attrs={"a": "b"}),
DynamicModel(attrs={"a": "b", "c": "d"}),
DynamicModel(attrs={"c": "d"}),
DynamicModel(attrs={}),
DynamicModel(
attrs={
"datetimey": dt.datetime(2001, 1, 4, 14, 15, 16),
"datey": dt.date(2001, 1, 4),
"floaty": 128.5,
"inty": 9001,
"stry": "strvalue",
"str_underscorey": "strvalue2",
"timey": dt.time(14, 15, 16),
"nesty": {"level2": "chirp"},
}
),
]
DynamicModel.objects.bulk_create(cls.objs)
DynamicModel.objects.bulk_create(
[
DynamicModel(attrs={"a": "b"}),
DynamicModel(attrs={"a": "b", "c": "d"}),
DynamicModel(attrs={"c": "d"}),
DynamicModel(attrs={}),
DynamicModel(
attrs={
"datetimey": dt.datetime(2001, 1, 4, 14, 15, 16),
"datey": dt.date(2001, 1, 4),
"floaty": 128.5,
"inty": 9001,
"stry": "strvalue",
"str_underscorey": "strvalue2",
"timey": dt.time(14, 15, 16),
"nesty": {"level2": "chirp"},
}
),
]
)
cls.objs = list(DynamicModel.objects.order_by("id"))

def test_equal(self):
Expand Down Expand Up @@ -271,14 +274,17 @@ def test_key_transform_nesty__level2__startswith(self):


class SpeclessQueryTests(DynColTestCase):
objs: list[SpeclessDynamicModel]

@classmethod
def setUpTestData(cls):
super().setUpTestData()
objs = [
SpeclessDynamicModel(attrs={"a": "b"}),
SpeclessDynamicModel(attrs={"a": "c"}),
]
SpeclessDynamicModel.objects.bulk_create(objs)
SpeclessDynamicModel.objects.bulk_create(
[
SpeclessDynamicModel(attrs={"a": "b"}),
SpeclessDynamicModel(attrs={"a": "c"}),
]
)
cls.objs = list(SpeclessDynamicModel.objects.order_by("id"))

def test_simple(self):
Expand All @@ -290,7 +296,7 @@ def test_simple(self):
@isolate_apps("tests.testapp")
class TestCheck(DynColTestCase):

databases = ["default", "other"]
databases = {"default", "other"}

def test_db_not_mariadb(self):
class Valid(models.Model):
Expand Down Expand Up @@ -337,7 +343,9 @@ class Invalid(models.Model):
assert len(errors) == 1
assert errors[0].id == "django_mysql.E009"
assert "'spec' must be a dict" in errors[0].msg
assert "The value passed is of type list" in errors[0].hint
hint = errors[0].hint
assert hint is not None
assert "The value passed is of type list" in hint

def test_spec_key_not_valid(self):
class Invalid(models.Model):
Expand All @@ -347,8 +355,10 @@ class Invalid(models.Model):
assert len(errors) == 1
assert errors[0].id == "django_mysql.E010"
assert "The key '2.0' in 'spec' is not a string" in errors[0].msg
assert "'spec' keys must be of type " in errors[0].hint
assert "'2.0' is of type float" in errors[0].hint
hint = errors[0].hint
assert hint is not None
assert "'spec' keys must be of type " in hint
assert "'2.0' is of type float" in hint

def test_spec_value_not_valid(self):
class Invalid(models.Model):
Expand All @@ -358,9 +368,10 @@ class Invalid(models.Model):
assert len(errors) == 1
assert errors[0].id == "django_mysql.E011"
assert "The value for 'bad' in 'spec' is not an allowed type" in errors[0].msg
hint = errors[0].hint
assert hint is not None
assert (
"'spec' values must be one of the following types: date, datetime"
in errors[0].hint
"'spec' values must be one of the following types: date, datetime" in hint
)

def test_spec_nested_value_not_valid(self):
Expand All @@ -375,9 +386,10 @@ class Invalid(models.Model):
assert (
"The value for 'bad' in 'spec.l1' is not an allowed type" in errors[0].msg
)
hint = errors[0].hint
assert hint is not None
assert (
"'spec' values must be one of the following types: date, datetime"
in errors[0].hint
"'spec' values must be one of the following types: date, datetime" in hint
)


Expand Down
2 changes: 1 addition & 1 deletion tests/testapp/test_enumfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def test_contains_lookup(self):

class TestCheck(TestCase):

databases = ["default", "other"]
databases = {"default", "other"}

def test_check(self):
errors = EnumModel.check()
Expand Down
Loading