get some tests working
This commit is contained in:
12
apps/cic-eth/cic_eth/pytest/helpers/accounts.py
Normal file
12
apps/cic-eth/cic_eth/pytest/helpers/accounts.py
Normal file
@@ -0,0 +1,12 @@
|
||||
# standard imports
|
||||
import os
|
||||
import random
|
||||
import uuid
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def blockchain_address() -> str:
|
||||
return os.urandom(20).hex().lower()
|
||||
|
||||
0
apps/cic-eth/cic_eth/pytest/patches/__init__.py
Normal file
0
apps/cic-eth/cic_eth/pytest/patches/__init__.py
Normal file
132
apps/cic-eth/cic_eth/pytest/patches/account.py
Normal file
132
apps/cic-eth/cic_eth/pytest/patches/account.py
Normal 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
|
||||
@@ -1,9 +1,20 @@
|
||||
import logging
|
||||
|
||||
import cic_eth.cli
|
||||
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
|
||||
|
||||
|
||||
config = get_config()
|
||||
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)
|
||||
|
||||
|
||||
# Setup Celery App
|
||||
@@ -18,13 +29,9 @@ redis_port = config.get('REDIS_PORT')
|
||||
redis_db = config.get('REDIS_DB')
|
||||
redis_timeout = config.get('REDIS_TIMEOUT')
|
||||
|
||||
|
||||
app = create_app(chain_spec,
|
||||
celery_queue,
|
||||
redis_host,
|
||||
redis_port,
|
||||
redis_db,
|
||||
redis_timeout)
|
||||
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 = create_app(celery_wrapper)
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
|
||||
@@ -12,16 +12,8 @@ from fastapi import FastAPI, Query
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def create_app(chain_spec,
|
||||
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)
|
||||
def create_app(celery_wrapper):
|
||||
|
||||
app = FastAPI(debug=True,
|
||||
title="Grassroots Economics",
|
||||
description="CIC ETH API",
|
||||
@@ -99,7 +91,6 @@ def create_app(chain_spec,
|
||||
if token == None:
|
||||
sys.stderr.write(f"Cached Token {token_symbol} not found")
|
||||
data = celery_wrapper('token', token_symbol, proof=proof)
|
||||
cache.set_token_data(token_symbol, data)
|
||||
token = Token.new(data)
|
||||
sys.stderr.write(f"Token {token}")
|
||||
|
||||
@@ -119,20 +110,10 @@ def create_app(chain_spec,
|
||||
|
||||
@app.get("/default_token", response_model=DefaultToken)
|
||||
def default_token():
|
||||
data = cache.get_default_token()
|
||||
if data is None:
|
||||
data = celery_wrapper('default_token')
|
||||
if data is not None:
|
||||
cache.set_default_token(data)
|
||||
data = celery_wrapper('default_token')
|
||||
return data
|
||||
|
||||
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)
|
||||
log.debug(
|
||||
f"No token data setting token data for: {token_symbol} to {data}")
|
||||
cache.set_token_data(token_symbol, data)
|
||||
data = celery_wrapper('token', token_symbol)
|
||||
return Token.new(data)
|
||||
return app
|
||||
|
||||
@@ -18,7 +18,7 @@ def create_celery_wrapper(chain_spec,
|
||||
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.
|
||||
"""
|
||||
log.debug(f"Using chainspec: {chain_spec}")
|
||||
log.debug(f"Using redis: {redis_host}, {redis_port}, {redis_db}")
|
||||
redis_channel = str(uuid.uuid4())
|
||||
r = redis.Redis(redis_host, redis_port, redis_db)
|
||||
ps = r.pubsub()
|
||||
@@ -39,6 +39,7 @@ def create_celery_wrapper(chain_spec,
|
||||
if catch == 1:
|
||||
message = ps.get_message(timeout=redis_timeout)
|
||||
data = json.loads(message['data'])["result"]
|
||||
raise data
|
||||
else:
|
||||
for _i in range(catch):
|
||||
message = ps.get_message(
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user