# Path: pushkin/request/event_handlers.py
# class EventHandler():
#     def __init__(self, event_id):
#         self.event_id = event_id
# 
#     def handle_event(self, event, event_params):
#         raise Exception("Not implemented!")
# 
#     def validate(self, event, event_params):
#         return event.has_field('user_id') and event.has_field('timestamp')
# 
# class EventHandlerManager():
#     """Responsible for invoking event handlers for given events."""
# 
#     def __init__(self):
#         self._event_handler_map = defaultdict(list)
#         self._build()
# 
#     def _add_event_handler(self, handler):
#         self._event_handler_map[handler.event_id].append(handler)
# 
#     def _build(self):
#         self._add_event_handler(LoginEventHandler())
#         self._add_event_handler(TurnOffNotificationEventHandler())
#         event_to_message_mapping = database.get_event_to_message_mapping()
#         for event_id, message_ids in event_to_message_mapping.iteritems():
#             self._add_event_handler(EventToMessagesHandler(event_id, message_ids))
# 
#     def get_handlers(self, event_id):
#         return self._event_handler_map[event_id]
# 
#     def get_event_ids(self):
#         return self._event_handler_map.keys()
# 
# Path: pushkin/database/database.py
# ENGINE = None
# SESSION = None
# ALEMBIC_CONFIG = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'alembic.ini')
#         ENGINE = create_engine(config.sqlalchemy_url, poolclass=pool.NullPool)
#         SESSION = sessionmaker(bind=ENGINE)
# def init_db():
# def create_database():
# def upgrade_database():
# def get_head_revision():
# def get_current_revision():
# def execute_query(query):
# def execute_query_with_results(query):
# def session_scope():
# def get_device_tokens(login_id):
# def get_raw_messages(login_id, title, content, screen, game, world_id, dry_run, message_id=0, event_ts_bigint=None,
#                      expiry_millis=None, priority=GCM2.PRIORITY_NORMAL, filter_platform_id=None, filter_device_token=None):
# def update_canonicals(canonicals):
# def update_unregistered_devices(unregistered):
# def process_user_login(login_id, language_id, platform_id, device_token, application_version):
# def upsert_login(login_id, language_id):
# def upsert_device(login_id, platform_id, device_token, application_version, unregistered_ts=None):
# def get_all_logins():
# def get_all_message_blacklist():
# def upsert_message_blacklist(login_id, blacklist):
# def get_login(login_id):
# def get_devices(login):
# def delete_login(login):
# def delete_device(device):
# def get_localized_message(login_id, message_id):
# def upsert_message(message_name, cooldown_ts, trigger_event_id, screen, expiry_millis, priority):
# def upsert_message_localization(message_name, language_id, message_title, message_text):
# def add_message(message_name, language_id, message_title, message_text, trigger_event_id=None, cooldown_ts=None,
#                 screen='', expiry_millis=None, priority=GCM2.PRIORITY_NORMAL):
# def get_all_messages():
# def get_message(message_name):
# def get_message_localizations(message):
# def delete_message(message):
# def delete_message_localization(message_localization):
# def get_event_to_message_mapping():
# def get_and_update_messages_to_send(user_message_set):


import pytest
from pushkin.request.event_handlers import EventHandler, EventHandlerManager
from pushkin.database import database


def test_non_handled_event(setup, manager):
    '''Test that empty list is returned for non handled events.'''
    assert manager.get_handlers(3) == []


def test_handled_event_one_handler(setup, manager):
    '''Test that handler is returned for events with one handler'''
    handlers = manager.get_handlers(2)
    assert len(handlers) == 1
    assert handlers[0].__class__ == EventHandlerA2


def test_handled_event_multiple_handlers(setup, manager):
    '''Test that all handlers are returned for events with multiple handlers.'''
    handlers = manager.get_handlers(1)
    assert len(handlers) == 2
    assert handlers[0].__class__ == EventHandlerA1
    assert handlers[1].__class__ == EventHandlerB1


def test_event_ids(setup, manager):
    '''Test that correct event ids are returned.'''
    event_ids = manager.get_event_ids()
    assert event_ids == [1, 2]


def test_event_manager_event_mapping_handlers(setup):
    """Test of event mapping handlers are properly created according to data in db."""

# Path: pushkin/request/event_handlers.py
# class EventHandler():
#     def __init__(self, event_id):
#         self.event_id = event_id
# 
#     def handle_event(self, event, event_params):
#         raise Exception("Not implemented!")
# 
#     def validate(self, event, event_params):
#         return event.has_field('user_id') and event.has_field('timestamp')
# 
# class EventHandlerManager():
#     """Responsible for invoking event handlers for given events."""
# 
#     def __init__(self):
#         self._event_handler_map = defaultdict(list)
#         self._build()
# 
#     def _add_event_handler(self, handler):
#         self._event_handler_map[handler.event_id].append(handler)
# 
#     def _build(self):
#         self._add_event_handler(LoginEventHandler())
#         self._add_event_handler(TurnOffNotificationEventHandler())
#         event_to_message_mapping = database.get_event_to_message_mapping()
#         for event_id, message_ids in event_to_message_mapping.iteritems():
#             self._add_event_handler(EventToMessagesHandler(event_id, message_ids))
# 
#     def get_handlers(self, event_id):
#         return self._event_handler_map[event_id]
# 
#     def get_event_ids(self):
#         return self._event_handler_map.keys()
# 
# Path: pushkin/database/database.py
# ENGINE = None
# SESSION = None
# ALEMBIC_CONFIG = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'alembic.ini')
#         ENGINE = create_engine(config.sqlalchemy_url, poolclass=pool.NullPool)
#         SESSION = sessionmaker(bind=ENGINE)
# def init_db():
# def create_database():
# def upgrade_database():
# def get_head_revision():
# def get_current_revision():
# def execute_query(query):
# def execute_query_with_results(query):
# def session_scope():
# def get_device_tokens(login_id):
# def get_raw_messages(login_id, title, content, screen, game, world_id, dry_run, message_id=0, event_ts_bigint=None,
#                      expiry_millis=None, priority=GCM2.PRIORITY_NORMAL, filter_platform_id=None, filter_device_token=None):
# def update_canonicals(canonicals):
# def update_unregistered_devices(unregistered):
# def process_user_login(login_id, language_id, platform_id, device_token, application_version):
# def upsert_login(login_id, language_id):
# def upsert_device(login_id, platform_id, device_token, application_version, unregistered_ts=None):
# def get_all_logins():
# def get_all_message_blacklist():
# def upsert_message_blacklist(login_id, blacklist):
# def get_login(login_id):
# def get_devices(login):
# def delete_login(login):
# def delete_device(device):
# def get_localized_message(login_id, message_id):
# def upsert_message(message_name, cooldown_ts, trigger_event_id, screen, expiry_millis, priority):
# def upsert_message_localization(message_name, language_id, message_title, message_text):
# def add_message(message_name, language_id, message_title, message_text, trigger_event_id=None, cooldown_ts=None,
#                 screen='', expiry_millis=None, priority=GCM2.PRIORITY_NORMAL):
# def get_all_messages():
# def get_message(message_name):
# def get_message_localizations(message):
# def delete_message(message):
# def delete_message_localization(message_localization):
# def get_event_to_message_mapping():
# def get_and_update_messages_to_send(user_message_set):


# Path: pushkin/tests/request/test_event_handler_manager.py
import pytest
from pushkin.request.event_handlers import EventHandler, EventHandlerManager
from pushkin.database import database



def test_non_handled_event(setup, manager):
    '''Test that empty list is returned for non handled events.'''
    assert manager.get_handlers(3) == []


def test_handled_event_one_handler(setup, manager):
    '''Test that handler is returned for events with one handler'''
    handlers = manager.get_handlers(2)
    assert len(handlers) == 1
    assert handlers[0].__class__ == EventHandlerA2


def test_handled_event_multiple_handlers(setup, manager):
    '''Test that all handlers are returned for events with multiple handlers.'''
    handlers = manager.get_handlers(1)
    assert len(handlers) == 2
    assert handlers[0].__class__ == EventHandlerA1
    assert handlers[1].__class__ == EventHandlerB1


def test_event_ids(setup, manager):
    '''Test that correct event ids are returned.'''
    event_ids = manager.get_event_ids()
    assert event_ids == [1, 2]


def test_event_manager_event_mapping_handlers(setup):
    """Test of event mapping handlers are properly created according to data in db."""

    manager = EventHandlerManager()