def test_handleCreateAccount_ok(self):

        repository = InMemoryRepository()
        commandHandler = AccountCommandHandler(repository)
        command = CreateAccount(
            '{ "account": { "activeCard": true, "availableLimit": 100 } }')

        expectedResult = {
            "account": {
                "activeCard": True,
                "availableLimit": 100
            },
            "violations": []
        }
        expectedAccount = {
            "account": {
                "activeCard": True,
                "availableLimit": 100
            }
        }

        result = commandHandler.handle_CreateAccount(command)

        self.assertEqual(expectedResult, result)

        resultAccount = repository.getByFilter(lambda _: True)[0].toDict()
        self.assertDictEqual(resultAccount, expectedAccount)
    def test_handleCreateAccount_ok(self):

        accountRepository = InMemoryRepository()
        transactionRepository = InMemoryRepository()

        commandHandler = TransactionCommandHandler(transactionRepository,
                                                   accountRepository)

        accountRepository.add(Account(True, 100))

        command = AuthorizeTransaction(
            '{ "transaction": { "merchant": "Burger King", "amount": 10, "time": "2019-02-13T10:00:00.000Z" } }'
        )

        expectedResult = {
            "account": {
                "activeCard": True,
                "availableLimit": 90
            },
            "violations": []
        }
        expectedAccount = {
            "account": {
                "activeCard": True,
                "availableLimit": 90
            }
        }

        result = commandHandler.handle_AuthorizeTransaction(command)

        self.assertEqual(expectedResult, result)

        resultAccount = accountRepository.getByFilter(
            lambda _: True)[0].toDict()
        self.assertDictEqual(resultAccount, expectedAccount)
    def test_handleCreateAccount_InsufficientLimit(self):

        accountRepository = InMemoryRepository()
        transactionRepository = InMemoryRepository()

        commandHandler = TransactionCommandHandler(transactionRepository,
                                                   accountRepository)

        accountRepository.add(Account(True, 100))

        command = AuthorizeTransaction(
            '{ "transaction": { "merchant": "Burger King", "amount": 101, "time": "2019-02-13T10:00:00.000Z" } }'
        )

        expectedResult = {
            "account": {
                "activeCard": True,
                "availableLimit": 100
            },
            "violations": ['insufficient-limit']
        }

        result = commandHandler.handle_AuthorizeTransaction(command)

        self.assertEqual(expectedResult, result)

        transactions = transactionRepository.getByFilter(lambda _: True)
        self.assertListEqual([], transactions)
    def test_RepoGetByFilter_NotFound(self):
        repo = InMemoryRepository()

        class TestModel(Model):
            def __init__(self):
                super().__init__()

        model = TestModel()

        repo.add(model)

        result = repo.getByFilter(lambda x: False)

        self.assertListEqual([], result)
    def test_RepoGetByFilter_ok(self):
        repo = InMemoryRepository()

        class TestModel(Model):
            def __init__(self):
                super().__init__()

        model = TestModel()

        repo.add(model)

        result = repo.getByFilter(lambda x: x.id == model.id)

        self.assertListEqual([model], result)
    def test_handleCreateAccount_AlreadyInitialized(self):

        repository = InMemoryRepository()
        repository.add(Account(True, 42))
        commandHandler = AccountCommandHandler(repository)
        command = CreateAccount(
            '{ "account": { "activeCard": true, "availableLimit": 100 } }')

        expectedResult = {
            "account": {
                "activeCard": True,
                "availableLimit": 42
            },
            "violations": ["account-already-initialized"]
        }

        result = commandHandler.handle_CreateAccount(command)

        self.assertDictEqual(expectedResult, result)

        emptyResults = repository.getByFilter(
            lambda account: account.availableLimit == 100)
        self.assertListEqual([], emptyResults)