Creating a database that already exists in the glue data catalog raises an exception

This commit is contained in:
TheDooner64 2018-07-11 11:39:40 -04:00
parent e67a8c6f1b
commit c5c57efbb5
3 changed files with 29 additions and 5 deletions

View File

@ -1,9 +1,15 @@
from __future__ import unicode_literals
from moto.core.exceptions import RESTError
from moto.core.exceptions import JsonRESTError
class GlueClientError(RESTError):
class GlueClientError(JsonRESTError):
code = 400
def __init__(self, *args, **kwargs):
kwargs.setdefault('template', 'single_error')
super(GlueClientError, self).__init__(*args, **kwargs)
class DatabaseAlreadyExistsException(GlueClientError):
def __init__(self):
self.code = 400
super(DatabaseAlreadyExistsException, self).__init__(
'DatabaseAlreadyExistsException',
'Database already exists.'
)

View File

@ -2,6 +2,7 @@ from __future__ import unicode_literals
from moto.core import BaseBackend, BaseModel
from moto.compat import OrderedDict
from.exceptions import DatabaseAlreadyExistsException
class GlueBackend(BaseBackend):
@ -10,6 +11,9 @@ class GlueBackend(BaseBackend):
self.databases = OrderedDict()
def create_database(self, database_name):
if database_name in self.databases:
raise DatabaseAlreadyExistsException()
database = FakeDatabase(database_name)
self.databases[database_name] = database
return database

View File

@ -1,7 +1,9 @@
from __future__ import unicode_literals
import sure # noqa
from nose.tools import assert_raises
import boto3
from botocore.client import ClientError
from moto import mock_glue
@ -28,3 +30,15 @@ def test_create_database():
database = response['Database']
database.should.equal({'Name': database_name})
@mock_glue
def test_create_database_already_exists():
client = boto3.client('glue', region_name='us-east-1')
database_name = 'anewdatabase'
create_database(client, database_name)
with assert_raises(ClientError) as exc:
create_database(client, database_name)
exc.exception.response['Error']['Code'].should.equal('DatabaseAlreadyExistsException')