mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-25 11:01:12 +00:00
Adds query logger to more tests
This commit is contained in:
parent
9984e853e5
commit
ca822e1b83
1 changed files with 59 additions and 26 deletions
|
@ -4,15 +4,18 @@ from unittest.mock import patch
|
||||||
from django.contrib.auth.models import AnonymousUser, Group, Permission
|
from django.contrib.auth.models import AnonymousUser, Group, Permission
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.core.exceptions import PermissionDenied
|
from django.core.exceptions import PermissionDenied
|
||||||
|
from django.db import connection
|
||||||
from django.template.response import TemplateResponse
|
from django.template.response import TemplateResponse
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.test.client import RequestFactory
|
from django.test.client import RequestFactory
|
||||||
|
|
||||||
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.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 AuthorViews(TestCase):
|
class AuthorViews(TestCase):
|
||||||
"""author views"""
|
"""author views"""
|
||||||
|
|
||||||
|
@ -61,9 +64,12 @@ class AuthorViews(TestCase):
|
||||||
self.book.authors.add(author)
|
self.book.authors.add(author)
|
||||||
request = self.factory.get("")
|
request = self.factory.get("")
|
||||||
request.user = self.local_user
|
request.user = self.local_user
|
||||||
with patch("bookwyrm.views.author.is_api_request") as is_api:
|
query_logger = QueryLogger()
|
||||||
is_api.return_value = False
|
with connection.execute_wrapper(query_logger):
|
||||||
result = view(request, author.id)
|
with patch("bookwyrm.views.author.is_api_request") as is_api:
|
||||||
|
is_api.return_value = False
|
||||||
|
result = view(request, author.id)
|
||||||
|
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)
|
||||||
|
@ -81,9 +87,12 @@ class AuthorViews(TestCase):
|
||||||
self.book.authors.add(author)
|
self.book.authors.add(author)
|
||||||
request = self.factory.get("")
|
request = self.factory.get("")
|
||||||
request.user = self.local_user
|
request.user = self.local_user
|
||||||
with patch("bookwyrm.views.author.is_api_request") as is_api:
|
query_logger = QueryLogger()
|
||||||
is_api.return_value = False
|
with connection.execute_wrapper(query_logger):
|
||||||
result = view(request, author.id)
|
with patch("bookwyrm.views.author.is_api_request") as is_api:
|
||||||
|
is_api.return_value = False
|
||||||
|
result = view(request, author.id)
|
||||||
|
raise_long_query_runtime(query_logger.queries)
|
||||||
books = result.context_data["books"]
|
books = result.context_data["books"]
|
||||||
self.assertEqual(books.object_list.count(), 1)
|
self.assertEqual(books.object_list.count(), 1)
|
||||||
|
|
||||||
|
@ -97,9 +106,12 @@ class AuthorViews(TestCase):
|
||||||
author = models.Author.objects.create(name="Jessica")
|
author = models.Author.objects.create(name="Jessica")
|
||||||
request = self.factory.get("")
|
request = self.factory.get("")
|
||||||
request.user = self.local_user
|
request.user = self.local_user
|
||||||
with patch("bookwyrm.views.author.is_api_request") as is_api:
|
query_logger = QueryLogger()
|
||||||
is_api.return_value = False
|
with connection.execute_wrapper(query_logger):
|
||||||
result = view(request, author.id)
|
with patch("bookwyrm.views.author.is_api_request") as is_api:
|
||||||
|
is_api.return_value = False
|
||||||
|
result = view(request, author.id)
|
||||||
|
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)
|
||||||
|
@ -110,9 +122,12 @@ class AuthorViews(TestCase):
|
||||||
author = models.Author.objects.create(name="Jessica")
|
author = models.Author.objects.create(name="Jessica")
|
||||||
request = self.factory.get("")
|
request = self.factory.get("")
|
||||||
request.user = self.anonymous_user
|
request.user = self.anonymous_user
|
||||||
with patch("bookwyrm.views.author.is_api_request") as is_api:
|
query_logger = QueryLogger()
|
||||||
is_api.return_value = False
|
with connection.execute_wrapper(query_logger):
|
||||||
result = view(request, author.id)
|
with patch("bookwyrm.views.author.is_api_request") as is_api:
|
||||||
|
is_api.return_value = False
|
||||||
|
result = view(request, author.id)
|
||||||
|
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)
|
||||||
|
@ -123,9 +138,12 @@ class AuthorViews(TestCase):
|
||||||
author = models.Author.objects.create(name="Jessica")
|
author = models.Author.objects.create(name="Jessica")
|
||||||
request = self.factory.get("")
|
request = self.factory.get("")
|
||||||
request.user = self.local_user
|
request.user = self.local_user
|
||||||
with patch("bookwyrm.views.author.is_api_request") as is_api:
|
query_logger = QueryLogger()
|
||||||
is_api.return_value = True
|
with connection.execute_wrapper(query_logger):
|
||||||
result = view(request, author.id)
|
with patch("bookwyrm.views.author.is_api_request") as is_api:
|
||||||
|
is_api.return_value = True
|
||||||
|
result = view(request, author.id)
|
||||||
|
raise_long_query_runtime(query_logger.queries)
|
||||||
self.assertIsInstance(result, ActivitypubResponse)
|
self.assertIsInstance(result, ActivitypubResponse)
|
||||||
self.assertEqual(result.status_code, 200)
|
self.assertEqual(result.status_code, 200)
|
||||||
|
|
||||||
|
@ -137,7 +155,10 @@ class AuthorViews(TestCase):
|
||||||
request.user = self.local_user
|
request.user = self.local_user
|
||||||
request.user.is_superuser = True
|
request.user.is_superuser = True
|
||||||
|
|
||||||
result = view(request, author.id)
|
query_logger = QueryLogger()
|
||||||
|
with connection.execute_wrapper(query_logger):
|
||||||
|
result = view(request, author.id)
|
||||||
|
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)
|
||||||
|
@ -153,8 +174,11 @@ class AuthorViews(TestCase):
|
||||||
request = self.factory.post("", form.data)
|
request = self.factory.post("", form.data)
|
||||||
request.user = self.local_user
|
request.user = self.local_user
|
||||||
|
|
||||||
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
|
query_logger = QueryLogger()
|
||||||
view(request, author.id)
|
with connection.execute_wrapper(query_logger):
|
||||||
|
with patch("bookwyrm.models.activitypub_mixin.broadcast_task.apply_async"):
|
||||||
|
view(request, author.id)
|
||||||
|
raise_long_query_runtime(query_logger.queries)
|
||||||
author.refresh_from_db()
|
author.refresh_from_db()
|
||||||
self.assertEqual(author.name, "New Name")
|
self.assertEqual(author.name, "New Name")
|
||||||
self.assertEqual(author.last_edited_by, self.local_user)
|
self.assertEqual(author.last_edited_by, self.local_user)
|
||||||
|
@ -169,8 +193,11 @@ class AuthorViews(TestCase):
|
||||||
request = self.factory.post("", form.data)
|
request = self.factory.post("", form.data)
|
||||||
request.user = self.local_user
|
request.user = self.local_user
|
||||||
|
|
||||||
with self.assertRaises(PermissionDenied):
|
query_logger = QueryLogger()
|
||||||
view(request, author.id)
|
with connection.execute_wrapper(query_logger):
|
||||||
|
with self.assertRaises(PermissionDenied):
|
||||||
|
view(request, author.id)
|
||||||
|
raise_long_query_runtime(query_logger.queries)
|
||||||
author.refresh_from_db()
|
author.refresh_from_db()
|
||||||
self.assertEqual(author.name, "Test Author")
|
self.assertEqual(author.name, "Test Author")
|
||||||
|
|
||||||
|
@ -185,7 +212,10 @@ class AuthorViews(TestCase):
|
||||||
request = self.factory.post("", form.data)
|
request = self.factory.post("", form.data)
|
||||||
request.user = self.local_user
|
request.user = self.local_user
|
||||||
|
|
||||||
resp = view(request, author.id)
|
query_logger = QueryLogger()
|
||||||
|
with connection.execute_wrapper(query_logger):
|
||||||
|
resp = view(request, author.id)
|
||||||
|
raise_long_query_runtime(query_logger.queries)
|
||||||
author.refresh_from_db()
|
author.refresh_from_db()
|
||||||
self.assertEqual(author.name, "Test Author")
|
self.assertEqual(author.name, "Test Author")
|
||||||
validate_html(resp.render())
|
validate_html(resp.render())
|
||||||
|
@ -208,8 +238,11 @@ class AuthorViews(TestCase):
|
||||||
request = self.factory.post("")
|
request = self.factory.post("")
|
||||||
request.user = self.local_user
|
request.user = self.local_user
|
||||||
|
|
||||||
with patch(
|
query_logger = QueryLogger()
|
||||||
"bookwyrm.connectors.openlibrary.Connector.update_author_from_remote"
|
with connection.execute_wrapper(query_logger):
|
||||||
) as mock:
|
with patch(
|
||||||
views.update_author_from_remote(request, author.id, "openlibrary.org")
|
"bookwyrm.connectors.openlibrary.Connector.update_author_from_remote"
|
||||||
self.assertEqual(mock.call_count, 1)
|
) as mock:
|
||||||
|
views.update_author_from_remote(request, author.id, "openlibrary.org")
|
||||||
|
self.assertEqual(mock.call_count, 1)
|
||||||
|
raise_long_query_runtime(query_logger.queries)
|
||||||
|
|
Loading…
Reference in a new issue