# Path: onefl/config.py
# class Config(dict):
#     """"
#     :arg root_path: the prefix for building
#         paths to the config files when calling :meth:from_pyfile
#     """
#     def __init__(self, root_path, defaults=None):
#         dict.__init__(self, defaults or {})
#         self.root_path = root_path
# 
#     def from_pyfile(self, filename, silent=False):
#         """Updates the values in the config from a Python file.
# 
#         :param filename: the filename of the config.  This can either be an
#                          absolute filename or a filename relative to the
#                          root path.
#         :param silent: set to ``True`` if you want silent failure for missing
#                        files.
# 
#         """
#         filename = os.path.join(self.root_path, filename)
#         d = types.ModuleType('config')
#         d.__file__ = filename
#         try:
#             with open(filename) as config_file:
#                 exec(compile(config_file.read(), filename, 'exec'), d.__dict__)
#         except IOError as e:
#             if silent and e.errno in (errno.ENOENT, errno.EISDIR):
#                 return False
#             e.strerror = 'Unable to load configuration file (%s)' % e.strerror
#             raise
#         self.from_object(d)
#         return True
# 
#     def from_object(self, obj):
#         """Updates the values from the given object.  An object can be of one
#         of the following two types:
#         -   a string: in this case the object with that name will be imported
#         -   an actual object reference: that object is used directly
#         :param obj: an import name or object
#         """
#         string_types = (str,)
# 
#         if isinstance(obj, string_types):
#             # github.com/pallets/werkzeug/blob/master/werkzeug/utils.py+L399
#             # obj = import_string(obj)
#             obj = importlib.import_module(obj)
# 
#         for key in dir(obj):
#             if key.isupper():
#                 self[key] = getattr(obj, key)
# 
#     def from_dictionary(self, dictionary):
#         """
#         Helper function for overriding config values
#         """
#         for key, value in dictionary.items():
#             if key.isupper():
#                 self[key] = value
# 
# Path: onefl/models/base.py
# def init(engine):
# def get_session():
# 
# Path: onefl/utils/db.py
# DB_TYPE_MSSQL = 'MSSQL'
# DB_TYPE_MYSQL = 'MYSQL'
# DB_TYPE_TEST = 'TEST'
# DB_POOL = None
# def get_db_url_sqlserver(config):
# def get_db_url_mysql(config):
# def get_db_url(config):
# def get_db_engine(config):
# def get_db_session(engine, create_tables=False):
# 
# Path: onefl/models/partner_entity.py
# class PartnerEntity(CRUDMixin, DeclarativeBase):
# 
#     """ Store partners sendig us the data """
#     __tablename__ = 'PARTNER'
# 
#     partner_code = db.Column('PARTNER_CODE', db.Text(3), primary_key=True)
#     partner_description = db.Column('PARTNER_DESCRIPTION', db.Text,
#                                     nullable=False)
#     partner_added_at = db.Column('PARTNER_ADDED_AT', db.DateTime,
#                                  nullable=False)
# 
#     def __repr__(self):
#         """ Return a friendly object representation """
#         return "<PartnerEntity (partner_code: {0.partner_code}, " \
#             "partner_description: {0.partner_description}, " \
#             "partner_addded_at: {0.partner_added_at})>".format(self)
# 
# Path: onefl/models/rule_entity.py
# class RuleEntity(CRUDMixin, DeclarativeBase):
# 
#     """ Store rules used by the partners sending us the data """
#     __tablename__ = 'RULE'
# 
#     id = db.Column('RULE_ID', db.Integer, primary_key=True)
#     rule_code = db.Column('RULE_CODE', db.Text(10),
#                           nullable=False, unique=True)
#     rule_description = db.Column('RULE_DESCRIPTION', db.Text(255),
#                                  nullable=False)
#     rule_added_at = db.Column('RULE_ADDED_AT', db.DateTime,
#                               nullable=False)
# 
#     @staticmethod
#     def get_rules_cache(session):
#         """
#         :return dictionary: with all available {rule_code -> rule.id} mappings
#         """
#         rules = session.query(RuleEntity).all()
#         result = {rule.rule_code: rule.id for rule in rules}
#         return result
# 
#     def __repr__(self):
#         """ Return a friendly object representation """
#         return "<RuleEntity(rule_id: {0.id}, "\
#             "rule_code: {0.rule_code}, " \
#             "rule_description: {0.rule_description}, " \
#             "rule_added_at: {0.rule_added_at})>".format(self)
# 
# Path: onefl/rules.py
# RULE_CODE_F_L_D_R = 'F_L_D_R'
# 
# RULE_CODE_F_L_D_S = 'F_L_D_S'
# 
# RULE_CODE_NO_HASH = 'NO_HASH'


from unittest import TestCase  # noqa
from datetime import datetime
from sqlalchemy.orm.exc import NoResultFound  # noqa
from onefl.config import Config
from onefl.models import base
from onefl.utils import db
from sqlalchemy.orm.exc import MultipleResultsFound  # noqa
from onefl.models.partner_entity import PartnerEntity
from onefl.models.rule_entity import RuleEntity
from onefl.rules import RULE_CODE_F_L_D_R, RULE_CODE_F_L_D_S, RULE_CODE_NO_HASH  # noqa
"""
Goal: implement base class for tests using the database

@authors:
    Andrei Sura <sura.andrei@gmail.com>
"""


SETTINGS_FILE = 'config/test_settings_linker.py'


class BaseTestCase(TestCase):
    """
    Tests for database interaction extend this class.
    @see http://docs.sqlalchemy.org/en/latest/orm/session_transaction.html#session-external-transaction  # noqa
    """

    def __init__(self, *args, **kwargs):
        super(BaseTestCase, self).__init__(*args, **kwargs)

    def setUp(self):
        """ Set the test database engine/session """

        super(BaseTestCase, self).setUp()

# Path: onefl/config.py
# class Config(dict):
#     """"
#     :arg root_path: the prefix for building
#         paths to the config files when calling :meth:from_pyfile
#     """
#     def __init__(self, root_path, defaults=None):
#         dict.__init__(self, defaults or {})
#         self.root_path = root_path
# 
#     def from_pyfile(self, filename, silent=False):
#         """Updates the values in the config from a Python file.
# 
#         :param filename: the filename of the config.  This can either be an
#                          absolute filename or a filename relative to the
#                          root path.
#         :param silent: set to ``True`` if you want silent failure for missing
#                        files.
# 
#         """
#         filename = os.path.join(self.root_path, filename)
#         d = types.ModuleType('config')
#         d.__file__ = filename
#         try:
#             with open(filename) as config_file:
#                 exec(compile(config_file.read(), filename, 'exec'), d.__dict__)
#         except IOError as e:
#             if silent and e.errno in (errno.ENOENT, errno.EISDIR):
#                 return False
#             e.strerror = 'Unable to load configuration file (%s)' % e.strerror
#             raise
#         self.from_object(d)
#         return True
# 
#     def from_object(self, obj):
#         """Updates the values from the given object.  An object can be of one
#         of the following two types:
#         -   a string: in this case the object with that name will be imported
#         -   an actual object reference: that object is used directly
#         :param obj: an import name or object
#         """
#         string_types = (str,)
# 
#         if isinstance(obj, string_types):
#             # github.com/pallets/werkzeug/blob/master/werkzeug/utils.py+L399
#             # obj = import_string(obj)
#             obj = importlib.import_module(obj)
# 
#         for key in dir(obj):
#             if key.isupper():
#                 self[key] = getattr(obj, key)
# 
#     def from_dictionary(self, dictionary):
#         """
#         Helper function for overriding config values
#         """
#         for key, value in dictionary.items():
#             if key.isupper():
#                 self[key] = value
# 
# Path: onefl/models/base.py
# def init(engine):
# def get_session():
# 
# Path: onefl/utils/db.py
# DB_TYPE_MSSQL = 'MSSQL'
# DB_TYPE_MYSQL = 'MYSQL'
# DB_TYPE_TEST = 'TEST'
# DB_POOL = None
# def get_db_url_sqlserver(config):
# def get_db_url_mysql(config):
# def get_db_url(config):
# def get_db_engine(config):
# def get_db_session(engine, create_tables=False):
# 
# Path: onefl/models/partner_entity.py
# class PartnerEntity(CRUDMixin, DeclarativeBase):
# 
#     """ Store partners sendig us the data """
#     __tablename__ = 'PARTNER'
# 
#     partner_code = db.Column('PARTNER_CODE', db.Text(3), primary_key=True)
#     partner_description = db.Column('PARTNER_DESCRIPTION', db.Text,
#                                     nullable=False)
#     partner_added_at = db.Column('PARTNER_ADDED_AT', db.DateTime,
#                                  nullable=False)
# 
#     def __repr__(self):
#         """ Return a friendly object representation """
#         return "<PartnerEntity (partner_code: {0.partner_code}, " \
#             "partner_description: {0.partner_description}, " \
#             "partner_addded_at: {0.partner_added_at})>".format(self)
# 
# Path: onefl/models/rule_entity.py
# class RuleEntity(CRUDMixin, DeclarativeBase):
# 
#     """ Store rules used by the partners sending us the data """
#     __tablename__ = 'RULE'
# 
#     id = db.Column('RULE_ID', db.Integer, primary_key=True)
#     rule_code = db.Column('RULE_CODE', db.Text(10),
#                           nullable=False, unique=True)
#     rule_description = db.Column('RULE_DESCRIPTION', db.Text(255),
#                                  nullable=False)
#     rule_added_at = db.Column('RULE_ADDED_AT', db.DateTime,
#                               nullable=False)
# 
#     @staticmethod
#     def get_rules_cache(session):
#         """
#         :return dictionary: with all available {rule_code -> rule.id} mappings
#         """
#         rules = session.query(RuleEntity).all()
#         result = {rule.rule_code: rule.id for rule in rules}
#         return result
# 
#     def __repr__(self):
#         """ Return a friendly object representation """
#         return "<RuleEntity(rule_id: {0.id}, "\
#             "rule_code: {0.rule_code}, " \
#             "rule_description: {0.rule_description}, " \
#             "rule_added_at: {0.rule_added_at})>".format(self)
# 
# Path: onefl/rules.py
# RULE_CODE_F_L_D_R = 'F_L_D_R'
# 
# RULE_CODE_F_L_D_S = 'F_L_D_S'
# 
# RULE_CODE_NO_HASH = 'NO_HASH'


# Path: tests/base_test.py
from unittest import TestCase  # noqa
from datetime import datetime
from sqlalchemy.orm.exc import NoResultFound  # noqa
from onefl.config import Config
from onefl.models import base
from onefl.utils import db
from sqlalchemy.orm.exc import MultipleResultsFound  # noqa
from onefl.models.partner_entity import PartnerEntity
from onefl.models.rule_entity import RuleEntity
from onefl.rules import RULE_CODE_F_L_D_R, RULE_CODE_F_L_D_S, RULE_CODE_NO_HASH  # noqa

"""
Goal: implement base class for tests using the database

@authors:
    Andrei Sura <sura.andrei@gmail.com>
"""


SETTINGS_FILE = 'config/test_settings_linker.py'


class BaseTestCase(TestCase):
    """
    Tests for database interaction extend this class.
    @see http://docs.sqlalchemy.org/en/latest/orm/session_transaction.html#session-external-transaction  # noqa
    """

    def __init__(self, *args, **kwargs):
        super(BaseTestCase, self).__init__(*args, **kwargs)

    def setUp(self):
        """ Set the test database engine/session """

        super(BaseTestCase, self).setUp()

        config = Config(root_path='.', defaults={})