def test_account_withdraw_with_correct_params(account_domain_entity, params, unit_of_work):
    uowm = unit_of_work
    unit_of_work.accounts.add_account(account_domain_entity)

    account_withdraw = uc_account.AccountWithdrawUseCase(uowm)
    req = rq.AccountOperationRequest.from_dict({'account':params})
    resp = account_withdraw.execute(req)
    
    assert bool(resp) is True
    assert unit_of_work._committed
def test_account_withdraw_nonexistent_account(params, unit_of_work):
    uowm = unit_of_work

    account_withdraw = uc_account.AccountWithdrawUseCase(uowm)
    req = rq.AccountOperationRequest(account=params)
    resp = account_withdraw.execute(req)
    
    assert bool(resp) is False
    assert unit_of_work._committed  is False
    assert resp.value == {'type': rs.ResponseFailure.RESOURCE_ERROR,
                        'message': 'account not found'}
def test_account_withdraw_wrong_params_format(unit_of_work):
    uowm = unit_of_work

    account_withdraw = uc_account.AccountWithdrawUseCase(uowm)
    req = rq.AccountOperationRequest(account=0)
    resp = account_withdraw.execute(req)
    
    assert bool(resp) is False
    assert unit_of_work._committed  is False
    assert resp.value == {'type': rs.ResponseFailure.PARAMETERS_ERROR,
                        'message': 'account : parameter not in the correct format'}
def test_account_withdraw_no_params(unit_of_work):
    uowm = unit_of_work

    account_withdraw = uc_account.AccountWithdrawUseCase(uowm)
    req = rq.AccountOperationRequest()
    resp = account_withdraw.execute(req)
    
    assert bool(resp) is False
    assert unit_of_work._committed  is False
    assert resp.value == {'type': rs.ResponseFailure.PARAMETERS_ERROR,
                        'message': 'account : parameter is required'}
def test_account_withdraw_wrong_amount(account_domain_entity, params, unit_of_work):
    uowm = unit_of_work
    unit_of_work.accounts.add_account(account_domain_entity)

    account_withdraw = uc_account.AccountWithdrawUseCase(uowm)
    wrong_params = {'code':'1234567890F','amount':5000.00}
    req = rq.AccountOperationRequest.from_dict({'account':wrong_params})
    resp = account_withdraw.execute(req)
    
    assert bool(resp) is False
    assert unit_of_work._committed  is False
    assert resp.value == {'type': rs.ResponseFailure.PARAMETERS_ERROR,
                        'message': 'incorrect withdraw amount'}
def test_account_withdraw_error(account_domain_entity, params, unit_of_work):
    with mock.patch.object(unit_of_work.accounts,'account_withdraw', side_effect=Exception('error message')):
        uowm = unit_of_work
        uowm.accounts.add_account(account_domain_entity)

        account_withdraw = uc_account.AccountWithdrawUseCase(uowm)
        req = rq.AccountOperationRequest(account=params)
        resp = account_withdraw.execute(req)
        
        assert bool(resp) is False
        assert unit_of_work._committed  is False
        assert unit_of_work._rolledback
        assert resp.value == {'type': rs.ResponseFailure.SYSTEM_ERROR,
                            'message': 'Exception : error message'}
def account_withdraw():
    if request.headers['Content-Type'] != 'application/json':
        return Response(json.dumps({'message':'content type must be json'}),
                    mimetype='application/json',
                    status=STATUS_CODES[resp.ResponseFailure.PARAMETERS_ERROR])
    else:
        req_object = req.AccountOperationRequest(account=request.json)
        uowm = db.unit_of_work_manager
        usecase = acc_uc.AccountWithdrawUseCase(uowm)

        resp_object = usecase.execute(req_object)

        return Response(json.dumps(resp_object.value, cls=acc_ser.AccountEncoder),
                        mimetype='application/json',
                        status=STATUS_CODES[resp_object.type])