Skip to content

Commit

Permalink
test: move covid genome to fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlougheed committed Aug 26, 2024
1 parent 4d1f826 commit 9e4fe45
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 38 deletions.
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
from bento_reference_service.logger import get_logger
from bento_reference_service.main import app

from .shared_data import TEST_GENOME_SARS_COV_2
from .shared_functions import create_genome_with_permissions


@pytest.fixture()
def config() -> Config:
Expand Down Expand Up @@ -99,3 +102,9 @@ def test_client(db: Database):
def aioresponse():
with aioresponses() as m:
yield m


@pytest.fixture
def sars_cov_2_genome(test_client: TestClient, aioresponse: aioresponses, db_cleanup):
create_genome_with_permissions(test_client, aioresponse, TEST_GENOME_SARS_COV_2)
return TEST_GENOME_SARS_COV_2
10 changes: 2 additions & 8 deletions tests/test_genome_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,7 @@ async def test_genome_create(test_client: TestClient, aioresponse: aioresponses,
assert len(res.json()) == 2


async def test_genome_detail_endpoints(test_client: TestClient, aioresponse: aioresponses, db_cleanup):
# setup: create genome TODO: fixture
create_covid_genome_with_permissions(test_client, aioresponse)

async def test_genome_detail_endpoints(test_client: TestClient, sars_cov_2_genome):
# tests

res = test_client.get(f"/genomes/{SARS_COV_2_GENOME_ID}")
Expand Down Expand Up @@ -194,10 +191,7 @@ async def test_genome_without_gff3_and_then_patch(test_client: TestClient, aiore
assert res.status_code == status.HTTP_200_OK


async def test_genome_delete(test_client: TestClient, aioresponse: aioresponses, db_cleanup):
# setup: create genome TODO: fixture
create_covid_genome_with_permissions(test_client, aioresponse)

async def test_genome_delete(test_client: TestClient, sars_cov_2_genome, aioresponse: aioresponses):
aioresponse.post("https://authz.local/policy/evaluate", payload={"result": [[True]]})
res = test_client.delete(f"/genomes/{SARS_COV_2_GENOME_ID}", headers=AUTHORIZATION_HEADER)
assert res.status_code == status.HTTP_204_NO_CONTENT
Expand Down
43 changes: 13 additions & 30 deletions tests/test_refget.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import pysam

from aioresponses import aioresponses
from fastapi import status
from fastapi.testclient import TestClient

from .shared_data import SARS_COV_2_FASTA_PATH, TEST_GENOME_SARS_COV_2
from .shared_functions import create_genome_with_permissions
from .shared_data import SARS_COV_2_FASTA_PATH


REFGET_2_0_0_TYPE = {"group": "org.ga4gh", "artifact": "refget", "version": "2.0.0"}
Expand Down Expand Up @@ -38,10 +36,8 @@ def test_refget_sequence_not_found(test_client: TestClient, db_cleanup):
assert res.status_code == status.HTTP_404_NOT_FOUND


def test_refget_sequence_invalid_requests(test_client: TestClient, aioresponse: aioresponses, db_cleanup):
# TODO: fixture
create_genome_with_permissions(test_client, aioresponse, TEST_GENOME_SARS_COV_2)
test_contig = TEST_GENOME_SARS_COV_2["contigs"][0]
def test_refget_sequence_invalid_requests(test_client: TestClient, sars_cov_2_genome):
test_contig = sars_cov_2_genome["contigs"][0]
seq_url = f"/sequence/{test_contig['md5']}"

# ------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -86,11 +82,8 @@ def test_refget_sequence_invalid_requests(test_client: TestClient, aioresponse:
assert res.content == b"Range Not Satisfiable"


def test_refget_sequence_full(test_client: TestClient, aioresponse: aioresponses, db_cleanup):
# TODO: fixture
create_genome_with_permissions(test_client, aioresponse, TEST_GENOME_SARS_COV_2)

test_contig = TEST_GENOME_SARS_COV_2["contigs"][0]
def test_refget_sequence_full(test_client: TestClient, sars_cov_2_genome):
test_contig = sars_cov_2_genome["contigs"][0]

# Load COVID contig bytes
rf = pysam.FastaFile(str(SARS_COV_2_FASTA_PATH))
Expand All @@ -115,11 +108,8 @@ def test_refget_sequence_full(test_client: TestClient, aioresponse: aioresponses
assert res.content == seq


def test_refget_sequence_partial(test_client, aioresponse: aioresponses, db_cleanup):
# TODO: fixture
create_genome_with_permissions(test_client, aioresponse, TEST_GENOME_SARS_COV_2)

test_contig = TEST_GENOME_SARS_COV_2["contigs"][0]
def test_refget_sequence_partial(test_client, sars_cov_2_genome):
test_contig = sars_cov_2_genome["contigs"][0]
seq_url = f"/sequence/{test_contig['md5']}"

# Load COVID contig bytes
Expand Down Expand Up @@ -164,10 +154,8 @@ def _check_first_10(r, sc, ar="none"):
assert res.content == seq[-10:]


def test_refget_metadata(test_client: TestClient, aioresponse: aioresponses, db_cleanup):
# TODO: fixture
create_genome_with_permissions(test_client, aioresponse, TEST_GENOME_SARS_COV_2)
test_contig = TEST_GENOME_SARS_COV_2["contigs"][0]
def test_refget_metadata(test_client: TestClient, sars_cov_2_genome):
test_contig = sars_cov_2_genome["contigs"][0]
seq_m_url = f"/sequence/{test_contig['md5']}/metadata"

# ------------------------------------------------------------------------------------------------------------------
Expand All @@ -185,17 +173,12 @@ def test_refget_metadata(test_client: TestClient, aioresponse: aioresponses, db_
}


def test_refget_metadata_errors(test_client: TestClient, aioresponse: aioresponses, db_cleanup):
# TODO: fixture
create_genome_with_permissions(test_client, aioresponse, TEST_GENOME_SARS_COV_2)
test_contig = TEST_GENOME_SARS_COV_2["contigs"][0]
seq_m_url = f"/sequence/{test_contig['md5']}/metadata"

# ------------------------------------------------------------------------------------------------------------------

res = test_client.get(seq_m_url, headers=HEADERS_ACCEPT_PLAIN)
def test_refget_metadata_406(test_client: TestClient, sars_cov_2_genome):
res = test_client.get(f"/sequence/{sars_cov_2_genome['contigs'][0]['md5']}/metadata", headers=HEADERS_ACCEPT_PLAIN)
assert res.status_code == status.HTTP_406_NOT_ACCEPTABLE


def test_refget_metadata_404(test_client: TestClient):
res = test_client.get("/sequence/does-not-exist/metadata")
# TODO: proper content type for exception - RefGet error class?
assert res.status_code == status.HTTP_404_NOT_FOUND

0 comments on commit 9e4fe45

Please sign in to comment.