import json
import unittest

from aws_lambda_tools.common.response import Response

from conio_sdk.common import exceptions
from conio_sdk.common.exceptions_mappings import get_response_for_exception


class TestExceptionMappings(unittest.TestCase):
    def test(self):
        i = None
        for i, e in enumerate(filter(
                lambda ex: isinstance(ex, type) and issubclass(ex, Exception) and ex != exceptions.BFFException,
                map(lambda k: getattr(exceptions, k), dir(exceptions)))):
            self.assertIsInstance(get_response_for_exception(e()), Response)
        self.assertTrue(i)

    def test_names(self):
        resp = get_response_for_exception(exceptions.WalletAlreadyOwnedByAnotherUserException())
        self.assertEqual(409, resp.status_code)
        self.assertEqual(
            {"error_code": "WALLET_ALREADY_OWNED_BY_ANOTHER_USER", "arg": None},
            json.loads(resp.content)
        )

    def test_no_such_exception(self):
        with self.assertRaises(ValueError):
            # noinspection PyTypeChecker
            get_response_for_exception(FileExistsError())
