File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ async def get_hourly_average(ts_key: str, top_of_the_hour: int):
129129
)
130130
# Return the average without the timestamp. The response is a list
131131
# of the structure [timestamp, average].
132-
print(response)
133132
return response[0][1]
134133

135134

@@ -187,6 +186,15 @@ async def bitcoin(keys: Keys = Depends(make_keys)):
187186
await refresh_hourly_cache(keys)
188187

189188

189+
def get_direction(last_three_hours, key: str):
190+
if last_three_hours[0][key] < last_three_hours[-1][key]:
191+
return 'rising'
192+
elif last_three_hours[0][key] > last_three_hours[-1][key]:
193+
return 'falling'
194+
else:
195+
return 'flat'
196+
197+
190198
@app.get('/is-bitcoin-lit')
191199
async def bitcoin(keys: Keys = Depends(make_keys)):
192200
now = datetime.utcnow()
@@ -205,7 +213,13 @@ async def bitcoin(keys: Keys = Depends(make_keys)):
205213
'time': datetime.fromtimestamp(data[0][0] / 1000, tz=timezone.utc),
206214
}
207215
for data in zip(price, sentiment)]
208-
return past_hours + [current_hour_stats_cached]
216+
last_three_hours = past_hours + [current_hour_stats_cached]
217+
218+
return {
219+
'hourly_average_of_averages': last_three_hours,
220+
'sentiment_direction': get_direction(last_three_hours, 'sentiment'),
221+
'price_direction': get_direction(last_three_hours, 'price'),
222+
}
209223

210224

211225
async def make_timeseries(key):
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import json
2+
import os.path
3+
from unittest import mock
4+
15
import pytest
26
from aioredis import Redis
37
from httpx import AsyncClient
@@ -6,13 +10,35 @@
610
REFRESH_URL = '/refresh'
711
URL = '/is-bitcoin-lit'
812
EXPECTED_FIELDS = (
9-
'lit', 'time', 'mean_of_means_sentiment',
10-
'mean_of_means_price',
13+
'hourly_average_of_averages',
14+
'sentiment_direction',
15+
'price_direction',
16+
)
17+
JSON_FIXTURE = os.path.join(
18+
os.path.dirname(os.path.realpath(__file__)),
19+
'fixtures',
20+
'sentiment_response.json',
1121
)
1222

1323

24+
@pytest.fixture
25+
def mock_bitcoin_api():
26+
with mock.('requests.get') as mock_get:
27+
with mock.('requests.Response') as mock_response:
28+
# Mock out Response.json()
29+
m = mock.MagicMock()
30+
with open(JSON_FIXTURE) as f:
31+
m.return_value = json.loads(f.read())
32+
mock_response.json = m
33+
34+
# Make get() return our fake Response.
35+
mock_get.return_value = mock_response
36+
37+
yield mock_get
38+
39+
1440
@pytest.mark.asyncio
15-
async def test_api(client: AsyncClient):
41+
async def test_api(client: AsyncClient, mock_bitcoin_api: mock.MagicMock):
1642
await client.get(REFRESH_URL)
1743
res = await client.get(URL)
1844
summary = res.json()
@@ -24,7 +50,10 @@ async def test_api(client: AsyncClient):
2450

2551

2652
@pytest.mark.asyncio
27-
async def test_api_timeseries(client: AsyncClient, redis: Redis):
53+
async def test_api_timeseries(
54+
client: AsyncClient, redis: Redis,
55+
mock_bitcoin_api: mock.MagicMock
56+
):
2857
await client.get(REFRESH_URL)
2958
data = await client.get(URL)
3059
summary = data.json()

0 commit comments

Comments
 (0)