# Path: mamba/unittest/database_helpers.py
# class TestableDatabase(Database):
#     """Testable database object
#     """
# 
#     pool = DummyThreadPool()
# 
#     def __init__(self, engine=ENGINE.NATIVE):
#         super(TestableDatabase, self).__init__(testing=True)
#         self.engine = engine
#         self.tmp_file = None
#         self._initialize()
# 
#     def __del__(self):
#         if self.tmp_file is not None:
#             os.unlink(self.tmp_file)
#             self.tmp_file = None
# 
#     def _initialize(self):
#         """Initialize the object with the right database configuration
#         """
# 
#         provideUtility(global_zstorm, IZStorm)
#         zstorm = getUtility(IZStorm)
#         zstorm._reset()  # make sure we are not using an old configuration
#         if self.engine == ENGINE.NATIVE:
#             if self.backend == 'sqlite' and sqlite_version_info >= (3, 6, 19):
#                 uri = '{}?foreign_keys=1'.format(config.Database().uri)
#             else:
#                 uri = config.Database().uri
#         elif self.engine == ENGINE.INMEMORY:
#             if sqlite_version_info >= (3, 6, 19):
#                 uri = 'sqlite:?foreign_keys=1'
#             else:
#                 uri = 'sqlite:'
#         else:
#             tmpfile = tempfile.NamedTemporaryFile(delete=False)
#             self.tmp_file = tmpfile.name
#             if sqlite_version_info >= (3, 6, 19):
#                 uri = 'sqlite:{}?foreign_keys=1'.format(tmpfile.name)
#             else:
#                 uri = 'sqlite:{}'.format(tmpfile.name)
#             tmpfile.close()
# 
#         zstorm.set_default_uri('mamba', uri)
#         self.started = True
# 
#     def start(self):
#         """Stub method that mimic Database.start
#         """
# 
#         if not self.started:
#             self.started = True
# 
#     def stop(self):
#         """Stub method that mimic Database.stop
#         """
# 
#         if self.started:
#             self.started = False
# 
# class ENGINE(Names):
#     """Constants representing different ENGINE options
#     """
# 
#     NATIVE = NamedConstant()        # what is configured
#     INMEMORY = NamedConstant()      # in memory SQLite
#     PERSISTENT = NamedConstant()    # persistent SQLite


import os
from contextlib import contextmanager
from mamba.enterprise import schema
from mamba.unittest.database_helpers import TestableDatabase, ENGINE
# -*- test-case-name: mamba.test.test_fixtures -*-
# Copyright (c) 2012 - 2014 Oscar Campos <oscar.campos@member.fsf.org>
# See LICENSE for more details

"""
.. module:: fixtures
    :paltform: Unix, Windows
    :synopsis: Wrapper around Storm patch/schema mechanism to make mamba model
               objects testing easier

.. moduleauthor:: Oscar Campos <oscar.campos@member.fsf.org>
"""




class FixtureError(Exception):
    """Raised on fixture errors
    """


class Fixture(schema.Schema):
    """Create, insert, drop and clean table schemas.
    """


# Path: mamba/unittest/database_helpers.py
# class TestableDatabase(Database):
#     """Testable database object
#     """
# 
#     pool = DummyThreadPool()
# 
#     def __init__(self, engine=ENGINE.NATIVE):
#         super(TestableDatabase, self).__init__(testing=True)
#         self.engine = engine
#         self.tmp_file = None
#         self._initialize()
# 
#     def __del__(self):
#         if self.tmp_file is not None:
#             os.unlink(self.tmp_file)
#             self.tmp_file = None
# 
#     def _initialize(self):
#         """Initialize the object with the right database configuration
#         """
# 
#         provideUtility(global_zstorm, IZStorm)
#         zstorm = getUtility(IZStorm)
#         zstorm._reset()  # make sure we are not using an old configuration
#         if self.engine == ENGINE.NATIVE:
#             if self.backend == 'sqlite' and sqlite_version_info >= (3, 6, 19):
#                 uri = '{}?foreign_keys=1'.format(config.Database().uri)
#             else:
#                 uri = config.Database().uri
#         elif self.engine == ENGINE.INMEMORY:
#             if sqlite_version_info >= (3, 6, 19):
#                 uri = 'sqlite:?foreign_keys=1'
#             else:
#                 uri = 'sqlite:'
#         else:
#             tmpfile = tempfile.NamedTemporaryFile(delete=False)
#             self.tmp_file = tmpfile.name
#             if sqlite_version_info >= (3, 6, 19):
#                 uri = 'sqlite:{}?foreign_keys=1'.format(tmpfile.name)
#             else:
#                 uri = 'sqlite:{}'.format(tmpfile.name)
#             tmpfile.close()
# 
#         zstorm.set_default_uri('mamba', uri)
#         self.started = True
# 
#     def start(self):
#         """Stub method that mimic Database.start
#         """
# 
#         if not self.started:
#             self.started = True
# 
#     def stop(self):
#         """Stub method that mimic Database.stop
#         """
# 
#         if self.started:
#             self.started = False
# 
# class ENGINE(Names):
#     """Constants representing different ENGINE options
#     """
# 
#     NATIVE = NamedConstant()        # what is configured
#     INMEMORY = NamedConstant()      # in memory SQLite
#     PERSISTENT = NamedConstant()    # persistent SQLite


# Path: mamba/unittest/fixtures.py
import os
from contextlib import contextmanager
from mamba.enterprise import schema
from mamba.unittest.database_helpers import TestableDatabase, ENGINE

# -*- test-case-name: mamba.test.test_fixtures -*-
# Copyright (c) 2012 - 2014 Oscar Campos <oscar.campos@member.fsf.org>
# See LICENSE for more details

"""
.. module:: fixtures
    :paltform: Unix, Windows
    :synopsis: Wrapper around Storm patch/schema mechanism to make mamba model
               objects testing easier

.. moduleauthor:: Oscar Campos <oscar.campos@member.fsf.org>
"""




class FixtureError(Exception):
    """Raised on fixture errors
    """


class Fixture(schema.Schema):
    """Create, insert, drop and clean table schemas.
    """


    def __init__(self, model=None, path='../', engine=ENGINE.NATIVE):