Add basic tests for LMOTFY endpoints

This commit is contained in:
Jake Howard 2022-08-25 00:51:29 +01:00
parent fb44600ca2
commit a7aefb5ffc
Signed by: jake
GPG key ID: 57AFB45680EDD477
2 changed files with 53 additions and 0 deletions

View file

@ -1,6 +1,7 @@
from django.urls import reverse
from rest_framework.test import APISimpleTestCase, APITestCase
from website.blog.factories import BlogPostPageFactory
from website.common.factories import ContentPageFactory
from website.home.models import HomePage
@ -27,3 +28,50 @@ class PageLinksAPIViewTestCase(APITestCase):
with self.assertNumQueries(3):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
class LMOTFYAPIViewTestCase(APITestCase):
url = reverse("api:lmotfy")
@classmethod
def setUpTestData(cls) -> None:
cls.home_page = HomePage.objects.get()
for i in range(4):
BlogPostPageFactory(parent=cls.home_page, title=f"Post {i}")
cls.exact = BlogPostPageFactory(parent=cls.home_page, title="Post exact")
def test_accessible(self) -> None:
with self.assertNumQueries(4):
response = self.client.get(self.url, {"search": "Post"})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["count"], 5)
def test_case_insensitive_search(self) -> None:
with self.assertNumQueries(4):
response = self.client.get(self.url, {"search": "post"})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["count"], 5)
def test_no_search_term(self) -> None:
with self.assertNumQueries(0):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
def test_empty_search_term(self) -> None:
with self.assertNumQueries(0):
response = self.client.get(self.url, {"search": ""})
self.assertEqual(response.status_code, 200)
def test_exact(self) -> None:
with self.assertNumQueries(4):
response = self.client.get(self.url, {"search": "Post exact"})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["count"], 1)
result = response.data["results"][0]
self.assertEqual(result["title"], "Post exact")
self.assertEqual(result["full_url"], self.exact.full_url)
self.assertEqual(result["date"], self.exact.date.isoformat())

View file

@ -6,3 +6,8 @@ from . import models
class BlogPostListPageFactory(BaseContentFactory):
class Meta:
model = models.BlogPostListPage
class BlogPostPageFactory(BaseContentFactory):
class Meta:
model = models.BlogPostPage