Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manual Tally Policy - CSV Tally Reports #4074

Merged
merged 8 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 149 additions & 37 deletions apps/admin/backend/src/app.tally_report_csv_export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ it('exports expected results for full election', async () => {
'Contest ID',
'Selection',
'Selection ID',
'Votes',
'Total Votes',
]);

const bestAnimalMammalExpectedValues: Record<string, string> = {
Expand Down Expand Up @@ -160,7 +160,7 @@ it('logs failure if export fails for some reason', async () => {
);
});

it('incorporates wia and manual data', async () => {
it('incorporates wia and manual data (grouping by voting method)', async () => {
const { electionDefinition, castVoteRecordExport } =
electionGridLayoutNewHampshireAmherstFixtures;
const { election } = electionDefinition;
Expand All @@ -180,105 +180,217 @@ it('incorporates wia and manual data', async () => {
const candidateContestId =
'State-Representatives-Hillsborough-District-34-b1012d38';
const officialCandidateId = 'Obadiah-Carrigan-5c95145a';
const officialCandidateName = 'Obadiah Carrigan';

function rowExists(
rows: ReturnType<typeof parseCsv>['rows'],
selectionId: string,
votingMethod: string,
votes: number
{
selection,
selectionId,
votingMethod,
totalVotes,
scannedVotes,
manualVotes,
}: {
selection: string;
selectionId: string;
votingMethod: string;
totalVotes: number;
scannedVotes?: number;
manualVotes?: number;
}
): boolean {
return rows.some(
(row) =>
row['Selection'] === selection &&
row['Selection ID'] === selectionId &&
row['Voting Method'] === votingMethod &&
row['Votes'] === votes.toString()
row['Total Votes'] === totalVotes.toString() &&
(!scannedVotes || row['Scanned Votes'] === scannedVotes.toString()) &&
(!manualVotes || row['Manual Votes'] === manualVotes.toString())
);
}

// check initial export, without wia and manual data
const { rows: rowsInitial } = await getParsedExport({
const { headers: headersInitial, rows: rowsInitial } = await getParsedExport({
apiClient,
groupBy,
});
expect(headersInitial).toEqual([
'Voting Method',
'Contest',
'Contest ID',
'Selection',
'Selection ID',
'Total Votes',
]);

// initial official candidate counts
expect(
rowExists(rowsInitial, officialCandidateId, 'Precinct', 30)
rowExists(rowsInitial, {
selection: officialCandidateName,
selectionId: officialCandidateId,
votingMethod: 'Precinct',
totalVotes: 30,
})
).toBeTruthy();
expect(
rowExists(rowsInitial, officialCandidateId, 'Absentee', 30)
rowExists(rowsInitial, {
selection: officialCandidateName,
selectionId: officialCandidateId,
votingMethod: 'Precinct',
totalVotes: 30,
})
).toBeTruthy();

// initial generic write-in counts
expect(rowExists(rowsInitial, 'write-in', 'Absentee', 28)).toBeTruthy();
expect(rowExists(rowsInitial, 'write-in', 'Precinct', 28)).toBeTruthy();
expect(
rowExists(rowsInitial, {
selection: Tabulation.PENDING_WRITE_IN_NAME,
selectionId: Tabulation.PENDING_WRITE_IN_ID,
votingMethod: 'Precinct',
totalVotes: 28,
})
).toBeTruthy();
expect(
rowExists(rowsInitial, {
selection: Tabulation.PENDING_WRITE_IN_NAME,
selectionId: Tabulation.PENDING_WRITE_IN_ID,
votingMethod: 'Absentee',
totalVotes: 28,
})
).toBeTruthy();

// adjudicate write-ins for unofficial candidate
const writeInCandidate = await apiClient.addWriteInCandidate({
contestId: candidateContestId,
name: 'Mr. Pickles',
});
const writeInIds = await apiClient.getWriteInAdjudicationQueue({
contestId: candidateContestId,
});
for (const writeInId of writeInIds) {
await apiClient.adjudicateWriteIn({
writeInId,
type: 'write-in-candidate',
candidateId: writeInCandidate.id,
});
}

// add manual data
const manualOnlyWriteInCandidate = await apiClient.addWriteInCandidate({
contestId: candidateContestId,
name: 'Ms. Bean',
});
await apiClient.setManualResults({
precinctId: election.precincts[0]!.id,
votingMethod: 'absentee',
ballotStyleId: election.ballotStyles[0]!.id,
manualResults: buildManualResultsFixture({
election,
ballotCount: 10,
ballotCount: 20,
contestResultsSummaries: {
[candidateContestId]: {
type: 'candidate',
ballots: 10,
ballots: 20,
overvotes: 0,
undervotes: 0,
officialOptionTallies: {
[officialCandidateId]: 10,
},
writeInOptionTallies: {
[writeInCandidate.id]: {
name: writeInCandidate.name,
tally: 5,
},
[manualOnlyWriteInCandidate.id]: {
name: manualOnlyWriteInCandidate.name,
tally: 5,
},
},
},
},
}),
});

// adjudicate write-ins for unofficial candidate
const writeInCandidate = await apiClient.addWriteInCandidate({
contestId: candidateContestId,
name: 'Mr. Pickles',
});
const writeInIds = await apiClient.getWriteInAdjudicationQueue({
contestId: candidateContestId,
});
for (const writeInId of writeInIds) {
await apiClient.adjudicateWriteIn({
writeInId,
type: 'write-in-candidate',
candidateId: writeInCandidate.id,
});
}

// check final export, with wia and manual data
const { rows: rowsFinal } = await getParsedExport({
const { headers: headersFinal, rows: rowsFinal } = await getParsedExport({
apiClient,
groupBy,
});
expect(headersFinal).toEqual([
'Voting Method',
'Contest',
'Contest ID',
'Selection',
'Selection ID',
'Manual Votes',
'Scanned Votes',
'Total Votes',
]);

// final official candidate counts
expect(
rowExists(rowsFinal, officialCandidateId, 'Precinct', 30)
rowExists(rowsFinal, {
selection: officialCandidateName,
selectionId: officialCandidateId,
votingMethod: 'Precinct',
manualVotes: 0,
scannedVotes: 30,
totalVotes: 30,
})
).toBeTruthy();
expect(
rowExists(rowsFinal, officialCandidateId, 'Absentee', 40)
rowExists(rowsFinal, {
selection: officialCandidateName,
selectionId: officialCandidateId,
votingMethod: 'Absentee',
manualVotes: 10,
scannedVotes: 30,
totalVotes: 40,
})
).toBeTruthy(); // manual data reflected

// added write-in candidate counts
// adjudicated write-in candidate counts
expect(
rowExists(rowsFinal, writeInCandidate.id, 'Absentee', 28)
rowExists(rowsFinal, {
selection: `${writeInCandidate.name} (Write-In)`,
selectionId: writeInCandidate.id,
votingMethod: 'Precinct',
manualVotes: 0,
scannedVotes: 28,
totalVotes: 28,
})
).toBeTruthy();
expect(
rowExists(rowsFinal, writeInCandidate.id, 'Precinct', 28)
rowExists(rowsFinal, {
selection: `${writeInCandidate.name} (Write-In)`,
selectionId: writeInCandidate.id,
votingMethod: 'Absentee',
manualVotes: 5,
scannedVotes: 28,
totalVotes: 33,
})
).toBeTruthy();

// manual-only write-in candidate counts
expect(
rowExists(rowsFinal, {
selection: `${manualOnlyWriteInCandidate.name} (Write-In)`,
selectionId: manualOnlyWriteInCandidate.id,
votingMethod: 'Absentee',
manualVotes: 5,
scannedVotes: 0,
totalVotes: 5,
})
).toBeTruthy();

// generic write-in counts should be gone
// pending write-in counts should be gone
expect(
rowsFinal.some(
(r) =>
r['Contest ID'] === candidateContestId &&
r['Selection ID'] === 'write-in'
r['Selection ID'] === Tabulation.PENDING_WRITE_IN_ID
)
).toBeFalsy();
});
6 changes: 3 additions & 3 deletions apps/admin/backend/src/app.tally_reports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getFeatureFlagMock,
} from '@votingworks/utils';
import { assert, find } from '@votingworks/basics';
import { writeInCandidate } from '@votingworks/types';
import { Tabulation } from '@votingworks/types';
import {
buildTestEnvironment,
configureMachine,
Expand Down Expand Up @@ -261,9 +261,9 @@ test('general, reports by voting method, manual data', async () => {
type: 'candidate',
ballots: 10,
writeInOptionTallies: {
[writeInCandidate.id]: {
[Tabulation.GENERIC_WRITE_IN_ID]: {
tally: 10,
name: writeInCandidate.name,
name: Tabulation.GENERIC_WRITE_IN_NAME,
},
},
},
Expand Down
6 changes: 3 additions & 3 deletions apps/admin/backend/src/exports/batch_results.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { electionTwoPartyPrimaryDefinition } from '@votingworks/fixtures';
import { Tabulation, writeInCandidate } from '@votingworks/types';
import { Tabulation } from '@votingworks/types';
import { buildElectionResultsFixture } from '@votingworks/utils';
import { Buffer } from 'buffer';
import { generateBatchResultsFile } from './batch_results';
Expand All @@ -22,7 +22,7 @@ test('generateBatchResultsFile', async () => {
overvotes: 3,
officialOptionTallies: {
lion: 25,
[writeInCandidate.id]: 5,
[Tabulation.GENERIC_WRITE_IN_ID]: 5,
},
},
fishing: {
Expand Down Expand Up @@ -50,7 +50,7 @@ test('generateBatchResultsFile', async () => {
overvotes: 3,
officialOptionTallies: {
lion: 20,
[writeInCandidate.id]: 6,
[Tabulation.GENERIC_WRITE_IN_ID]: 6,
},
},
fishing: {
Expand Down
9 changes: 6 additions & 3 deletions apps/admin/backend/src/exports/batch_results.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Election, Tabulation, writeInCandidate } from '@votingworks/types';
import { Election, Tabulation } from '@votingworks/types';
import { assert } from '@votingworks/basics';
import { Readable } from 'stream';
import { getBallotCount, groupMapToGroupList } from '@votingworks/utils';
Expand Down Expand Up @@ -65,8 +65,11 @@ function generateResultsRow(
}
if (contest.allowWriteIns) {
contestVoteTotals.push(
/* c8 ignore next - trivial fallback case */
contestResults.tallies[writeInCandidate.id]?.tally.toString() ?? '0'
/* c8 ignore start - trivial fallback case */
contestResults.tallies[
Tabulation.GENERIC_WRITE_IN_ID
]?.tally.toString() ?? '0'
/* c8 ignore end */
);
}
} else if (contest.type === 'yesno') {
Expand Down
Loading