Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Robinson committed Aug 5, 2021
1 parent 8221451 commit 44ebefc
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 136 deletions.
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
77 changes: 48 additions & 29 deletions tests/test_action_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<ul class="errorlist"><li>Please choose a page or enter a URL above.</li></ul>'
external_error_message = '<ul class="errorlist"><li>Please enter a URL or choose a page below.</li></ul>'

value = {
"text": "testing",
"external_url": None,
"internal_page": None,
"new_window": False,
}
internal_error_message = (
'<ul class="errorlist"><li>Please choose a page or enter a URL above.</li></ul>'
)
external_error_message = (
'<ul class="errorlist"><li>Please enter a URL or choose a page below.</li></ul>'
)

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 = '<ul class="errorlist"><li>Please only enter a URL or choose a page.</li></ul>'
value = {
"text": "testing",
"external_url": "https://example.com/",
"internal_page": "https://internal.com/",
"new_window": False,
}
error_message = (
'<ul class="errorlist"><li>Please only enter a URL or choose a page.</li></ul>'
)

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)
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)
16 changes: 8 additions & 8 deletions tests/test_breadcrumb_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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
Expand All @@ -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
44 changes: 22 additions & 22 deletions tests/test_card_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
39 changes: 22 additions & 17 deletions tests/test_card_clickable_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<ul class="errorlist"><li>Please choose a page or enter a URL below.</li></ul>'
external_error_message = '<ul class="errorlist"><li>Please enter a URL or choose a page above.</li></ul>'

value = {"url": None, "internal_page": None}
internal_error_message = (
'<ul class="errorlist"><li>Please choose a page or enter a URL below.</li></ul>'
)
external_error_message = (
'<ul class="errorlist"><li>Please enter a URL or choose a page above.</li></ul>'
)

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 = '<ul class="errorlist"><li>Please only enter a URL or choose a page.</li></ul>'
value = {"url": "https://example.com/", "internal_page": "https://internal.com/"}
error_message = (
'<ul class="errorlist"><li>Please only enter a URL or choose a page.</li></ul>'
)

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)
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)
21 changes: 11 additions & 10 deletions tests/test_card_image_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 = '<ul class="errorlist"><li>Please only enter a URL or choose a page.</li></ul>'
value = {"url": "https://example.com/", "internal_page": "https://internal.com/"}
error_message = (
'<ul class="errorlist"><li>Please only enter a URL or choose a page.</li></ul>'
)

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)
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)
5 changes: 3 additions & 2 deletions tests/test_chunk_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading

0 comments on commit 44ebefc

Please sign in to comment.