|
34 | 34 | from cloudant._client_session import BasicSession, CookieSession |
35 | 35 | from cloudant.client import Cloudant, CouchDB |
36 | 36 | from cloudant.database import CloudantDatabase |
37 | | -from cloudant.error import CloudantArgumentError, CloudantClientException |
| 37 | +from cloudant.error import (CloudantArgumentError, CloudantClientException, |
| 38 | + CloudantDatabaseException) |
38 | 39 | from cloudant.feed import Feed, InfiniteFeed |
39 | 40 | from nose.plugins.attrib import attr |
40 | 41 | from requests import ConnectTimeout, HTTPError |
@@ -398,6 +399,56 @@ def test_create_existing_database(self): |
398 | 399 | self.client.delete_database(dbname) |
399 | 400 | self.client.disconnect() |
400 | 401 |
|
| 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 | + |
401 | 452 | def test_delete_non_existing_database(self): |
402 | 453 | """ |
403 | 454 | Test deletion of non-existing database |
|
0 commit comments