Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
remove asynctest
Browse files Browse the repository at this point in the history
  • Loading branch information
blankdots committed Dec 31, 2021
1 parent 2d065e7 commit aa4044f
Show file tree
Hide file tree
Showing 9 changed files with 473 additions and 775 deletions.
2 changes: 1 addition & 1 deletion beacon_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def main():
# sslcontext.check_hostname = False
web.run_app(
init(),
host=os.environ.get("HOST", "0.0.0.0"),
host=os.environ.get("HOST", "0.0.0.0"), # nosec
port=os.environ.get("PORT", "5050"),
shutdown_timeout=0,
ssl_context=None,
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ cyvcf2==0.30.14
uvloop==0.16.0
aiocache==0.11.1
ujson==5.1.0
aiomcache==0.6.0
Authlib==0.15.5
gunicorn==20.1.0
2 changes: 0 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
"uvloop==0.16.0",
"cyvcf2==0.30.14",
"aiocache==0.11.1",
"aiomcache==0.6.0",
"ujson==5.1.0",
],
extras_require={
Expand All @@ -63,7 +62,6 @@
"tox==3.24.4",
"flake8==4.0.1",
"flake8-docstrings==1.6.0",
"asynctest==0.13.0",
"aioresponses==0.7.2",
"black==21.12b0",
],
Expand Down
156 changes: 60 additions & 96 deletions tests/test_app.py

Large diffs are not rendered by default.

43 changes: 23 additions & 20 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import asynctest
import unittest
import aiohttp
from beacon_api.utils.db_load import parse_arguments, init_beacon_db, main
from beacon_api.conf.config import init_db_pool
Expand Down Expand Up @@ -70,7 +70,7 @@ async def load_datafile(self, vcf, datafile, datasetId, n=1000, min_ac=1):
return ["datasetId", "variants"]


class TestBasicFunctions(asynctest.TestCase):
class TestBasicFunctions(unittest.IsolatedAsyncioTestCase):
"""Test supporting functions."""

def setUp(self):
Expand All @@ -87,17 +87,17 @@ def test_parser(self):
self.assertEqual(parsed.datafile, "/path/to/datafile.csv")
self.assertEqual(parsed.metadata, "/path/to/metadata.json")

@asynctest.mock.patch("beacon_api.conf.config.asyncpg")
@unittest.mock.patch("beacon_api.conf.config.asyncpg")
async def test_init_pool(self, db_mock):
"""Test database connection pool creation."""
db_mock.return_value = asynctest.CoroutineMock(name="create_pool")
db_mock.create_pool = asynctest.CoroutineMock()
db_mock.return_value = unittest.mock.AsyncMock(name="create_pool")
db_mock.create_pool = unittest.mock.AsyncMock()
await init_db_pool()
db_mock.create_pool.assert_called()

@asynctest.mock.patch("beacon_api.utils.db_load.LOG")
@asynctest.mock.patch("beacon_api.utils.db_load.BeaconDB")
@asynctest.mock.patch("beacon_api.utils.db_load.VCF")
@unittest.mock.patch("beacon_api.utils.db_load.LOG")
@unittest.mock.patch("beacon_api.utils.db_load.BeaconDB")
@unittest.mock.patch("beacon_api.utils.db_load.VCF")
async def test_init_beacon_db(self, mock_vcf, db_mock, mock_log):
"""Test beacon_init db call."""
db_mock.return_value = MockBeaconDB()
Expand All @@ -112,9 +112,12 @@ async def test_init_beacon_db(self, mock_vcf, db_mock, mock_log):
data = """MOCK VCF file"""
datafile = self._dir.write("data.vcf", data.encode("utf-8"))
await init_beacon_db([datafile, metafile])
mock_log.info.mock_calls = ["Mark the database connection to be closed", "The database connection has been closed"]
mock_log.info.mock_calls = [
"Mark the database connection to be closed",
"The database connection has been closed",
]

@asynctest.mock.patch("beacon_api.utils.db_load.init_beacon_db")
@unittest.mock.patch("beacon_api.utils.db_load.init_beacon_db")
def test_main_db(self, mock_init):
"""Test run asyncio main beacon init."""
main()
Expand Down Expand Up @@ -318,7 +321,7 @@ def test_access_resolution_controlled_never_reached2(self):
with self.assertRaises(aiohttp.web_exceptions.HTTPForbidden):
access_resolution(request, token, host, [], [], ["8"])

@asynctest.mock.patch("beacon_api.permissions.ga4gh.validate_passport")
@unittest.mock.patch("beacon_api.permissions.ga4gh.validate_passport")
async def test_ga4gh_controlled(self, m_validation):
"""Test ga4gh permissions claim parsing."""
# Test: no passports, no permissions
Expand Down Expand Up @@ -396,9 +399,9 @@ async def test_ga4gh_bona_fide(self):
bona_fide_status = await get_ga4gh_bona_fide(passports_empty)
self.assertEqual(bona_fide_status, False) # doesn't have bona fide

@asynctest.mock.patch("beacon_api.permissions.ga4gh.get_jwk")
@asynctest.mock.patch("beacon_api.permissions.ga4gh.jwt")
@asynctest.mock.patch("beacon_api.permissions.ga4gh.LOG")
@unittest.mock.patch("beacon_api.permissions.ga4gh.get_jwk")
@unittest.mock.patch("beacon_api.permissions.ga4gh.jwt")
@unittest.mock.patch("beacon_api.permissions.ga4gh.LOG")
async def test_validate_passport(self, mock_log, m_jwt, m_jwk):
"""Test passport validation."""
m_jwk.return_value = "jwk"
Expand All @@ -414,7 +417,7 @@ async def test_validate_passport(self, mock_log, m_jwt, m_jwk):
# need to assert the log called
mock_log.error.assert_called_with("Something went wrong when processing JWT tokens: 1")

@asynctest.mock.patch("beacon_api.permissions.ga4gh.get_ga4gh_permissions")
@unittest.mock.patch("beacon_api.permissions.ga4gh.get_ga4gh_permissions")
async def test_check_ga4gh_token(self, m_get_perms):
"""Test token scopes."""
# Test: no scope found
Expand All @@ -441,10 +444,10 @@ async def test_decode_passport(self):
self.assertEqual(header.get("alg"), "HS256")
self.assertEqual(payload.get("iss"), "http://test.csc.fi")

@asynctest.mock.patch("beacon_api.permissions.ga4gh.get_ga4gh_bona_fide")
@asynctest.mock.patch("beacon_api.permissions.ga4gh.get_ga4gh_controlled")
@asynctest.mock.patch("beacon_api.permissions.ga4gh.decode_passport")
@asynctest.mock.patch("beacon_api.permissions.ga4gh.retrieve_user_data")
@unittest.mock.patch("beacon_api.permissions.ga4gh.get_ga4gh_bona_fide")
@unittest.mock.patch("beacon_api.permissions.ga4gh.get_ga4gh_controlled")
@unittest.mock.patch("beacon_api.permissions.ga4gh.decode_passport")
@unittest.mock.patch("beacon_api.permissions.ga4gh.retrieve_user_data")
async def test_get_ga4gh_permissions(self, m_userinfo, m_decode, m_controlled, m_bonafide):
"""Test GA4GH permissions main function."""
# Test: no data (nothing)
Expand Down Expand Up @@ -480,4 +483,4 @@ async def test_get_ga4gh_permissions(self, m_userinfo, m_decode, m_controlled, m


if __name__ == "__main__":
asynctest.main()
unittest.main()
197 changes: 28 additions & 169 deletions tests/test_data_query.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import asynctest
import aiohttp
import unittest
from unittest import mock
from beacon_api.utils.data_query import filter_exists, transform_record
from beacon_api.utils.data_query import transform_misses, transform_metadata, find_datasets, add_handover
from beacon_api.utils.data_query import fetch_datasets_access, fetch_dataset_metadata, fetch_filtered_dataset
from beacon_api.extensions.handover import make_handover
from datetime import datetime
from beacon_api.utils.data_query import handle_wildcard
from .test_db_load import Connection, ConnectionException


class Record:
Expand Down Expand Up @@ -72,17 +69,9 @@ def values(self):
return self.data.values()


class TestDataQueryFunctions(asynctest.TestCase):
class TestDataQueryFunctions(unittest.IsolatedAsyncioTestCase):
"""Test Data Query functions."""

def setUp(self):
"""Set up."""
pass

def tearDown(self):
"""Close database connection after tests."""
pass

def test_filter_exists(self):
"""Test filtering hits and miss datasets."""
datasets = [{"exists": True, "name": "DATASET1"}, {"exists": False, "name": "DATASET2"}]
Expand All @@ -107,7 +96,16 @@ def test_transform_record(self):
"variantCount": 3,
"variantType": "MNP",
}
record = Record("PUBLIC", 0.009112875989879, referenceBases="CT", alternateBases="AT", start=10, end=12, variantCount=3, variantType="MNP")
record = Record(
"PUBLIC",
0.009112875989879,
referenceBases="CT",
alternateBases="AT",
start=10,
end=12,
variantCount=3,
variantType="MNP",
)
result = transform_record(record)
self.assertEqual(result, response)

Expand All @@ -131,7 +129,11 @@ def test_transform_misses(self):

def test_transform_metadata(self):
"""Test transform medata record."""
response = {"createDateTime": "2018-10-20T20:33:40Z", "updateDateTime": "2018-10-20T20:33:40Z", "info": {"accessType": "PUBLIC"}}
response = {
"createDateTime": "2018-10-20T20:33:40Z",
"updateDateTime": "2018-10-20T20:33:40Z",
"info": {"accessType": "PUBLIC"},
}
record = Record(
"PUBLIC",
createDateTime=datetime.strptime("2018-10-20 20:33:40+00", "%Y-%m-%d %H:%M:%S+00"),
Expand All @@ -144,7 +146,15 @@ def test_add_handover(self):
"""Test that add handover."""
# Test that the handover actually is added
handovers = [{"handover1": "info"}, {"handover2": "url"}]
record = {"datasetId": "test", "referenceName": "22", "referenceBases": "A", "alternateBases": "C", "start": 10, "end": 11, "variantType": "SNP"}
record = {
"datasetId": "test",
"referenceName": "22",
"referenceBases": "A",
"alternateBases": "C",
"start": 10,
"end": 11,
"variantType": "SNP",
}
with mock.patch("beacon_api.extensions.handover.make_handover", return_value=handovers):
result = add_handover(record)
record["datasetHandover"] = handovers
Expand All @@ -159,7 +169,7 @@ def test_make_handover(self):
self.assertIn("path1", result[0]["url"])
self.assertEqual(result[0]["description"], "desc1")

@asynctest.mock.patch("beacon_api.utils.data_query.fetch_filtered_dataset")
@unittest.mock.patch("beacon_api.utils.data_query.fetch_filtered_dataset")
async def test_find_datasets(self, mock_filtered):
"""Test find datasets."""
mock_filtered.return_value = []
Expand All @@ -171,157 +181,6 @@ async def test_find_datasets(self, mock_filtered):
result_all = await find_datasets(None, "GRCh38", None, "Y", "T", "C", [], token, "ALL")
self.assertEqual(result_all, [])

async def test_datasets_access_call_public(self):
"""Test db call of getting public datasets access."""
pool = asynctest.CoroutineMock()
pool.acquire().__aenter__.return_value = Connection(accessData=[{"accesstype": "PUBLIC", "datasetid": "mock:public:id"}])
result = await fetch_datasets_access(pool, None)
# for now it can return a tuple of empty datasets
# in order to get a response we will have to mock it
# in Connection() class
self.assertEqual(result, (["mock:public:id"], [], []))

async def test_datasets_access_call_exception(self):
"""Test db call of getting public datasets access with exception."""
pool = asynctest.CoroutineMock()
pool.acquire().__aenter__.return_value = ConnectionException()
with self.assertRaises(aiohttp.web_exceptions.HTTPInternalServerError):
await fetch_datasets_access(pool, None)

async def test_datasets_access_call_registered(self):
"""Test db call of getting registered datasets access."""
pool = asynctest.CoroutineMock()
pool.acquire().__aenter__.return_value = Connection(accessData=[{"accesstype": "REGISTERED", "datasetid": "mock:registered:id"}])
result = await fetch_datasets_access(pool, None)
# for now it can return a tuple of empty datasets
# in order to get a response we will have to mock it
# in Connection() class
self.assertEqual(result, ([], ["mock:registered:id"], []))

async def test_datasets_access_call_controlled(self):
"""Test db call of getting controlled datasets access."""
pool = asynctest.CoroutineMock()
pool.acquire().__aenter__.return_value = Connection(accessData=[{"accesstype": "CONTROLLED", "datasetid": "mock:controlled:id"}])
result = await fetch_datasets_access(pool, None)
# for now it can return a tuple of empty datasets
# in order to get a response we will have to mock it
# in Connection() class
self.assertEqual(result, ([], [], ["mock:controlled:id"]))

async def test_datasets_access_call_multiple(self):
"""Test db call of getting controlled and public datasets access."""
pool = asynctest.CoroutineMock()
pool.acquire().__aenter__.return_value = Connection(
accessData=[{"accesstype": "CONTROLLED", "datasetid": "mock:controlled:id"}, {"accesstype": "PUBLIC", "datasetid": "mock:public:id"}]
)
result = await fetch_datasets_access(pool, None)
# for now it can return a tuple of empty datasets
# in order to get a response we will have to mock it
# in Connection() class
self.assertEqual(result, (["mock:public:id"], [], ["mock:controlled:id"]))

async def test_fetch_dataset_metadata_call(self):
"""Test db call of getting datasets metadata."""
pool = asynctest.CoroutineMock()
pool.acquire().__aenter__.return_value = Connection()
result = await fetch_dataset_metadata(pool, None, None)
# for now it can return empty dataset
# in order to get a response we will have to mock it
# in Connection() class
self.assertEqual(result, [])

async def test_fetch_dataset_metadata_call_exception(self):
"""Test db call of getting datasets metadata with exception."""
pool = asynctest.CoroutineMock()
pool.acquire().__aenter__.return_value = ConnectionException()
with self.assertRaises(aiohttp.web_exceptions.HTTPInternalServerError):
await fetch_dataset_metadata(pool, None, None)

async def test_fetch_filtered_dataset_call(self):
"""Test db call for retrieving main data."""
pool = asynctest.CoroutineMock()
db_response = {
"referenceBases": "",
"alternateBases": "",
"variantType": "",
"referenceName": "Chr38",
"frequency": 0,
"callCount": 0,
"sampleCount": 0,
"variantCount": 0,
"start": 0,
"end": 0,
"accessType": "PUBLIC",
"datasetId": "test",
}
pool.acquire().__aenter__.return_value = Connection(accessData=[db_response])
assembly_id = "GRCh38"
position = (10, 20, None, None, None, None)
chromosome = 1
reference = "A"
alternate = ("DUP", None)
result = await fetch_filtered_dataset(pool, assembly_id, position, chromosome, reference, alternate, None, None, False)
# for now it can return empty dataset
# in order to get a response we will have to mock it
# in Connection() class
expected = {
"referenceName": "Chr38",
"callCount": 0,
"sampleCount": 0,
"variantCount": 0,
"datasetId": "test",
"referenceBases": "",
"alternateBases": "",
"variantType": "",
"start": 0,
"end": 0,
"frequency": 0,
"info": {"accessType": "PUBLIC"},
"datasetHandover": [
{
"handoverType": {"id": "CUSTOM", "label": "Variants"},
"description": "browse the variants matched by the query",
"url": "https://examplebrowser.org/dataset/test/browser/variant/Chr38-1--",
},
{
"handoverType": {"id": "CUSTOM", "label": "Region"},
"description": "browse data of the region matched by the query",
"url": "https://examplebrowser.org/dataset/test/browser/region/Chr38-1-1",
},
{
"handoverType": {"id": "CUSTOM", "label": "Data"},
"description": "retrieve information of the datasets",
"url": "https://examplebrowser.org/dataset/test/browser",
},
],
}

self.assertEqual(result, [expected])

async def test_fetch_filtered_dataset_call_misses(self):
"""Test db call for retrieving miss data."""
pool = asynctest.CoroutineMock()
pool.acquire().__aenter__.return_value = Connection() # db_response is []
assembly_id = "GRCh38"
position = (10, 20, None, None, None, None)
chromosome = 1
reference = "A"
alternate = ("DUP", None)
result_miss = await fetch_filtered_dataset(pool, assembly_id, position, chromosome, reference, alternate, None, None, True)
self.assertEqual(result_miss, [])

async def test_fetch_filtered_dataset_call_exception(self):
"""Test db call of retrieving main data with exception."""
assembly_id = "GRCh38"
position = (10, 20, None, None, None, None)
chromosome = 1
reference = "A"
alternate = ("DUP", None)
pool = asynctest.CoroutineMock()
pool.acquire().__aenter__.return_value = ConnectionException()
with self.assertRaises(aiohttp.web_exceptions.HTTPInternalServerError):
await fetch_filtered_dataset(pool, assembly_id, position, chromosome, reference, alternate, None, None, False)

def test_handle_wildcard(self):
"""Test PostgreSQL wildcard handling."""
sequence1 = "ATCG"
Expand All @@ -333,4 +192,4 @@ def test_handle_wildcard(self):


if __name__ == "__main__":
asynctest.main()
unittest.main()
Loading

0 comments on commit aa4044f

Please sign in to comment.