Uses query duration tester in more places

This commit is contained in:
Mouse Reeve 2024-06-08 19:21:39 -07:00
parent fdc6ae24bc
commit b84ca3d03f
3 changed files with 37 additions and 20 deletions

View file

@ -17,7 +17,7 @@ class QueryLogger:
start = time.monotonic() start = time.monotonic()
try: try:
result = execute(sql, params, many, context) result = execute(sql, params, many, context)
except Exception as err: # pylint: disable=broad-except except Exception as err: # pylint: disable=broad-except
current_query["status"] = "error" current_query["status"] = "error"
current_query["exception"] = err current_query["exception"] = err
raise raise
@ -34,6 +34,6 @@ def raise_long_query_runtime(queries, threshold=0.0006):
"""Raises an exception if any query took longer than the threshold""" """Raises an exception if any query took longer than the threshold"""
for query in queries: for query in queries:
if query["duration"] > threshold: if query["duration"] > threshold:
raise Exception( # pylint: disable=broad-exception-raised raise Exception( # pylint: disable=broad-exception-raised
"This looks like a slow query:", query["duration"], query["sql"] "This looks like a slow query:", query["duration"], query["sql"]
) )

View file

@ -16,8 +16,8 @@ from django.utils import timezone
from bookwyrm import forms, models, views from bookwyrm import forms, models, views
from bookwyrm.activitypub import ActivitypubResponse from bookwyrm.activitypub import ActivitypubResponse
from bookwyrm.tests.validate_html import validate_html
from bookwyrm.tests.query_logger import QueryLogger, raise_long_query_runtime from bookwyrm.tests.query_logger import QueryLogger, raise_long_query_runtime
from bookwyrm.tests.validate_html import validate_html
# pylint: disable=invalid-name # pylint: disable=invalid-name
class BookViews(TestCase): class BookViews(TestCase):
@ -117,26 +117,34 @@ class BookViews(TestCase):
request = self.factory.get("") request = self.factory.get("")
request.user = self.local_user request.user = self.local_user
with patch("bookwyrm.views.books.books.is_api_request") as is_api: query_logger = QueryLogger()
is_api.return_value = False with connection.execute_wrapper(query_logger):
result = view(request, self.book.id, user_statuses="review") with patch("bookwyrm.views.books.books.is_api_request") as is_api:
is_api.return_value = False
result = view(request, self.book.id, user_statuses="review")
raise_long_query_runtime(query_logger.queries)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
validate_html(result.render()) validate_html(result.render())
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
self.assertEqual(result.context_data["statuses"].object_list[0], review) self.assertEqual(result.context_data["statuses"].object_list[0], review)
with patch("bookwyrm.views.books.books.is_api_request") as is_api: with connection.execute_wrapper(query_logger):
is_api.return_value = False with patch("bookwyrm.views.books.books.is_api_request") as is_api:
result = view(request, self.book.id, user_statuses="comment") is_api.return_value = False
result = view(request, self.book.id, user_statuses="comment")
raise_long_query_runtime(query_logger.queries)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
validate_html(result.render()) validate_html(result.render())
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
self.assertEqual(result.context_data["statuses"].object_list[0], comment) self.assertEqual(result.context_data["statuses"].object_list[0], comment)
with patch("bookwyrm.views.books.books.is_api_request") as is_api: with connection.execute_wrapper(query_logger):
is_api.return_value = False with patch("bookwyrm.views.books.books.is_api_request") as is_api:
result = view(request, self.book.id, user_statuses="quotation") is_api.return_value = False
result = view(request, self.book.id, user_statuses="quotation")
raise_long_query_runtime(query_logger.queries)
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
validate_html(result.render()) validate_html(result.render())
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)

View file

@ -1,6 +1,7 @@
""" test for app action functionality """ """ test for app action functionality """
from unittest.mock import patch from unittest.mock import patch
from django.contrib.auth.models import AnonymousUser from django.contrib.auth.models import AnonymousUser
from django.db import connection
from django.http import Http404 from django.http import Http404
from django.template.response import TemplateResponse from django.template.response import TemplateResponse
from django.test import TestCase from django.test import TestCase
@ -8,9 +9,11 @@ from django.test.client import RequestFactory
from bookwyrm import models from bookwyrm import models
from bookwyrm import views from bookwyrm import views
from bookwyrm.tests.query_logger import QueryLogger, raise_long_query_runtime
from bookwyrm.tests.validate_html import validate_html from bookwyrm.tests.validate_html import validate_html
# pylint: disable=invalid-name
class LandingViews(TestCase): class LandingViews(TestCase):
"""pages you land on without really trying""" """pages you land on without really trying"""
@ -39,7 +42,7 @@ class LandingViews(TestCase):
@patch("bookwyrm.suggested_users.SuggestedUsers.get_suggestions") @patch("bookwyrm.suggested_users.SuggestedUsers.get_suggestions")
def test_home_page(self, _): def test_home_page(self, _):
"""there are so many views, this just makes sure it LOADS""" """home page for a logged in user"""
view = views.Home.as_view() view = views.Home.as_view()
request = self.factory.get("") request = self.factory.get("")
request.user = self.local_user request.user = self.local_user
@ -54,6 +57,19 @@ class LandingViews(TestCase):
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
validate_html(result.render()) validate_html(result.render())
def test_landing(self):
"""tests landing page for an anonymous user"""
view = views.Landing.as_view()
request = self.factory.get("")
query_logger = QueryLogger()
with connection.execute_wrapper(query_logger):
result = view(request)
raise_long_query_runtime(query_logger.queries)
validate_html(result.render())
self.assertIsInstance(result, TemplateResponse)
def test_about_page(self): def test_about_page(self):
"""there are so many views, this just makes sure it LOADS""" """there are so many views, this just makes sure it LOADS"""
view = views.about view = views.about
@ -105,10 +121,3 @@ class LandingViews(TestCase):
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
validate_html(result.render()) validate_html(result.render())
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
def test_landing(self):
"""there are so many views, this just makes sure it LOADS"""
view = views.Landing.as_view()
request = self.factory.get("")
result = view(request)
self.assertIsInstance(result, TemplateResponse)