-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_app.py
276 lines (246 loc) · 9.45 KB
/
test_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import asyncio
import unittest
from decimal import Decimal
from time import time
from unittest.mock import Mock, patch
from fastapi.testclient import TestClient
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.future import select
from sqlalchemy.orm import sessionmaker
from models import Base, Pair, Swap, Token
from processing import XOR_ID
from run_node_processing import DENOM, update_all_pairs_liquidity, update_volumes
from web import app, get_db
SQLALCHEMY_DATABASE_URL = "sqlite+aiosqlite:///:memory:"
engine = create_async_engine(
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
)
TestingSessionLocal = sessionmaker(
autocommit=False, autoflush=False, bind=engine, class_=AsyncSession
)
async def override_get_db():
async with TestingSessionLocal() as session:
yield session
app.dependency_overrides[get_db] = override_get_db
client = TestClient(app)
class DBTestCase(unittest.TestCase):
async def asyncSetUp(self):
# create tables
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
async def asyncTearDown(self):
# drop tables
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
def setUp(self):
asyncio.run(self.asyncSetUp())
def tearDown(self):
asyncio.run(self.asyncTearDown())
class ImportTest(DBTestCase):
@patch("PoolXYK.substrate.query")
async def test_update_all_pairs_liquidity(self, mock_query):
mock_query.side_effect = lambda module, storage_function, params: {
(
"PoolXYK",
"Reserves",
[
"0x" + hex(1)[2:].zfill(64),
"0x" + hex(2)[2:].zfill(64),
],
): Mock(value=(1000 * DENOM, 2000 * DENOM)),
(
"PoolXYK",
"Reserves",
[
"0x" + hex(2)[2:].zfill(64),
"0x" + hex(1)[2:].zfill(64),
],
): Mock(value=(3000 * DENOM, 4000 * DENOM)),
}.get((module, storage_function, params), None)
async with TestingSessionLocal() as session:
dai = Token(id=1, name="D", decimals=18, symbol="DAI")
xor = Token(id=2, name="X", decimals=18, symbol="XOR")
session.add_all([dai, xor])
pair = Pair(from_token=dai, to_token=xor)
session.add(pair)
await session.commit()
await update_all_pairs_liquidity(session, substrate=mock_query)
updated_pair = (await session.execute(select(Pair))).scalar()
self.assertEqual(updated_pair.from_token_liquidity, Decimal("4000"))
self.assertEqual(updated_pair.to_token_liquidity, Decimal("5000"))
def test_update_volumes(self):
async def inner():
# insert test data
async with TestingSessionLocal() as session:
dai = Token(id=1, name="D", decimals=18, symbol="DAI")
session.add(dai)
xor = Token(id=int(XOR_ID, 16), name="X", decimals=18, symbol="XOR")
session.add(xor)
pair = Pair(from_token=dai, to_token=xor)
session.add(pair)
swap = Swap(
id=1,
block=2,
timestamp=time() * 1000,
pair=pair,
xor_fee=4,
from_amount=1 * 10 ** 17,
to_amount=2 * 10 ** 17,
filter_mode="mode",
)
session.add(swap)
swap = Swap(
id=2,
block=4,
timestamp=time() * 1000,
pair=pair,
xor_fee=4,
from_amount=1 * 10 ** 17,
to_amount=3 * 10 ** 17,
filter_mode="mode",
)
session.add(swap)
await session.commit()
# call update_volumes()
last_24h = (time() - 24 * 3600) * 1000
await update_volumes(session, last_24h)
await session.commit()
# check volume columns filled
pair = (await session.execute(select(Pair))).scalar()
self.assertEqual(pair.from_volume, Decimal("0.2"))
self.assertEqual(pair.to_volume, Decimal("0.5"))
dai, xor = (
await session.execute(select(Token).order_by(Token.id))
).scalars()
self.assertEqual(dai.trade_volume, Decimal(".2"))
self.assertEqual(xor.trade_volume, Decimal(".5"))
asyncio.run(inner())
class WebAppTest(DBTestCase):
async def asyncSetUp(self):
await super().asyncSetUp()
# insert test data
async with TestingSessionLocal() as session:
dai = Token(id=1, name="D", decimals=18, symbol="DAI")
session.add(dai)
xor = Token(id=int(XOR_ID, 16), name="X", decimals=18, symbol="XOR")
session.add(xor)
dai_xor = Pair(from_token=dai, to_token=xor, from_volume=1, to_volume=2)
xor_dai = Pair(from_token=xor, to_token=dai, from_volume=4, to_volume=8)
session.add(dai_xor)
session.add(xor_dai)
swap = Swap(
id=1,
txid=0x1234,
block=2,
timestamp=3,
pair=dai_xor,
xor_fee=4,
from_amount=1,
to_amount=2,
filter_mode="mode",
)
session.add(swap)
swap = Swap(
id=2,
txid=0x5678,
block=4,
timestamp=5,
pair=dai_xor,
xor_fee=4,
from_amount=1,
to_amount=3,
filter_mode="mode",
)
session.add(swap)
await session.commit()
def test_pairs_get(self):
response = client.get("/pairs/")
assert response.status_code == 200, response.text
data = response.json()
self.assertEqual(
data,
{
"0x"
+ "0" * 63
+ "1_"
+ XOR_ID: {
"base_id": "0x" + "0" * 63 + "1",
"base_name": "D",
"base_symbol": "DAI",
"base_volume": 9,
"last_price": 3,
"quote_id": XOR_ID,
"quote_name": "X",
"quote_symbol": "XOR",
"quote_volume": 6,
}
},
)
@patch("web.get_whitelist")
def test_pair_get(self, whitelist_mock):
self.maxDiff = 1024
whitelist_mock.return_value = [{"address": "0x1"}, {"address": XOR_ID}]
response = client.get("/pairs/DAI-XOR")
assert response.status_code == 200, response.text
data = response.json()
self.assertEqual(
data,
{
"base_id": "0x" + "0" * 63 + "1",
"base_name": "D",
"base_symbol": "DAI",
"base_volume": 9,
"last_price": 3,
"quote_id": XOR_ID,
"quote_name": "X",
"quote_symbol": "XOR",
"last_block": 4,
"last_txid": "0x0000000000000000000000000000000000000000000000000000000000005678",
"quote_volume": 6,
},
)
def test_graphql_post(self):
response = client.post(
"/graph", json=dict(query="{pairs{id, fromToken{symbol}, toToken{symbol}}}")
)
assert response.status_code == 200, response.text
data = response.json()
self.assertEqual(
data,
{
"data": {
"pairs": [
{
"id": "1",
"fromToken": {"symbol": "DAI"},
"toToken": {"symbol": "XOR"},
},
{
"id": "2",
"fromToken": {"symbol": "XOR"},
"toToken": {"symbol": "DAI"},
},
]
}
},
)
async def test_tickers_get(self):
response = await client.get("/tickers/")
assert response.status_code == 200, response.text
data = response.json()
self.assertIsInstance(data, list)
self.assertGreater(len(data), 0)
ticker = data[0]
self.assertEqual(ticker["ticker_id"], "0x" + "0" * 63 + "1_" + XOR_ID)
self.assertEqual(ticker["base_currency"], "0x" + "0" * 63 + "1")
self.assertEqual(ticker["base_name"], "D")
self.assertEqual(ticker["base_symbol"], "DAI")
self.assertEqual(ticker["target_currency"], XOR_ID)
self.assertEqual(ticker["target_name"], "X")
self.assertEqual(ticker["target_symbol"], "XOR")
self.assertEqual(ticker["last_price"], "0.5")
self.assertEqual(ticker["base_volume"], "1.0")
self.assertEqual(ticker["target_volume"], "2.0")
self.assertEqual(ticker["liquidity_in_usd"], "1.0")
self.assertEqual(ticker["high"], "0.5")
self.assertEqual(ticker["low"], "0.5")