Return empty array for skip When skip is more than total rows, we return an empty array rather than throw an error.
Fixes: COUCHDB-2652 Project: http://git-wip-us.apache.org/repos/asf/couchdb-mango/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-mango/commit/d128f16d Tree: http://git-wip-us.apache.org/repos/asf/couchdb-mango/tree/d128f16d Diff: http://git-wip-us.apache.org/repos/asf/couchdb-mango/diff/d128f16d Branch: refs/heads/2652-index-pagination Commit: d128f16dd738734e466f763ae6fc805d08e50544 Parents: f6f517b Author: Tony Sun <[email protected]> Authored: Mon Apr 20 10:06:07 2015 -0700 Committer: Tony Sun <[email protected]> Committed: Mon Apr 20 10:06:07 2015 -0700 ---------------------------------------------------------------------- src/mango_error.erl | 2 +- src/mango_httpd.erl | 5 ++++- test/01-index-crud-test.py | 6 +----- 3 files changed, 6 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/d128f16d/src/mango_error.erl ---------------------------------------------------------------------- diff --git a/src/mango_error.erl b/src/mango_error.erl index 7832b82..dfd4bd7 100644 --- a/src/mango_error.erl +++ b/src/mango_error.erl @@ -81,7 +81,7 @@ info(mango_httpd, invalid_list_index_params) -> { 500, <<"invalid_list_index_params">>, - <<"Index parameter ranges: limit > 1, 0 <= skip <= total indexes" >> + <<"Index parameter ranges: limit > 1, skip > 0" >> }; info(mango_idx, {invalid_index_type, BadType}) -> http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/d128f16d/src/mango_httpd.erl ---------------------------------------------------------------------- diff --git a/src/mango_httpd.erl b/src/mango_httpd.erl index 2ccc7f3..3d98535 100644 --- a/src/mango_httpd.erl +++ b/src/mango_httpd.erl @@ -54,6 +54,7 @@ handle_index_req(#httpd{method='GET', path_parts=[_, _]}=Req, Db) -> chttpd:qs(Req)), Idxs = lists:sort(mango_idx:list(Db)), JsonIdxs0 = lists:map(fun mango_idx:to_json/1, Idxs), + TotalRows = length(JsonIdxs0) , Limit = case couch_util:get_value(limit, Params, 10000000000) of Limit0 when Limit0 < 1 -> ?MANGO_ERROR(invalid_list_index_params); @@ -61,8 +62,10 @@ handle_index_req(#httpd{method='GET', path_parts=[_, _]}=Req, Db) -> Limit0 end, Skip = case couch_util:get_value(skip, Params, 0) of - Skip0 when Skip0 < 0; Skip0 > length(JsonIdxs0) -> + Skip0 when Skip0 < 0 -> ?MANGO_ERROR(invalid_list_index_params); + Skip0 when Skip0 > TotalRows -> + TotalRows; Skip0 -> Skip0 end, http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/d128f16d/test/01-index-crud-test.py ---------------------------------------------------------------------- diff --git a/test/01-index-crud-test.py b/test/01-index-crud-test.py index 43681dc..d83e97f 100644 --- a/test/01-index-crud-test.py +++ b/test/01-index-crud-test.py @@ -240,14 +240,10 @@ class IndexCrudTests(mango.DbPerClass): assert len(self.db.list_indexes(limit=5,skip=4)) == 2 assert len(self.db.list_indexes(skip=5)) == 1 assert len(self.db.list_indexes(skip=6)) == 0 + assert len(self.db.list_indexes(skip=100)) == 0 assert len(self.db.list_indexes(limit=10000000)) == 6 try: - self.db.list_indexes(skip=10) - except Exception, e: - assert e.response.status_code == 500 - - try: self.db.list_indexes(skip=-1) except Exception, e: assert e.response.status_code == 500
