get some tests working

This commit is contained in:
William Luke 2022-01-10 15:43:10 +03:00
parent 64c9fa2538
commit 8aab6c4c2b
10 changed files with 216 additions and 107 deletions

View File

@ -0,0 +1,12 @@
# standard imports
import os
import random
import uuid
def blockchain_address() -> str:
return os.urandom(20).hex().lower()

View File

@ -0,0 +1,132 @@
# standard imports
import os
# external imports
import pytest
from celery import uuid
# test imports
from cic_eth.pytest.helpers.accounts import blockchain_address
@pytest.fixture(scope='function')
def task_uuid():
return uuid()
@pytest.fixture(scope='function')
def default_token_data(foo_token_symbol, foo_token):
return {
'symbol': foo_token_symbol,
'address': foo_token,
'name': 'Giftable Token',
'decimals': 6,
"converters": []
}
@pytest.fixture(scope='function')
def mock_account_creation_task_request(mocker, task_uuid):
def mock_request(self):
mocked_task_request = mocker.patch('celery.app.task.Task.request')
mocked_task_request.id = task_uuid
return mocked_task_request
mocker.patch('cic_eth.api.api_task.Api.create_account', mock_request)
@pytest.fixture(scope='function')
def mock_account_creation_task_result(mocker, task_uuid):
def task_result(self):
sync_res = mocker.patch('celery.result.AsyncResult')
sync_res.id = task_uuid
sync_res.get.return_value = blockchain_address()
return sync_res
mocker.patch('cic_eth.api.api_task.Api.create_account', task_result)
@pytest.fixture(scope='function')
def mock_token_api_query(foo_token_symbol, foo_token, mocker, task_uuid):
def mock_query(self, token_symbol, proof=None):
sync_res = mocker.patch('celery.result.AsyncResult')
sync_res.id = task_uuid
sync_res.get.return_value = [
{
'address': foo_token,
'converters': [],
'decimals': 6,
'name': 'Giftable Token',
'proofs': ['5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3'],
'symbol': foo_token_symbol,
},{'5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3': ['Eb3907eCad74a0013c259D5874AE7f22DcBcC95C','Eb3907eCad74a0013c259D5874AE7f22DcBcC95C']}
]
return sync_res
mocker.patch('cic_eth.api.api_task.Api.token', mock_query)
@pytest.fixture(scope='function')
def mock_tokens_api_query(foo_token_symbol, foo_token, mocker, task_uuid):
def mock_query(self, token_symbol, proof=None):
sync_res = mocker.patch('celery.result.AsyncResult')
sync_res.id = task_uuid
sync_res.get.return_value = [[
{
'address': foo_token,
'converters': [],
'decimals': 6,
'name': 'Giftable Token',
'proofs': ['5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3'],
'symbol': foo_token_symbol,
},{'5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3': ['Eb3907eCad74a0013c259D5874AE7f22DcBcC95C','Eb3907eCad74a0013c259D5874AE7f22DcBcC95C']}
], [
{
'address': foo_token,
'converters': [],
'decimals': 6,
'name': 'Giftable Token',
'proofs': ['5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3'],
'symbol': foo_token_symbol,
},{'5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3': ['Eb3907eCad74a0013c259D5874AE7f22DcBcC95C','Eb3907eCad74a0013c259D5874AE7f22DcBcC95C']}
]]
return sync_res
mocker.patch('cic_eth.api.api_task.Api.tokens', mock_query)
@pytest.fixture(scope='function')
def mock_sync_balance_api_query(balances, mocker, task_uuid):
def sync_api_query(self, address: str, token_symbol: str):
sync_res = mocker.patch('celery.result.AsyncResult')
sync_res.id = task_uuid
sync_res.get.return_value = balances
return sync_res
mocker.patch('cic_eth.api.api_task.Api.balance', sync_api_query)
@pytest.fixture(scope='function')
def mock_sync_default_token_api_query(default_token_data, mocker, task_uuid):
def mock_query(self):
sync_res = mocker.patch('celery.result.AsyncResult')
sync_res.id = task_uuid
sync_res.get.return_value = default_token_data
return sync_res
mocker.patch('cic_eth.api.api_task.Api.default_token', mock_query)
@pytest.fixture(scope='function')
def mock_transaction_list_query(mocker):
query_args = {}
def mock_query(self, address: str, limit: int):
query_args['address'] = address
query_args['limit'] = limit
mocker.patch('cic_eth.api.api_task.Api.list', mock_query)
return query_args
@pytest.fixture(scope='function')
def mock_transfer_api(mocker):
transfer_args = {}
def mock_transfer(self, from_address: str, to_address: str, value: int, token_symbol: str):
transfer_args['from_address'] = from_address
transfer_args['to_address'] = to_address
transfer_args['value'] = value
transfer_args['token_symbol'] = token_symbol
mocker.patch('cic_eth.api.api_task.Api.transfer', mock_transfer)
return transfer_args

View File

@ -1,9 +1,20 @@
import logging
import cic_eth.cli import cic_eth.cli
from cic_eth.server.app import create_app from cic_eth.server.app import create_app
from cic_eth.server.config import get_config from cic_eth.server.celery import create_celery_wrapper
arg_flags = cic_eth.cli.argflag_std_base
config = get_config() local_arg_flags = cic_eth.cli.argflag_local_taskcallback
argparser = cic_eth.cli.ArgumentParser(arg_flags)
argparser.process_local_flags(local_arg_flags)
args = argparser.parse_args()
config = cic_eth.cli.Config.from_args(args, arg_flags, local_arg_flags)
# Define log levels
if args.vv:
logging.getLogger().setLevel(logging.DEBUG)
elif args.v:
logging.getLogger().setLevel(logging.INFO)
# Setup Celery App # Setup Celery App
@ -18,13 +29,9 @@ redis_port = config.get('REDIS_PORT')
redis_db = config.get('REDIS_DB') redis_db = config.get('REDIS_DB')
redis_timeout = config.get('REDIS_TIMEOUT') redis_timeout = config.get('REDIS_TIMEOUT')
celery_wrapper = create_celery_wrapper(celery_queue=celery_queue, chain_spec=chain_spec,
app = create_app(chain_spec, redis_db=redis_db, redis_host=redis_host, redis_port=redis_port, redis_timeout=redis_timeout)
celery_queue, app = create_app(celery_wrapper)
redis_host,
redis_port,
redis_db,
redis_timeout)
if __name__ == "__main__": if __name__ == "__main__":
import uvicorn import uvicorn

View File

@ -12,16 +12,8 @@ from fastapi import FastAPI, Query
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def create_app(chain_spec, def create_app(celery_wrapper):
celery_queue,
redis_host,
redis_port,
redis_db,
redis_timeout):
setup_cache(redis_db=redis_db, redis_host=redis_host,
redis_port=redis_port)
celery_wrapper = create_celery_wrapper(celery_queue=celery_queue, chain_spec=chain_spec,
redis_db=redis_db, redis_host=redis_host, redis_port=redis_port, redis_timeout=redis_timeout)
app = FastAPI(debug=True, app = FastAPI(debug=True,
title="Grassroots Economics", title="Grassroots Economics",
description="CIC ETH API", description="CIC ETH API",
@ -99,7 +91,6 @@ def create_app(chain_spec,
if token == None: if token == None:
sys.stderr.write(f"Cached Token {token_symbol} not found") sys.stderr.write(f"Cached Token {token_symbol} not found")
data = celery_wrapper('token', token_symbol, proof=proof) data = celery_wrapper('token', token_symbol, proof=proof)
cache.set_token_data(token_symbol, data)
token = Token.new(data) token = Token.new(data)
sys.stderr.write(f"Token {token}") sys.stderr.write(f"Token {token}")
@ -119,20 +110,10 @@ def create_app(chain_spec,
@app.get("/default_token", response_model=DefaultToken) @app.get("/default_token", response_model=DefaultToken)
def default_token(): def default_token():
data = cache.get_default_token()
if data is None:
data = celery_wrapper('default_token') data = celery_wrapper('default_token')
if data is not None:
cache.set_default_token(data)
return data return data
def get_token(token_symbol: str): def get_token(token_symbol: str):
data = cache.get_token_data(token_symbol)
log.debug(f"cached token data: {data}")
if data == None:
data = celery_wrapper('token', token_symbol) data = celery_wrapper('token', token_symbol)
log.debug(
f"No token data setting token data for: {token_symbol} to {data}")
cache.set_token_data(token_symbol, data)
return Token.new(data) return Token.new(data)
return app return app

View File

@ -18,7 +18,7 @@ def create_celery_wrapper(chain_spec,
def call(method, *args, catch=1, **kwargs): def call(method, *args, catch=1, **kwargs):
""" Creates a redis channel and calls `cic_eth.api` with the provided `method` and `*args`. Returns the result of the api call. Catch allows you to specify how many messages to catch before returning. """ Creates a redis channel and calls `cic_eth.api` with the provided `method` and `*args`. Returns the result of the api call. Catch allows you to specify how many messages to catch before returning.
""" """
log.debug(f"Using chainspec: {chain_spec}") log.debug(f"Using redis: {redis_host}, {redis_port}, {redis_db}")
redis_channel = str(uuid.uuid4()) redis_channel = str(uuid.uuid4())
r = redis.Redis(redis_host, redis_port, redis_db) r = redis.Redis(redis_host, redis_port, redis_db)
ps = r.pubsub() ps = r.pubsub()
@ -39,6 +39,7 @@ def create_celery_wrapper(chain_spec,
if catch == 1: if catch == 1:
message = ps.get_message(timeout=redis_timeout) message = ps.get_message(timeout=redis_timeout)
data = json.loads(message['data'])["result"] data = json.loads(message['data'])["result"]
raise data
else: else:
for _i in range(catch): for _i in range(catch):
message = ps.get_message( message = ps.get_message(

View File

@ -1,20 +0,0 @@
import logging
import cic_eth.cli
def get_config():
# Parse Args
arg_flags = cic_eth.cli.argflag_std_base
local_arg_flags = cic_eth.cli.argflag_local_taskcallback
argparser = cic_eth.cli.ArgumentParser(arg_flags)
argparser.process_local_flags(local_arg_flags)
args = argparser.parse_args([])
config = cic_eth.cli.Config.from_args(args, arg_flags, local_arg_flags)
# Define log levels
if args.vv:
logging.getLogger().setLevel(logging.DEBUG)
elif args.v:
logging.getLogger().setLevel(logging.INFO)
return config

View File

@ -24,6 +24,8 @@ from cic_eth.pytest.fixtures_database import *
from cic_eth.pytest.fixtures_role import * from cic_eth.pytest.fixtures_role import *
from cic_eth.pytest.fixtures_contract import * from cic_eth.pytest.fixtures_contract import *
from cic_eth.pytest.fixtures_token import * from cic_eth.pytest.fixtures_token import *
from cic_eth.pytest.patches.account import *
from chainlib.eth.pytest import * from chainlib.eth.pytest import *
from eth_contract_registry.pytest import * from eth_contract_registry.pytest import *
from cic_eth_registry.pytest.fixtures_contracts import * from cic_eth_registry.pytest.fixtures_contracts import *

View File

@ -2,9 +2,9 @@
set -e set -e
pip install --extra-index-url https://pip.grassrootseconomics.net --extra-index-url https://gitlab.com/api/v4/projects/27624814/packages/pypi/simple pip install --extra-index-url https://pip.grassrootseconomics.net --extra-index-url https://gitlab.com/api/v4/projects/27624814/packages/pypi/simple \
-r admin_requirements.txt -r admin_requirements.txt \
-r services_requirements.txt -r services_requirements.txt \
-r test_requirements.txt -r test_requirements.txt
export PYTHONPATH=. && pytest -x --cov=cic_eth --cov-fail-under=90 --cov-report term-missing tests export PYTHONPATH=. && pytest -x --cov=cic_eth --cov-fail-under=90 --cov-report term-missing tests

View File

@ -5,89 +5,83 @@ import logging
import time import time
import hexathon import hexathon
import pytest
from cic_eth.server.app import create_app from cic_eth.server.app import create_app
from cic_eth_registry.pytest.fixtures_tokens import * from cic_eth.server.celery import create_celery_wrapper
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@pytest.fixture(scope='session') @pytest.fixture(scope='function')
def client(config, default_chain_spec, celery_session_worker): def celery_wrapper(api):
config.add(str(default_chain_spec), 'CHAIN_SPEC', exists_ok=True) """ Creates a redis channel and calls `cic_eth.api` with the provided `method` and `*args`. Returns the result of the api call. Catch allows you to specify how many messages to catch before returning.
config.add('cic_eth_test', 'CELERY_QUEUE', exists_ok=True) """
chain_spec = config.get('CHAIN_SPEC') def wrapper(method, *args, catch=1, **kwargs):
celery_queue = config.get('CELERY_QUEUE') t = getattr(api, method)(*args, **kwargs)
redis_host = config.get('REDIS_REDIS_HOST') return t.get()
redis_port = config.get('REDIS_REDIS_PORT') return wrapper
redis_db = config.get('REDIS_DB')
redis_timeout = 20
app = create_app(str(chain_spec), celery_queue, redis_host, @pytest.fixture(scope='function')
redis_port, redis_db, redis_timeout) def client(celery_wrapper):
app = create_app(celery_wrapper)
return TestClient(app) return TestClient(app)
def test_default_token(client, foo_token, default_chain_spec): def test_default_token(client,mock_sync_default_token_api_query):
print(foo_token)
print(f"default_chain_spec: {default_chain_spec.asdict()}")
# Default Token # Default Token
response = client.get('/default_token') response = client.get('/default_token')
log.debug(f"balance response {response}") log.debug(f"balance response {response}")
default_token = response.json() default_token = response.json()
assert default_token == { assert default_token == {'symbol': 'FOO', 'address': '0xe7c559c40B297d7f039767A2c3677E20B24F1385', 'name': 'Giftable Token', 'decimals': 6}
'address': '3FF776B6f888980DEf9d4220858803f9dC5e341e',
'decimals': 7,
'name': 'Giftable Token',
'symbol': 'GFT',
}
def test_token(client, mock_token_api_query):
def test_token(client):
# Default Token # Default Token
response = client.get('/token?token_symbol=FOO') response = client.get('/token?token_symbol=FOO')
log.debug(f"token response {response}") log.debug(f"token response {response}")
token = response.json() token = response.json()
assert token == { assert token == {
'address': '3ff776b6f888980def9d4220858803f9dc5e341e', 'address': '0xe7c559c40B297d7f039767A2c3677E20B24F1385',
'converters': [], 'converters': [],
'decimals': 6, 'decimals': 6,
'name': 'Giftable Token', 'name': 'Giftable Token',
'proofs': ['3af82fa124235f84e78145f008054b11fe477e2b043ac5e4979c3afa737fd328'], 'proofs': ['5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3'],
'proofs_with_signers': [{'proof': '3af82fa124235f84e78145f008054b11fe477e2b043ac5e4979c3afa737fd328', 'proofs_with_signers': [{'proof': '5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3',
'signers': ['Eb3907eCad74a0013c259D5874AE7f22DcBcC95C']}], 'signers': ['Eb3907eCad74a0013c259D5874AE7f22DcBcC95C', 'Eb3907eCad74a0013c259D5874AE7f22DcBcC95C']}],
'symbol': 'GFT', 'symbol': 'FOO',
} }
def test_tokens(client, mock_tokens_api_query):
def test_tokens(client):
# Default Token # Default Token
response = client.get( response = client.get(
'/tokens', params={'token_symbols': ['GFT', 'COFE', 'FOO']}) '/tokens', params={'token_symbols': ['FOO', 'FOO']})
log.debug(f"tokens response {response}") log.debug(f"tokens response {response}")
tokens = response.json() tokens = response.json()
assert tokens == [ assert tokens == [
{'address': '3ff776b6f888980def9d4220858803f9dc5e341e', {
'address': '0xe7c559c40B297d7f039767A2c3677E20B24F1385',
'converters': [], 'converters': [],
'decimals': 6, 'decimals': 6,
'name': 'Giftable Token', 'name': 'Giftable Token',
'proofs': ['3af82fa124235f84e78145f008054b11fe477e2b043ac5e4979c3afa737fd328'],
'proofs_with_signers': [{'proof': '3af82fa124235f84e78145f008054b11fe477e2b043ac5e4979c3afa737fd328',
'signers': ['Eb3907eCad74a0013c259D5874AE7f22DcBcC95C']}],
'symbol': 'GFT'},
{'address': '9c9506cf6c50f5b0371be72920fc6060d1a88a6a',
'converters': [],
'decimals': 6,
'name': 'Coffee',
'proofs': ['5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3'], 'proofs': ['5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3'],
'proofs_with_signers': [{'proof': '5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3', 'proofs_with_signers': [{'proof': '5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3',
'signers': ['Eb3907eCad74a0013c259D5874AE7f22DcBcC95C', 'signers': ['Eb3907eCad74a0013c259D5874AE7f22DcBcC95C', 'Eb3907eCad74a0013c259D5874AE7f22DcBcC95C']}],
'Eb3907eCad74a0013c259D5874AE7f22DcBcC95C']}], 'symbol': 'FOO',
'symbol': 'COFE'}, },
{
'address': '0xe7c559c40B297d7f039767A2c3677E20B24F1385',
'converters': [],
'decimals': 6,
'name': 'Giftable Token',
'proofs': ['5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3'],
'proofs_with_signers': [{'proof': '5b1549818725ca07c19fc47fda5d8d85bbebb1283855d5ab99785dcb7d9051d3',
'signers': ['Eb3907eCad74a0013c259D5874AE7f22DcBcC95C', 'Eb3907eCad74a0013c259D5874AE7f22DcBcC95C']}],
'symbol': 'FOO',
},
] ]
@pytest.mark.skip("Not implemented")
def test_account(client): def test_account(client):
# Default Token # Default Token
response = client.get('/default_token') response = client.get('/default_token')