Skip to content

Commit

Permalink
Use assert_ok everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahkagan committed Apr 7, 2020
1 parent 7eaacb4 commit 8b5cbe2
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 84 deletions.
18 changes: 11 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import pytest
from flask.testing import FlaskClient
import json, io, uuid
import io, uuid
from typing import List, Generator
from flask import jsonify

from arlo_server import app, db
from arlo_server.models import Election, Jurisdiction, USState
from arlo_server.auth import with_election_access, with_jurisdiction_access
from tests.helpers import put_json, create_election
from tests.helpers import (
assert_ok,
put_json,
create_election,
)
from bgcompute import (
bgcompute_update_election_jurisdictions_file,
bgcompute_update_ballot_manifest_file,
Expand Down Expand Up @@ -55,7 +59,7 @@ def jurisdiction_ids(
)
},
)
assert json.loads(rv.data) == {"status": "ok"}
assert_ok(rv)
bgcompute_update_election_jurisdictions_file()
jurisdictions = Jurisdiction.query.filter_by(election_id=election_id).all()
yield [j.id for j in jurisdictions]
Expand All @@ -80,7 +84,7 @@ def contest_id(
"jurisdictionIds": jurisdiction_ids,
}
rv = put_json(client, f"/election/{election_id}/contest", [contest])
assert json.loads(rv.data) == {"status": "ok"}
assert_ok(rv)
yield contest_id


Expand All @@ -94,7 +98,7 @@ def election_settings(client: FlaskClient, election_id: str) -> None:
"state": USState.California,
}
rv = put_json(client, f"/election/{election_id}/settings", settings)
assert json.loads(rv.data) == {"status": "ok"}
assert_ok(rv)


@pytest.fixture
Expand All @@ -114,7 +118,7 @@ def manifests(client: FlaskClient, election_id: str, jurisdiction_ids: List[str]
)
},
)
assert rv.status_code == 200
assert_ok(rv)
rv = client.put(
f"/election/{election_id}/jurisdiction/{jurisdiction_ids[1]}/manifest",
data={
Expand All @@ -130,7 +134,7 @@ def manifests(client: FlaskClient, election_id: str, jurisdiction_ids: List[str]
)
},
)
assert rv.status_code == 200
assert_ok(rv)
bgcompute_update_ballot_manifest_file()


Expand Down
6 changes: 3 additions & 3 deletions tests/routes_tests/test_audit_basic_update.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json, uuid

from tests.helpers import post_json
from tests.helpers import assert_ok, post_json


def test_audit_basic_update_create_contest(client, election_id):
Expand Down Expand Up @@ -33,7 +33,7 @@ def test_audit_basic_update_create_contest(client, election_id):
},
)

assert json.loads(rv.data)["status"] == "ok"
assert_ok(rv)


def test_audit_basic_update_sets_default_for_contest_is_targeted(client, election_id):
Expand Down Expand Up @@ -65,7 +65,7 @@ def test_audit_basic_update_sets_default_for_contest_is_targeted(client, electio
},
)

assert json.loads(rv.data)["status"] == "ok"
assert_ok(rv)

rv = client.get(f"/election/{election_id}/audit/status")
assert json.loads(rv.data)["contests"][0]["isTargeted"] is True
6 changes: 3 additions & 3 deletions tests/routes_tests/test_ballot_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from tests.helpers import post_json, create_election
from tests.helpers import assert_ok, post_json, create_election
from tests.test_app import setup_whole_audit
import bgcompute

Expand Down Expand Up @@ -59,7 +59,7 @@ def test_ballot_list_jurisdiction_two_rounds(client, election_id):
]
},
)
assert json.loads(rv.data)["status"] == "ok"
assert_ok(rv)
bgcompute.bgcompute()

# Get the sample size and round id for the second round
Expand Down Expand Up @@ -127,7 +127,7 @@ def test_ballot_list_audit_board_two_rounds(client, election_id):
]
},
)
assert json.loads(rv.data)["status"] == "ok"
assert_ok(rv)
bgcompute.bgcompute()

# Get the sample size and round id for the second round
Expand Down
14 changes: 7 additions & 7 deletions tests/routes_tests/test_contests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import List
import json, uuid

from tests.helpers import post_json, put_json
from tests.helpers import assert_ok, post_json, put_json
from arlo_server.models import RoundContest
from arlo_server.contests import JSONDict
from arlo_server import db
Expand Down Expand Up @@ -64,7 +64,7 @@ def test_contests_list_empty(client, election_id):
def test_contests_create_get_update_one(client, election_id, json_contests):
contest = json_contests[0]
rv = put_json(client, f"/election/{election_id}/contest", [contest])
assert json.loads(rv.data) == {"status": "ok"}
assert_ok(rv)

rv = client.get(f"/election/{election_id}/contest")
contests = json.loads(rv.data)
Expand All @@ -78,7 +78,7 @@ def test_contests_create_get_update_one(client, election_id, json_contests):
)

rv = put_json(client, f"/election/{election_id}/contest", [contest])
assert json.loads(rv.data) == {"status": "ok"}
assert_ok(rv)

rv = client.get(f"/election/{election_id}/contest")
contests = json.loads(rv.data)
Expand All @@ -93,7 +93,7 @@ def test_contests_create_get_update_multiple(
jurisdiction_ids: List[str],
):
rv = put_json(client, f"/election/{election_id}/contest", json_contests)
assert json.loads(rv.data) == {"status": "ok"}
assert_ok(rv)

rv = client.get(f"/election/{election_id}/contest")
contests = json.loads(rv.data)
Expand All @@ -107,7 +107,7 @@ def test_contests_create_get_update_multiple(
json_contests[2]["jurisdictionIds"] = jurisdiction_ids[1:]

rv = put_json(client, f"/election/{election_id}/contest", json_contests)
assert json.loads(rv.data) == {"status": "ok"}
assert_ok(rv)

rv = client.get(f"/election/{election_id}/contest")
contests = json.loads(rv.data)
Expand All @@ -125,15 +125,15 @@ def test_contests_round_status(
manifests, # pylint: disable=unused-argument
):
rv = put_json(client, f"/election/{election_id}/contest", json_contests)
assert rv.status_code == 200
assert_ok(rv)

SAMPLE_SIZE = 119 # Bravo sample size
rv = post_json(
client,
f"/election/{election_id}/round",
{"roundNum": 1, "sampleSize": SAMPLE_SIZE},
)
assert rv.status_code == 200
assert_ok(rv)

rv = client.get(f"/election/{election_id}/contest")
contests = json.loads(rv.data)["contests"]
Expand Down
5 changes: 2 additions & 3 deletions tests/routes_tests/test_election_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from arlo_server.models import Election, USState

from tests.helpers import put_json, compare_json, asserts_startswith
from tests.helpers import assert_ok, put_json, compare_json, asserts_startswith


def test_get_empty(client: FlaskClient, election_id: str):
Expand Down Expand Up @@ -31,8 +31,7 @@ def test_update_election(client: FlaskClient, election_id: str):
election["state"] = USState.Mississippi

rv = put_json(client, f"/election/{election_id}/settings", election)
assert rv.status_code == 200, f"unexpected response: {rv.data}"
assert json.loads(rv.data) == {"status": "ok"}
assert_ok(rv)

election_record = Election.query.filter_by(id=election_id).one()
assert election_record.election_name == "An Updated Name"
Expand Down
13 changes: 7 additions & 6 deletions tests/routes_tests/test_jurisdictions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import List

from tests.helpers import (
assert_ok,
put_json,
post_json,
compare_json,
Expand Down Expand Up @@ -45,7 +46,7 @@ def jurisdiction_ids(client: FlaskClient, election_id: str) -> List[str]:
)
},
)
assert json.loads(rv.data) == {"status": "ok"}
assert_ok(rv)
bgcompute_update_election_jurisdictions_file()
jurisdictions = (
Jurisdiction.query.filter_by(election_id=election_id)
Expand Down Expand Up @@ -124,7 +125,7 @@ def test_jurisdictions_list_with_manifest(
)
},
)
assert json.loads(rv.data) == {"status": "ok"}
assert_ok(rv)
assert bgcompute_update_ballot_manifest_file() == 1

rv = client.get(f"/election/{election_id}/jurisdiction")
Expand Down Expand Up @@ -184,7 +185,7 @@ def test_duplicate_batch_name(client, election_id, jurisdiction_ids):
)
},
)
assert json.loads(rv.data) == {"status": "ok"}
assert_ok(rv)

with pytest.raises(SQLAlchemyError):
bgcompute_update_ballot_manifest_file()
Expand Down Expand Up @@ -257,7 +258,7 @@ def test_jurisdictions_round_status(
f"/election/{election_id}/round",
{"roundNum": 1, "sampleSize": SAMPLE_SIZE},
)
assert rv.status_code == 200
assert_ok(rv)

rv = client.get(f"/election/{election_id}/jurisdiction")
jurisdictions = json.loads(rv.data)["jurisdictions"]
Expand Down Expand Up @@ -376,14 +377,14 @@ def test_jurisdictions_round_status_offline(
"state": USState.California,
}
rv = put_json(client, f"/election/{election_id}/settings", settings)
assert json.loads(rv.data) == {"status": "ok"}
assert_ok(rv)

rv = post_json(
client,
f"/election/{election_id}/round",
{"roundNum": 1, "sampleSize": SAMPLE_SIZE},
)
assert rv.status_code == 200
assert_ok(rv)

rv = client.get(f"/election/{election_id}/jurisdiction")
jurisdictions = json.loads(rv.data)["jurisdictions"]
Expand Down
4 changes: 2 additions & 2 deletions tests/routes_tests/test_new_election.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from arlo_server.auth import UserType
from arlo_server.routes import create_organization
from tests.helpers import create_org_and_admin, set_logged_in_user, post_json
from tests.helpers import assert_ok, create_org_and_admin, set_logged_in_user, post_json


def test_without_org_with_anonymous_user(client: FlaskClient):
Expand Down Expand Up @@ -222,7 +222,7 @@ def test_two_orgs_same_name(client: FlaskClient):

def test_election_reset(client, election_id):
rv = client.post(f"/election/{election_id}/audit/reset")
assert json.loads(rv.data) == {"status": "ok"}
assert_ok(rv)

rv = client.get(f"/election/{election_id}/audit/status")
status = json.loads(rv.data)
Expand Down
6 changes: 3 additions & 3 deletions tests/routes_tests/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from tests.helpers import post_json, create_election
from tests.helpers import assert_ok, post_json, create_election
from tests.test_app import setup_whole_audit, run_whole_audit_flow
import bgcompute

Expand Down Expand Up @@ -42,7 +42,7 @@ def run_audit_round(
f"/election/{election_id}/jurisdiction/{jurisdiction_id}/batch/{ballot['batch']['id']}/ballot/{ballot['position']}",
{"vote": vote, "comment": f"Comment for ballot {i}" if i % 3 == 0 else "",},
)
assert json.loads(rv.data)["status"] == "ok"
assert_ok(rv)

# The results won't be exact since we used a (seeded) random choice above.
# If we need exact results, we can always query the db or track results above.
Expand All @@ -63,7 +63,7 @@ def run_audit_round(
]
},
)
assert json.loads(rv.data)["status"] == "ok"
assert_ok(rv)

return len(ballot_list)

Expand Down
Loading

0 comments on commit 8b5cbe2

Please sign in to comment.