Skip to content

Commit

Permalink
adds-unit-test-cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Alihassanc5 committed Dec 27, 2022
1 parent 361d329 commit 5175d47
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 20 deletions.
48 changes: 31 additions & 17 deletions home/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion home/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
29 changes: 28 additions & 1 deletion home/tests/tests.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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'])
2 changes: 1 addition & 1 deletion requirements.dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ django-test-plus>=1.4.0
coverage
selenium==3.141.0
factory-boy==3.2.*
wagtail-factories==2.0.*
wagtail-factories==4.0.*

0 comments on commit 5175d47

Please sign in to comment.