Working on persistence api
This commit is contained in:
parent
2da14fc554
commit
92d3afd6e8
@ -2,6 +2,7 @@ import boto3
|
||||
import simplejson as json
|
||||
import decimal
|
||||
|
||||
|
||||
class DecimalEncoder(json.JSONEncoder):
|
||||
def default(self, o):
|
||||
if isinstance(o, decimal.Decimal):
|
||||
@ -30,16 +31,15 @@ class Persistence:
|
||||
table = self.db.Table(self.table)
|
||||
|
||||
response = table.scan()
|
||||
result = []
|
||||
result = response['Items']
|
||||
|
||||
while 'LastEvaluatedKey' in response:
|
||||
for i in response['Items']:
|
||||
result.append(i)
|
||||
response = table.scan(
|
||||
ExclusiveStartKey=response['LastEvaluatedKey']
|
||||
)
|
||||
|
||||
for i in response['Items']:
|
||||
result.append(i)
|
||||
|
||||
return result
|
||||
|
||||
def load(self, sample):
|
||||
|
@ -19,11 +19,15 @@ def names(event, context):
|
||||
"""
|
||||
table = Persistence(os.environ['strategyTable'])
|
||||
|
||||
# map results and hide informations
|
||||
data = list(map(lambda x: {'name': x['name'], 'public': x['public'], 'user': x['user']}, table.list()))
|
||||
|
||||
return {
|
||||
"statusCode": 200,
|
||||
"body": json.dumps(table.list())
|
||||
"body": json.dumps(data)
|
||||
}
|
||||
|
||||
|
||||
def performance(event, context):
|
||||
"""
|
||||
returns the performance of the specified strategy
|
||||
@ -71,9 +75,8 @@ def submit(event, context):
|
||||
# try to load the strategy
|
||||
StrategyResolver().compile(data['name'], strategy)
|
||||
|
||||
# generate id
|
||||
data['id'] = str(uuid.uuid4())
|
||||
data['time'] = int(time.time() * 1000)
|
||||
data['type'] = "strategy"
|
||||
|
||||
# save to DB
|
||||
table = Persistence(os.environ['strategyTable'])
|
||||
|
@ -1,6 +1,6 @@
|
||||
import simplejson as json
|
||||
from base64 import urlsafe_b64encode
|
||||
from freqtrade.aws.strategy import submit
|
||||
import freqtrade.aws.strategy as aws
|
||||
|
||||
|
||||
def test_strategy(lambda_context):
|
||||
@ -56,8 +56,44 @@ class TestStrategy(IStrategy):
|
||||
"public": False
|
||||
}
|
||||
|
||||
print(json.dumps(request))
|
||||
|
||||
submit({
|
||||
# db should be empty
|
||||
assert (len(json.loads(aws.names({}, {})['body'])) == 0)
|
||||
# now we add an entry
|
||||
aws.submit({
|
||||
"body": json.dumps(request)
|
||||
}, {})
|
||||
|
||||
# now we should have items
|
||||
assert (len(json.loads(aws.names({}, {})['body'])) == 1)
|
||||
|
||||
# able to add a second strategy with the sample name, but different user
|
||||
|
||||
request = {
|
||||
"user": "GCU4LW2XXZW3A3FM2XZJTEJHNWHTWDKY2DIJLCZJ5ULVZ4K7LZ7D23TH",
|
||||
"description": "simple test strategy",
|
||||
"name": "TestStrategy",
|
||||
"content": urlsafe_b64encode(content.encode('utf-8')),
|
||||
"public": False
|
||||
}
|
||||
|
||||
aws.submit({
|
||||
"body": json.dumps(request)
|
||||
}, {})
|
||||
|
||||
assert (len(json.loads(aws.names({}, {})['body'])) == 2)
|
||||
|
||||
# able to add a duplicated strategy, which should overwrite the existing strategy
|
||||
|
||||
request = {
|
||||
"user": "GCU4LW2XXZW3A3FM2XZJTEJHNWHTWDKY2DIJLCZJ5ULVZ4K7LZ7D23TH",
|
||||
"description": "simple test strategy",
|
||||
"name": "TestStrategy",
|
||||
"content": urlsafe_b64encode(content.encode('utf-8')),
|
||||
"public": False
|
||||
}
|
||||
|
||||
aws.submit({
|
||||
"body": json.dumps(request)
|
||||
}, {})
|
||||
|
||||
assert (len(json.loads(aws.names({}, {})['body'])) == 2)
|
||||
|
@ -497,6 +497,7 @@ def result():
|
||||
with open('freqtrade/tests/testdata/UNITTEST_BTC-1m.json') as data_file:
|
||||
return Analyze.parse_ticker_dataframe(json.load(data_file))
|
||||
|
||||
|
||||
# FIX:
|
||||
# Create an fixture/function
|
||||
# that inserts a trade of some type and open-status
|
||||
@ -593,7 +594,6 @@ def buy_order_fee():
|
||||
|
||||
@pytest.fixture
|
||||
def lambda_context():
|
||||
|
||||
# mock the different AWS features we need
|
||||
sns = moto.mock_sns()
|
||||
sns.start()
|
||||
@ -608,6 +608,34 @@ def lambda_context():
|
||||
|
||||
client = session.client('sns')
|
||||
dynamodb = boto3.resource('dynamodb')
|
||||
os.environ["strategyTable"] = "StrategyTable"
|
||||
|
||||
dynamodb.create_table(
|
||||
TableName=os.environ["strategyTable"],
|
||||
KeySchema=[
|
||||
{
|
||||
'AttributeName': 'user',
|
||||
'KeyType': 'HASH'
|
||||
},
|
||||
{
|
||||
'AttributeName': 'name',
|
||||
'KeyType': 'RANGE'
|
||||
}
|
||||
],
|
||||
AttributeDefinitions=[
|
||||
{
|
||||
'AttributeName': 'user',
|
||||
'AttributeType': 'S'
|
||||
}, {
|
||||
'AttributeName': 'name',
|
||||
'AttributeType': 'S'
|
||||
}
|
||||
],
|
||||
ProvisionedThroughput={
|
||||
'ReadCapacityUnits': 1,
|
||||
'WriteCapacityUnits': 1
|
||||
}
|
||||
)
|
||||
|
||||
# here we will define required tables later
|
||||
yield
|
||||
|
Loading…
Reference in New Issue
Block a user