Skip to content

Commit

Permalink
Use django-fastdev for tests instead of iommi test specific code, fix…
Browse files Browse the repository at this point in the history
… issues found from that, and update tests for `root` object
  • Loading branch information
boxed committed Dec 5, 2024
1 parent 652af4d commit 666010f
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 34 deletions.
1 change: 0 additions & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
sphinx
furo
Django
django-fastdev
-r../test_requirements.txt
14 changes: 0 additions & 14 deletions iommi/base__tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,6 @@ def test_get_display_name():
assert get_display_name(mock) == 'Some other THING'


def test_crash_in_templates():
# We should crash in template rendering during tests if we try to render non-existent stuff
with pytest.raises(Exception) as e:
Template('{{ foo }}').render(context=RequestContext(req('get')))

assert (
str(e.value) == 'Tried to render non-existent variable foo'
or str(e.value) == "Undefined template variable 'foo' in '<unknown source>'"
)

# ...but inside if it's fine
assert Template('{% if foo %}foo{% endif %}').render(context=RequestContext(req('get'))) == ''


def function_based_view(request):
return HttpResponse('hello!') # pragma: no cover

Expand Down
9 changes: 5 additions & 4 deletions iommi/form__tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1120,10 +1120,11 @@ class MyForm(Form):

assert [x.pk for x in MyForm().bind(request=req('get')).fields.foo.choices] == [user.pk]
assert MyForm().bind(request=req('get', foo=smart_str(user2.pk))).fields.foo._errors == {
'User matching query does not exist.'
f"User matching query does not exist.\n\nQuery kwargs:\n\n pk: '{user2.pk}'",
}
assert MyForm().bind(request=req('get', foo=[smart_str(user2.pk), smart_str(user3.pk)])).fields.foo._errors == {
'User matching query does not exist.'
f"User matching query does not exist.\n\nQuery kwargs:\n\n pk: '{user2.pk}'",
f"User matching query does not exist.\n\nQuery kwargs:\n\n pk: '{user3.pk}'",
}

form = MyForm().bind(request=req('get', foo=[smart_str(user.pk)]))
Expand Down Expand Up @@ -1167,7 +1168,7 @@ class MyForm(Form):

assert [x.pk for x in MyForm().bind(request=req('get')).fields.foo.choices] == [user.pk]
assert MyForm().bind(request=req('get', foo=smart_str(user2.pk))).fields.foo._errors == {
'User matching query does not exist.'
f"User matching query does not exist.\n\nQuery kwargs:\n\n pk: '{user2.pk}'"
}

form = MyForm().bind(request=req('get', foo=[smart_str(user.pk)]))
Expand Down Expand Up @@ -3638,7 +3639,7 @@ class MyForm(Form):

assert [x.pk for x in MyForm().bind(request=req('get')).fields.foo.choices] == [user.pk]
assert MyForm().bind(request=req('get', foo=smart_str(user2.pk))).fields.foo._errors == {
'User matching query does not exist.'
f"User matching query does not exist.\n\nQuery kwargs:\n\n pk: '{user2.pk}'"
}

form = MyForm().bind(request=req('get', foo=[smart_str(user.pk)]))
Expand Down
2 changes: 1 addition & 1 deletion iommi/traversable.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def invoke_callback(self, callback, **kwargs):
):
raise TypeError(
f'TypeError when invoking callback {get_callable_description(callback)}.\n'
f'(Keyword arguments: {", ".join(sorted([*kwargs, *self.iommi_evaluate_parameters()]))})'
f'Keyword arguments:\n {"\n ".join(sorted([*kwargs, *self.iommi_evaluate_parameters()]))}'
) from e
raise

Expand Down
17 changes: 12 additions & 5 deletions iommi/traversable__tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import itertools
from textwrap import dedent
from typing import Dict
from unittest import mock

Expand Down Expand Up @@ -471,10 +472,16 @@ def test_invoke_callback_error_message_lambda():
with pytest.raises(TypeError) as e:
t.invoke_callback(lambda a: None, b=2)

assert str(e.value) == (
'TypeError when invoking callback lambda found at: `t.invoke_callback(lambda a: None, b=2)`.\n'
'(Keyword arguments: b, params, request, traversable, user)'
)
assert str(e.value) == dedent('''
TypeError when invoking callback lambda found at: `t.invoke_callback(lambda a: None, b=2)`.
Keyword arguments:
b
params
request
root
traversable
user
''').strip()


def test_invoke_callback_error_message_function():
Expand All @@ -489,7 +496,7 @@ def broken_callback(a):
assert actual.startswith(
'TypeError when invoking callback `<function test_invoke_callback_error_message_function.<locals>.broken_callback at 0x'
)
assert actual.endswith('`.\n(Keyword arguments: params, request, traversable, user)')
assert actual.endswith('`.\nKeyword arguments:\n params\n request\n root\n traversable\n user')


def test_invoke_callback_transparent_type_error():
Expand Down
1 change: 1 addition & 0 deletions test_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ pytest==8.1.1
pytest_snapshot==0.9.0
ruff==0.3.7
time-machine==2.14.1
django-fastdev
-rrequirements.txt
9 changes: 0 additions & 9 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,13 @@
TEMPLATE_DEBUG = True


class HighlightBrokenVariable:
def __contains__(self, item):
return True

def __mod__(self, other):
assert False, f'Tried to render non-existent variable {other}'


TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': TEMPLATE_DIRS,
'APP_DIRS': True,
'OPTIONS': {
'debug': TEMPLATE_DEBUG,
'string_if_invalid': HighlightBrokenVariable(),
'context_processors': [
'tests.context_processors.context_processor_is_called',
],
Expand Down

0 comments on commit 666010f

Please sign in to comment.