Skip to content

Commit

Permalink
tests for exporting second session, plus fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
struan committed May 1, 2024
1 parent cf27cc1 commit fb38a7f
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 10 deletions.
2 changes: 1 addition & 1 deletion crowdsourcer/fixtures/assignments.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"fields": {
"marking_session": 2,
"user": 5,
"section": 9,
"section": 22,
"authority": 2,
"response_type": 1,
"active": true,
Expand Down
22 changes: 22 additions & 0 deletions crowdsourcer/fixtures/audit_second_session_marks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"model": "crowdsourcer.response",
"pk": 2000,
"fields": {
"authority": 1,
"question": 2002,
"user": 2,
"option": 20021,
"response_type": 3,
"public_notes": "public notrs",
"page_number": "0",
"evidence": "",
"private_notes": "private notes",
"revision_type": null,
"revision_notes": null,
"created": "2023-03-15T17:22:10+0000",
"last_update": "2023-03-15T17:22:10+0000",
"multi_option": []
}
}
]
4 changes: 2 additions & 2 deletions crowdsourcer/fixtures/basics.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@
},
{
"model": "crowdsourcer.section",
"pk": 8,
"pk": 21,
"fields": {
"marking_session": 2,
"title": "Second Session Section"
}
},
{
"model": "crowdsourcer.section",
"pk": 9,
"pk": 22,
"fields": {
"marking_session": 2,
"title": "Transport"
Expand Down
2 changes: 1 addition & 1 deletion crowdsourcer/fixtures/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@
"pk": 20022,
"fields": {
"question": 2002,
"score": 1,
"score": 2,
"description": "Section Session Transport Q1 Opt 2",
"ordering": 2
}
Expand Down
4 changes: 2 additions & 2 deletions crowdsourcer/fixtures/questions.json
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@
"number_part": null,
"description": "Second Session Section One Q1",
"criteria": "Second Session Criteria",
"section": 7,
"section": 21,
"clarifications": "Second Session clarification",
"topic": "",
"weighting": "low",
Expand All @@ -588,7 +588,7 @@
"number_part": null,
"description": "Second Session Transport Q1",
"criteria": "Second Session Criteria",
"section": 9,
"section": 22,
"clarifications": "Second Session clarification",
"topic": "",
"weighting": "low",
Expand Down
25 changes: 21 additions & 4 deletions crowdsourcer/scoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,15 @@ def q_is_exception(q, section, group, country, council):

def update_with_housing_exceptions(session):
rt = ResponseType.objects.get(type="Audit")
q = Question.objects.get(
number=3, section__title="Buildings & Heating", section__marking_session=session
)
try:
q = Question.objects.get(
number=3,
section__title="Buildings & Heating",
section__marking_session=session,
)
except Question.DoesNotExist:
return

try:
o = Option.objects.get(
question=q,
Expand Down Expand Up @@ -462,6 +468,17 @@ def get_section_scores(scoring, session):
scoring["weighted_scores"] = weighted


def get_section_weighting(section, council_group):
if (
SECTION_WEIGHTINGS.get(section, None) is not None
and SECTION_WEIGHTINGS[section].get(council_group, None) is not None
):
return SECTION_WEIGHTINGS[section][council_group]

print(f"No weighting for {section} and {council_group}")
return 0


def calculate_council_totals(scoring):
section_totals = defaultdict(dict)
totals = {}
Expand Down Expand Up @@ -500,7 +517,7 @@ def calculate_council_totals(scoring):
weighted_score = (
scoring["weighted_scores"][council][section]
/ council_weighted_max[section][council_group]
) * SECTION_WEIGHTINGS[section][council_group]
) * get_section_weighting(section, council_group)
weighted_score = round(weighted_score, 2)

unweighted_percentage = (
Expand Down
134 changes: 134 additions & 0 deletions crowdsourcer/tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -1425,3 +1425,137 @@ def test_export_100_percent(self, write_mock):

self.assertEquals(raw, expected_raw)
self.assertEquals(percent, expected_percent)


class ExportSecondSessionTestCase(BaseCommandTestCase):
fixtures = [
"authorities.json",
"basics.json",
"users.json",
"questions.json",
"options.json",
"audit_marking_many_marks.json",
"audit_second_session_marks.json",
]

max_section = {
"Second Session Section": {
"Single Tier": 1,
"District": 1,
"County": 1,
"Northern Ireland": 1,
"Combined Authority": 0,
},
"Transport": {
"Single Tier": 2,
"District": 2,
"County": 2,
"Northern Ireland": 2,
"Combined Authority": 0,
},
}
max_totals = {
"Single Tier": 3,
"District": 3,
"County": 3,
"Northern Ireland": 3,
"Combined Authority": 0,
}

max_questions = {
"Second Session Section": {
"1": 1,
},
"Transport": {"1": 2},
}

max_weighted = {
"Second Session Section": {
"Single Tier": 1,
"District": 1,
"County": 1,
"Northern Ireland": 1,
"Combined Authority": 0,
},
"Transport": {
"Single Tier": 1,
"District": 1,
"County": 1,
"Northern Ireland": 1,
"Combined Authority": 0,
},
}

expected_percent = [
{
"council": "Aberdeen City Council",
"gss": "S12000033",
"political_control": None,
"Second Session Section": 0.0,
"Transport": 0.5,
"raw_total": 0.33,
"weighted_total": 0.1,
},
{
"council": "Aberdeenshire Council",
"gss": "S12000034",
"political_control": None,
"Second Session Section": 0.0,
"Transport": 0.0,
"raw_total": 0.0,
"weighted_total": 0.0,
},
{
"council": "Adur District Council",
"gss": "E07000223",
"political_control": None,
"Second Session Section": 0.0,
"Transport": 0.0,
"raw_total": 0.0,
"weighted_total": 0.0,
},
]

expected_raw = [
{
"Second Session Section": 0,
"Transport": 1,
"council": "Aberdeen City Council",
"gss": "S12000033",
"total": 1,
},
{
"Second Session Section": 0,
"Transport": 0,
"council": "Aberdeenshire Council",
"gss": "S12000034",
"total": 0,
},
{
"Second Session Section": 0,
"Transport": 0,
"council": "Adur District Council",
"gss": "E07000223",
"total": 0,
},
]

def test_max_calculation(self):
scoring = {}
get_section_maxes(scoring, MarkingSession.objects.get(label="Second Session"))

self.assertEquals(scoring["section_maxes"], self.max_section)
self.assertEquals(scoring["group_maxes"], self.max_totals)
self.assertEquals(scoring["q_maxes"], self.max_questions)
self.assertEquals(scoring["section_weighted_maxes"], self.max_weighted)

@mock.patch("crowdsourcer.management.commands.export_marks.Command.write_files")
@mock.patch("crowdsourcer.scoring.EXCEPTIONS", {})
@mock.patch("crowdsourcer.scoring.SCORE_EXCEPTIONS", {})
def test_export(self, write_mock):
self.call_command("export_marks", session="Second Session")

percent, raw, linear = write_mock.call_args[0]

self.assertEquals(raw, self.expected_raw)
self.assertEquals(percent, self.expected_percent)

0 comments on commit fb38a7f

Please sign in to comment.