"""This module contains the tests for the generic import system."""

from django.test import TestCase
from django.test.utils import override_settings

from . import mtp_import


TEST_CLIENT = 'test_client'
NO_SUCH_CLIENT = 'no_such_client'

MODULE_PATH = 'mtp.apps.test_app'

GENERIC = 'GENERIC'
SPECIFIC = 'SPECIFIC'


class GenericImportTestCase(TestCase):
    """Test the generic import system."""

    def test_mtp_import(self):
        """Test the nominal ``mtp_import`` case."""

        with override_settings(INSTANCE_NAME=TEST_CLIENT):
            module = mtp_import(MODULE_PATH)
            self.assertEqual(module.test(), SPECIFIC)

        with override_settings(INSTANCE_NAME=NO_SUCH_CLIENT):
            module = mtp_import(MODULE_PATH)
            self.assertEqual(module.test(), GENERIC)

    def test_force_generic_mtp_import(self):
        """Test the ``force_generic`` ``mtp_import`` case."""

        with override_settings(INSTANCE_NAME=TEST_CLIENT):
            module = mtp_import(MODULE_PATH, force_generic=True)
            self.assertEqual(module.test(), GENERIC)

        with override_settings(INSTANCE_NAME=NO_SUCH_CLIENT):
            module = mtp_import(MODULE_PATH, force_generic=True)
            self.assertEqual(module.test(), GENERIC)

    def test_force_specific_mtp_import(self):
        """Test the ``force_specific`` ``mtp_import`` case."""

        with override_settings(INSTANCE_NAME=TEST_CLIENT):
            module = mtp_import(MODULE_PATH, force_specific=True)
            self.assertEqual(module.test(), SPECIFIC)

        with override_settings(INSTANCE_NAME=NO_SUCH_CLIENT):
            with self.assertRaises(ImportError):
                mtp_import(MODULE_PATH, force_specific=True)

    def test_sanity_check_mtp_import(self):
        """Test that it is impossible to call ``mtp_import`` with absurd
        parameters."""

        with self.assertRaises(RuntimeError):
            mtp_import(MODULE_PATH, force_generic=True, force_specific=True)
