Skip to content

Commit

Permalink
Move implementation details from cli to service
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytrostriletskyi committed Apr 23, 2019
1 parent f53d1dc commit 739fcc3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
14 changes: 7 additions & 7 deletions cli/account/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
Provide implementation of the command line interface's account commands.
"""
import asyncio
import sys

import click
Expand All @@ -20,8 +19,6 @@
print_result,
)

loop = asyncio.get_event_loop()


@click.group('account', chain=True)
def account_commands():
Expand All @@ -44,7 +41,7 @@ def get_balance(address, node_url):
})

if errors:
print_errors(errors)
print_errors(errors=errors)
sys.exit(FAILED_EXIT_FROM_COMMAND_CODE)

address = arguments.get('address')
Expand All @@ -54,7 +51,10 @@ def get_balance(address, node_url):
'node_address': str(node_url) + ':8080',
})

account_service = Account(service=remme)
balance = loop.run_until_complete(account_service.get_balance(address=address))
result, errors = Account(service=remme).get_balance(address=address)

if errors is not None:
print_errors(errors=errors)
sys.exit(FAILED_EXIT_FROM_COMMAND_CODE)

print_result(balance)
print_result(result=result)
2 changes: 1 addition & 1 deletion cli/account/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class AccountInterface:
Implements account interface.
"""

async def get_balance(self, address):
def get_balance(self, address):
"""
Get balance of the account by its address.
"""
Expand Down
12 changes: 10 additions & 2 deletions cli/account/service.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
"""
Provide implementation of the account.
"""
import asyncio

from accessify import implements

from cli.account.interfaces import AccountInterface

loop = asyncio.get_event_loop()


@implements(AccountInterface)
class Account:
Expand All @@ -21,8 +25,12 @@ def __init__(self, service):
"""
self.service = service

async def get_balance(self, address):
def get_balance(self, address):
"""
Get balance of the account by its address.
"""
return await self.service.token.get_balance(address=address)
balance = loop.run_until_complete(self.service.token.get_balance(address=address))

return {
'result': balance,
}, None
24 changes: 13 additions & 11 deletions tests/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
PASSED_EXIT_FROM_COMMAND_CODE,
)
from cli.entrypoint import cli
from cli.utils import (
dict_to_pretty_json,
return_async_value,
)
from cli.utils import dict_to_pretty_json


def test_get_balance():
Expand All @@ -32,8 +29,10 @@ def test_get_balance():
NODE_IP_ADDRESS_FOR_TESTING,
])

balance = json.loads(result.output).get('result')

assert PASSED_EXIT_FROM_COMMAND_CODE == result.exit_code
assert isinstance(json.loads(result.output), int)
assert isinstance(balance, int)


def test_get_balance_invalid_address():
Expand Down Expand Up @@ -66,12 +65,12 @@ def test_get_balance_invalid_address():
def test_get_balance_without_node_url(mocker):
"""
Case: get a balance of an account by address without passing node URL.
Expect: balance is returned.
Expect: balance is returned from node on localhost.
"""
balance_from_localhost = 13500
balance = 13500

mock_account_get_balance = mocker.patch('cli.account.service.Account.get_balance')
mock_account_get_balance.return_value = return_async_value(balance_from_localhost)
mock_account_get_balance = mocker.patch('cli.account.service.loop.run_until_complete')
mock_account_get_balance.return_value = balance

runner = CliRunner()
result = runner.invoke(cli, [
Expand All @@ -81,9 +80,12 @@ def test_get_balance_without_node_url(mocker):
'1120076ecf036e857f42129b58303bcf1e03723764a1702cbe98529802aad8514ee3cf',
])

expected_result = {
'result': 13500,
}

assert PASSED_EXIT_FROM_COMMAND_CODE == result.exit_code
assert isinstance(json.loads(result.output), int)
assert str(balance_from_localhost) in result.output
assert expected_result == json.loads(result.output)


def test_get_balance_invalid_node_url():
Expand Down

0 comments on commit 739fcc3

Please sign in to comment.