From 5175d4724dd567d86fd212173d7a4d5c6746e9dd Mon Sep 17 00:00:00 2001 From: Ali Hassan Date: Wed, 28 Dec 2022 02:01:03 +0500 Subject: [PATCH] adds-unit-test-cases --- home/factories.py | 48 ++++++++++++++++++++++++++++---------------- home/models.py | 2 +- home/tests/tests.py | 29 +++++++++++++++++++++++++- requirements.dev.txt | 2 +- 4 files changed, 61 insertions(+), 20 deletions(-) diff --git a/home/factories.py b/home/factories.py index fa5e43460..e95929ae8 100644 --- a/home/factories.py +++ b/home/factories.py @@ -8,7 +8,10 @@ PageFactory, StreamFieldFactory, ) +from wagtail_factories.blocks import StructBlockFactory + from comments.models import CommentStatus +from home.blocks import ArticleBlock from home.models import ( Article, FooterIndexPage, @@ -22,8 +25,36 @@ ) +class ArticleFactory(PageFactory): + title = factory.Sequence(lambda n: f'article{n}') + lead_image = factory.SubFactory(ImageFactory) + commenting_status = CommentStatus.OPEN + body = StreamFieldFactory( + { + "image": ImageChooserBlockFactory, + } + ) + + class Meta: + model = Article + + +class ArticleBlockFactory(StructBlockFactory): + title = '' + display_section_title = True + article = factory.SubFactory(ArticleFactory) + + class Meta: + model = ArticleBlock + + class HomePageFactory(PageFactory): title = factory.Sequence(lambda n: f'homepage{n}') + home_featured_content = StreamFieldFactory( + { + "article": factory.SubFactory(ArticleBlockFactory), + } + ) class Meta: model = HomePage @@ -42,35 +73,18 @@ class SectionFactory(PageFactory): class Meta: model = Section - -class ArticleFactory(PageFactory): - title = factory.Sequence(lambda n: f'article{n}') - lead_image = factory.SubFactory(ImageFactory) - commenting_status = CommentStatus.OPEN - body = StreamFieldFactory( - { - "image": ImageChooserBlockFactory, - } - ) - - class Meta: - model = Article - - class MiscellaneousIndexPageFactory(PageFactory): title = factory.Sequence(lambda n: f'miscellaneous{n}') class Meta: model = MiscellaneousIndexPage - class OfflineContentIndexPageFactory(ArticleFactory): title = factory.Sequence(lambda n: f'offline-content-index{n}') class Meta: model = OfflineContentIndexPage - class SVGToPNGMapFactory(DjangoModelFactory): png_image_file = factory.LazyAttribute( lambda _: ContentFile( diff --git a/home/models.py b/home/models.py index 2b36a6fe0..08a0cc003 100644 --- a/home/models.py +++ b/home/models.py @@ -402,7 +402,7 @@ def is_complete(self, request): @property def top_level_section(self): - return self.get_ancestors().filter(depth=4).first().specific + return self.get_ancestors().filter(depth=4).first().specific if self.get_ancestors().filter(depth=4).first() else self @property def get_image_urls(self): diff --git a/home/tests/tests.py b/home/tests/tests.py index a0790a29a..5d64285ee 100644 --- a/home/tests/tests.py +++ b/home/tests/tests.py @@ -1,9 +1,10 @@ +from bs4 import BeautifulSoup from django.test import TestCase from django.http import HttpRequest from wagtail.core.models import Site from home.wagtail_hooks import limit_page_chooser -from home.factories import SectionFactory, ArticleFactory, HomePageFactory +from home.factories import SectionFactory, ArticleFactory, HomePageFactory, ArticleBlockFactory from wagtail_factories import SiteFactory @@ -55,3 +56,29 @@ def test_do_not_change_queryset_when_parent_page_is_not_a_section(self): pages_after = limit_page_chooser(pages_before, request) self.assertEqual(pages_after, pages_before) + + +class HomePageFeaturedItemTest(TestCase): + def setUp(self): + self.site = Site.objects.get(is_default_site=True) + self.home_page = HomePageFactory( + parent=self.site.root_page, + home_featured_content__0__article=ArticleBlockFactory(), + home_featured_content__1__article=ArticleBlockFactory(title='new title'), + ) + + def test_home_page_featured_item_with_empty_title(self): + response = self.client.get(self.home_page.url) + parsed_response = BeautifulSoup(response.content) + title = parsed_response.find("p", {"class": "article-title"}).text + + self.assertEqual(response.status_code, 200) + self.assertEqual(title, self.home_page.home_featured_content[0].value['article'].title) + + def test_home_page_featured_item_with_new_title(self): + response = self.client.get(self.home_page.url) + parsed_response = BeautifulSoup(response.content) + title = parsed_response.find("p", {"class": "article-title"}).findNext("p", {"class": "article-title"}).text + + self.assertEqual(response.status_code, 200) + self.assertEqual(title, self.home_page.home_featured_content[1].value['title']) diff --git a/requirements.dev.txt b/requirements.dev.txt index d643cefb0..22f6e5e70 100644 --- a/requirements.dev.txt +++ b/requirements.dev.txt @@ -7,4 +7,4 @@ django-test-plus>=1.4.0 coverage selenium==3.141.0 factory-boy==3.2.* -wagtail-factories==2.0.* \ No newline at end of file +wagtail-factories==4.0.* \ No newline at end of file