Skip to content

Commit

Permalink
Update tests to allow for external citations
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinEtchells committed Oct 24, 2024
1 parent 1138388 commit d46a78e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion django_app/redbox_app/templates/citation_fragment.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ <h3 class="govuk-heading-s govuk-!-margin-bottom-0">
{% endif %}
</h3>
{% if citation.page_numbers %}
<div class="govuk-body-s govuk-!-margin-top-3">Page number(s): {{ citation.page_numbers | join(", ") }}</div>
<div class="govuk-body-s govuk-!-margin-top-3"><strong>Page number(s):</strong> {{ citation.page_numbers | join(", ") }}</div>
{% endif %}
<markdown-converter class="iai-chat-bubble__text">{{ citation.text }}</markdown-converter>
</li>
Expand Down
26 changes: 12 additions & 14 deletions django_app/tests/views/test_citation_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,31 @@


@pytest.mark.django_db()
def test_citations_shown_in_correct_order(client: Client, alice: User, chat: Chat, several_files: Sequence[File]):
def test_citations_shown(client: Client, alice: User, chat: Chat, several_files: Sequence[File]):
# Given
client.force_login(alice)
chat_message = ChatMessage.objects.create(chat=chat, text="Some answer.", role=ChatRoleEnum.ai)

Citation.objects.create(file=several_files[1], chat_message=chat_message, text="Citation 1")
Citation.objects.create(file=several_files[0], chat_message=chat_message, text="Citation 2")
Citation.objects.create(file=several_files[1], chat_message=chat_message, text="Citation 3")
Citation.objects.create(file=several_files[2], chat_message=chat_message, text="Citation 4")
Citation.objects.create(file=several_files[0], chat_message=chat_message, text="Citation 5")
Citation.objects.create(file=several_files[0], chat_message=chat_message, text="Citation 1")
Citation.objects.create(file=several_files[1], chat_message=chat_message, text="Citation 2")
Citation.objects.create(
source=Citation.Origin.WIKIPEDIA, url="https://wikipedia-test", chat_message=chat_message, text="Citation 3"
)

# When
response = client.get(f"/citations/{chat_message.id}/")

# Then
assert response.status_code == HTTPStatus.OK
soup = BeautifulSoup(response.content)
sources_panel = soup.select("div.iai-panel")[1]
soup = BeautifulSoup(response.content, features="html.parser")
sources_panel = soup.select("ul.rb-citations__list")[0]
files = sources_panel.find_all("h3")
citation_items = sources_panel.find_all("markdown-converter")
filenames = [h3.get_text().strip() for h3 in files]
citations = [
[li.get_text().strip() for li in citations.find_all(class_="rb-citations__item")]
for citations in [h3.next_sibling.next_sibling for h3 in files]
]
citations = [element.get_text().strip() for element in citation_items]

assert filenames == ["original_file_1.txt", "original_file_0.txt", "original_file_2.txt"]
assert citations == [["Citation 1", "Citation 3"], ["Citation 2", "Citation 5"], ["Citation 4"]]
assert filenames == ["original_file_0.txt", "original_file_1.txt", "https://wikipedia-test"]
assert citations == ["Citation 1", "Citation 2", "Citation 3"]


@pytest.mark.django_db()
Expand Down

0 comments on commit d46a78e

Please sign in to comment.