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

Commit afb66c5

Browse files
authored
Merge pull request #206 from cloudant/205-cloudant-tests
205 cloudant tests
2 parents cc47724 + 38696a1 commit afb66c5

File tree

7 files changed

+72
-68
lines changed

7 files changed

+72
-68
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
the 2.0.0 behaviour use: ``adapter=Replay429Adapter(retries=10, initialBackoff=0.25)``. If ``retries`` or
1717
``initialBackoff`` are not specified they will default to 3 retries and a 0.25 s initial backoff.
1818
- [FIX] ``415 Client Error: Unsupported Media Type`` when using keys with ``db.all_docs``.
19+
- [FIX] Allowed strings as well as lists for search ``group_sort`` arguments.
1920

2021
2.0.3 (2016-06-03)
2122
==================

src/cloudant/_common_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
'drilldown': list,
121121
'group_field': STRTYPE,
122122
'group_limit': (int, NONETYPE),
123-
'group_sort': list,
123+
'group_sort': (STRTYPE, list),
124124
'include_docs': bool,
125125
'limit': (int, NONETYPE),
126126
'query': (STRTYPE, int),

tests/unit/client_tests.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import base64
2727
import sys
2828
import os
29-
from datetime import datetime
29+
import datetime
3030

3131
from cloudant import cloudant, couchdb, couchdb_admin_party
3232
from cloudant.client import Cloudant, CouchDB
@@ -471,17 +471,19 @@ def test_billing_data(self):
471471
"""
472472
try:
473473
self.client.connect()
474+
now = datetime.datetime.now()
474475
expected = [
475476
'data_volume',
476477
'total',
477478
'start',
478479
'end',
479480
'http_heavy',
480-
'http_light'
481+
'http_light',
482+
'bill_type'
481483
]
482484
# Test using year and month
483-
year = 2016
484-
month = 1
485+
year = now.year
486+
month = now.month
485487
data = self.client.bill(year, month)
486488
self.assertTrue(all(x in expected for x in data.keys()))
487489
#Test without year and month arguments
@@ -563,15 +565,16 @@ def test_volume_usage_data(self):
563565
"""
564566
try:
565567
self.client.connect()
568+
now = datetime.datetime.now()
566569
expected = [
567570
'data_vol',
568571
'granularity',
569572
'start',
570573
'end'
571574
]
572575
# Test using year and month
573-
year = 2016
574-
month = 12
576+
year = now.year
577+
month = now.month
575578
data = self.client.volume_usage(year, month)
576579
self.assertTrue(all(x in expected for x in data.keys()))
577580
#Test without year and month arguments
@@ -653,15 +656,16 @@ def test_requests_usage_data(self):
653656
"""
654657
try:
655658
self.client.connect()
659+
now = datetime.datetime.now()
656660
expected = [
657661
'requests',
658662
'granularity',
659663
'start',
660664
'end'
661665
]
662666
# Test using year and month
663-
year = 2016
664-
month = 1
667+
year = now.year
668+
month = now.month
665669
data = self.client.requests_usage(year, month)
666670
self.assertTrue(all(x in expected for x in data.keys()))
667671
#Test without year and month arguments

tests/unit/database_tests.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ def test_create_json_index(self):
987987
'reduce': '_count',
988988
'options': {'def': {'fields': ['name',
989989
'age']},
990-
'w': 2}}}}
990+
}}}}
991991
)
992992

993993
def test_create_text_index(self):
@@ -1078,7 +1078,7 @@ def test_create_multiple_indexes_one_ddoc(self):
10781078
'reduce': '_count',
10791079
'options': {'def': {'fields': ['name',
10801080
'age']},
1081-
'w': 2}}},
1081+
}}},
10821082
'indexes': {'text-index-001': {
10831083
'index': {'index_array_lengths': True,
10841084
'fields': [{'name': 'name', 'type': 'string'},
@@ -1175,7 +1175,8 @@ def test_get_query_indexes_raw(self):
11751175
'default_field': {},
11761176
'default_analyzer': 'keyword',
11771177
'selector': {}}}
1178-
]}
1178+
],
1179+
'total_rows' : 3}
11791180
)
11801181

11811182
def test_get_query_indexes(self):
@@ -1217,7 +1218,7 @@ def test_get_search_result_with_invalid_value_types(self):
12171218
{'drilldown': 'blah'}, # Should be a list
12181219
{'group_field': ['blah']}, # Should be a STRTYPE
12191220
{'group_limit': 'int'}, # Should be an int
1220-
{'group_sort': 'blah'}, # Should be a list
1221+
{'group_sort': 3}, # Should be a STRTYPE or list
12211222
{'include_docs': 'blah'}, # Should be a boolean
12221223
{'limit': 'blah'}, # Should be an int
12231224
{'ranges': 1}, # Should be a dict
@@ -1278,6 +1279,7 @@ def test_get_search_result_executes_search_query(self):
12781279
'searchddoc001',
12791280
'searchindex001',
12801281
query='julia*',
1282+
sort='_id<string>',
12811283
limit=5,
12821284
include_docs=True
12831285
)
@@ -1292,23 +1294,23 @@ def test_get_search_result_executes_search_query(self):
12921294
{'rows': [{'fields': {'name': 'julia'}, 'doc': {'_id': 'julia000',
12931295
'age': 0,
12941296
'name': 'julia'},
1295-
'id': 'julia000', 'order': [1.0, 0]},
1297+
'id': 'julia000', 'order': ['julia000', 0]},
12961298
{'fields': {'name': 'julia'}, 'doc': {'_id': 'julia001',
12971299
'age': 1,
12981300
'name': 'julia'},
1299-
'id': 'julia001', 'order': [1.0, 0]},
1301+
'id': 'julia001', 'order': ['julia001', 0]},
13001302
{'fields': {'name': 'julia'},'doc': {'_id': 'julia002',
13011303
'age': 2,
13021304
'name': 'julia'},
1303-
'id': 'julia002', 'order': [1.0, 0]},
1304-
{'fields': {'name': 'julia'}, 'doc': {'_id': 'julia004',
1305-
'age': 4,
1305+
'id': 'julia002', 'order': ['julia002', 0]},
1306+
{'fields': {'name': 'julia'}, 'doc': {'_id': 'julia003',
1307+
'age': 3,
13061308
'name': 'julia'},
1307-
'id': 'julia004', 'order': [1.0, 1]},
1309+
'id': 'julia003', 'order': ['julia003', 0]},
13081310
{'fields': {'name': 'julia'},
1309-
'doc': {'_id': 'julia005', 'age': 5,
1311+
'doc': {'_id': 'julia004', 'age': 4,
13101312
'name': 'julia'},
1311-
'id': 'julia005', 'order': [1.0, 1]}], 'total_rows': 100}
1313+
'id': 'julia004', 'order': ['julia004', 1]}], 'total_rows': 100}
13121314
)
13131315

13141316
def test_get_search_result_executes_search_query_with_group_option(self):
@@ -1322,7 +1324,8 @@ def test_get_search_result_executes_search_query_with_group_option(self):
13221324
'searchindex001',
13231325
query='name:julia*',
13241326
group_field='_id',
1325-
group_limit=5
1327+
group_limit=5,
1328+
group_sort='_id<string>'
13261329
)
13271330
# for group parameter options, 'rows' results are within 'groups' key
13281331
self.assertEqual(len(resp['groups']), 5)
@@ -1332,18 +1335,18 @@ def test_get_search_result_executes_search_query_with_group_option(self):
13321335
{'rows': [{'fields': {'name': 'julia'}, 'id': 'julia000',
13331336
'order': [1.0, 0]}], 'total_rows': 1,
13341337
'by': 'julia000'},
1338+
{'rows': [{'fields': {'name': 'julia'}, 'id': 'julia001',
1339+
'order': [1.0, 0]}], 'total_rows': 1,
1340+
'by': 'julia001'},
1341+
{'rows': [{'fields': {'name': 'julia'}, 'id': 'julia002',
1342+
'order': [1.0, 0]}], 'total_rows': 1,
1343+
'by': 'julia002'},
1344+
{'rows': [{'fields': {'name': 'julia'}, 'id': 'julia003',
1345+
'order': [1.0, 0]}], 'total_rows': 1,
1346+
'by': 'julia003'},
13351347
{'rows': [{'fields': {'name': 'julia'}, 'id': 'julia004',
13361348
'order': [1.0, 1]}], 'total_rows': 1,
1337-
'by': 'julia004'},
1338-
{'rows': [{'fields': {'name': 'julia'}, 'id': 'julia008',
1339-
'order': [1.0, 2]}], 'total_rows': 1,
1340-
'by': 'julia008'},
1341-
{'rows': [{'fields': {'name': 'julia'}, 'id': 'julia010',
1342-
'order': [1.0, 3]}], 'total_rows': 1,
1343-
'by': 'julia010'},
1344-
{'rows': [{'fields': {'name': 'julia'}, 'id': 'julia014',
1345-
'order': [1.0, 4]}], 'total_rows': 1,
1346-
'by': 'julia014'}
1349+
'by': 'julia004'}
13471350
]}
13481351
)
13491352

tests/unit/design_document_tests.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -694,21 +694,26 @@ def test_get_info(self):
694694
ddoc_remote = DesignDocument(self.db, '_design/ddoc001')
695695
ddoc_remote.fetch()
696696
info = ddoc_remote.info()
697+
# Remove variable fields to make equality easier to check
697698
info['view_index'].pop('signature')
698699
info['view_index'].pop('disk_size')
699-
# Remove Cloudant specific sizes object
700+
# Remove Cloudant/Couch 2 fields if present to allow test to pass on Couch 1.6
700701
if 'sizes' in info['view_index']:
701702
info['view_index'].pop('sizes')
702-
name = ddoc_remote['_id']
703-
else:
704-
name = 'ddoc001'
703+
if 'updates_pending' in info['view_index']:
704+
info['view_index'].pop('updates_pending')
705+
706+
name = 'ddoc001'
705707
self.assertEqual(
706708
info,
707709
{'view_index': {'update_seq': 0, 'waiting_clients': 0,
708710
'language': 'javascript',
709711
'purge_seq': 0, 'compact_running': False,
710712
'waiting_commit': False, 'updater_running': False,
711-
'data_size': 0}, 'name': name})
713+
'data_size': 0
714+
},
715+
'name': name
716+
})
712717

713718
@unittest.skipUnless(
714719
os.environ.get('RUN_CLOUDANT_TESTS') is not None,
@@ -1405,13 +1410,17 @@ def test_geospatial_index(self):
14051410
'geoidx?g=point(-71.1%2042.3)'])).json()
14061411
self.assertIsNotNone(geo_result['bookmark'])
14071412
geo_result.pop('bookmark')
1408-
self.assertEqual(geo_result,
1409-
{'rows': [
1413+
rows = geo_result.pop('rows')
1414+
self.assertEqual(1, len(rows), "There should be 1 row.")
1415+
row = rows[0]
1416+
# Remove the rev before comparison
1417+
row.pop('rev')
1418+
self.assertEqual(row,
1419+
14101420
{'id': 'doc001',
14111421
'geometry':
14121422
{'type': 'Point',
1413-
'coordinates': [-71.1, 42.3]}}]
1414-
})
1423+
'coordinates': [-71.1, 42.3]}})
14151424

14161425
def test_add_a_show_function(self):
14171426
"""

tests/unit/index_tests.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def test_create_an_index_using_ddoc_index_name(self):
145145
'reduce': '_count',
146146
'options': {'def': {'fields': ['name',
147147
'age']},
148-
'w': 2}}},
148+
}}},
149149
'lists': {},
150150
'shows': {}
151151
}
@@ -175,7 +175,7 @@ def test_create_an_index_without_ddoc_index_name(self):
175175
'reduce': '_count',
176176
'options': {'def': {'fields': ['name',
177177
'age']},
178-
'w': 2}}},
178+
}}},
179179
'lists': {},
180180
'shows': {}
181181
}
@@ -205,7 +205,7 @@ def test_create_an_index_with_empty_ddoc_index_name(self):
205205
'reduce': '_count',
206206
'options': {'def': {'fields': ['name',
207207
'age']},
208-
'w': 2}}},
208+
}}},
209209
'lists': {},
210210
'shows': {}
211211
}
@@ -235,7 +235,7 @@ def test_create_an_index_using_design_prefix(self):
235235
'reduce': '_count',
236236
'options': {'def': {'fields': ['name',
237237
'age']},
238-
'w': 2}}},
238+
}}},
239239
'lists': {},
240240
'shows': {}
241241
}
@@ -351,20 +351,15 @@ def test_index_via_query(self):
351351

352352
def test_index_usage_via_query(self):
353353
"""
354-
Test that a query will fail if the indexes that exist do not satisfy the
354+
Test that a query will warn if the indexes that exist do not satisfy the
355355
query selector.
356356
"""
357357
index = Index(self.db, 'ddoc001', 'index001', fields=['name'])
358358
index.create()
359359
self.populate_db_with_documents(100)
360-
query = Query(self.db)
361-
with self.assertRaises(requests.HTTPError) as cm:
362-
resp = query(
363-
fields=['name', 'age'],
364-
selector={'age': {'$eq': 6}}
365-
)
366-
err = cm.exception
367-
self.assertEqual(err.response.status_code, 400)
360+
result = self.db.get_query_result(fields=['name', 'age'],
361+
selector={'age': {'$eq': 6}}, raw_result=True)
362+
self.assertTrue(str(result['warning']).startswith("no matching index found"))
368363

369364
@unittest.skipUnless(
370365
os.environ.get('RUN_CLOUDANT_TESTS') is not None,

tests/unit/result_tests.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -463,22 +463,14 @@ def test_get_item_key_slice_using_start_stop(self):
463463
def test_get_item_key_slice_start_greater_than_stop(self):
464464
"""
465465
Test getting a key slice by using start value greater than stop value.
466-
The behavior when using CouchDB is to return an HTTP 400 Bad Request
467-
error whereas with Cloudant an empty result collection is returned.
468-
Unfortunately a 400 response cannot definitively be attributed to a
469-
startkey value being greater than an endkey value so the decision to
470-
leave this CouchDB/Cloudant behavior inconsistency as is. We have an
471-
"if-else" branch as part of the test to handle the two differing
472-
behaviors.
466+
The behavior when using CouchDB and newer versions of Cloudant
467+
is to return an HTTP 400 Bad Request.
473468
"""
474469
result = Result(self.view001)
475-
if os.environ.get('RUN_CLOUDANT_TESTS') is None:
476-
with self.assertRaises(HTTPError) as cm:
477-
invalid_result = result['foo': 'bar']
478-
self.assertTrue(
479-
str(cm.exception).startswith('400 Client Error: Bad Request'))
480-
else:
481-
self.assertEqual(result['foo': 'bar'], [])
470+
with self.assertRaises(HTTPError) as cm:
471+
invalid_result = result['foo': 'bar']
472+
self.assertTrue(
473+
str(cm.exception).startswith('400 Client Error: Bad Request'))
482474

483475
def test_get_item_key_slice_using_start_only(self):
484476
"""

0 commit comments

Comments
 (0)