From b84ca3d03f1e9f6a95cae1446ee05a9c5a282aa2 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 8 Jun 2024 19:21:39 -0700 Subject: [PATCH] Uses query duration tester in more places --- bookwyrm/tests/query_logger.py | 4 +-- bookwyrm/tests/views/books/test_book.py | 28 +++++++++++++------- bookwyrm/tests/views/landing/test_landing.py | 25 +++++++++++------ 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/bookwyrm/tests/query_logger.py b/bookwyrm/tests/query_logger.py index 6474ff41b..e8ee7bdd6 100644 --- a/bookwyrm/tests/query_logger.py +++ b/bookwyrm/tests/query_logger.py @@ -17,7 +17,7 @@ class QueryLogger: start = time.monotonic() try: 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["exception"] = err 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""" for query in queries: 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"] ) diff --git a/bookwyrm/tests/views/books/test_book.py b/bookwyrm/tests/views/books/test_book.py index fa0a328a3..c89120839 100644 --- a/bookwyrm/tests/views/books/test_book.py +++ b/bookwyrm/tests/views/books/test_book.py @@ -16,8 +16,8 @@ from django.utils import timezone from bookwyrm import forms, models, views 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.validate_html import validate_html # pylint: disable=invalid-name class BookViews(TestCase): @@ -117,26 +117,34 @@ class BookViews(TestCase): request = self.factory.get("") request.user = self.local_user - 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") + query_logger = QueryLogger() + with connection.execute_wrapper(query_logger): + 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) validate_html(result.render()) self.assertEqual(result.status_code, 200) self.assertEqual(result.context_data["statuses"].object_list[0], 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="comment") + with connection.execute_wrapper(query_logger): + 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="comment") + raise_long_query_runtime(query_logger.queries) self.assertIsInstance(result, TemplateResponse) validate_html(result.render()) self.assertEqual(result.status_code, 200) self.assertEqual(result.context_data["statuses"].object_list[0], comment) - 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="quotation") + with connection.execute_wrapper(query_logger): + 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="quotation") + raise_long_query_runtime(query_logger.queries) self.assertIsInstance(result, TemplateResponse) validate_html(result.render()) self.assertEqual(result.status_code, 200) diff --git a/bookwyrm/tests/views/landing/test_landing.py b/bookwyrm/tests/views/landing/test_landing.py index c68c9cd53..240741cac 100644 --- a/bookwyrm/tests/views/landing/test_landing.py +++ b/bookwyrm/tests/views/landing/test_landing.py @@ -1,6 +1,7 @@ """ test for app action functionality """ from unittest.mock import patch from django.contrib.auth.models import AnonymousUser +from django.db import connection from django.http import Http404 from django.template.response import TemplateResponse from django.test import TestCase @@ -8,9 +9,11 @@ from django.test.client import RequestFactory from bookwyrm import models from bookwyrm import views +from bookwyrm.tests.query_logger import QueryLogger, raise_long_query_runtime from bookwyrm.tests.validate_html import validate_html +# pylint: disable=invalid-name class LandingViews(TestCase): """pages you land on without really trying""" @@ -39,7 +42,7 @@ class LandingViews(TestCase): @patch("bookwyrm.suggested_users.SuggestedUsers.get_suggestions") 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() request = self.factory.get("") request.user = self.local_user @@ -54,6 +57,19 @@ class LandingViews(TestCase): self.assertEqual(result.status_code, 200) 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): """there are so many views, this just makes sure it LOADS""" view = views.about @@ -105,10 +121,3 @@ class LandingViews(TestCase): self.assertIsInstance(result, TemplateResponse) validate_html(result.render()) 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)