Skip to content

Commit fd3b6ad

Browse files
Split up warning hook function to be more customization-friendly
1 parent a727733 commit fd3b6ad

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

django_expanded_test_cases/test_cases/integration_test_case.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2222,6 +2222,31 @@ def _hook_function_warning_check(self, user, *args, **kwargs):
22222222
per-project, if needed.
22232223
"""
22242224

2225+
should_raise_warning = self._hook_function_warning_check_if_statement(user)
2226+
if should_raise_warning:
2227+
# No custom hooks have been implemented.
2228+
# Check if any args or kwargs have been provided.
2229+
if len(args) > 0 or len(kwargs) > 0:
2230+
# Either args or kwargs have been provided, which are exclusively meant for hook functions.
2231+
# But no hook functions have been implemented.
2232+
# Raise warning, as this is probably not an intended state.
2233+
warn_msg = (
2234+
"Supplemental args/kwargs have been provided to an assertResponse statement. "
2235+
"Any supplemental args/kwargs are exclusively used to provide custom data to "
2236+
"built-in hook functions, but no hook functions seem to be implemented for your project. "
2237+
"Either remove the use of args/kwargs in the assertion, or implement one of the hook functions."
2238+
)
2239+
# Create console warning message.
2240+
warnings.warn(warn_msg)
2241+
# Create logging warning message.
2242+
logging.warning(warn_msg)
2243+
2244+
def _hook_function_warning_check_if_statement(self, user):
2245+
"""If statement to determine if hook functions should raise warning.
2246+
2247+
Separate function for maximum customizability per project.
2248+
"""
2249+
22252250
# Django imports here to avoid situational "Apps aren't loaded yet" error.
22262251
from django.contrib.auth.models import AnonymousUser
22272252

@@ -2246,23 +2271,8 @@ def _hook_function_warning_check(self, user, *args, **kwargs):
22462271
):
22472272
should_raise_warning = True
22482273

2249-
if should_raise_warning:
2250-
# No custom hooks have been implemented.
2251-
# Check if any args or kwargs have been provided.
2252-
if len(args) > 0 or len(kwargs) > 0:
2253-
# Either args or kwargs have been provided, which are exclusively meant for hook functions.
2254-
# But no hook functions have been implemented.
2255-
# Raise warning, as this is probably not an intended state.
2256-
warn_msg = (
2257-
"Supplemental args/kwargs have been provided to an assertResponse statement. "
2258-
"Any supplemental args/kwargs are exclusively used to provide custom data to "
2259-
"built-in hook functions, but no hook functions seem to be implemented for your project. "
2260-
"Either remove the use of args/kwargs in the assertion, or implement one of the hook functions."
2261-
)
2262-
# Create console warning message.
2263-
warnings.warn(warn_msg)
2264-
# Create logging warning message.
2265-
logging.warning(warn_msg)
2274+
# Return calculated value.
2275+
return should_raise_warning
22662276

22672277
# endregion Hook Functions
22682278

docs/source/test_cases/integration_test_case/other_functionality.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,27 @@ If any additional args/kwargs are provided to a **Response Assertion**
404404
are passed on to all hooks, so that the end-user can provide any additional
405405
data their project needs to function.
406406

407+
408+
Hook Warnings and Warning Customization
409+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
410+
411+
If any supplemental args/kwargs are passed into any of the
412+
:doc:`Response Assertions<./response_assertions>` (beyond those the package
413+
is aware of), then they are assumed to be needed for one or more of the built-in
414+
hook functions.
415+
416+
However, the hook functions do nothing by default, and are meant to be
417+
overridden.
418+
Thus, if supplemental args/kwargs are provided and no hooks functions are
419+
overridden, the package will raise warnings indicating such, to alert the
420+
programmer that one or more provided values are probably not
421+
working as expected.
422+
423+
To customize how these warnings behave, override either
424+
``_hook_function_warning_check()`` (which displays the warning messages),
425+
or ``_hook_function_warning_check_if_statement()`` (which controls the
426+
if-statement that determines if the warnings should display).
427+
428+
See source code for these functions for more details.
429+
They're both towards the bottom of the file
430+
`django_expanded_test_cases/test_cases/integration_test_case.py <https://github.com/brodriguez8774/django-expanded-test-cases/blob/main/django_expanded_test_cases/test_cases/integration_test_case.py>`_.

0 commit comments

Comments
 (0)