"""Settings to use when testing the project

Tests may not work without these settings
"""

from .base import *


DEBUG = False

if get_bool_env_variable('INSTALL_MTP_TEST_APPS', default=True):
    INSTALLED_APPS += (
        # Some test cases define new django models only for test purpose, so to let
        # django create the db table, they must be apps in ``INSTALLED_APPS``
        'mtp.apps.api.tests.api_testapp',
        'mtp.apps.common.tests.limpyd_base',
        'mtp.apps.common.tests.common_testapp',
        'mtp.discrete_apps.versionable.tests.versionable_testapp',
        'mtp.discrete_apps.duplicable.tests.duplicable_testapp',
        'mtp.discrete_apps.lockable.tests.lockable_testapp',
        'mtp.discrete_apps.hashable.tests.hashable_testapp',
        'mtp.apps.likes.tests.likeable_testapp',
        'mtp.apps.pdf_reporting.tests.pdf_reporting_testapp',
    )

# See https://docs.djangoproject.com/en/1.8/topics/testing/overview/#speeding-up-the-tests
PASSWORD_HASHERS = (
    'django.contrib.auth.hashers.MD5PasswordHasher',
)

USE_FAKEREDIS = get_bool_env_variable('USE_FAKEREDIS', default=True)

if USE_FAKEREDIS:
    # Make the cache use ``fakeredis`` if it is configured to use redis.
    if get_bool_env_variable('USE_REDIS_FOR_CACHE', default=True):
        CACHES['default']['BACKEND'] = 'mtp.apps.common.tests.redis_cache.FakeRedisCache'

    # Make the sessions use ``fakeredis`` if they are configured to use redis.
    if get_bool_env_variable('USE_REDIS_FOR_SESSIONS', default=True):
        SESSION_ENGINE = 'mtp.apps.common.tests.redis_session'

    # Make all our limpyd models use ``fakeredis``.
    from mtp.apps.common.limpyd_base import  BaseModel
    from mtp.apps.common.tests.limpyd_base.limpyd_database import limpyd_test_database
    BaseModel.use_database(limpyd_test_database)

import logging
for logger_name in ('factory', 'duplicate'):
    logger = logging.getLogger(logger_name)
    logger.setLevel(logging.ERROR)


if get_bool_env_variable('TESTS_SKIP_MIGRATIONS', default=False):
    """
    Hack to disable migrations for tests. It both makes creating the test database quicker,
    and allows experimenting without having to create migrations for each model change.
    It has no effect if you are reusing the database (which is the default with py.test).

    Do not use if you want to tests newly added migrations.

    Run it like this:
    $ TESTS_SKIP_MIGRATIONS='True' py.test --create-db path.to.tests

    """

    class DisableMigrations(object):
        """
        Hack to disable migrations for tests. It both makes creating the test database quicker,
        and allows experimenting without having to create migrations for each model change.
        It has no effect if you are reusing the database (which is the default).

        Do not use if you want to tests newly added migrations.

        http://stackoverflow.com/questions/25161425/disable-migrations-when-running-unit-tests-in-django-1-7#comment43230116_25267435

        Te be set in settings this way:

            MIGRATIONS_MODULE = DisableMigrations
        """

        def __contains__(self, app_label):
            """
            Always return True to tell django we have the migrations for all the apps.
            Then in `__getitem__` we'll return an invalid migration.
            """
            return True

        def __getitem__(self, app_label):
            """
            Always return a invalid migration to tell django to skip trying to apply
            this migration.
            """
            return "notmigrations"

    MIGRATION_MODULES = DisableMigrations()


# Celery setttings for tests
# --------------------------

# See http://docs.celeryproject.org/en/2.5/django/unit-testing.html
# 
# If you want to, you can also run a real Cerlery worker in the background.
# 
# To do so, you'll need :
# - A working redis instance (as defined in settings/base.py)
# - Set the ENV var `CELERY_ALWAYS_EAGER` to False (CELERY_ALWAYS_EAGER='False')
# - Launch the Celery worker: 
# >>> celery worker -A mtp --loglevel=INFO

CELERY_ALWAYS_EAGER = get_bool_env_variable('CELERY_ALWAYS_EAGER', default=True)
