Skip to content

Commit

Permalink
✅ Update pagination tests
Browse files Browse the repository at this point in the history
  • Loading branch information
znatty22 committed Aug 9, 2023
1 parent b77f12a commit 516859b
Showing 1 changed file with 54 additions and 51 deletions.
105 changes: 54 additions & 51 deletions tests/test_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@

pytest_plugins = ['tests.mocks']

DEFAULT_PAGE_LIMIT = 100
MAX_PAGE_LIMIT = 1000


class TestPagination:
"""
Expand All @@ -46,34 +49,34 @@ class TestPagination:
def participants(client):

# Add a bunch of studies for pagination
for i in range(101):
for i in range(MAX_PAGE_LIMIT + 1):
params = {
'external_id': f'Study_{i}',
'short_code': f'KF_{i}'
}
s = Study(**params)
db.session.add(s)

for i in range(101):
for i in range(MAX_PAGE_LIMIT + 1):
ca = CavaticaApp(name='app', revision=0)
db.session.add(ca)

# Add a bunch of study files
s0 = Study.query.filter_by(external_id='Study_0').one()
s1 = Study.query.filter_by(external_id='Study_1').one()
for i in range(101):
for i in range(MAX_PAGE_LIMIT+1):
sf = StudyFile(file_name='blah', study_id=s0.kf_id)
db.session.add(sf)

# Add a bunch of investigators
for _ in range(102):
for _ in range(MAX_PAGE_LIMIT + 2):
inv = Investigator(name='test')
inv.studies.extend([s0, s1])
db.session.add(inv)

# Add a bunch of families
families = []
for i in range(101):
for i in range(MAX_PAGE_LIMIT + 1):
families.append(Family(external_id='Family_{}'.format(i)))
db.session.add_all(families)
db.session.flush()
Expand All @@ -82,9 +85,9 @@ def participants(client):
f0 = Family.query.filter_by(external_id='Family_0').one()
f1 = Family.query.filter_by(external_id='Family_1').one()
seq_cen = None
for i in range(102):
f = f0 if i < 50 else f1
s = s0 if i < 50 else s1
for i in range(MAX_PAGE_LIMIT + 2):
f = f0 if i < int(MAX_PAGE_LIMIT/2) else f1
s = s0 if i < int(MAX_PAGE_LIMIT/2) else s1
data = {
'external_id': "test",
'is_proband': True,
Expand Down Expand Up @@ -168,26 +171,26 @@ def participants(client):
db.session.commit()

@pytest.mark.parametrize('endpoint, expected_total', [
('/participants', 50),
('/study-files', 101),
('/participants', int(MAX_PAGE_LIMIT/2)),
('/study-files', MAX_PAGE_LIMIT+1),
('/investigators', 1),
('/biospecimens', 50),
('/sequencing-experiments', 50),
('/diagnoses', 50),
('/outcomes', 50),
('/phenotypes', 50),
('/biospecimens', int(MAX_PAGE_LIMIT/2)),
('/sequencing-experiments', int(MAX_PAGE_LIMIT/2)),
('/diagnoses', int(MAX_PAGE_LIMIT/2)),
('/outcomes', int(MAX_PAGE_LIMIT/2)),
('/phenotypes', int(MAX_PAGE_LIMIT/2)),
('/families', 1),
('/family-relationships', 50),
('/genomic-files', 50),
('/read-groups', 50),
('/family-relationships', int(MAX_PAGE_LIMIT/2)),
('/genomic-files', int(MAX_PAGE_LIMIT/2)),
('/read-groups', int(MAX_PAGE_LIMIT/2)),
('/sequencing-centers', 1),
('/cavatica-apps', 1),
('/tasks', 50),
('/task-genomic-files', 50),
('/read-group-genomic-files', 50),
('/sequencing-experiment-genomic-files', 50),
('/biospecimen-genomic-files', 50),
('/biospecimen-diagnoses', 50)
('/tasks', int(MAX_PAGE_LIMIT/2)),
('/task-genomic-files', int(MAX_PAGE_LIMIT/2)),
('/read-group-genomic-files', int(MAX_PAGE_LIMIT/2)),
('/sequencing-experiment-genomic-files', int(MAX_PAGE_LIMIT/2)),
('/biospecimen-genomic-files', int(MAX_PAGE_LIMIT/2)),
('/biospecimen-diagnoses', int(MAX_PAGE_LIMIT/2))
])
def test_study_filter(self, client, participants,
endpoint, expected_total):
Expand All @@ -198,8 +201,8 @@ def test_study_filter(self, client, participants,
endpoint = '{}?study_id={}'.format(endpoint, s.kf_id)
resp = client.get(endpoint)
resp = json.loads(resp.data.decode('utf-8'))
assert len(resp['results']) == min(expected_total, 10)
assert resp['limit'] == 10
assert len(resp['results']) == min(expected_total, DEFAULT_PAGE_LIMIT)
assert resp['limit'] == DEFAULT_PAGE_LIMIT
assert resp['total'] == expected_total

ids_seen = []
Expand Down Expand Up @@ -233,7 +236,7 @@ def test_non_exist_study_filter(self, client, participants,
resp = json.loads(resp.data.decode('utf-8'))

assert len(resp['results']) == 0
assert resp['limit'] == 10
assert resp['limit'] == DEFAULT_PAGE_LIMIT
assert resp['total'] == 0

@pytest.mark.parametrize('study_id', ['blah', 3489, 'PT_00001111'])
Expand Down Expand Up @@ -261,32 +264,32 @@ def test_invalid_study_filter(self, client, participants,
assert 'study_id' in resp['_status']['message']

@pytest.mark.parametrize('endpoint, expected_total', [
('/studies', 101),
('/investigators', 102),
('/participants', 102),
('/outcomes', 102),
('/phenotypes', 102),
('/diagnoses', 102),
('/family-relationships', 101),
('/study-files', 101),
('/families', 101),
('/read-groups', 102),
('/studies', MAX_PAGE_LIMIT+1),
('/investigators', MAX_PAGE_LIMIT+2),
('/participants', MAX_PAGE_LIMIT+2),
('/outcomes', MAX_PAGE_LIMIT+2),
('/phenotypes', MAX_PAGE_LIMIT+2),
('/diagnoses', MAX_PAGE_LIMIT+2),
('/family-relationships', MAX_PAGE_LIMIT+1),
('/study-files', MAX_PAGE_LIMIT+1),
('/families', MAX_PAGE_LIMIT+1),
('/read-groups', MAX_PAGE_LIMIT+2),
('/sequencing-centers', 1),
('/cavatica-apps', 101),
('/tasks', 102),
('/task-genomic-files', 102),
('/read-group-genomic-files', 102),
('/sequencing-experiment-genomic-files', 102),
('/biospecimen-genomic-files', 102),
('/biospecimen-diagnoses', 102)
('/cavatica-apps', MAX_PAGE_LIMIT+1),
('/tasks', MAX_PAGE_LIMIT+2),
('/task-genomic-files', MAX_PAGE_LIMIT+2),
('/read-group-genomic-files', MAX_PAGE_LIMIT+2),
('/sequencing-experiment-genomic-files', MAX_PAGE_LIMIT+2),
('/biospecimen-genomic-files', MAX_PAGE_LIMIT+2),
('/biospecimen-diagnoses', MAX_PAGE_LIMIT+2)
])
def test_pagination(self, client, participants, endpoint, expected_total):
""" Test pagination of resource """
resp = client.get(endpoint)
resp = json.loads(resp.data.decode('utf-8'))

assert len(resp['results']) == min(expected_total, 10)
assert resp['limit'] == 10
assert len(resp['results']) == min(expected_total, DEFAULT_PAGE_LIMIT)
assert resp['limit'] == DEFAULT_PAGE_LIMIT
assert resp['total'] == expected_total

ids_seen = []
Expand All @@ -311,7 +314,7 @@ def test_same_created_at(self, client):
"""
created_at = datetime.now()
studies = [Study(external_id=f'Study_{i}', short_code=f'KF-ST{i}')
for i in range(50)]
for i in range(int(MAX_PAGE_LIMIT/2))]
db.session.add_all(studies)
db.session.flush()
for study in studies:
Expand Down Expand Up @@ -343,21 +346,21 @@ def test_limit(self, client, participants, endpoint):
assert len(response['results']) == 5
assert response['limit'] == 5

response = client.get(endpoint + '?limit=200')
response = client.get(endpoint + f'?limit={MAX_PAGE_LIMIT * 2}')
response = json.loads(response.data.decode('utf-8'))
if '/sequencing-centers' in endpoint:
assert len(response['results']) == 1
else:
assert len(response['results']) == 100
assert len(response['results']) == MAX_PAGE_LIMIT

# Check unexpected limit param uses default
response = client.get(endpoint + '?limit=dog')
response = json.loads(response.data.decode('utf-8'))
if '/sequencing-centers' in endpoint:
assert len(response['results']) == 1
else:
assert len(response['results']) == 10
assert response['limit'] == 10
assert len(response['results']) == DEFAULT_PAGE_LIMIT
assert response['limit'] == DEFAULT_PAGE_LIMIT

@pytest.mark.parametrize('endpoint', [
(ept) for ept in ENDPOINTS
Expand Down

0 comments on commit 516859b

Please sign in to comment.