Skip to content
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

✨ Add pagination to resources #174

Merged
merged 3 commits into from
Mar 9, 2018
Merged
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
9 changes: 6 additions & 3 deletions dataservice/api/demographic/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from marshmallow import ValidationError

from dataservice.extensions import db
from dataservice.api.common.pagination import paginated, Pagination
from dataservice.api.demographic.models import Demographic
from dataservice.api.demographic.schemas import DemographicSchema
from dataservice.api.common.views import CRUDView
Expand All @@ -16,7 +17,8 @@ class DemographicListAPI(CRUDView):
rule = '/demographics'
schemas = {'Demographic': DemographicSchema}

def get(self):
@paginated
def get(self, after, limit):
"""
Get all demographics
---
Expand All @@ -27,8 +29,9 @@ def get(self):
resource:
Demographic
"""
d = Demographic.query.all()
return DemographicSchema(many=True).jsonify(d)
q = Demographic.query
return (DemographicSchema(many=True)
.jsonify(Pagination(q, after, limit)))

def post(self):
"""
Expand Down
1 change: 1 addition & 0 deletions dataservice/api/demographic/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class DemographicSchema(BaseSchema):
load_only=True)

class Meta(BaseSchema.Meta):
resource_url = 'api.demographics_list'
model = Demographic

_links = ma.Hyperlinks({
Expand Down
10 changes: 7 additions & 3 deletions dataservice/api/diagnosis/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from marshmallow import ValidationError

from dataservice.extensions import db
from dataservice.api.common.pagination import paginated, Pagination
from dataservice.api.diagnosis.models import Diagnosis
from dataservice.api.diagnosis.schemas import DiagnosisSchema
from dataservice.api.common.views import CRUDView
Expand All @@ -16,7 +17,8 @@ class DiagnosisListAPI(CRUDView):
rule = '/diagnoses'
schemas = {'Diagnosis': DiagnosisSchema}

def get(self):
@paginated
def get(self, after, limit):
"""
Get all diagnoses
---
Expand All @@ -28,8 +30,10 @@ def get(self):
resource:
Diagnosis
"""
d = Diagnosis.query.all()
return DiagnosisSchema(many=True).jsonify(d)
q = Diagnosis.query

return (DiagnosisSchema(many=True)
.jsonify(Pagination(q, after, limit)))

def post(self):
"""
Expand Down
1 change: 1 addition & 0 deletions dataservice/api/diagnosis/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class DiagnosisSchema(BaseSchema):

class Meta(BaseSchema.Meta):
model = Diagnosis
resource_url = 'api.diagnoses_list'

_links = ma.Hyperlinks({
'self': ma.URLFor('api.diagnoses', kf_id='<kf_id>'),
Expand Down
10 changes: 7 additions & 3 deletions dataservice/api/sample/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from marshmallow import ValidationError

from dataservice.extensions import db
from dataservice.api.common.pagination import paginated, Pagination
from dataservice.api.sample.models import Sample
from dataservice.api.sample.schemas import SampleSchema
from dataservice.api.common.views import CRUDView
Expand All @@ -16,7 +17,8 @@ class SampleListAPI(CRUDView):
rule = '/samples'
schemas = {'Sample': SampleSchema}

def get(self):
@paginated
def get(self, after, limit):
"""
Get all samples
---
Expand All @@ -28,8 +30,10 @@ def get(self):
resource:
Sample
"""
s = Sample.query.all()
return SampleSchema(many=True).jsonify(s)
q = Sample.query

return (SampleSchema(many=True)
.jsonify(Pagination(q, after, limit)))

def post(self):
"""
Expand Down
1 change: 1 addition & 0 deletions dataservice/api/sample/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class SampleSchema(BaseSchema):

class Meta(BaseSchema.Meta):
model = Sample
resource_url = 'api.samples_list'

_links = ma.Hyperlinks({
'self': ma.URLFor('api.samples', kf_id='<kf_id>'),
Expand Down
22 changes: 21 additions & 1 deletion tests/test_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

from dataservice.extensions import db
from dataservice.api.participant.models import Participant
from dataservice.api.demographic.models import Demographic
from dataservice.api.sample.models import Sample
from dataservice.api.diagnosis.models import Diagnosis
from dataservice.api.study.models import Study


Expand All @@ -21,11 +24,20 @@ def participants(client):
p = Participant(external_id="test",
study_id=s.kf_id,
is_proband=True)
d = Demographic(race='cat')
p.demographic = d
samp = Sample()
p.samples = [samp]
diag = Diagnosis()
p.diagnoses = [diag]
db.session.add(p)
db.session.commit()

@pytest.mark.parametrize('endpoint', [
('/participants'),
('/demographics'),
('/samples'),
('/diagnoses'),
])
def test_pagination(self, client, participants, endpoint):
""" Test pagination of resource """
Expand All @@ -51,10 +63,12 @@ def test_pagination(self, client, participants, endpoint):
ids_seen.extend([r['kf_id'] for r in resp['results']])

assert len(ids_seen) == resp['total']
assert set(ids_seen) == {p.kf_id for p in Participant.query.all()}

@pytest.mark.parametrize('endpoint', [
('/participants'),
('/demographics'),
('/samples'),
('/diagnoses'),
])
def test_limit(self, client, participants, endpoint):
# Check that limit param operates correctly
Expand All @@ -75,6 +89,9 @@ def test_limit(self, client, participants, endpoint):

@pytest.mark.parametrize('endpoint', [
('/participants'),
('/demographics'),
('/samples'),
('/diagnoses'),
])
def test_after(self, client, participants, endpoint):
""" Test `after` offeset paramater """
Expand All @@ -100,6 +117,9 @@ def test_after(self, client, participants, endpoint):

@pytest.mark.parametrize('endpoint', [
('/participants'),
('/demographics'),
('/samples'),
('/diagnoses'),
])
def test_self(self, client, participants, endpoint):
""" Test that the self link gives the same page """
Expand Down