Skip to content

Don't persist fixtures in test database between tests #2595

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
28 changes: 16 additions & 12 deletions codelists/tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from opencodelists.tests.assertions import assert_difference, assert_no_difference


def test_create_codelist(organisation):
def test_create_codelist(snomedct_data, organisation):
cl = actions.create_old_style_codelist(
owner=organisation,
name="Test Codelist",
Expand All @@ -34,7 +34,7 @@ def test_create_codelist(organisation):
assert clv.is_under_review


def test_create_codelist_for_user(user):
def test_create_codelist_for_user(snomedct_data, user):
cl = actions.create_old_style_codelist(
owner=user,
name="Test Codelist",
Expand All @@ -56,7 +56,7 @@ def test_create_codelist_for_user(user):
assert "whilst swimming" in clv.csv_data


def test_create_codelist_with_duplicate_name(organisation):
def test_create_codelist_with_duplicate_name(snomedct_data, organisation):
actions.create_old_style_codelist(
owner=organisation,
name="Test",
Expand All @@ -81,7 +81,9 @@ def test_create_codelist_with_duplicate_name(organisation):
assert Codelist.objects.filter(handles__name="Test").count() == 1


def test_create_codelist_with_codes(user, disorder_of_elbow_excl_arthritis_codes):
def test_create_codelist_with_codes(
snomedct_data, user, disorder_of_elbow_excl_arthritis_codes
):
cl = actions.create_codelist_with_codes(
owner=user,
name="Test",
Expand Down Expand Up @@ -110,7 +112,7 @@ def test_create_codelist_with_codes(user, disorder_of_elbow_excl_arthritis_codes


def test_create_codelist_with_codes_with_metadata(
user, disorder_of_elbow_excl_arthritis_codes
snomedct_data, user, disorder_of_elbow_excl_arthritis_codes
):
cl = actions.create_codelist_with_codes(
owner=user,
Expand All @@ -129,7 +131,9 @@ def test_create_codelist_with_codes_with_metadata(
assert cl.signoffs.count() == 1


def test_create_or_update_codelist_create(user, disorder_of_elbow_excl_arthritis_codes):
def test_create_or_update_codelist_create(
snomedct_data, user, disorder_of_elbow_excl_arthritis_codes
):
cl = actions.create_or_update_codelist(
owner=user,
name="Test",
Expand All @@ -151,15 +155,15 @@ def test_create_or_update_codelist_create(user, disorder_of_elbow_excl_arthritis


def test_create_or_update_codelist_update(
organisation, codelist, disorder_of_elbow_excl_arthritis_codes
organisation, codelist, disorder_of_elbow_codes
):
with assert_difference(codelist.versions.count, expected_difference=1):
actions.create_or_update_codelist(
owner=organisation,
name=codelist.name,
coding_system_id="snomedct",
coding_system_database_alias=most_recent_database_alias("snomedct"),
codes=disorder_of_elbow_excl_arthritis_codes,
codes=disorder_of_elbow_codes,
description="This is a test (updated)",
methodology="This is how we did it (updated)",
)
Expand All @@ -168,19 +172,19 @@ def test_create_or_update_codelist_update(
assert codelist.description == "This is a test (updated)"
assert codelist.methodology == "This is how we did it (updated)"
clv = codelist.versions.order_by("id").last()
assert clv.codes == tuple(sorted(disorder_of_elbow_excl_arthritis_codes))
assert clv.codes == tuple(sorted(disorder_of_elbow_codes))


def test_create_or_update_codelist_update_no_change_to_codes(
organisation, codelist, disorder_of_elbow_codes
organisation, codelist, disorder_of_elbow_excl_arthritis_codes
):
with assert_no_difference(codelist.versions.count):
actions.create_or_update_codelist(
owner=organisation,
name=codelist.name,
coding_system_id="snomedct",
coding_system_database_alias=most_recent_database_alias("snomedct"),
codes=disorder_of_elbow_codes,
codes=disorder_of_elbow_excl_arthritis_codes,
description="This is a test (updated)",
methodology="This is how we did it (updated)",
)
Expand Down Expand Up @@ -476,7 +480,7 @@ def test_publish(version_under_review):
assert version_under_review.is_published


def test_delete_version(old_style_codelist):
def test_delete_version(old_style_codelist, old_style_version):
# Verify that delete_version deletes the given version, and that when the last
# remaining version is deleted, the codelist is too.

Expand Down
27 changes: 21 additions & 6 deletions codelists/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
from opencodelists.tests.assertions import assert_difference, assert_no_difference


def test_codelists_get(client, organisation):
def test_codelists_get(
client,
organisation,
many_organisation_versions,
):
rsp = client.get(
f"/api/v1/codelist/{organisation.slug}/?description&methodology&references"
)
Expand Down Expand Up @@ -238,7 +242,12 @@ def test_codelists_get(client, organisation):
]


def test_codelists_get_all(client, organisation, organisation_user):
def test_codelists_get_all(
client,
organisation,
organisation_user,
many_organisation_and_user_codelists,
):
rsp = client.get("/api/v1/codelist/?include-users")
data = json.loads(rsp.content)
assert rsp.status_code == 200
Expand All @@ -254,7 +263,9 @@ def test_codelists_get_all(client, organisation, organisation_user):
assert {cl["user"] for cl in user_codelists} == {organisation_user.username}


def test_codelists_get_with_coding_system_id(client, organisation):
def test_codelists_get_with_coding_system_id(
client, organisation, many_organisation_codelists
):
rsp = client.get(f"/api/v1/codelist/{organisation.slug}/?coding_system_id=snomedct")
data = json.loads(rsp.content)
assert len(data["codelists"]) == 4
Expand Down Expand Up @@ -297,7 +308,9 @@ def test_codelists_get_exclude_previous_owner(
assert len(data["codelists"]) == 0


def test_codelists_get_with_no_organisation(client, organisation):
def test_codelists_get_with_no_organisation(
client, organisation, many_organisation_and_user_codelists
):
rsp = client.get("/api/v1/codelist/?coding_system_id=snomedct&include-users")
data = json.loads(rsp.content)
user_codelists = [
Expand All @@ -308,13 +321,15 @@ def test_codelists_get_with_no_organisation(client, organisation):
assert user_codelist["organisation"] == ""


def test_codelists_get_with_tag(client, universe):
def test_codelists_get_with_tag(client, many_organisation_and_user_codelists):
rsp = client.get("/api/v1/codelist/?tag=new-style")
data = json.loads(rsp.content)
assert len(data["codelists"]) == 2


def test_codelists_get_with_tag_and_include_users(client, universe):
def test_codelists_get_with_tag_and_include_users(
client, many_organisation_and_user_codelists
):
rsp = client.get("/api/v1/codelist/?tag=new-style&include-users")
data = json.loads(rsp.content)
assert len(data["codelists"]) == 4
Expand Down
1 change: 1 addition & 0 deletions codelists/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ def test_new_style_is_new_style(new_style_codelist):

def test_visible_versions_user_has_edit_permissions(
new_style_codelist,
version_with_complete_searches,
user,
):
assert len(new_style_codelist.visible_versions(user)) == 3
Expand Down
8 changes: 5 additions & 3 deletions codelists/tests/views/test_codelist_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_post_unauthorised_for_user(client, user):
assert_post_unauthorised(client, user.get_codelist_create_url())


def test_get_for_organisation(client, organisation):
def test_get_for_organisation(client, organisation, organisation_user):
force_login(organisation, client)
response = client.get(organisation.get_codelist_create_url())
assert response.status_code == 200
Expand Down Expand Up @@ -90,7 +90,7 @@ def test_post_success(client, organisation, user, bnf_data):
assert signoff.user == user


def test_post_invalid(client, organisation):
def test_post_invalid(client, organisation, organisation_user):
force_login(organisation, client)

csv_data = "code,description\n0301012A0AA,Adrenaline (Asthma)"
Expand Down Expand Up @@ -126,7 +126,9 @@ def test_post_invalid(client, organisation):
assert response.context_data["signoff_formset"].errors


def test_post_with_duplicate_name(client, organisation):
def test_post_with_duplicate_name(
client, organisation, organisation_user, bnf_codelist
):
force_login(organisation, client)

csv_data = "code,description\n0301012A0AA,Adrenaline (Asthma)"
Expand Down
6 changes: 4 additions & 2 deletions codelists/tests/views/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_search_only_returns_codelists_with_published_versions(
assert codelist.slug == "new-style-codelist"


def test_paginate_codelists(client, organisation, create_codelists):
def test_paginate_codelists(client, organisation, codelist, create_codelists):
# Create enough published codelists to paginate (codelist index page is paginated by 15)
create_codelists(30, owner=organisation, status=Status.PUBLISHED)
published_for_organisation = [
Expand Down Expand Up @@ -95,7 +95,9 @@ def test_under_review_index(
assert version.status == "under review"


def test_paginate_under_review_versions(client, organisation, create_codelists):
def test_paginate_under_review_versions(
client, organisation, version_under_review, create_codelists
):
# Create enough published codelists to paginate (under-review index page is paginated by 30)
create_codelists(40, status=Status.UNDER_REVIEW, owner=organisation)
under_review_for_organisation = flatten(
Expand Down
2 changes: 1 addition & 1 deletion codelists/tests/views/test_version_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_post_unauthorised(client, version):
assert_post_unauthorised(client, version.get_publish_url())


def test_post_success(client, old_style_codelist):
def test_post_success(client, organisation_user, old_style_codelist, old_style_version):
codelist = old_style_codelist
version1, version2 = codelist.versions.order_by("id")
force_login(codelist, client)
Expand Down
6 changes: 3 additions & 3 deletions codelists/tests/views/test_version_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ def test_post_unauthorised(client, old_style_codelist):
assert_post_unauthorised(client, old_style_codelist.get_version_upload_url())


def test_get_success(client, old_style_codelist):
def test_get_success(client, organisation_user, old_style_codelist):
force_login(old_style_codelist, client)
response = client.get(old_style_codelist.get_version_upload_url())
form = response.context_data["form"]
assert form.fields["coding_system_id"].initial == "snomedct"


def test_post_success(client, old_style_codelist):
def test_post_success(client, organisation_user, old_style_codelist):
force_login(old_style_codelist, client)

csv_data = "code,description\n73583000,Epicondylitis (disorder)"
Expand All @@ -47,7 +47,7 @@ def test_post_success(client, old_style_codelist):
assert response.url == clv.get_absolute_url()


def test_post_missing_field(client, old_style_codelist):
def test_post_missing_field(client, organisation_user, old_style_codelist):
force_login(old_style_codelist, client)

with assert_no_difference(old_style_codelist.versions.count):
Expand Down
7 changes: 7 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ def reset_connections():
del connections.databases[db]


@pytest.fixture(autouse=True)
def fast_password_hasher(settings):
settings.PASSWORD_HASHERS = [
"django.contrib.auth.hashers.MD5PasswordHasher",
]


################################################################################################################
# Setup global tracing infrastructure for testing purposes.

Expand Down
Loading
Loading