# Path: falconswagger/middlewares.py
# class SessionMiddleware(object):
# 
#     def __init__(self, sqlalchemy_bind=None, redis_bind=None):
#         self.sqlalchemy_bind = sqlalchemy_bind
#         self.redis_bind = redis_bind
# 
#     def process_resource(self, req, resp, model, uri_params):
#         if getattr(model, '__session__', None):
#             req.context['session'] = model.__session__
#             return
# 
#         req.context['session'] = Session(bind=self.sqlalchemy_bind, redis_bind=self.redis_bind)
# 
#     def process_response(self, req, resp, model):
#         session = req.context.pop('session', None)
#         if session is not None \
#                 and hasattr(session, 'close') \
#                 and not getattr(model, '__session__', None):
#             session.close()
# 
# Path: falconswagger/models/orm/sqlalchemy_redis.py
# class ModelSQLAlchemyRedisFactory(object):
# 
#     @staticmethod
#     def make(name='ModelSQLAlchemyRedisBase', bind=None, metadata=None,
#              mapper=None, class_registry=None, authorizer=None, keys_separator=b'|'):
#         base = declarative_base(
#             name=name, metaclass=ModelSQLAlchemyRedisMeta,
#             cls=_ModelSQLAlchemyRedisBase, bind=bind, metadata=metadata,
#             mapper=mapper, constructor=_ModelSQLAlchemyRedisBase.__init__)
#         base.__authorizer__ = authorizer
#         base.__keys_separator__ = \
#             keys_separator.decode() if isinstance(keys_separator, str) else keys_separator
#         return base


from falconswagger.middlewares import SessionMiddleware
from falconswagger.models.orm.sqlalchemy_redis import ModelSQLAlchemyRedisFactory
from unittest import mock
import pytest


@pytest.fixture
def bind():
    return mock.MagicMock()


@pytest.fixture
def redis_bind():
    return mock.MagicMock()


@pytest.fixture
def sqlalchemy_middleware(bind, redis_bind):
    return SessionMiddleware(bind, redis_bind)


class TestFalconSQLAlchemyRedisMiddleware(object):
    @mock.patch('falconswagger.middlewares.Session')
    def test_process_resource(self, session, sqlalchemy_middleware, bind, redis_bind):
        req = mock.MagicMock(
            method='POST', uri_template='/test', context=dict())
        resp = mock.MagicMock()
        resource = ModelSQLAlchemyRedisBase
        params = mock.MagicMock()

        sqlalchemy_middleware.process_resource(req, resp, resource, params)

        assert session.call_args_list == [
            mock.call(bind=bind, redis_bind=redis_bind)]

# Path: falconswagger/middlewares.py
# class SessionMiddleware(object):
# 
#     def __init__(self, sqlalchemy_bind=None, redis_bind=None):
#         self.sqlalchemy_bind = sqlalchemy_bind
#         self.redis_bind = redis_bind
# 
#     def process_resource(self, req, resp, model, uri_params):
#         if getattr(model, '__session__', None):
#             req.context['session'] = model.__session__
#             return
# 
#         req.context['session'] = Session(bind=self.sqlalchemy_bind, redis_bind=self.redis_bind)
# 
#     def process_response(self, req, resp, model):
#         session = req.context.pop('session', None)
#         if session is not None \
#                 and hasattr(session, 'close') \
#                 and not getattr(model, '__session__', None):
#             session.close()
# 
# Path: falconswagger/models/orm/sqlalchemy_redis.py
# class ModelSQLAlchemyRedisFactory(object):
# 
#     @staticmethod
#     def make(name='ModelSQLAlchemyRedisBase', bind=None, metadata=None,
#              mapper=None, class_registry=None, authorizer=None, keys_separator=b'|'):
#         base = declarative_base(
#             name=name, metaclass=ModelSQLAlchemyRedisMeta,
#             cls=_ModelSQLAlchemyRedisBase, bind=bind, metadata=metadata,
#             mapper=mapper, constructor=_ModelSQLAlchemyRedisBase.__init__)
#         base.__authorizer__ = authorizer
#         base.__keys_separator__ = \
#             keys_separator.decode() if isinstance(keys_separator, str) else keys_separator
#         return base


# Path: tests/unit/base/test_middlewares.py
from falconswagger.middlewares import SessionMiddleware
from falconswagger.models.orm.sqlalchemy_redis import ModelSQLAlchemyRedisFactory
from unittest import mock
import pytest



@pytest.fixture
def bind():
    return mock.MagicMock()


@pytest.fixture
def redis_bind():
    return mock.MagicMock()


@pytest.fixture
def sqlalchemy_middleware(bind, redis_bind):
    return SessionMiddleware(bind, redis_bind)


class TestFalconSQLAlchemyRedisMiddleware(object):
    @mock.patch('falconswagger.middlewares.Session')
    def test_process_resource(self, session, sqlalchemy_middleware, bind, redis_bind):
        req = mock.MagicMock(
            method='POST', uri_template='/test', context=dict())
        resp = mock.MagicMock()
        resource = ModelSQLAlchemyRedisBase
        params = mock.MagicMock()

        sqlalchemy_middleware.process_resource(req, resp, resource, params)

        assert session.call_args_list == [
            mock.call(bind=bind, redis_bind=redis_bind)]

        assert req.context['session'] == session.return_value