# Path: postgraas_server/backends/exceptions.py
# class PostgraasApiException(Exception):
#     """Base exception which should be used by backends instead of their own
#     backend specific exceptions.
# 
#     .. example::
# 
#         try:
#             docker_backend.do_something()
#         except docker.errors.APIError as e:
#             raise PostgraasApiException(str(e))
# 
#     """
# 
#     def __init__(self, message=None):
#         self.message = message
# 
#     def __str__(self):
#         return self.message
# 
# Path: postgraas_server/create_app.py
# def create_app(config):
#     app = Flask(__name__)
# 
#     app.config['SQLALCHEMY_DATABASE_URI'] = get_meta_db_config_path(config)
#     app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
#     app.config['BUNDLE_ERRORS'] = True
#     app_config = get_application_config(config)
# 
#     for key in app_config:
#         app.config[key.upper()] = int(app_config[key]) if key.upper() in INT_OPTIONS else app_config[key]
#     app.config['SENTRY_INCLUDE_PATHS'] = [
#         'postgraas_server',
#     ]
#     app.config['SENTRY_RELEASE'] = postgraas_server.__version__
#     sentry.init_app(app)
# 
#     restful_api = Api(app)
#     restful_api.add_resource(DBInstanceResource, "/api/v2/postgraas_instances/<int:id>")
#     restful_api.add_resource(DBInstanceCollectionResource, "/api/v2/postgraas_instances")
#     db.init_app(app)
#     app.postgraas_backend = get_backend(config)
# 
#     @app.route('/health')
#     def health():
#         return "ok"
# 
#     return app
# 
# Path: postgraas_server/management_resources.py
# class DBInstance(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     postgraas_instance_name = db.Column(db.String(100))
#     creation_timestamp = db.Column(db.DateTime)
#     db_name = db.Column(db.String(100))
#     username = db.Column(db.String(500))
#     password = db.Column(db.String(100))
#     hostname = db.Column(db.String(500))
#     port = db.Column(db.Integer)
#     container_id = db.Column(db.String(100))
# 
#     def __init__(
#             self,
#             postgraas_instance_name,
#             db_name,
#             username,
#             password,
#             hostname,
#             port,
#             container_id=None
#     ):
#         self.postgraas_instance_name = postgraas_instance_name
#         self.creation_timestamp = datetime.datetime.now()
#         self.db_name = db_name
#         self.username = username
#         self.password = password
#         self.hostname = hostname
#         self.port = port
#         self.container_id = container_id
# 
#     def __repr__(self):
#         return '<PGName {} User {} Docker_id {}>'.format(
#             self.postgraas_instance_name, self.username, self.container_id
#         )


import uuid
import json
import os
import pytest
import postgraas_server.backends.docker.postgres_instance_driver as pid
import postgraas_server.backends.postgres_cluster.postgres_cluster_driver as pgcd
import postgraas_server.configuration as configuration
from postgraas_server.backends.exceptions import PostgraasApiException
from postgraas_server.create_app import create_app
from postgraas_server.management_resources import DBInstance
    from postgraas_server.management_resources import db
    def test_create_postgraas_bad_username(self):
        db_credentials = {
            "db_name": 'tests_postgraas_instance_name',
            "db_username": 'tests_postgraas_db-bad username',
            "db_pwd": 'test_db_pwd',
            "host": pid.get_hostname(),
            "port": pid.get_open_port()
        }
        db_entry = DBInstance(
            postgraas_instance_name=db_credentials['db_name'],
            db_name=db_credentials['db_name'],
            username=db_credentials['db_username'],
            password="",
            hostname=db_credentials['host'],
            port=db_credentials['port']
        )
        if self.backend == "pg_cluster":
            with pytest.raises(PostgraasApiException) as excinfo:
                db_entry.container_id = self.this_app.postgraas_backend.create(db_entry, db_credentials)
                self.this_app.postgraas_backend.delete(db_entry)
            assert 'syntax error at or near "-"' in excinfo.value.message

    def test_delete_nonexisting_db(self):
        db_credentials = {
            "db_name": 'tests_postgraas_instance_name',
            "db_username": 'tests_postgraas_db-bad username',
            "db_pwd": 'test_db_pwd',
            "host": pid.get_hostname(),
            "port": pid.get_open_port()
        }

# Path: postgraas_server/backends/exceptions.py
# class PostgraasApiException(Exception):
#     """Base exception which should be used by backends instead of their own
#     backend specific exceptions.
# 
#     .. example::
# 
#         try:
#             docker_backend.do_something()
#         except docker.errors.APIError as e:
#             raise PostgraasApiException(str(e))
# 
#     """
# 
#     def __init__(self, message=None):
#         self.message = message
# 
#     def __str__(self):
#         return self.message
# 
# Path: postgraas_server/create_app.py
# def create_app(config):
#     app = Flask(__name__)
# 
#     app.config['SQLALCHEMY_DATABASE_URI'] = get_meta_db_config_path(config)
#     app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
#     app.config['BUNDLE_ERRORS'] = True
#     app_config = get_application_config(config)
# 
#     for key in app_config:
#         app.config[key.upper()] = int(app_config[key]) if key.upper() in INT_OPTIONS else app_config[key]
#     app.config['SENTRY_INCLUDE_PATHS'] = [
#         'postgraas_server',
#     ]
#     app.config['SENTRY_RELEASE'] = postgraas_server.__version__
#     sentry.init_app(app)
# 
#     restful_api = Api(app)
#     restful_api.add_resource(DBInstanceResource, "/api/v2/postgraas_instances/<int:id>")
#     restful_api.add_resource(DBInstanceCollectionResource, "/api/v2/postgraas_instances")
#     db.init_app(app)
#     app.postgraas_backend = get_backend(config)
# 
#     @app.route('/health')
#     def health():
#         return "ok"
# 
#     return app
# 
# Path: postgraas_server/management_resources.py
# class DBInstance(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     postgraas_instance_name = db.Column(db.String(100))
#     creation_timestamp = db.Column(db.DateTime)
#     db_name = db.Column(db.String(100))
#     username = db.Column(db.String(500))
#     password = db.Column(db.String(100))
#     hostname = db.Column(db.String(500))
#     port = db.Column(db.Integer)
#     container_id = db.Column(db.String(100))
# 
#     def __init__(
#             self,
#             postgraas_instance_name,
#             db_name,
#             username,
#             password,
#             hostname,
#             port,
#             container_id=None
#     ):
#         self.postgraas_instance_name = postgraas_instance_name
#         self.creation_timestamp = datetime.datetime.now()
#         self.db_name = db_name
#         self.username = username
#         self.password = password
#         self.hostname = hostname
#         self.port = port
#         self.container_id = container_id
# 
#     def __repr__(self):
#         return '<PGName {} User {} Docker_id {}>'.format(
#             self.postgraas_instance_name, self.username, self.container_id
#         )


# Path: tests/test_integration/test_backend_behaviour.py
import uuid
import json
import os
import pytest
import postgraas_server.backends.docker.postgres_instance_driver as pid
import postgraas_server.backends.postgres_cluster.postgres_cluster_driver as pgcd
import postgraas_server.configuration as configuration
from postgraas_server.backends.exceptions import PostgraasApiException
from postgraas_server.create_app import create_app
from postgraas_server.management_resources import DBInstance
    from postgraas_server.management_resources import db

    def test_create_postgraas_bad_username(self):
        db_credentials = {
            "db_name": 'tests_postgraas_instance_name',
            "db_username": 'tests_postgraas_db-bad username',
            "db_pwd": 'test_db_pwd',
            "host": pid.get_hostname(),
            "port": pid.get_open_port()
        }
        db_entry = DBInstance(
            postgraas_instance_name=db_credentials['db_name'],
            db_name=db_credentials['db_name'],
            username=db_credentials['db_username'],
            password="",
            hostname=db_credentials['host'],
            port=db_credentials['port']
        )
        if self.backend == "pg_cluster":
            with pytest.raises(PostgraasApiException) as excinfo:
                db_entry.container_id = self.this_app.postgraas_backend.create(db_entry, db_credentials)
                self.this_app.postgraas_backend.delete(db_entry)
            assert 'syntax error at or near "-"' in excinfo.value.message

    def test_delete_nonexisting_db(self):
        db_credentials = {
            "db_name": 'tests_postgraas_instance_name',
            "db_username": 'tests_postgraas_db-bad username',
            "db_pwd": 'test_db_pwd',
            "host": pid.get_hostname(),
            "port": pid.get_open_port()
        }

        db_entry = DBInstance(