Ejemplo n.º 1
0
 def from_dict(cls, dict):
     trs = cls(trs_id=dict['trs_id'],
               trs_timestamp=dict['trs_timestamp'],
               trs_from=Account.from_dict(dict['trs_from']),
               trs_to=Account.from_dict(dict['trs_to']),
               trs_amount=dict['trs_amount'])
     return trs
def unit_of_work():
    uow = FakeUnitOfWork()
    uow.accounts.add_account(
        Account.from_dict({
            'code': '1234567890F',
            'balance': 1000.00
        }))
    uow.accounts.add_account(
        Account.from_dict({
            'code': '7765432255B',
            'balance': 10000.00
        }))
    return uow
Ejemplo n.º 3
0
def test_account_from_dict():
    code = '2222222222M'
    dict = {'id': id, 'code': code, 'balance': 321.10}
    account = Account.from_dict(dict)

    assert account.code == code
    assert account.balance == 321.10
Ejemplo n.º 4
0
def test_account_serielizer():
    test_account = Account(account_code="1234556789V",
                           account_balance=12340.00)
    expected = '{"code" : "1234556789V", "balance" : 12340.00}'

    json_account = json.dumps(test_account, cls=AccountEncoder)
    assert json.loads(json_account) == json.loads(expected)
Ejemplo n.º 5
0
def test_transfer_serielizer():
    _id = str(uuid.uuid4())
    test_trs = Transfer(trs_id=_id,
                        trs_timestamp="2019-01-22 09:00:00",
                        trs_from=Account.from_dict({
                            "code": "222222222M",
                            "balance": 1000
                        }),
                        trs_to=Account.from_dict({
                            "code": "1111111111V",
                            "balance": 1000
                        }),
                        trs_amount=321.45)
    expected = '''
            {
                "trs_id":"''' + _id + '''",
                "trs_timestamp":"2019-01-22 09:00:00",
                "trs_from":{"code":"222222222M", "balance":1000},
                "trs_to":{"code":"1111111111V", "balance":1000},
                "trs_amount":321.45
            }'''

    json_trs = json.dumps(test_trs, cls=TransferEncoder)
    assert json.loads(json_trs) == json.loads(expected)
Ejemplo n.º 6
0
 def execute(self, request):
     with self._uowm.start() as unit_of_work:
         try:
             if request:
                 _found_account = unit_of_work.accounts.find_account(account_code= request.account['code'])
                 if(_found_account is None):
                     unit_of_work.accounts.add_account(account= Account(account_code=request.account['code'], 
                                                                         account_balance=request.account['amount']))
                     unit_of_work.commit()
                     response = rs.ResponseSuccess()    
                 else:
                     response = rs.ResponseFailure.build_resource_error('account already exists')
             else:
                 response = rs.ResponseFailure.from_invalid_request(request)
         except Exception as e:
             unit_of_work.rollback()
             response = rs.ResponseFailure.build_system_error(e)
     return response
def transfer_domain_entities():
    test_trs1 = Transfer(trs_id=uuid.uuid4(),
                         trs_timestamp="2019-01-22 09:00:00",
                         trs_from=Account("1234567890F", 1000),
                         trs_to=Account("3333333333A", 1000),
                         trs_amount=321.00)
    test_trs2 = Transfer(trs_id=uuid.uuid4(),
                         trs_timestamp="2019-01-22 09:10:20",
                         trs_from=Account("4444444444C", 2000),
                         trs_to=Account("7765432255B", 1000),
                         trs_amount=1234.50)
    test_trs3 = Transfer(trs_id=uuid.uuid4(),
                         trs_timestamp="2019-01-23 15:05:04",
                         trs_from=Account("5555555555E", 1000),
                         trs_to=Account("6666666666V", 1000),
                         trs_amount=400.00)
    test_trs4 = Transfer(trs_id=uuid.uuid4(),
                         trs_timestamp="2019-01-24 14:00:59",
                         trs_from=Account("7777777777A", 1000),
                         trs_to=Account("1111188888B", 1000),
                         trs_amount=492.50)

    return [test_trs1, test_trs2, test_trs3, test_trs4]
Ejemplo n.º 8
0
def test_account_entity_init():
    code = '1111111111V'
    account = Account(code, account_balance=100.00)

    assert account.code == code
    assert account.balance == 100.00
Ejemplo n.º 9
0
def test_account_comparison():
    dict = {'code': '1234522222F', 'balance': 1200.00}
    account1 = Account.from_dict(dict)
    account2 = Account.from_dict(dict)

    assert account1 == account2
Ejemplo n.º 10
0
def test_account_to_dict():
    dict = {'code': '1234522222F', 'balance': 1200.00}

    account = Account.from_dict(dict)

    assert dict == account.to_dict()
Ejemplo n.º 11
0
import uuid
from bankaccount.entities.account import Account
from bankaccount.entities.transfer import Transfer

acc1 = Account('1111111111V', 1500.00)
acc2 = Account('2222222222M', 3200.00)


def test_transfer_entity_init():
    id = uuid.uuid4()
    trs = Transfer(trs_id=id,
                   trs_timestamp='2019-01-21 00:00:00',
                   trs_from=acc1,
                   trs_to=acc2,
                   trs_amount=45.30)

    assert trs.trs_id == id
    assert trs.trs_timestamp == '2019-01-21 00:00:00'
    assert trs.trs_from == acc1
    assert trs.trs_to == acc2
    assert trs.trs_amount == 45.30


def test_transfer_from_dict():
    id = uuid.uuid4()
    dict = {
        'trs_id': id,
        'trs_timestamp': '2019-01-21 09:00:00',
        'trs_from': acc1.to_dict(),
        'trs_to': acc2.to_dict(),
        'trs_amount': 100.00
Ejemplo n.º 12
0
import json
from unittest import mock

from bankaccount.usecases import response_objects as resp
from bankaccount.entities.account import Account


test_account = Account(
        account_code="1234567890F",
        account_balance=9900.00
    )

mimetype = 'application/json'
headers = {
        'Content-Type': mimetype,
        'Accept': mimetype
    }
data = {
        'code': '1234567890F',
        'amount': 100.00
    }

@mock.patch('bankaccount.usecases.account_usecases.AccountWithdrawUseCase')
def test_put_account_withdraw_success(mock_usecase, client):
    mock_usecase().execute.return_value = resp.ResponseSuccess(test_account)
    http_response = client.put('/account-withdraw', data=json.dumps(data), headers=headers)

    assert http_response.status_code == 200
    assert http_response.mimetype == 'application/json'

Ejemplo n.º 13
0
 def find_account(self, account_code= None):
     _elem = next((e for e in self._entries if e['code']==account_code), None)
     return Account.from_dict(_elem) if _elem else None
def account_domain_entity():
    return Account(
        account_code="1234567890F",
        account_balance=3200.00
    )
Ejemplo n.º 15
0
import pytest
import json
from bankaccount.adapters.orm import SqlAlchemy
from bankaccount.entities.transfer import Transfer
from bankaccount.entities.account import Account

test_acc_req1 = {'code': '1234567890F', 'amount': 10000.00}
test_acc_req2 = {'code': '0101010101A', 'amount': 10000.00}
test_acc_req3 = {'code': '3216540987V', 'amount': 10000.00}

test_acc1 = Account(account_code='1234567890F', account_balance=10000.00)
test_acc2 = Account(account_code='0101010101A', account_balance=10000.00)

test_trs_req1 = {'from': '1234567890F', 'to': '0101010101A', 'amount': 100.00}

test_trs_req2 = {'from': '0101010101A', 'to': '1234567890F', 'amount': 500.00}

mimetype = 'application/json'

headers = {'Content-Type': mimetype, 'Accept': mimetype}


@pytest.fixture(scope='module')
def db():
    db = SqlAlchemy('sqlite:///bankaccount.db')
    db.configure_mappings()
    db.recreate_schema()
    return db


@pytest.fixture
import json
from unittest import mock

from bankaccount.usecases import response_objects as resp
from bankaccount.entities.transfer import Transfer
from bankaccount.entities.account import Account

test_trs = Transfer(trs_id=1,
                    trs_timestamp="2019-01-22 09:00:00",
                    trs_from=Account.from_dict({
                        'code': "1234567890F",
                        'balance': 1000
                    }),
                    trs_to=Account.from_dict({
                        'code': "3333333333A",
                        'balance': 1000
                    }),
                    trs_amount=321.00)

mimetype = 'application/json'
headers = {'Content-Type': mimetype, 'Accept': mimetype}
data = {'from': '1234567890F', 'to': '3333333333A', 'amount': 321.00}


@mock.patch('bankaccount.usecases.transfer_usecases.TransferAmountUseCase')
def test_post_transfer(mock_usecase, client):
    mock_usecase().execute.return_value = resp.ResponseSuccess(test_trs)
    http_response = client.post('/transfer',
                                data=json.dumps(data),
                                headers=headers)