diff options
Diffstat (limited to 'test.py')
-rw-r--r-- | test.py | 313 |
1 files changed, 312 insertions, 1 deletions
@@ -126,6 +126,317 @@ class poloniexETest(unittest.TestCase): | |||
126 | } | 126 | } |
127 | self.assertEqual(expected, self.s.margin_summary()) | 127 | self.assertEqual(expected, self.s.margin_summary()) |
128 | 128 | ||
129 | def test_create_order(self): | ||
130 | with mock.patch.object(self.s, "create_exchange_order") as exchange,\ | ||
131 | mock.patch.object(self.s, "create_margin_order") as margin: | ||
132 | with self.subTest(account="unspecified"): | ||
133 | self.s.create_order("symbol", "type", "side", "amount", price="price", lending_rate="lending_rate", params="params") | ||
134 | exchange.assert_called_once_with("symbol", "type", "side", "amount", price="price", params="params") | ||
135 | margin.assert_not_called() | ||
136 | exchange.reset_mock() | ||
137 | margin.reset_mock() | ||
138 | |||
139 | with self.subTest(account="exchange"): | ||
140 | self.s.create_order("symbol", "type", "side", "amount", account="exchange", price="price", lending_rate="lending_rate", params="params") | ||
141 | exchange.assert_called_once_with("symbol", "type", "side", "amount", price="price", params="params") | ||
142 | margin.assert_not_called() | ||
143 | exchange.reset_mock() | ||
144 | margin.reset_mock() | ||
145 | |||
146 | with self.subTest(account="margin"): | ||
147 | self.s.create_order("symbol", "type", "side", "amount", account="margin", price="price", lending_rate="lending_rate", params="params") | ||
148 | margin.assert_called_once_with("symbol", "type", "side", "amount", lending_rate="lending_rate", price="price", params="params") | ||
149 | exchange.assert_not_called() | ||
150 | exchange.reset_mock() | ||
151 | margin.reset_mock() | ||
152 | |||
153 | with self.subTest(account="unknown"), self.assertRaises(NotImplementedError): | ||
154 | self.s.create_order("symbol", "type", "side", "amount", account="unknown") | ||
155 | |||
156 | def test_parse_ticker(self): | ||
157 | ticker = { | ||
158 | "high24hr": "12", | ||
159 | "low24hr": "10", | ||
160 | "highestBid": "10.5", | ||
161 | "lowestAsk": "11.5", | ||
162 | "last": "11", | ||
163 | "percentChange": "0.1", | ||
164 | "quoteVolume": "10", | ||
165 | "baseVolume": "20" | ||
166 | } | ||
167 | market = { | ||
168 | "symbol": "BTC/ETC" | ||
169 | } | ||
170 | with mock.patch.object(self.s, "milliseconds") as ms: | ||
171 | ms.return_value = 1520292715123 | ||
172 | result = self.s.parse_ticker(ticker, market) | ||
173 | |||
174 | expected = { | ||
175 | "symbol": "BTC/ETC", | ||
176 | "timestamp": 1520292715123, | ||
177 | "datetime": "2018-03-05T23:31:55.123Z", | ||
178 | "high": D("12"), | ||
179 | "low": D("10"), | ||
180 | "bid": D("10.5"), | ||
181 | "ask": D("11.5"), | ||
182 | "vwap": None, | ||
183 | "open": None, | ||
184 | "close": None, | ||
185 | "first": None, | ||
186 | "last": D("11"), | ||
187 | "change": D("0.1"), | ||
188 | "percentage": None, | ||
189 | "average": None, | ||
190 | "baseVolume": D("10"), | ||
191 | "quoteVolume": D("20"), | ||
192 | "info": ticker | ||
193 | } | ||
194 | self.assertEqual(expected, result) | ||
195 | |||
196 | def test_fetch_margin_balance(self): | ||
197 | with mock.patch.object(self.s, "privatePostGetMarginPosition") as get_margin_position: | ||
198 | get_margin_position.return_value = { | ||
199 | "BTC_DASH": { | ||
200 | "amount": "-0.1", | ||
201 | "basePrice": "0.06818560", | ||
202 | "lendingFees": "0.00000001", | ||
203 | "liquidationPrice": "0.15107132", | ||
204 | "pl": "-0.00000371", | ||
205 | "total": "0.00681856", | ||
206 | "type": "short" | ||
207 | }, | ||
208 | "BTC_ETC": { | ||
209 | "amount": "-0.6", | ||
210 | "basePrice": "0.1", | ||
211 | "lendingFees": "0.00000001", | ||
212 | "liquidationPrice": "0.6", | ||
213 | "pl": "0.00000371", | ||
214 | "total": "0.06", | ||
215 | "type": "short" | ||
216 | }, | ||
217 | "BTC_ETH": { | ||
218 | "amount": "0", | ||
219 | "basePrice": "0", | ||
220 | "lendingFees": "0", | ||
221 | "liquidationPrice": "-1", | ||
222 | "pl": "0", | ||
223 | "total": "0", | ||
224 | "type": "none" | ||
225 | } | ||
226 | } | ||
227 | balances = self.s.fetch_margin_balance() | ||
228 | self.assertEqual(2, len(balances)) | ||
229 | expected = { | ||
230 | "DASH": { | ||
231 | "amount": D("-0.1"), | ||
232 | "borrowedPrice": D("0.06818560"), | ||
233 | "lendingFees": D("1E-8"), | ||
234 | "pl": D("-0.00000371"), | ||
235 | "liquidationPrice": D("0.15107132"), | ||
236 | "type": "short", | ||
237 | "total": D("0.00681856"), | ||
238 | "baseCurrency": "BTC" | ||
239 | }, | ||
240 | "ETC": { | ||
241 | "amount": D("-0.6"), | ||
242 | "borrowedPrice": D("0.1"), | ||
243 | "lendingFees": D("1E-8"), | ||
244 | "pl": D("0.00000371"), | ||
245 | "liquidationPrice": D("0.6"), | ||
246 | "type": "short", | ||
247 | "total": D("0.06"), | ||
248 | "baseCurrency": "BTC" | ||
249 | } | ||
250 | } | ||
251 | self.assertEqual(expected, balances) | ||
252 | |||
253 | def test_sum(self): | ||
254 | self.assertEqual(D("1.1"), self.s.sum(D("1"), D("0.1"))) | ||
255 | |||
256 | def test_fetch_balance(self): | ||
257 | with mock.patch.object(self.s, "load_markets") as load_markets,\ | ||
258 | mock.patch.object(self.s, "privatePostReturnCompleteBalances") as balances,\ | ||
259 | mock.patch.object(self.s, "common_currency_code") as ccc: | ||
260 | ccc.side_effect = ["ETH", "BTC", "DASH"] | ||
261 | balances.return_value = { | ||
262 | "ETH": { | ||
263 | "available": "10", | ||
264 | "onOrders": "1", | ||
265 | }, | ||
266 | "BTC": { | ||
267 | "available": "1", | ||
268 | "onOrders": "0", | ||
269 | }, | ||
270 | "DASH": { | ||
271 | "available": "0", | ||
272 | "onOrders": "3" | ||
273 | } | ||
274 | } | ||
275 | |||
276 | expected = { | ||
277 | "info": { | ||
278 | "ETH": {"available": "10", "onOrders": "1"}, | ||
279 | "BTC": {"available": "1", "onOrders": "0"}, | ||
280 | "DASH": {"available": "0", "onOrders": "3"} | ||
281 | }, | ||
282 | "ETH": {"free": D("10"), "used": D("1"), "total": D("11")}, | ||
283 | "BTC": {"free": D("1"), "used": D("0"), "total": D("1")}, | ||
284 | "DASH": {"free": D("0"), "used": D("3"), "total": D("3")}, | ||
285 | "free": {"ETH": D("10"), "BTC": D("1"), "DASH": D("0")}, | ||
286 | "used": {"ETH": D("1"), "BTC": D("0"), "DASH": D("3")}, | ||
287 | "total": {"ETH": D("11"), "BTC": D("1"), "DASH": D("3")} | ||
288 | } | ||
289 | result = self.s.fetch_balance() | ||
290 | load_markets.assert_called_once() | ||
291 | self.assertEqual(expected, result) | ||
292 | |||
293 | def test_fetch_balance_per_type(self): | ||
294 | with mock.patch.object(self.s, "privatePostReturnAvailableAccountBalances") as balances: | ||
295 | balances.return_value = { | ||
296 | "exchange": { | ||
297 | "BLK": "159.83673869", | ||
298 | "BTC": "0.00005959", | ||
299 | "USDT": "0.00002625", | ||
300 | "XMR": "0.18719303" | ||
301 | }, | ||
302 | "margin": { | ||
303 | "BTC": "0.03019227" | ||
304 | } | ||
305 | } | ||
306 | expected = { | ||
307 | "info": { | ||
308 | "exchange": { | ||
309 | "BLK": "159.83673869", | ||
310 | "BTC": "0.00005959", | ||
311 | "USDT": "0.00002625", | ||
312 | "XMR": "0.18719303" | ||
313 | }, | ||
314 | "margin": { | ||
315 | "BTC": "0.03019227" | ||
316 | } | ||
317 | }, | ||
318 | "exchange": { | ||
319 | "BLK": D("159.83673869"), | ||
320 | "BTC": D("0.00005959"), | ||
321 | "USDT": D("0.00002625"), | ||
322 | "XMR": D("0.18719303") | ||
323 | }, | ||
324 | "margin": {"BTC": D("0.03019227")}, | ||
325 | "BLK": {"exchange": D("159.83673869")}, | ||
326 | "BTC": {"exchange": D("0.00005959"), "margin": D("0.03019227")}, | ||
327 | "USDT": {"exchange": D("0.00002625")}, | ||
328 | "XMR": {"exchange": D("0.18719303")} | ||
329 | } | ||
330 | result = self.s.fetch_balance_per_type() | ||
331 | self.assertEqual(expected, result) | ||
332 | |||
333 | def test_fetch_all_balances(self): | ||
334 | import json | ||
335 | with mock.patch.object(self.s, "load_markets") as load_markets,\ | ||
336 | mock.patch.object(self.s, "privatePostGetMarginPosition") as margin_balance,\ | ||
337 | mock.patch.object(self.s, "privatePostReturnCompleteBalances") as balance,\ | ||
338 | mock.patch.object(self.s, "privatePostReturnAvailableAccountBalances") as balance_per_type: | ||
339 | |||
340 | with open("test_samples/poloniexETest.test_fetch_all_balances.1.json") as f: | ||
341 | balance.return_value = json.load(f) | ||
342 | with open("test_samples/poloniexETest.test_fetch_all_balances.2.json") as f: | ||
343 | margin_balance.return_value = json.load(f) | ||
344 | with open("test_samples/poloniexETest.test_fetch_all_balances.3.json") as f: | ||
345 | balance_per_type.return_value = json.load(f) | ||
346 | |||
347 | result = self.s.fetch_all_balances() | ||
348 | expected_doge = { | ||
349 | "total": D("-12779.79821852"), | ||
350 | "exchange_used": D("0E-8"), | ||
351 | "exchange_total": D("0E-8"), | ||
352 | "exchange_free": D("0E-8"), | ||
353 | "margin_available": 0, | ||
354 | "margin_in_position": 0, | ||
355 | "margin_borrowed": D("12779.79821852"), | ||
356 | "margin_total": D("-12779.79821852"), | ||
357 | "margin_pending_gain": 0, | ||
358 | "margin_lending_fees": D("-9E-8"), | ||
359 | "margin_pending_base_gain": D("0.00024059"), | ||
360 | "margin_position_type": "short", | ||
361 | "margin_liquidation_price": D("0.00000246"), | ||
362 | "margin_borrowed_base_price": D("0.00599149"), | ||
363 | "margin_borrowed_base_currency": "BTC" | ||
364 | } | ||
365 | expected_btc = {"total": D("0.05432165"), | ||
366 | "exchange_used": D("0E-8"), | ||
367 | "exchange_total": D("0.00005959"), | ||
368 | "exchange_free": D("0.00005959"), | ||
369 | "margin_available": D("0.03019227"), | ||
370 | "margin_in_position": D("0.02406979"), | ||
371 | "margin_borrowed": 0, | ||
372 | "margin_total": D("0.05426206"), | ||
373 | "margin_pending_gain": D("0.00093955"), | ||
374 | "margin_lending_fees": 0, | ||
375 | "margin_pending_base_gain": 0, | ||
376 | "margin_position_type": None, | ||
377 | "margin_liquidation_price": 0, | ||
378 | "margin_borrowed_base_price": 0, | ||
379 | "margin_borrowed_base_currency": None | ||
380 | } | ||
381 | expected_xmr = {"total": D("0.18719303"), | ||
382 | "exchange_used": D("0E-8"), | ||
383 | "exchange_total": D("0.18719303"), | ||
384 | "exchange_free": D("0.18719303"), | ||
385 | "margin_available": 0, | ||
386 | "margin_in_position": 0, | ||
387 | "margin_borrowed": 0, | ||
388 | "margin_total": 0, | ||
389 | "margin_pending_gain": 0, | ||
390 | "margin_lending_fees": 0, | ||
391 | "margin_pending_base_gain": 0, | ||
392 | "margin_position_type": None, | ||
393 | "margin_liquidation_price": 0, | ||
394 | "margin_borrowed_base_price": 0, | ||
395 | "margin_borrowed_base_currency": None | ||
396 | } | ||
397 | self.assertEqual(expected_xmr, result["XMR"]) | ||
398 | self.assertEqual(expected_doge, result["DOGE"]) | ||
399 | self.assertEqual(expected_btc, result["BTC"]) | ||
400 | |||
401 | def test_create_margin_order(self): | ||
402 | with self.assertRaises(market.ExchangeError): | ||
403 | self.s.create_margin_order("FOO", "market", "buy", "10") | ||
404 | |||
405 | with mock.patch.object(self.s, "load_markets") as load_markets,\ | ||
406 | mock.patch.object(self.s, "privatePostMarginBuy") as margin_buy,\ | ||
407 | mock.patch.object(self.s, "privatePostMarginSell") as margin_sell,\ | ||
408 | mock.patch.object(self.s, "market") as market_mock,\ | ||
409 | mock.patch.object(self.s, "price_to_precision") as ptp,\ | ||
410 | mock.patch.object(self.s, "amount_to_precision") as atp: | ||
411 | |||
412 | margin_buy.return_value = { | ||
413 | "orderNumber": 123 | ||
414 | } | ||
415 | margin_sell.return_value = { | ||
416 | "orderNumber": 456 | ||
417 | } | ||
418 | market_mock.return_value = { "id": "BTC_ETC", "symbol": "BTC_ETC" } | ||
419 | ptp.return_value = D("0.1") | ||
420 | atp.return_value = D("12") | ||
421 | |||
422 | order = self.s.create_margin_order("BTC_ETC", "margin", "buy", "12", price="0.1") | ||
423 | self.assertEqual(123, order["id"]) | ||
424 | margin_buy.assert_called_once_with({"currencyPair": "BTC_ETC", "rate": D("0.1"), "amount": D("12")}) | ||
425 | margin_sell.assert_not_called() | ||
426 | margin_buy.reset_mock() | ||
427 | margin_sell.reset_mock() | ||
428 | |||
429 | order = self.s.create_margin_order("BTC_ETC", "margin", "sell", "12", lending_rate="0.01", price="0.1") | ||
430 | self.assertEqual(456, order["id"]) | ||
431 | margin_sell.assert_called_once_with({"currencyPair": "BTC_ETC", "rate": D("0.1"), "amount": D("12"), "lendingRate": "0.01"}) | ||
432 | margin_buy.assert_not_called() | ||
433 | |||
434 | def test_create_exchange_order(self): | ||
435 | with mock.patch.object(market.ccxt.poloniex, "create_order") as create_order: | ||
436 | self.s.create_order("symbol", "type", "side", "amount", price="price", params="params") | ||
437 | |||
438 | create_order.assert_called_once_with("symbol", "type", "side", "amount", price="price", params="params") | ||
439 | |||
129 | @unittest.skipUnless("unit" in limits, "Unit skipped") | 440 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
130 | class PortfolioTest(WebMockTestCase): | 441 | class PortfolioTest(WebMockTestCase): |
131 | def fill_data(self): | 442 | def fill_data(self): |
@@ -135,7 +446,7 @@ class PortfolioTest(WebMockTestCase): | |||
135 | def setUp(self): | 446 | def setUp(self): |
136 | super(PortfolioTest, self).setUp() | 447 | super(PortfolioTest, self).setUp() |
137 | 448 | ||
138 | with open("test_portfolio.json") as example: | 449 | with open("test_samples/test_portfolio.json") as example: |
139 | self.json_response = example.read() | 450 | self.json_response = example.read() |
140 | 451 | ||
141 | self.wm.get(portfolio.Portfolio.URL, text=self.json_response) | 452 | self.wm.get(portfolio.Portfolio.URL, text=self.json_response) |