Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 30efe3c

Browse files
authored
Merge pull request #270 from cloudant/237-support-large-numbers
237 add Python 2 long type support to argument types
2 parents 007b2f6 + f036c17 commit 30efe3c

File tree

5 files changed

+80
-18
lines changed

5 files changed

+80
-18
lines changed

src/cloudant/_2to3.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
# pylint: disable=undefined-variable
3030
UNITYPE = unicode if PY2 else str
3131

32+
# pylint: disable=undefined-variable
33+
LONGTYPE = long if PY2 else int
3234

3335
if PY2:
3436
# pylint: disable=wrong-import-position,no-name-in-module,import-error,unused-import

src/cloudant/_common_util.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import json
2424
from requests import Session
2525

26-
from ._2to3 import STRTYPE, NONETYPE, UNITYPE, iteritems_, url_parse
26+
from ._2to3 import LONGTYPE, STRTYPE, NONETYPE, UNITYPE, iteritems_, url_parse
2727
from .error import CloudantArgumentError, CloudantException
2828

2929
# Library Constants
@@ -50,19 +50,19 @@
5050

5151
RESULT_ARG_TYPES = {
5252
'descending': (bool,),
53-
'endkey': (int, STRTYPE, Sequence,),
53+
'endkey': (int, LONGTYPE, STRTYPE, Sequence,),
5454
'endkey_docid': (STRTYPE,),
5555
'group': (bool,),
56-
'group_level': (int, NONETYPE,),
56+
'group_level': (int, LONGTYPE, NONETYPE,),
5757
'include_docs': (bool,),
5858
'inclusive_end': (bool,),
59-
'key': (int, STRTYPE, Sequence,),
59+
'key': (int, LONGTYPE, STRTYPE, Sequence,),
6060
'keys': (list,),
61-
'limit': (int, NONETYPE,),
61+
'limit': (int, LONGTYPE, NONETYPE,),
6262
'reduce': (bool,),
63-
'skip': (int, NONETYPE,),
63+
'skip': (int, LONGTYPE, NONETYPE,),
6464
'stale': (STRTYPE,),
65-
'startkey': (int, STRTYPE, Sequence,),
65+
'startkey': (int, LONGTYPE, STRTYPE, Sequence,),
6666
'startkey_docid': (STRTYPE,),
6767
}
6868

@@ -75,23 +75,24 @@
7575
list: lambda x: json.dumps(x),
7676
tuple: lambda x: json.dumps(list(x)),
7777
int: lambda x: x,
78+
LONGTYPE: lambda x: x,
7879
bool: lambda x: 'true' if x else 'false',
7980
NONETYPE: lambda x: x
8081
}
8182

8283
_COUCH_DB_UPDATES_ARG_TYPES = {
8384
'feed': (STRTYPE,),
8485
'heartbeat': (bool,),
85-
'timeout': (int, NONETYPE,),
86+
'timeout': (int, LONGTYPE, NONETYPE,),
8687
}
8788

8889
_DB_UPDATES_ARG_TYPES = {
8990
'descending': (bool,),
90-
'limit': (int, NONETYPE,),
91-
'since': (int, STRTYPE,),
91+
'limit': (int, LONGTYPE, NONETYPE,),
92+
'since': (int, LONGTYPE, STRTYPE,),
9293
}
9394
_DB_UPDATES_ARG_TYPES.update(_COUCH_DB_UPDATES_ARG_TYPES)
94-
_DB_UPDATES_ARG_TYPES['heartbeat'] = (int, NONETYPE,)
95+
_DB_UPDATES_ARG_TYPES['heartbeat'] = (int, LONGTYPE, NONETYPE,)
9596

9697
_CHANGES_ARG_TYPES = {
9798
'conflicts': (bool,),
@@ -104,11 +105,11 @@
104105

105106
QUERY_ARG_TYPES = {
106107
'selector': dict,
107-
'limit': (int, NONETYPE),
108-
'skip': (int, NONETYPE),
108+
'limit': (int, LONGTYPE, NONETYPE),
109+
'skip': (int, LONGTYPE, NONETYPE),
109110
'sort': list,
110111
'fields': list,
111-
'r': (int, NONETYPE),
112+
'r': (int, LONGTYPE, NONETYPE),
112113
'bookmark': STRTYPE,
113114
'use_index': STRTYPE
114115
}
@@ -124,16 +125,16 @@
124125
'group_sort': (STRTYPE, list),
125126
'include_docs': bool,
126127
'limit': (int, NONETYPE),
127-
'query': (STRTYPE, int),
128-
'q': (STRTYPE, int),
128+
'query': (STRTYPE, int, LONGTYPE),
129+
'q': (STRTYPE, int, LONGTYPE),
129130
'ranges': dict,
130131
'sort': (STRTYPE, list),
131132
'stale': STRTYPE,
132133
'highlight_fields': list,
133134
'highlight_pre_tag': STRTYPE,
134135
'highlight_post_tag': STRTYPE,
135-
'highlight_number': (int, NONETYPE),
136-
'highlight_size': (int, NONETYPE),
136+
'highlight_number': (int, LONGTYPE, NONETYPE),
137+
'highlight_size': (int, LONGTYPE, NONETYPE),
137138
'include_fields': list
138139
}
139140

tests/unit/_test_util.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python
2+
# Copyright (C) 2017 IBM Corp. All rights reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
"""
16+
Module containing miscellaneous functions, and constants
17+
used for unit testing.
18+
"""
19+
from cloudant._2to3 import PY2
20+
21+
# Constants
22+
23+
# Test long type in Python 2
24+
LONG_NUMBER = PY2 and long(1) or 1

tests/unit/database_tests.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from cloudant.security_document import SecurityDocument
3737
from cloudant.index import Index, TextIndex, SpecialIndex
3838
from cloudant.feed import Feed, InfiniteFeed
39+
from tests.unit._test_util import LONG_NUMBER
3940

4041
from .unit_t_db_base import UnitTestDbBase
4142
from .. import unicode_
@@ -442,6 +443,17 @@ def test_all_docs_get(self):
442443
self.assertEqual(data['rows'][1]['key'], 'julia011')
443444
self.assertEqual(data['rows'][2]['key'], 'julia012')
444445

446+
def test_all_docs_get_with_long_type(self):
447+
"""
448+
Test the all_docs GET request functionality
449+
"""
450+
self.populate_db_with_documents()
451+
data = self.db.all_docs(limit=LONG_NUMBER, skip=10)
452+
self.assertEqual(len(data.get('rows')), 1)
453+
self.assertEqual(data['rows'][0]['key'], 'julia010')
454+
data = self.db.all_docs(limit=1, skip=LONG_NUMBER)
455+
self.assertEqual(len(data.get('rows')), 1)
456+
445457
def test_custom_result_context_manager(self):
446458
"""
447459
Test using the database custom result context manager

tests/unit/param_translation_tests.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
from cloudant.error import CloudantArgumentError
2121
from cloudant._common_util import python_to_couch
22+
from tests.unit._test_util import LONG_NUMBER
23+
2224

2325
class PythonToCouchTests(unittest.TestCase):
2426
"""
@@ -43,6 +45,8 @@ def test_valid_endkey(self):
4345
Test endkey translation is successful.
4446
"""
4547
self.assertEqual(python_to_couch({'endkey': 10}), {'endkey': 10})
48+
# Test with long type
49+
self.assertEqual(python_to_couch({'endkey': LONG_NUMBER}), {'endkey': LONG_NUMBER})
4650
self.assertEqual(
4751
python_to_couch({'endkey': 'foo'}),
4852
{'endkey': '"foo"'}
@@ -76,6 +80,11 @@ def test_valid_group_level(self):
7680
python_to_couch({'group_level': 100}),
7781
{'group_level': 100}
7882
)
83+
# Test with long type
84+
self.assertEqual(
85+
python_to_couch({'group_level': LONG_NUMBER}),
86+
{'group_level': LONG_NUMBER}
87+
)
7988
self.assertEqual(
8089
python_to_couch({'group_level': None}),
8190
{'group_level': None}
@@ -112,6 +121,8 @@ def test_valid_key(self):
112121
Test key translation is successful.
113122
"""
114123
self.assertEqual(python_to_couch({'key': 10}), {'key': 10})
124+
# Test with long type
125+
self.assertEqual(python_to_couch({'key': LONG_NUMBER}), {'key': LONG_NUMBER})
115126
self.assertEqual(python_to_couch({'key': 'foo'}), {'key': '"foo"'})
116127
self.assertEqual(
117128
python_to_couch({'key': ['foo', 10]}),
@@ -126,6 +137,12 @@ def test_valid_keys(self):
126137
python_to_couch({'keys': [100, 200]}),
127138
{'keys': [100, 200]}
128139
)
140+
# Test with long type
141+
LONG_NUM_KEY = 92233720368547758071
142+
self.assertEqual(
143+
python_to_couch({'keys': [LONG_NUMBER, LONG_NUM_KEY]}),
144+
{'keys': [LONG_NUMBER, LONG_NUM_KEY]}
145+
)
129146
self.assertEqual(
130147
python_to_couch({'keys': ['foo', 'bar']}),
131148
{'keys': ['foo', 'bar']}
@@ -140,6 +157,8 @@ def test_valid_limit(self):
140157
Test limit translation is successful.
141158
"""
142159
self.assertEqual(python_to_couch({'limit': 100}), {'limit': 100})
160+
# Test with long type
161+
self.assertEqual(python_to_couch({'limit': LONG_NUMBER}), {'limit': LONG_NUMBER})
143162
self.assertEqual(python_to_couch({'limit': None}), {'limit': None})
144163

145164
def test_valid_reduce(self):
@@ -157,6 +176,8 @@ def test_valid_skip(self):
157176
Test skip translation is successful.
158177
"""
159178
self.assertEqual(python_to_couch({'skip': 100}), {'skip': 100})
179+
# Test with long type
180+
self.assertEqual(python_to_couch({'skip': LONG_NUMBER}), {'skip': LONG_NUMBER})
160181
self.assertEqual(python_to_couch({'skip': None}), {'skip': None})
161182

162183
def test_valid_stale(self):
@@ -174,6 +195,8 @@ def test_valid_startkey(self):
174195
Test startkey translation is successful.
175196
"""
176197
self.assertEqual(python_to_couch({'startkey': 10}), {'startkey': 10})
198+
# Test with long type
199+
self.assertEqual(python_to_couch({'startkey': LONG_NUMBER}), {'startkey': LONG_NUMBER})
177200
self.assertEqual(
178201
python_to_couch({'startkey': 'foo'}),
179202
{'startkey': '"foo"'}

0 commit comments

Comments
 (0)