Add tests for post similarity
This commit is contained in:
parent
b7bcaeff63
commit
e8cd4f6628
2 changed files with 41 additions and 2 deletions
|
@ -97,8 +97,8 @@ class BlogPostPage(BaseContentPage):
|
||||||
|
|
||||||
similar_posts = similar_posts.annotate(
|
similar_posts = similar_posts.annotate(
|
||||||
similarity=(models.F("tag_similarity") * 2)
|
similarity=(models.F("tag_similarity") * 2)
|
||||||
* (models.F("title_similarity") * 10)
|
+ (models.F("title_similarity") * 10)
|
||||||
* (models.F("subtitle_similarity"))
|
+ (models.F("subtitle_similarity"))
|
||||||
).order_by("-similarity")[:3]
|
).order_by("-similarity")[:3]
|
||||||
|
|
||||||
return similar_posts
|
return similar_posts
|
||||||
|
|
|
@ -21,6 +21,45 @@ class BlogPostPageTestCase(TestCase):
|
||||||
self.client.get(self.page.url)
|
self.client.get(self.page.url)
|
||||||
|
|
||||||
|
|
||||||
|
class BlogPostPageSimilarityTestCase(TestCase):
|
||||||
|
@classmethod
|
||||||
|
def setUpTestData(cls) -> None:
|
||||||
|
cls.home_page = HomePage.objects.get()
|
||||||
|
cls.blog_post_list_page = BlogPostListPageFactory(parent=cls.home_page)
|
||||||
|
cls.page = BlogPostPageFactory(
|
||||||
|
parent=cls.blog_post_list_page, title="Blog post 1"
|
||||||
|
)
|
||||||
|
|
||||||
|
cls.similar_page_1 = BlogPostPageFactory(
|
||||||
|
parent=cls.blog_post_list_page, title="Blog post 2"
|
||||||
|
)
|
||||||
|
cls.similar_page_2 = BlogPostPageFactory(
|
||||||
|
parent=cls.blog_post_list_page, title="Blog post 3"
|
||||||
|
)
|
||||||
|
cls.similar_page_3 = BlogPostPageFactory(
|
||||||
|
parent=cls.blog_post_list_page, title="Blog post 4"
|
||||||
|
)
|
||||||
|
|
||||||
|
BlogPostPageFactory(parent=cls.blog_post_list_page, title="Legal documents")
|
||||||
|
|
||||||
|
def test_similar_pages(self) -> None:
|
||||||
|
self.assertEqual(
|
||||||
|
set(self.page.get_similar_posts()),
|
||||||
|
{self.similar_page_1, self.similar_page_2, self.similar_page_3},
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_page_similarity(self) -> None:
|
||||||
|
for page in self.page.get_similar_posts():
|
||||||
|
self.assertNotEqual(page.similarity, 0.0)
|
||||||
|
|
||||||
|
def test_ordered_by_similarity(self) -> None:
|
||||||
|
similar_pages = self.page.get_similar_posts()
|
||||||
|
self.assertEqual(
|
||||||
|
list(similar_pages),
|
||||||
|
sorted(similar_pages, key=lambda p: p.similarity, reverse=True),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class BlogPostListPageTestCase(TestCase):
|
class BlogPostListPageTestCase(TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpTestData(cls) -> None:
|
def setUpTestData(cls) -> None:
|
||||||
|
|
Loading…
Reference in a new issue