jmcarp / nplusone

Auto-detecting the n+1 queries problem in Python
MIT License
996 stars 47 forks source link

NPlusOneError failing test when getting page response via test client during GitHub actions testing #48

Open formacube opened 3 months ago

formacube commented 3 months ago

Hi, I just had a strange faillure during testing which does not occur when I run testing on my computer, but get the test to fail when run on Github via Github actions: nplusone.core.exceptions.NPlusOneError: Potential unnecessary eager load detected

class MyTests(TestCase):
    def process_tests(self)
        staff_user = User.objects.create(is_staff=True, is_superuser=True)
        self.client.force_login(staff_user)
        ...
        response = self.client.get(link.url)

I can reproduce the error on my computer if I set-up 'NPLUSONE_RAISE = True' in settings

But it is the first time in 5 years that I have a failed test because of a NPlusOneError

The full Traceback (I'm running Django 3.2):

Traceback (most recent call last):
  File "C:\MyApp\goodies\testing.py", line 1411, in process_tests
    response = self.client.get(link.url)
  File "C:\MyApp\venv\lib\site-packages\django\test\client.py", 
line 742, in get
    response = super().get(path, data=data, secure=secure, **extra)
  File "C:\MyApp\venv\lib\site-packages\django\test\client.py", 
line 398, in get
    **extra,
  File "C:\MyApp\venv\lib\site-packages\django\test\client.py", 
line 473, in generic
    return self.request(**r)
  File "C:\MyApp\venv\lib\site-packages\django\test\client.py", 
line 719, in request
    self.check_exception(response)
  File "C:\MyApp\venv\lib\site-packages\django\test\client.py", 
line 580, in check_exception
    raise exc_value
  File "C:\MyApp\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\MyApp\venv\lib\site-packages\django\utils\deprecation.py", line 119, in __call__
    response = self.process_response(request, response)
  File "C:\MyApp\venv\lib\site-packages\nplusone\ext\django\middleware.py", line 57, in process_response
    listener.teardown()
  File "C:\MyApp\venv\lib\site-packages\nplusone\core\listeners.py", line 114, in teardown
    self.log_eager()
  File "C:\MyApp\venv\lib\site-packages\nplusone\core\listeners.py", line 129, in log_eager
    self.parent.notify(message)
  File "C:\MyApp\venv\lib\site-packages\nplusone\ext\django\middleware.py", line 63, in notify
    notifier.notify(message)
  File "C:\MyApp\venv\lib\site-packages\nplusone\core\notifiers.py", line 52, in notify
    raise self.error(message.message)
nplusone.core.exceptions.NPlusOneError: Potential unnecessary eager load detected on `Play.user`

I tried to force 'NPLUSONE_RAISE = False' in settings or to suppress notifications locally:

with nplusone_signals.ignore(nplusone_signals.lazy_load):
      # to prevent test to fail because there is a 'NPlusOneError'
      response = self.client.get(link.url)

but I'm unable to bypass the error raising in GitHub actions.

Does nplusone react the same for you ?