コード例 #1
0
    def test_load(self):
        """ load: check if schama loads data without errors"""
        # given
        data = get_transaction_data()

        # when
        transaction, errors = self.schema.load(data)

        # then
        self.assertIsNotNone(transaction)
        self.assertFalse(errors)
コード例 #2
0
    def test_post_deposito_account_not_found(self):
        # given
        transaction_data = get_transaction_data()

        # when
        response = self.app.test_client().post(
            f'/transactions/99',
            data=json.dumps(transaction_data),
            headers=self.header
        )
        json.loads(response.data)

        # then
        self.assert404(response)
コード例 #3
0
    def test_load_invalid_transaction_type(self):
        """ load: check if schema raises exception if invalid transaction
        type is provided.
        """
        # given
        expected_msg = operation_type_error.format(INVALID_TRANSACTION_TYPE)
        data = get_transaction_data()
        data['transaction_type'] = INVALID_TRANSACTION_TYPE

        # when
        with self.assertRaises(ApiException) as error_context:
            _, _ = self.schema.load(data)

        # then
        exception = error_context.exception
        self.assertEqual(exception.errors['details'], expected_msg)
        exception.errors['details'] = None
        self.assertEqual(exception.errors, Failures.unsupported_operation)
コード例 #4
0
    def test_post_deposito(self):
        # given
        account = self.create_account()
        balance = account.balance
        transaction_data = get_transaction_data()
        transaction_value = Decimal(transaction_data['value'])
        transaction_data['account_id'] = account.id

        # when
        response = self.app.test_client().post(
            '/transactions', data=json.dumps(transaction_data),
            headers=self.header)
        response_data = json.loads(response.data)

        # then
        self.assertTrue(response_data['transaction_date'])
        self.assertEqual(response.status_code, 201)
        self.assertEqual(account.balance, balance + transaction_value)
コード例 #5
0
    def test_post_saque_blocked_account(self):
        # given
        account = self.create_account(active_flag=False)
        transaction_data = get_transaction_data()
        transaction_data['transaction_type'] = 'withdraw'
        transaction_data['value'] = '10.00'
        transaction_data['account_id'] = account.id

        # when
        response = self.app.test_client().post(
            '/transactions', data=json.dumps(transaction_data),
            headers=self.header)
        response_data = json.loads(response.data)

        # then
        self.assert400(response)
        self.assertEqual(
            response_data['error'], AccountFailures.blocked_account
        )
コード例 #6
0
    def test_post_saque_limit_exceeded(self):
        # given
        account = self.create_account(daily_withdraw_limit=Decimal('50.00'))

        transaction_data = get_transaction_data()
        transaction_data['transaction_type'] = 'withdraw'
        transaction_data['value'] = '60.00'
        transaction_data['account_id'] = account.id

        # when
        response = self.app.test_client().post(
            '/transactions', data=json.dumps(transaction_data),
            headers=self.header)
        response_data = json.loads(response.data)
        expected_response = AccountFailures.limit_exceeded
        expected_response['details'] = daily_withdraw_limit

        # then
        self.assert400(response)
        self.assertEqual(response_data['error'], expected_response)
コード例 #7
0
    def test_load_missing_fields(self):
        """ load: check if schema raises errors if any required field is
        missing
        """
        # given
        data = get_transaction_data()
        required_fields = get_schema_required_fields(TransactionSchema)
        [data.pop(k) for k in required_fields]

        # when
        with self.assertRaises(RequestDataException) as error_context:
            _, _ = self.schema.load(data)

        # then
        self.assertEqual(
            set(error_context.exception.errors['details'].keys()) -
            set(required_fields),
            set([]))
        error_context.exception.errors['details'] = None
        self.assertEqual(
            error_context.exception.errors, CommonFailures.information_missing
        )
コード例 #8
0
    def test_post_saque_insufficient_funds(self):
        # given
        account = self.create_account(
            balance=Decimal('200.00'), daily_withdraw_limit=Decimal('1000.00')
        )

        transaction_data = get_transaction_data()
        transaction_data['transaction_type'] = 'withdraw'
        transaction_data['value'] = '1000.00'
        transaction_data['account_id'] = account.id

        # when
        response = self.app.test_client().post(
            '/transactions', data=json.dumps(transaction_data),
            headers=self.header)
        response_data = json.loads(response.data)

        # then
        self.assert400(response)
        self.assertEqual(
            response_data['error'], AccountFailures.insufficiente_funds
        )