Skip to content

Commit

Permalink
Merge pull request #93 from besfahbod/limit-1
Browse files Browse the repository at this point in the history
api/jsonapi: Add .limit() to Resource/Collection
  • Loading branch information
kbairak authored Dec 23, 2022
2 parents f329972 + 22bd459 commit 5fd1f97
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
30 changes: 30 additions & 0 deletions tests/api/test_queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,33 @@ def test_include():
item1, item2 = test_api.Item.list()
assert item1.tag.name == "tag1"
assert item2.tag.name == "tag2"


@responses.activate
def test_limit():
responses.add(
responses.GET,
"{}/items".format(host),
json={"data": payloads[1:5]},
match_querystring=True,
)
responses.add(
responses.GET,
"{}/items?limit=2".format(host),
json={"data": payloads[1:3]},
match_querystring=True,
)

all_items = test_api.Item.list()
limited_items = test_api.Item.limit(2)

assert len(all_items) == 4
assert len(limited_items) == 2

assert list(limited_items) == all_items[:2]

assert (
list(test_api.Item.limit(2))
== list(test_api.Item.list().limit(2))
== list(test_api.Item.list().limit(5).limit(2))
)
3 changes: 2 additions & 1 deletion transifex/api/jsonapi/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,13 @@ def page(self, *args, **kwargs):
def _param_method(param_name):
def _method(self, *fields):
params = dict(self._params)
params[param_name] = ",".join(fields)
params[param_name] = ",".join((str(field) for field in fields))
return self.__class__(self.API, self._url, params)

return _method

include = _param_method("include")
limit = _param_method("limit")
sort = _param_method("sort")
fields = _param_method("fields")

Expand Down
1 change: 1 addition & 0 deletions transifex/api/jsonapi/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ def _method(cls, *args, **kwargs):
filter = _collection_method("filter")
page = _collection_method("page")
include = _collection_method("include")
limit = _collection_method("limit")
sort = _collection_method("sort")
fields = _collection_method("fields")
extra = _collection_method("extra")
Expand Down

0 comments on commit 5fd1f97

Please sign in to comment.