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

Commit aef879c

Browse files
authored
Updated 'create_database' to raise exception when database name is invalid (#447)
1 parent e0ba190 commit aef879c

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Unreleased
2+
3+
- [FIXED] Correctly raise exceptions from `create_database` calls.
4+
15
# 2.12.0 (2019-03-28)
26

37
- [NEW] Added partitioned database support.

src/cloudant/client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ def create_database(self, dbname, partitioned=False, **kwargs):
290290
except CloudantDatabaseException as ex:
291291
if ex.status_code == 412:
292292
raise CloudantClientException(412, dbname)
293+
raise ex
293294
super(CouchDB, self).__setitem__(dbname, new_db)
294295
return new_db
295296

src/cloudant/error.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,13 @@ class CloudantDatabaseException(CloudantException):
107107
"""
108108
def __init__(self, code=100, *args):
109109
try:
110-
msg = DATABASE[code].format(*args)
110+
if code in DATABASE:
111+
msg = DATABASE[code].format(*args)
112+
elif isinstance(code, int):
113+
msg = ' '.join(args)
114+
else:
115+
code = 100
116+
msg = DATABASE[code]
111117
except (KeyError, IndexError):
112118
code = 100
113119
msg = DATABASE[code]

tests/unit/client_tests.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
from cloudant._client_session import BasicSession, CookieSession
3535
from cloudant.client import Cloudant, CouchDB
3636
from cloudant.database import CloudantDatabase
37-
from cloudant.error import CloudantArgumentError, CloudantClientException
37+
from cloudant.error import (CloudantArgumentError, CloudantClientException,
38+
CloudantDatabaseException)
3839
from cloudant.feed import Feed, InfiniteFeed
3940
from nose.plugins.attrib import attr
4041
from requests import ConnectTimeout, HTTPError
@@ -398,6 +399,56 @@ def test_create_existing_database(self):
398399
self.client.delete_database(dbname)
399400
self.client.disconnect()
400401

402+
def test_create_invalid_database_name(self):
403+
"""
404+
Test creation of database with an invalid name
405+
"""
406+
dbname = 'invalidDbName_'
407+
self.client.connect()
408+
with self.assertRaises(CloudantDatabaseException) as cm:
409+
self.client.create_database(dbname)
410+
self.assertEqual(cm.exception.status_code, 400)
411+
self.client.disconnect()
412+
413+
@skip_if_not_cookie_auth
414+
@mock.patch('cloudant._client_session.Session.request')
415+
def test_create_with_server_error(self, m_req):
416+
"""
417+
Test creation of database with a server error
418+
"""
419+
dbname = self.dbname()
420+
# mock 200 for authentication
421+
m_response_ok = mock.MagicMock()
422+
type(m_response_ok).status_code = mock.PropertyMock(return_value=200)
423+
424+
# mock 404 for head request when verifying if database exists
425+
m_response_bad = mock.MagicMock()
426+
type(m_response_bad).status_code = mock.PropertyMock(return_value=404)
427+
428+
# mock 500 when trying to create the database
429+
m_resp_service_error = mock.MagicMock()
430+
type(m_resp_service_error).status_code = mock.PropertyMock(
431+
return_value=500)
432+
type(m_resp_service_error).text = mock.PropertyMock(
433+
return_value='Internal Server Error')
434+
435+
m_req.side_effect = [m_response_ok, m_response_bad, m_resp_service_error]
436+
437+
self.client.connect()
438+
with self.assertRaises(CloudantDatabaseException) as cm:
439+
self.client.create_database(dbname)
440+
441+
self.assertEqual(cm.exception.status_code, 500)
442+
443+
self.assertEquals(m_req.call_count, 3)
444+
m_req.assert_called_with(
445+
'PUT',
446+
'/'.join([self.url, dbname]),
447+
data=None,
448+
params={'partitioned': 'false'},
449+
timeout=(30, 300)
450+
)
451+
401452
def test_delete_non_existing_database(self):
402453
"""
403454
Test deletion of non-existing database

0 commit comments

Comments
 (0)