Skip to content

Commit fc654aa

Browse files
authored
Reduce number of warning messages in test logs (#1610)
1 parent 5f7d8de commit fc654aa

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

home/tests/tests.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1+
from bs4 import BeautifulSoup
12
from django.test import TestCase, override_settings
23
from django.http import HttpRequest
34
from translation_manager.models import TranslationEntry
45
from wagtail.core.models import Site
6+
from wagtail_factories import SiteFactory, PageFactory
57
from wagtail_localize.operations import TranslationCreator
68

79
from home.wagtail_hooks import limit_page_chooser
8-
from home.factories import SectionFactory, ArticleFactory, HomePageFactory, MediaFactory, LocaleFactory, SiteSettingsFactory
9-
from wagtail_factories import SiteFactory, PageFactory
10-
from bs4 import BeautifulSoup
10+
from home.factories import (
11+
SectionFactory,
12+
ArticleFactory,
13+
HomePageFactory,
14+
MediaFactory,
15+
LocaleFactory,
16+
SiteSettingsFactory
17+
)
1118

1219

1320
class LimitPageChooserHookTests(TestCase):
@@ -109,7 +116,7 @@ def setUp(self):
109116

110117
def test_default_image_within_preset(self):
111118
response = self.client.get(self.article.url)
112-
parsed_response = BeautifulSoup(response.content)
119+
parsed_response = parse_html(response.content)
113120
rendered_image = parsed_response.find("img", {"class": "article__lead-img-featured"})
114121
image_rendition = self.article.lead_image.get_rendition('width-360')
115122

@@ -122,7 +129,7 @@ def test_default_image_within_preset(self):
122129

123130
def test_half_default_image_within_preset(self):
124131
response = self.client.get(self.section.url)
125-
parsed_response = BeautifulSoup(response.content)
132+
parsed_response = parse_html(response.content)
126133
rendered_image = parsed_response.find("div", {"class": "article-card"}).find("img")
127134
image_rendition = self.article.lead_image.get_rendition('width-180')
128135

@@ -135,7 +142,7 @@ def test_half_default_image_within_preset(self):
135142
@override_settings(IMAGE_SIZE_PRESET=750)
136143
def test_custom_image_within_preset(self):
137144
response = self.client.get(self.article.url)
138-
parsed_response = BeautifulSoup(response.content)
145+
parsed_response = parse_html(response.content)
139146
rendered_image = parsed_response.find("img", {"class": "article__lead-img-featured"})
140147
image_rendition = self.article.lead_image.get_rendition('width-750')
141148

@@ -148,7 +155,7 @@ def test_custom_image_within_preset(self):
148155
@override_settings(IMAGE_SIZE_PRESET=750)
149156
def test_half_custom_image_within_preset(self):
150157
response = self.client.get(self.section.url)
151-
parsed_response = BeautifulSoup(response.content)
158+
parsed_response = parse_html(response.content)
152159
rendered_image = parsed_response.find("div", {"class": "article-card"}).find("img")
153160
image_rendition = self.article.lead_image.get_rendition('width-375')
154161

@@ -157,3 +164,7 @@ def test_half_custom_image_within_preset(self):
157164
self.assertEqual(int(rendered_image.get('width')), image_rendition.width)
158165
self.assertEqual(int(rendered_image.get('height')), image_rendition.height)
159166
self.assertEqual(rendered_image.get('src'), image_rendition.url)
167+
168+
169+
def parse_html(html: str) -> BeautifulSoup:
170+
return BeautifulSoup(html, 'lxml')

questionnaires/api/v1/tests.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from datetime import timedelta
55

66
import pytz
7-
from bs4 import BeautifulSoup
87
from django.contrib.auth.models import Permission
98
from django.test import TestCase
109
from django.urls import reverse
@@ -14,7 +13,9 @@
1413
from rest_framework.test import APIClient
1514
from wagtail.core.models import Site
1615
from wagtail_factories import SiteFactory, PageFactory
16+
1717
from home.factories import HomePageFactory
18+
from home.tests.tests import parse_html
1819
from iogt_users.factories import (
1920
UserFactory,
2021
GroupFactory,
@@ -1053,7 +1054,7 @@ def test_drop_down_defaults_to_the_blank_option(self):
10531054
PollFormFieldFactory(page=self.poll, field_type='dropdown', choices='c1|c2')
10541055

10551056
response = self.client.get(self.poll.url)
1056-
parsed_response = BeautifulSoup(response.content)
1057+
parsed_response = parse_html(response.content)
10571058
default_drop_down_option = parsed_response.select_one('.quest-item select option').text
10581059

10591060
self.assertEqual(response.status_code, status.HTTP_200_OK)
@@ -1063,7 +1064,7 @@ def test_drop_down_blank_option_submission_for_required_field(self):
10631064
poll_question = PollFormFieldFactory(page=self.poll, field_type='dropdown', choices='c1|c2')
10641065

10651066
response = self.client.post(self.poll.url, {poll_question.clean_name: ''})
1066-
parsed_response = BeautifulSoup(response.content)
1067+
parsed_response = parse_html(response.content)
10671068
field_required_text = parsed_response.select_one('.quest-item .cust-input__error').text
10681069

10691070
self.assertEqual(response.status_code, status.HTTP_200_OK)
@@ -1073,7 +1074,7 @@ def test_drop_down_blank_option_submission_for_non_required_field(self):
10731074
poll_question = PollFormFieldFactory(page=self.poll, field_type='dropdown', choices='c1|c2', required=False)
10741075

10751076
response = self.client.post(self.poll.url, {poll_question.clean_name: ''})
1076-
parsed_response = BeautifulSoup(response.content)
1077+
parsed_response = parse_html(response.content)
10771078
results = parsed_response.select('.cust-check__title')
10781079

10791080
self.assertEqual(response.status_code, status.HTTP_200_OK)
@@ -1087,7 +1088,7 @@ def test_drop_down_non_blank_option_submission(self):
10871088
poll_question = PollFormFieldFactory(page=self.poll, field_type='dropdown', choices='c1|c2')
10881089

10891090
response = self.client.post(self.poll.url, {poll_question.clean_name: 'c1'})
1090-
parsed_response = BeautifulSoup(response.content)
1091+
parsed_response = parse_html(response.content)
10911092
results = parsed_response.select('.cust-check__title')
10921093

10931094
self.assertEqual(response.status_code, status.HTTP_200_OK)

0 commit comments

Comments
 (0)