From 144da6a0185197936bea7bd867026260e025b021 Mon Sep 17 00:00:00 2001 From: Alex Parsons Date: Fri, 20 Dec 2024 20:13:05 +0000 Subject: [PATCH] Add tests for annotation and whipreport viewing --- tests/test_annotations.py | 164 +++++++++++++++++++++++++++++++++++++ tests/test_whip_reports.py | 59 +++++++++++++ 2 files changed, 223 insertions(+) create mode 100644 tests/test_annotations.py create mode 100644 tests/test_whip_reports.py diff --git a/tests/test_annotations.py b/tests/test_annotations.py new file mode 100644 index 0000000..d675924 --- /dev/null +++ b/tests/test_annotations.py @@ -0,0 +1,164 @@ +from django.test import Client + +import pytest + +from votes.models import ( + Agreement, + AgreementAnnotation, + Division, + DivisionAnnotation, + VoteAnnotation, +) + +pytestmark = pytest.mark.django_db + + +@pytest.fixture +def agreement_annotation(): + da = AgreementAnnotation( + agreement=Agreement.objects.get(key="a-commons-2021-12-13-e.876.1.2"), + detail="Link to BBC Sport", + link="https://www.bbc.co.uk/sport", + ) + da.save() + yield da + da.delete() + + +@pytest.fixture +def division_annotation(): + da = DivisionAnnotation( + division=Division.objects.get(key="pw-2016-12-13-109-commons"), + detail="Link to BBC News", + link="https://www.bbc.co.uk/news", + ) + da.save() + yield da + da.delete() + + +@pytest.fixture +def vote_annotation(): + va = VoteAnnotation( + division=Division.objects.get(key="pw-2016-12-13-109-commons"), + person_id=10001, + detail="Link to Google", + link="https://www.google.com", + ) + va.save() + yield va + va.delete() + + +def test_original_data_agreement(client: Client): + response = client.get("/decisions/agreement/commons/2021-12-13/e.876.1.2") + content = response.content.decode() + assert "Link to BBC Sport" not in content + assert "https://www.bbc.co.uk/sport" not in content + + +def test_original_data_agreement_json(client: Client): + response = client.get("/decisions/agreement/commons/2021-12-13/e.876.1.2.json") + content = response.content.decode() + assert "Link to BBC Sport" not in content + assert "https://www.bbc.co.uk/sport" not in content + + +def test_agreement_annotation( + client: Client, agreement_annotation: AgreementAnnotation +): + response = client.get("/decisions/agreement/commons/2021-12-13/e.876.1.2") + content = response.content.decode() + assert "Link to BBC Sport" in content + assert "https://www.bbc.co.uk/sport" in content + + +def test_agreement_annotation_json( + client: Client, agreement_annotation: AgreementAnnotation +): + response = client.get("/decisions/agreement/commons/2021-12-13/e.876.1.2.json") + content = response.content.decode() + assert "Link to BBC Sport" in content + assert "https://www.bbc.co.uk/sport" in content + + +def test_original_data(client: Client): + response = client.get("/decisions/division/commons/2016-12-13/109") + content = response.content.decode() + for text in ["Link to BBC News", "Link to Google"]: + assert text not in content + for url in ["https://www.bbc.co.uk/news", "https://www.google.com"]: + assert url not in content + + +def test_original_data_json(client: Client): + response = client.get("/decisions/division/commons/2016-12-13/109.json") + content = response.content.decode() + for text in ["Link to BBC News", "Link to Google"]: + assert text not in content + for url in ["https://www.bbc.co.uk/news", "https://www.google.com"]: + assert url not in content + + +def test_division_annotation(client: Client, division_annotation: DivisionAnnotation): + response = client.get("/decisions/division/commons/2016-12-13/109") + content = response.content.decode() + assert "Link to BBC News" in content + assert "https://www.bbc.co.uk/news" in content + assert "Link to Google" not in content + assert "https://www.google.com" not in content + + +def test_division_annotation_json( + client: Client, division_annotation: DivisionAnnotation +): + response = client.get("/decisions/division/commons/2016-12-13/109.json") + content = response.content.decode() + assert "Link to BBC News" in content + assert "https://www.bbc.co.uk/news" in content + assert "Link to Google" not in content + assert "https://www.google.com" not in content + + +def test_vote_annotation(client: Client, vote_annotation: VoteAnnotation): + response = client.get("/decisions/division/commons/2016-12-13/109") + content = response.content.decode() + assert "Link to BBC News" not in content + assert "https://www.bbc.co.uk/news" not in content + assert "Link to Google" in content + assert "https://www.google.com" in content + + +def test_vote_annotation_json(client: Client, vote_annotation: VoteAnnotation): + response = client.get("/decisions/division/commons/2016-12-13/109.json") + content = response.content.decode() + assert "Link to BBC News" not in content + assert "https://www.bbc.co.uk/news" not in content + assert "Link to Google" in content + assert "https://www.google.com" in content + + +def test_division_and_vote_annotation( + client: Client, + division_annotation: DivisionAnnotation, + vote_annotation: VoteAnnotation, +): + response = client.get("/decisions/division/commons/2016-12-13/109") + content = response.content.decode() + assert "Link to BBC News" in content + assert "https://www.bbc.co.uk/news" in content + assert "Link to Google" in content + assert "https://www.google.com" in content + + +def test_division_and_vote_annotation_json( + client: Client, + division_annotation: DivisionAnnotation, + vote_annotation: VoteAnnotation, +): + response = client.get("/decisions/division/commons/2016-12-13/109.json") + content = response.content.decode() + assert "Link to BBC News" in content + assert "https://www.bbc.co.uk/news" in content + assert "Link to Google" in content + assert "https://www.google.com" in content diff --git a/tests/test_whip_reports.py b/tests/test_whip_reports.py new file mode 100644 index 0000000..a43cb7a --- /dev/null +++ b/tests/test_whip_reports.py @@ -0,0 +1,59 @@ +from django.test import Client + +import pytest + +from votes.consts import EvidenceType, WhipDirection, WhipPriority +from votes.models import ( + Division, + Organization, + WhipReport, +) + +pytestmark = pytest.mark.django_db + + +@pytest.fixture +def whip_report(): + da = WhipReport( + division=Division.objects.get(key="pw-2016-12-13-109-commons"), + whip_direction=WhipDirection.AGAINST, + whip_priority=WhipPriority.THREE_LINE, + party=Organization.objects.get(slug="labour"), + evidence_type=EvidenceType.OTHER, + ) + da.save() + yield da + da.delete() + + +def test_original_data(client: Client): + response = client.get("/decisions/division/commons/2016-12-13/109") + content = response.content.decode() + for text in ["Whip reports", "against", "three_line"]: + assert text not in content + + +def test_original_data_json(client: Client): + response = client.get("/decisions/division/commons/2016-12-13/109.json") + content = response.content.decode() + response = client.get("/decisions/division/commons/2016-12-13/109") + content = response.content.decode() + for text in ["Whip reports", "against", "three_line"]: + assert text not in content + + +def test_whip_report(client: Client, whip_report: WhipReport): + response = client.get("/decisions/division/commons/2016-12-13/109") + content = response.content.decode() + for text in ["Whip reports", "against", "three_line"]: + assert text in content + + +def test_whip_report_json(client: Client, whip_report: WhipReport): + response = client.get("/decisions/division/commons/2016-12-13/109.json") + data = response.json() + assert len(data["whip_reports"]) > 0 + labour_whip = [x for x in data["whip_reports"] if x["party"] == "Labour"] + assert len(labour_whip) == 1 + assert labour_whip[0]["whip_direction"] == "against" + assert labour_whip[0]["whip_priority"] == "three_line"