From c5c57efbb5d08828dea08822c90b292d0d183cdb Mon Sep 17 00:00:00 2001 From: TheDooner64 Date: Wed, 11 Jul 2018 11:39:40 -0400 Subject: [PATCH] Creating a database that already exists in the glue data catalog raises an exception --- moto/glue/exceptions.py | 16 +++++++++++----- moto/glue/models.py | 4 ++++ tests/test_glue/test_datacatalog.py | 14 ++++++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/moto/glue/exceptions.py b/moto/glue/exceptions.py index 0c8760f1..2f9d16d2 100644 --- a/moto/glue/exceptions.py +++ b/moto/glue/exceptions.py @@ -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.' + ) diff --git a/moto/glue/models.py b/moto/glue/models.py index 55cd46bc..357a2a52 100644 --- a/moto/glue/models.py +++ b/moto/glue/models.py @@ -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 diff --git a/tests/test_glue/test_datacatalog.py b/tests/test_glue/test_datacatalog.py index 77ad1c01..c7cdb1a7 100644 --- a/tests/test_glue/test_datacatalog.py +++ b/tests/test_glue/test_datacatalog.py @@ -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')