diff --git a/tests/conftest.py b/tests/conftest.py index afdfb92..371e0e9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,7 +3,7 @@ from django.core.management import call_command -@pytest.fixture(scope='session') +@pytest.fixture(scope="session") def django_db_setup(django_db_setup, django_db_blocker): with django_db_blocker.unblock(): - call_command('loaddata', 'testapp/testdata.json') + call_command("loaddata", "testapp/testdata.json") diff --git a/tests/test_action_block.py b/tests/test_action_block.py index afb3ade..377bc03 100644 --- a/tests/test_action_block.py +++ b/tests/test_action_block.py @@ -7,57 +7,76 @@ @pytest.mark.django_db def test_action_block_external_url(db, django_db_setup, client: Client): - response = client.get('/') - soup = BeautifulSoup(response.content, 'html.parser') - a_tag = soup.select_one('.block-action_link:nth-of-type(1) a') - span_tag = a_tag.select_one('span.nhsuk-action-link__text') + response = client.get("/") + soup = BeautifulSoup(response.content, "html.parser") + a_tag = soup.select_one(".block-action_link:nth-of-type(1) a") + span_tag = a_tag.select_one("span.nhsuk-action-link__text") - assert a_tag['href'] == 'https://example.com' - assert span_tag.text == 'Action Link External URL' + assert a_tag["href"] == "https://example.com" + assert span_tag.text == "Action Link External URL" @pytest.mark.django_db def test_action_block_internal_page(db, django_db_setup, client: Client): - response = client.get('/') - soup = BeautifulSoup(response.content, 'html.parser') - a_tag = soup.select_one('.block-action_link:nth-of-type(2) a') - span_tag = a_tag.select_one('span.nhsuk-action-link__text') + response = client.get("/") + soup = BeautifulSoup(response.content, "html.parser") + a_tag = soup.select_one(".block-action_link:nth-of-type(2) a") + span_tag = a_tag.select_one("span.nhsuk-action-link__text") - assert a_tag['href'] == '/page-1/' - assert span_tag.text == 'Action Link Internal Page' + assert a_tag["href"] == "/page-1/" + assert span_tag.text == "Action Link Internal Page" def test_action_block_clean_one_link(): block = ActionLinkBlock() - value = {'text': 'testing', 'external_url': 'https://example.com/', 'new_window': False} + value = { + "text": "testing", + "external_url": "https://example.com/", + "new_window": False, + } clean_value = block.clean(value) assert value == clean_value - + def test_action_block_clean_no_links(): block = ActionLinkBlock() - value = {'text': 'testing', 'external_url': None, 'internal_page': None, 'new_window': False} - internal_error_message = '' - external_error_message = '' - + value = { + "text": "testing", + "external_url": None, + "internal_page": None, + "new_window": False, + } + internal_error_message = ( + '' + ) + external_error_message = ( + '' + ) + with pytest.raises(ValidationError) as excinfo: - clean_value = block.clean(value) + block.clean(value) - assert str(excinfo.value.params['internal_page']) == internal_error_message - assert str(excinfo.value.params['external_url']) == external_error_message + assert str(excinfo.value.params["internal_page"]) == internal_error_message + assert str(excinfo.value.params["external_url"]) == external_error_message assert "['Validation error in ActionLinkBlock']" == str(excinfo.value) def test_action_block_clean_two_links(): block = ActionLinkBlock() - value = {'text': 'testing', 'external_url': 'https://example.com/', - 'internal_page': 'https://internal.com/', 'new_window': False} - error_message = '' + value = { + "text": "testing", + "external_url": "https://example.com/", + "internal_page": "https://internal.com/", + "new_window": False, + } + error_message = ( + '' + ) with pytest.raises(ValidationError) as excinfo: - clean_value = block.clean(value) - - assert str(excinfo.value.params['internal_page']) == error_message - assert str(excinfo.value.params['external_url']) == error_message - assert "['Validation error in ActionLinkBlock']" == str(excinfo.value) \ No newline at end of file + block.clean(value) + + assert str(excinfo.value.params["internal_page"]) == error_message + assert str(excinfo.value.params["external_url"]) == error_message + assert "['Validation error in ActionLinkBlock']" == str(excinfo.value) diff --git a/tests/test_breadcrumb_tag.py b/tests/test_breadcrumb_tag.py index 6ea9c4a..0eb392f 100644 --- a/tests/test_breadcrumb_tag.py +++ b/tests/test_breadcrumb_tag.py @@ -7,28 +7,28 @@ def get_breadcrumb_context(page): """Get the breadcrumb context that will be passed to the breadcrumb template.""" fake_context = { - 'page': page, + "page": page, } # The breadcrumb tag is an inclusion_tag which returns a new context. new_context = breadcrumb(fake_context) - return new_context['breadcrumb_pages'] + return new_context["breadcrumb_pages"] def get_level_2_breadcrumb(): """Get the breadcrumb context for a 2-levels-deep page.""" - page = Page.objects.get(url_path='/home/page-1/page-2/') + page = Page.objects.get(url_path="/home/page-1/page-2/") return get_breadcrumb_context(page) def get_level_1_breadcrumb(): """Get the breadcrumb context for a 1-level deep page.""" - page = Page.objects.get(url_path='/home/page-1/') + page = Page.objects.get(url_path="/home/page-1/") return get_breadcrumb_context(page) def get_homepage_breadcrumb(): """Get the breadcrumb context for a root page.""" - page = Page.objects.get(url_path='/home/') + page = Page.objects.get(url_path="/home/") return get_breadcrumb_context(page) @@ -43,8 +43,8 @@ def test_level_2_breadcrumb_length(db, django_db_setup): def test_level_2_breadcrumb_pages(db, django_db_setup): breadcrumb_pages = get_level_2_breadcrumb() - homepage = Page.objects.get(url_path='/home/') - page1 = Page.objects.get(url_path='/home/page-1/') + homepage = Page.objects.get(url_path="/home/") + page1 = Page.objects.get(url_path="/home/page-1/") assert breadcrumb_pages[0] == homepage assert breadcrumb_pages[1] == page1 @@ -61,6 +61,6 @@ def test_level_1_breadcrumb_length(db, django_db_setup): def test_level_1_breadcrumb_pages(db, django_db_setup): breadcrumb_pages = get_level_1_breadcrumb() - homepage = Page.objects.get(url_path='/home/') + homepage = Page.objects.get(url_path="/home/") assert breadcrumb_pages[0] == homepage diff --git a/tests/test_card_block.py b/tests/test_card_block.py index 070567f..48fbba0 100644 --- a/tests/test_card_block.py +++ b/tests/test_card_block.py @@ -5,47 +5,47 @@ @pytest.mark.django_db def test_card_clickable_block_external_url(db, django_db_setup, client: Client): - response = client.get('/promo-hub/') - soup = BeautifulSoup(response.content, 'html.parser') - a_tag = soup.select_one('.block-card_clickable:nth-of-type(2) a') + response = client.get("/promo-hub/") + soup = BeautifulSoup(response.content, "html.parser") + a_tag = soup.select_one(".block-card_clickable:nth-of-type(2) a") - assert a_tag['href'] == 'https://example.com/' + assert a_tag["href"] == "https://example.com/" @pytest.mark.django_db def test_card_clickable_block_internal_page(db, django_db_setup, client: Client): - response = client.get('/promo-hub/') - soup = BeautifulSoup(response.content, 'html.parser') - a_tag = soup.select_one('.block-card_clickable:nth-of-type(3) a') + response = client.get("/promo-hub/") + soup = BeautifulSoup(response.content, "html.parser") + a_tag = soup.select_one(".block-card_clickable:nth-of-type(3) a") - assert a_tag['href'] == '/page-1/' + assert a_tag["href"] == "/page-1/" @pytest.mark.django_db def test_card_image_block_external_url(db, django_db_setup, client: Client): - response = client.get('/promo-hub/') - soup = BeautifulSoup(response.content, 'html.parser') - block = soup.select('.block-card_image')[0] - a_tag = block.find('a') + response = client.get("/promo-hub/") + soup = BeautifulSoup(response.content, "html.parser") + block = soup.select(".block-card_image")[0] + a_tag = block.find("a") - assert a_tag['href'] == 'https://example.com/' + assert a_tag["href"] == "https://example.com/" @pytest.mark.django_db def test_card_image_block_internal_page(db, django_db_setup, client: Client): - response = client.get('/promo-hub/') - soup = BeautifulSoup(response.content, 'html.parser') - block = soup.select('.block-card_image')[1] - a_tag = block.find('a') + response = client.get("/promo-hub/") + soup = BeautifulSoup(response.content, "html.parser") + block = soup.select(".block-card_image")[1] + a_tag = block.find("a") - assert a_tag['href'] == '/page-1/' + assert a_tag["href"] == "/page-1/" @pytest.mark.django_db def test_card_image_block_no_internal_link_or_url(db, django_db_setup, client: Client): - response = client.get('/promo-hub/') - soup = BeautifulSoup(response.content, 'html.parser') - block = soup.select('.block-card_image')[2] - a_tag = block.find('a') + response = client.get("/promo-hub/") + soup = BeautifulSoup(response.content, "html.parser") + block = soup.select(".block-card_image")[2] + a_tag = block.find("a") assert not a_tag diff --git a/tests/test_card_clickable_block.py b/tests/test_card_clickable_block.py index 79f278a..2f306ae 100644 --- a/tests/test_card_clickable_block.py +++ b/tests/test_card_clickable_block.py @@ -5,35 +5,40 @@ def test_card_clickable_block_clean_one_link(): block = CardClickableBlock() - value = {'url': 'https://example.com/'} + value = {"url": "https://example.com/"} clean_value = block.clean(value) assert value == clean_value - + def test_card_clickable_block_clean_no_links(): block = CardClickableBlock() - value = {'url': None, 'internal_page': None} - internal_error_message = '' - external_error_message = '' - + value = {"url": None, "internal_page": None} + internal_error_message = ( + '' + ) + external_error_message = ( + '' + ) + with pytest.raises(ValidationError) as excinfo: - clean_value = block.clean(value) + block.clean(value) - assert str(excinfo.value.params['internal_page']) == internal_error_message - assert str(excinfo.value.params['url']) == external_error_message + assert str(excinfo.value.params["internal_page"]) == internal_error_message + assert str(excinfo.value.params["url"]) == external_error_message assert "['Validation error in CardClickableBlock']" == str(excinfo.value) def test_card_clickable_block_clean_two_links(): block = CardClickableBlock() - value = {'url': 'https://example.com/', - 'internal_page': 'https://internal.com/'} - error_message = '' + value = {"url": "https://example.com/", "internal_page": "https://internal.com/"} + error_message = ( + '' + ) with pytest.raises(ValidationError) as excinfo: - clean_value = block.clean(value) - - assert str(excinfo.value.params['internal_page']) == error_message - assert str(excinfo.value.params['url']) == error_message - assert "['Validation error in CardClickableBlock']" == str(excinfo.value) \ No newline at end of file + block.clean(value) + + assert str(excinfo.value.params["internal_page"]) == error_message + assert str(excinfo.value.params["url"]) == error_message + assert "['Validation error in CardClickableBlock']" == str(excinfo.value) diff --git a/tests/test_card_image_block.py b/tests/test_card_image_block.py index 90661e5..59a83f6 100644 --- a/tests/test_card_image_block.py +++ b/tests/test_card_image_block.py @@ -5,11 +5,11 @@ def test_card_image_block_clean_one_link(): block = CardImageBlock() - value = {'url': 'https://example.com/'} + value = {"url": "https://example.com/"} clean_value = block.clean(value) assert value == clean_value - + def test_card_image_block_clean_no_links(): block = CardImageBlock() @@ -21,13 +21,14 @@ def test_card_image_block_clean_no_links(): def test_card_image_block_clean_two_links(): block = CardImageBlock() - value = {'url': 'https://example.com/', - 'internal_page': 'https://internal.com/'} - error_message = '' + value = {"url": "https://example.com/", "internal_page": "https://internal.com/"} + error_message = ( + '' + ) with pytest.raises(ValidationError) as excinfo: - clean_value = block.clean(value) - - assert str(excinfo.value.params['internal_page']) == error_message - assert str(excinfo.value.params['url']) == error_message - assert "['Validation error in CardImageBlock']" == str(excinfo.value) \ No newline at end of file + block.clean(value) + + assert str(excinfo.value.params["internal_page"]) == error_message + assert str(excinfo.value.params["url"]) == error_message + assert "['Validation error in CardImageBlock']" == str(excinfo.value) diff --git a/tests/test_chunk_tag.py b/tests/test_chunk_tag.py index eb674b4..154d2b7 100644 --- a/tests/test_chunk_tag.py +++ b/tests/test_chunk_tag.py @@ -3,13 +3,14 @@ LIST = [1, 2, 3, 4, 5, 6, 7] + def test_chunk_tag_size_2(): result = chunk(LIST, 2) - expected = [[1,2], [3,4], [5,6], [7]] + expected = [[1, 2], [3, 4], [5, 6], [7]] assert result == expected def test_chunk_tag_size_8(): result = chunk(LIST, 8) - expected = [[1,2,3,4,5,6,7]] + expected = [[1, 2, 3, 4, 5, 6, 7]] assert result == expected diff --git a/tests/test_contents_list_tag.py b/tests/test_contents_list_tag.py index 0a0b80e..c4adbfb 100644 --- a/tests/test_contents_list_tag.py +++ b/tests/test_contents_list_tag.py @@ -7,11 +7,8 @@ def get_contents_list_context(page=None): """Get the contents_list context that will be passed to the template.""" - page = page or Page.objects.get(url_path='/home/pagination/pagination-page-1/') - fake_context = { - 'page': page, - 'request': RequestFactory().get('/fake/url/') - } + page = page or Page.objects.get(url_path="/home/pagination/pagination-page-1/") + fake_context = {"page": page, "request": RequestFactory().get("/fake/url/")} # The contents_list tag is an inclusion_tag which returns a new context. new_context = contents_list(fake_context) return new_context @@ -20,7 +17,7 @@ def get_contents_list_context(page=None): @pytest.mark.django_db def test_contents_list_siblings_are_dicts(db, django_db_setup): template_context = get_contents_list_context() - for item in template_context['links']: + for item in template_context["links"]: assert isinstance(item, dict) @@ -28,22 +25,22 @@ def test_contents_list_siblings_are_dicts(db, django_db_setup): def test_contents_list_length(db, django_db_setup): template_context = get_contents_list_context() # length = 4 ensures that our unpublished page didn't appear - assert len(template_context['links']) == 4 + assert len(template_context["links"]) == 4 @pytest.mark.django_db def test_contents_list_is_current_page(db, django_db_setup): - page = Page.objects.get(url_path='/home/pagination/pagination-page-3/') + page = Page.objects.get(url_path="/home/pagination/pagination-page-3/") template_context = get_contents_list_context(page=page) - assert template_context['links'][2]['is_current'] is True - assert template_context['links'][3]['is_current'] is False + assert template_context["links"][2]["is_current"] is True + assert template_context["links"][3]["is_current"] is False def test_contents_list_has_href(db, django_db_setup): template_context = get_contents_list_context() - assert template_context['links'][0]['href'] == '/pagination/pagination-page-1/' + assert template_context["links"][0]["href"] == "/pagination/pagination-page-1/" def test_contents_list_has_label(db, django_db_setup): template_context = get_contents_list_context() - assert template_context['links'][0]['label'] == 'Pagination page 1' + assert template_context["links"][0]["label"] == "Pagination page 1" diff --git a/tests/test_form_tags.py b/tests/test_form_tags.py index 7e2bcd2..c6eed07 100644 --- a/tests/test_form_tags.py +++ b/tests/test_form_tags.py @@ -1,24 +1,27 @@ -import pytest from django import forms -from wagtailnhsukfrontend.forms.templatetags.nhsukfrontendforms_tags import add_class, add_widget_classes, is_checkbox +from wagtailnhsukfrontend.forms.templatetags.nhsukfrontendforms_tags import ( + add_class, + add_widget_classes, + is_checkbox, +) def test_add_class_to_empty_widget(): """Test adding a class to widget without preexisting class attribute""" widget = forms.CheckboxInput(attrs={}) - new_class = 'new-class' - expected = {'attrs': {'class': 'new-class'}} + new_class = "new-class" + expected = {"attrs": {"class": "new-class"}} result = add_class(widget.__dict__, new_class) - assert result['attrs'] == expected['attrs'] + assert result["attrs"] == expected["attrs"] def test_add_class_to_widget_with_class(): """Test adding a class to widget with preexisting class attribute""" - widget = forms.CheckboxInput(attrs={'class': 'old-class'}) - new_class = 'new-class' - expected = {'attrs': {'class': 'old-class new-class'}} + widget = forms.CheckboxInput(attrs={"class": "old-class"}) + new_class = "new-class" + expected = {"attrs": {"class": "old-class new-class"}} result = add_class(widget.__dict__, new_class) - assert result['attrs'] == expected['attrs'] + assert result["attrs"] == expected["attrs"] class ExampleForm(forms.Form): @@ -28,8 +31,8 @@ class ExampleForm(forms.Form): def test_add_widget_classes_simple(): """Test when field has no errors and not initially hidden""" - form = ExampleForm({'text': 'some text here'}) - field = form['text'] + form = ExampleForm({"text": "some text here"}) + field = form["text"] result = add_widget_classes(field) expected = '' assert result == expected @@ -37,8 +40,8 @@ def test_add_widget_classes_simple(): def test_add_widget_classes_error_on_field(): """Test when field has errors""" - form = ExampleForm({'text': ''}) - field = form['text'] + form = ExampleForm({"text": ""}) + field = form["text"] result = add_widget_classes(field) expected = '' assert result == expected @@ -47,12 +50,12 @@ def test_add_widget_classes_error_on_field(): def test_widget_is_not_checkbox(): """Test widget is not a checkbox""" form = ExampleForm() - field = form['text'] - assert is_checkbox(field) == False + field = form["text"] + assert is_checkbox(field) is False def test_widget_is_checkbox(): """Test widget is not a checkbox""" form = ExampleForm() - field = form['checkbox'] - assert is_checkbox(field) == True \ No newline at end of file + field = form["checkbox"] + assert is_checkbox(field) is True diff --git a/tests/test_pagination_tag.py b/tests/test_pagination_tag.py index 777beec..cd922a7 100644 --- a/tests/test_pagination_tag.py +++ b/tests/test_pagination_tag.py @@ -9,10 +9,7 @@ def get_pagination_context(page, request): """Get the pagination context that will be passed to the pagination template.""" - fake_context = { - 'page': page, - 'request': request - } + fake_context = {"page": page, "request": request} # The pagination tag is an inclusion_tag which returns a new context. new_context = pagination(fake_context) return new_context @@ -20,9 +17,9 @@ def get_pagination_context(page, request): def get_pagination_page(number): """ Get paginated page """ - response = client.get(f'/home/pagination/pagination-page-{number}/') + response = client.get(f"/home/pagination/pagination-page-{number}/") request = response.wsgi_request - page = Page.objects.get(url_path=f'/home/pagination/pagination-page-{number}/') + page = Page.objects.get(url_path=f"/home/pagination/pagination-page-{number}/") return get_pagination_context(page, request) @@ -30,7 +27,10 @@ def get_pagination_page(number): def test_first_page(db, django_db_setup): """ Check first page has next keys but no prev keys """ context = get_pagination_page(1) - expected = {'next_label': 'Pagination page 2', 'next_url': '/pagination/pagination-page-2/'} + expected = { + "next_label": "Pagination page 2", + "next_url": "/pagination/pagination-page-2/", + } assert context == expected @@ -38,8 +38,12 @@ def test_first_page(db, django_db_setup): def test_third_page(db, django_db_setup): """ Check third page has next and prev keys """ context = get_pagination_page(3) - expected = {'prev_label': 'Pagination page 2', 'prev_url': '/pagination/pagination-page-2/', \ - 'next_label': 'Pagination page 4', 'next_url': '/pagination/pagination-page-4/'} + expected = { + "prev_label": "Pagination page 2", + "prev_url": "/pagination/pagination-page-2/", + "next_label": "Pagination page 4", + "next_url": "/pagination/pagination-page-4/", + } assert context == expected @@ -48,5 +52,8 @@ def test_final_page(db, django_db_setup): """ Check third page has only prev keys """ context = get_pagination_page(4) print(context) - expected = {'prev_label': 'Pagination page 3', 'prev_url': '/pagination/pagination-page-3/'} + expected = { + "prev_label": "Pagination page 3", + "prev_url": "/pagination/pagination-page-3/", + } assert context == expected diff --git a/tests/test_promo_group_column_class_tag.py b/tests/test_promo_group_column_class_tag.py index 68a2b5f..fe4c6a9 100644 --- a/tests/test_promo_group_column_class_tag.py +++ b/tests/test_promo_group_column_class_tag.py @@ -1,21 +1,23 @@ import pytest -from wagtailnhsukfrontend.templatetags.nhsukfrontend_tags import promo_group_column_class +from wagtailnhsukfrontend.templatetags.nhsukfrontend_tags import ( + promo_group_column_class, +) def test_column_size_0(): with pytest.raises(Exception) as excinfo: - result = promo_group_column_class(0) - - assert 'promo column sizes must be either 2 or 3' == str(excinfo.value) + promo_group_column_class(0) + + assert "promo column sizes must be either 2 or 3" == str(excinfo.value) def test_column_size_2(): result = promo_group_column_class(2) - expected = 'nhsuk-grid-column-one-half' + expected = "nhsuk-grid-column-one-half" assert result == expected def test_column_size_3(): result = promo_group_column_class(3) - expected = 'nhsuk-grid-column-one-third' - assert result == expected \ No newline at end of file + expected = "nhsuk-grid-column-one-third" + assert result == expected