Repository navigation
Expand file tree
/
Copy pathtests_async.py
More file actions
1605 lines (1532 loc) · 86.2 KB
/
Copy pathtests_async.py
File metadata and controls
1605 lines (1532 loc) · 86.2 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import asyncio
from tests_helpers import AuthenticationError, NotSupported, InvalidProxySettings, ExchangeNotAvailable, OperationFailed, OnMaintenance, get_cli_arg_value, get_root_dir, is_sync, dump, json_parse, json_stringify, convert_ascii, io_file_exists, io_file_read, io_dir_read, call_method, call_method_sync, call_exchange_method_dynamically, call_exchange_method_dynamically_sync, get_root_exception, exception_message, exit_script, get_exchange_prop, set_exchange_prop, init_exchange, get_test_files_sync, get_test_files, set_fetch_response, is_null_value, close, get_env_vars, get_lang, get_ext # noqa: F401
class testMainClass:
id_tests = False
request_tests_failed = False
response_tests_failed = False
request_tests = False
ws_tests = False
response_tests = False
static_tests = False
info = False
verbose = False
debug = False
private_test = False
private_test_only = False
load_keys = False
sandbox = False
only_specific_tests = []
skipped_settings_for_exchange = {}
skipped_methods = {}
checked_public_tests = {}
test_files = {}
public_tests = {}
ext = ''
lang = ''
proxy_test_file_name = 'proxies'
def parse_cli_args_and_props(self):
self.response_tests = get_cli_arg_value('--responseTests') or get_cli_arg_value('--response')
self.id_tests = get_cli_arg_value('--idTests')
self.request_tests = get_cli_arg_value('--requestTests') or get_cli_arg_value('--request')
self.info = get_cli_arg_value('--info')
self.verbose = get_cli_arg_value('--verbose')
self.debug = get_cli_arg_value('--debug')
self.private_test = get_cli_arg_value('--private')
self.private_test_only = get_cli_arg_value('--privateOnly')
self.sandbox = get_cli_arg_value('--sandbox')
self.load_keys = get_cli_arg_value('--loadKeys')
self.ws_tests = get_cli_arg_value('--ws')
self.lang = get_lang()
self.ext = get_ext()
async def init(self, exchange_id, symbol_argv, method_argv):
self.parse_cli_args_and_props()
if self.request_tests and self.response_tests:
await self.run_static_request_tests(exchange_id, symbol_argv)
await self.run_static_response_tests(exchange_id, symbol_argv)
return
if self.response_tests:
await self.run_static_response_tests(exchange_id, symbol_argv)
return
if self.request_tests:
await self.run_static_request_tests(exchange_id, symbol_argv) # symbol here is the testname
return
if self.id_tests:
await self.run_broker_id_tests()
return
new_line = '\n'
dump(new_line + '' + new_line + '' + '[INFO] TESTING ', self.ext, {
'exchange': exchange_id,
'symbol': symbol_argv,
'method': method_argv,
'isWs': self.ws_tests,
'useProxy': get_cli_arg_value('--useProxy'),
}, new_line)
exchange_args = {
'verbose': self.verbose,
'debug': self.debug,
'enableRateLimit': True,
'timeout': 30000,
}
exchange = init_exchange(exchange_id, exchange_args, self.ws_tests)
if exchange.alias:
exit_script(0)
await self.import_files(exchange)
assert len(list(self.test_files.keys())) > 0, 'Test files were not loaded' # ensure test files are found & filled
self.expand_settings(exchange)
self.check_if_specific_test_is_chosen(method_argv)
await self.start_test(exchange, symbol_argv)
exit_script(0) # needed to be explicitly finished for WS tests
def check_if_specific_test_is_chosen(self, method_argv):
if method_argv is not None:
test_file_names = list(self.test_files.keys())
possible_method_names = method_argv.split(',') # i.e. `test.ts binance fetchBalance,fetchDeposits`
if len(possible_method_names) >= 1:
for i in range(0, len(test_file_names)):
test_file_name = test_file_names[i]
for j in range(0, len(possible_method_names)):
method_name = possible_method_names[j]
method_name = method_name.replace('()', '')
if test_file_name == method_name:
self.only_specific_tests.append(test_file_name)
async def import_files(self, exchange):
properties = list(exchange.has.keys())
properties.append('loadMarkets')
if is_sync():
self.test_files = get_test_files_sync(properties, self.ws_tests)
else:
self.test_files = await get_test_files(properties, self.ws_tests)
def load_credentials_from_env(self, exchange):
exchange_id = exchange.id
req_creds = get_exchange_prop(exchange, 're' + 'quiredCredentials') # dont glue the r-e-q-u-i-r-e phrase, because leads to messed up transpilation
objkeys = list(req_creds.keys())
for i in range(0, len(objkeys)):
credential = objkeys[i]
is_required = req_creds[credential]
if is_required and get_exchange_prop(exchange, credential) is None:
full_key = exchange_id + '_' + credential
credential_env_name = full_key.upper() # example: KRAKEN_APIKEY
env_vars = get_env_vars()
credential_value = env_vars[credential_env_name] if (credential_env_name in env_vars) else None
if credential_value:
set_exchange_prop(exchange, credential, credential_value)
def expand_settings(self, exchange):
exchange_id = exchange.id
keys_global = get_root_dir() + 'keys.json'
keys_local = get_root_dir() + 'keys.local.json'
keys_global_exists = io_file_exists(keys_global)
keys_local_exists = io_file_exists(keys_local)
global_settings = io_file_read(keys_global) if keys_global_exists else {}
local_settings = io_file_read(keys_local) if keys_local_exists else {}
all_settings = exchange.deep_extend(global_settings, local_settings)
exchange_settings = exchange.safe_value(all_settings, exchange_id, {})
if exchange_settings:
setting_keys = list(exchange_settings.keys())
for i in range(0, len(setting_keys)):
key = setting_keys[i]
if exchange_settings[key]:
final_value = None
if isinstance(exchange_settings[key], dict):
existing = get_exchange_prop(exchange, key, {})
final_value = exchange.deep_extend(existing, exchange_settings[key])
else:
final_value = exchange_settings[key]
set_exchange_prop(exchange, key, final_value)
# credentials
if self.load_keys:
self.load_credentials_from_env(exchange)
# skipped tests
skipped_file = get_root_dir() + 'skip-tests.json'
skipped_settings = io_file_read(skipped_file)
self.skipped_settings_for_exchange = exchange.safe_value(skipped_settings, exchange_id, {})
skipped_settings_for_exchange = self.skipped_settings_for_exchange
# others
timeout = exchange.safe_value(skipped_settings_for_exchange, 'timeout')
if timeout is not None:
exchange.timeout = exchange.parse_to_int(timeout)
if get_cli_arg_value('--useProxy'):
exchange.http_proxy = exchange.safe_string(skipped_settings_for_exchange, 'httpProxy')
exchange.https_proxy = exchange.safe_string(skipped_settings_for_exchange, 'httpsProxy')
exchange.ws_proxy = exchange.safe_string(skipped_settings_for_exchange, 'wsProxy')
exchange.wss_proxy = exchange.safe_string(skipped_settings_for_exchange, 'wssProxy')
self.skipped_methods = exchange.safe_value(skipped_settings_for_exchange, 'skipMethods', {})
self.checked_public_tests = {}
def add_padding(self, message, size):
# has to be transpilable
res = ''
message_length = len(message) # avoid php transpilation issue
missing_space = size - message_length - 0 # - 0 is added just to trick transpile to treat the .length as a string for php
if missing_space > 0:
for i in range(0, missing_space):
res += ' '
return message + res
async def test_method(self, method_name, exchange, args, is_public):
# todo: temporary skip for c#
if 'OrderBook' in method_name and self.ext == 'cs':
exchange.options['checksum'] = False
# todo: temporary skip for php
if 'OrderBook' in method_name and self.ext == 'php':
return
skipped_properties_for_method = self.get_skips(exchange, method_name)
is_load_markets = (method_name == 'loadMarkets')
is_fetch_currencies = (method_name == 'fetchCurrencies')
is_proxy_test = (method_name == self.proxy_test_file_name)
is_feature_test = (method_name == 'features')
# if this is a private test, and the implementation was already tested in public, then no need to re-test it in private test (exception is fetchCurrencies, because our approach in base exchange)
if not is_public and (method_name in self.checked_public_tests) and not is_fetch_currencies:
return
skip_message = None
supported_by_exchange = (method_name in exchange.has) and exchange.has[method_name]
if not is_load_markets and (len(self.only_specific_tests) > 0 and not exchange.in_array(method_name, self.only_specific_tests)):
skip_message = '[INFO] IGNORED_TEST'
elif not is_load_markets and not supported_by_exchange and not is_proxy_test and not is_feature_test:
skip_message = '[INFO] UNSUPPORTED_TEST' # keep it aligned with the longest message
elif isinstance(skipped_properties_for_method, str):
skip_message = '[INFO] SKIPPED_TEST'
elif not (method_name in self.test_files):
skip_message = '[INFO] UNIMPLEMENTED_TEST'
# exceptionally for `loadMarkets` call, we call it before it's even checked for "skip" as we need it to be called anyway (but can skip "test.loadMarket" for it)
if is_load_markets:
await exchange.load_markets(True)
name = exchange.id
if skip_message:
if self.info:
dump(self.add_padding(skip_message, 25), name, method_name)
return
if self.info:
args_stringified = '(' + exchange.json(args) + ')' # args.join() breaks when we provide a list of symbols or multidimensional array; "args.toString()" breaks bcz of "array to string conversion"
dump(self.add_padding('[INFO] TESTING', 25), name, method_name, args_stringified)
if is_sync():
call_method_sync(self.test_files, method_name, exchange, skipped_properties_for_method, args)
else:
await call_method(self.test_files, method_name, exchange, skipped_properties_for_method, args)
if self.info:
dump(self.add_padding('[INFO] TESTING DONE', 25), name, method_name)
# add to the list of successed tests
if is_public:
self.checked_public_tests[method_name] = True
return
def get_skips(self, exchange, method_name):
final_skips = {}
# check the exact method (i.e. `fetchTrades`) and language-specific (i.e. `fetchTrades.php`)
method_names = [method_name, method_name + '.' + self.ext]
for i in range(0, len(method_names)):
m_name = method_names[i]
if m_name in self.skipped_methods:
# if whole method is skipped, by assigning a string to it, i.e. "fetchOrders":"blabla"
if isinstance(self.skipped_methods[m_name], str):
return self.skipped_methods[m_name]
else:
final_skips = exchange.deep_extend(final_skips, self.skipped_methods[m_name])
# get "object-specific" skips
object_skips = {
'orderBook': ['fetchOrderBook', 'fetchOrderBooks', 'fetchL2OrderBook', 'watchOrderBook', 'watchOrderBookForSymbols'],
'ticker': ['fetchTicker', 'fetchTickers', 'watchTicker', 'watchTickers'],
'trade': ['fetchTrades', 'watchTrades', 'watchTradesForSymbols'],
'ohlcv': ['fetchOHLCV', 'watchOHLCV', 'watchOHLCVForSymbols'],
'ledger': ['fetchLedger', 'fetchLedgerEntry'],
'depositWithdraw': ['fetchDepositsWithdrawals', 'fetchDeposits', 'fetchWithdrawals'],
'depositWithdrawFee': ['fetchDepositWithdrawFee', 'fetchDepositWithdrawFees'],
}
object_names = list(object_skips.keys())
for i in range(0, len(object_names)):
object_name = object_names[i]
object_methods = object_skips[object_name]
if exchange.in_array(method_name, object_methods):
# if whole object is skipped, by assigning a string to it, i.e. "orderBook":"blabla"
if (object_name in self.skipped_methods) and (isinstance(self.skipped_methods[object_name], str)):
return self.skipped_methods[object_name]
extra_skips = exchange.safe_dict(self.skipped_methods, object_name, {})
final_skips = exchange.deep_extend(final_skips, extra_skips)
# extend related skips
# - if 'timestamp' is skipped, we should do so for 'datetime' too
# - if 'bid' is skipped, skip 'ask' too
if ('timestamp' in final_skips) and not ('datetime' in final_skips):
final_skips['datetime'] = final_skips['timestamp']
if ('bid' in final_skips) and not ('ask' in final_skips):
final_skips['ask'] = final_skips['bid']
if ('baseVolume' in final_skips) and not ('quoteVolume' in final_skips):
final_skips['quoteVolume'] = final_skips['baseVolume']
return final_skips
async def test_safe(self, method_name, exchange, args=[], is_public=False):
# `testSafe` method does not throw an exception, instead mutes it. The reason we
# mute the thrown exceptions here is because we don't want to stop the whole
# tests queue if any single test-method fails. Instead, they are echoed with
# formatted message "[TEST_FAILURE] ..." and that output is then regex-matched by
# run-tests.js, so the exceptions are still printed out to console from there.
max_retries = 3
args_stringified = exchange.json(args) # args.join() breaks when we provide a list of symbols or multidimensional array; "args.toString()" breaks bcz of "array to string conversion"
for i in range(0, max_retries):
try:
await self.test_method(method_name, exchange, args, is_public)
return True
except Exception as ex:
e = get_root_exception(ex)
is_load_markets = (method_name == 'loadMarkets')
is_auth_error = (isinstance(e, AuthenticationError))
is_not_supported = (isinstance(e, NotSupported))
is_operation_failed = (isinstance(e, OperationFailed)) # includes "DDoSProtection", "RateLimitExceeded", "RequestTimeout", "ExchangeNotAvailable", "OperationFailed", "InvalidNonce", ...
if is_operation_failed:
# if last retry was gone with same `tempFailure` error, then let's eventually return false
if i == max_retries - 1:
is_on_maintenance = (isinstance(e, OnMaintenance))
is_exchange_not_available = (isinstance(e, ExchangeNotAvailable))
should_fail = None
return_success = None
if is_load_markets:
# if "loadMarkets" does not succeed, we must return "false" to caller method, to stop tests continual
return_success = False
# we might not break exchange tests, if exchange is on maintenance at this moment
if is_on_maintenance:
should_fail = False
else:
should_fail = True
else:
# for any other method tests:
if is_exchange_not_available and not is_on_maintenance:
# break exchange tests if "ExchangeNotAvailable" exception is thrown, but it's not maintenance
should_fail = True
return_success = False
else:
# in all other cases of OperationFailed, show Warning, but don't mark test as failed
should_fail = False
return_success = True
# output the message
fail_type = '[TEST_FAILURE]' if should_fail else '[TEST_WARNING]'
dump(fail_type, 'Method could not be tested due to a repeated Network/Availability issues', ' | ', exchange.id, method_name, args_stringified, exception_message(e))
return return_success
else:
# wait and retry again
# (increase wait time on every retry)
await exchange.sleep((i + 1) * 1000)
continue
else:
# if it's loadMarkets, then fail test, because it's mandatory for tests
if is_load_markets:
dump('[TEST_FAILURE]', 'Exchange can not load markets', exception_message(e), exchange.id, method_name, args_stringified)
return False
# if the specific arguments to the test method throws "NotSupported" exception
# then let's don't fail the test
if is_not_supported:
if self.info:
dump('[INFO] NOT_SUPPORTED', exception_message(e), exchange.id, method_name, args_stringified)
return True
# If public test faces authentication error, we don't break (see comments under `testSafe` method)
if is_public and is_auth_error:
if self.info:
dump('[INFO]', 'Authentication problem for public method', exception_message(e), exchange.id, method_name, args_stringified)
return True
else:
dump('[TEST_FAILURE]', exception_message(e), exchange.id, method_name, args_stringified)
return False
return True
async def run_public_tests(self, exchange, symbol):
tests = {
'features': [],
'fetchCurrencies': [],
'fetchTicker': [symbol],
'fetchTickers': [symbol],
'fetchLastPrices': [symbol],
'fetchOHLCV': [symbol],
'fetchTrades': [symbol],
'fetchOrderBook': [symbol],
'fetchL2OrderBook': [symbol],
'fetchOrderBooks': [],
'fetchBidsAsks': [],
'fetchStatus': [],
'fetchTime': [],
}
if self.ws_tests:
tests = {
'watchOHLCV': [symbol],
'watchOHLCVForSymbols': [symbol],
'watchTicker': [symbol],
'watchTickers': [symbol],
'watchBidsAsks': [symbol],
'watchOrderBook': [symbol],
'watchOrderBookForSymbols': [[symbol]],
'watchTrades': [symbol],
'watchTradesForSymbols': [[symbol]],
}
market = exchange.market(symbol)
is_spot = market['spot']
if not self.ws_tests:
if is_spot:
tests['fetchCurrencies'] = []
else:
tests['fetchFundingRates'] = [symbol]
tests['fetchFundingRate'] = [symbol]
tests['fetchFundingRateHistory'] = [symbol]
tests['fetchIndexOHLCV'] = [symbol]
tests['fetchMarkOHLCV'] = [symbol]
tests['fetchPremiumIndexOHLCV'] = [symbol]
self.public_tests = tests
await self.run_tests(exchange, tests, True)
async def run_tests(self, exchange, tests, is_public_test):
test_names = list(tests.keys())
promises = []
for i in range(0, len(test_names)):
test_name = test_names[i]
test_args = tests[test_name]
promises.append(self.test_safe(test_name, exchange, test_args, is_public_test))
# todo - not yet ready in other langs too
# promises.push (testThrottle ());
results = await asyncio.gather(*promises)
# now count which test-methods retuned `false` from "testSafe" and dump that info below
failed_methods = []
for i in range(0, len(test_names)):
test_name = test_names[i]
test_returned_value = results[i]
if not test_returned_value:
failed_methods.append(test_name)
test_prefix_string = 'PUBLIC_TESTS' if is_public_test else 'PRIVATE_TESTS'
if len(failed_methods):
errors_string = ', '.join(failed_methods)
dump('[TEST_FAILURE]', exchange.id, test_prefix_string, 'Failed methods : ' + errors_string)
if self.info:
dump(self.add_padding('[INFO] END ' + test_prefix_string + ' ' + exchange.id, 25))
async def load_exchange(self, exchange):
result = await self.test_safe('loadMarkets', exchange, [], True)
if not result:
return False
exchange_symbols_length = len(exchange.symbols)
dump('[INFO:MAIN] Exchange loaded', exchange_symbols_length, 'symbols')
return True
def get_test_symbol(self, exchange, is_spot, symbols):
symbol = None
preferred_spot_symbol = exchange.safe_string(self.skipped_settings_for_exchange, 'preferredSpotSymbol')
preferred_swap_symbol = exchange.safe_string(self.skipped_settings_for_exchange, 'preferredSwapSymbol')
if is_spot and preferred_spot_symbol:
return preferred_spot_symbol
elif not is_spot and preferred_swap_symbol:
return preferred_swap_symbol
for i in range(0, len(symbols)):
s = symbols[i]
market = exchange.safe_value(exchange.markets, s)
if market is not None:
active = exchange.safe_value(market, 'active')
if active or (active is None):
symbol = s
break
return symbol
def get_exchange_code(self, exchange, codes=None):
if codes is None:
codes = ['BTC', 'ETH', 'XRP', 'LTC', 'BCH', 'EOS', 'BNB', 'BSV', 'USDT']
code = codes[0]
for i in range(0, len(codes)):
if codes[i] in exchange.currencies:
return codes[i]
return code
def get_markets_from_exchange(self, exchange, spot=True):
res = {}
markets = exchange.markets
keys = list(markets.keys())
for i in range(0, len(keys)):
key = keys[i]
market = markets[key]
if spot and market['spot']:
res[market['symbol']] = market
elif not spot and not market['spot']:
res[market['symbol']] = market
return res
def get_valid_symbol(self, exchange, spot=True):
current_type_markets = self.get_markets_from_exchange(exchange, spot)
codes = ['BTC', 'ETH', 'XRP', 'LTC', 'BNB', 'DASH', 'DOGE', 'ETC', 'TRX', 'USDT', 'USDC', 'USD', 'EUR', 'TUSD', 'CNY', 'JPY', 'BRL']
spot_symbols = ['BTC/USDT', 'BTC/USDC', 'BTC/USD', 'BTC/CNY', 'BTC/EUR', 'BTC/AUD', 'BTC/BRL', 'BTC/JPY', 'ETH/USDT', 'ETH/USDC', 'ETH/USD', 'ETH/CNY', 'ETH/EUR', 'ETH/AUD', 'ETH/BRL', 'ETH/JPY', 'EUR/USDT', 'EUR/USD', 'EUR/USDC', 'USDT/EUR', 'USD/EUR', 'USDC/EUR', 'BTC/ETH', 'ETH/BTC']
swap_symbols = ['BTC/USDT:USDT', 'BTC/USDC:USDC', 'BTC/USD:USD', 'ETH/USDT:USDT', 'ETH/USDC:USDC', 'ETH/USD:USD', 'BTC/USD:BTC', 'ETH/USD:ETH']
target_symbols = spot_symbols if spot else swap_symbols
symbol = self.get_test_symbol(exchange, spot, target_symbols)
# if symbols wasn't found from above hardcoded list, then try to locate any symbol which has our target hardcoded 'base' code
if symbol is None:
for i in range(0, len(codes)):
current_code = codes[i]
markets_array_for_current_code = exchange.filter_by(current_type_markets, 'base', current_code)
indexed_mkts = exchange.index_by(markets_array_for_current_code, 'symbol')
symbols_array_for_current_code = list(indexed_mkts.keys())
symbols_length = len(symbols_array_for_current_code)
if symbols_length:
symbol = self.get_test_symbol(exchange, spot, symbols_array_for_current_code)
break
# if there wasn't found any symbol with our hardcoded 'base' code, then just try to find symbols that are 'active'
if symbol is None:
active_markets = exchange.filter_by(current_type_markets, 'active', True)
active_symbols = []
for i in range(0, len(active_markets)):
active_symbols.append(active_markets[i]['symbol'])
symbol = self.get_test_symbol(exchange, spot, active_symbols)
if symbol is None:
values = list(current_type_markets.values())
values_length = len(values)
if values_length > 0:
first = values[0]
if first is not None:
symbol = first['symbol']
return symbol
async def test_exchange(self, exchange, provided_symbol=None):
spot_symbol = None
swap_symbol = None
if provided_symbol is not None:
market = exchange.market(provided_symbol)
if market['spot']:
spot_symbol = provided_symbol
else:
swap_symbol = provided_symbol
else:
if exchange.has['spot']:
spot_symbol = self.get_valid_symbol(exchange, True)
if exchange.has['swap']:
swap_symbol = self.get_valid_symbol(exchange, False)
if spot_symbol is not None:
dump('[INFO:MAIN] Selected SPOT SYMBOL:', spot_symbol)
if swap_symbol is not None:
dump('[INFO:MAIN] Selected SWAP SYMBOL:', swap_symbol)
if not self.private_test_only:
# note, spot & swap tests should run sequentially, because of conflicting `exchange.options['defaultType']` setting
if exchange.has['spot'] and spot_symbol is not None:
if self.info:
dump('[INFO] ### SPOT TESTS ###')
exchange.options['defaultType'] = 'spot'
await self.run_public_tests(exchange, spot_symbol)
if exchange.has['swap'] and swap_symbol is not None:
if self.info:
dump('[INFO] ### SWAP TESTS ###')
exchange.options['defaultType'] = 'swap'
await self.run_public_tests(exchange, swap_symbol)
if self.private_test or self.private_test_only:
if exchange.has['spot'] and spot_symbol is not None:
exchange.options['defaultType'] = 'spot'
await self.run_private_tests(exchange, spot_symbol)
if exchange.has['swap'] and swap_symbol is not None:
exchange.options['defaultType'] = 'swap'
await self.run_private_tests(exchange, swap_symbol)
async def run_private_tests(self, exchange, symbol):
if not exchange.check_required_credentials(False):
dump('[INFO] Skipping private tests', 'Keys not found')
return
code = self.get_exchange_code(exchange)
# if (exchange.deepExtendedTest) {
# await test ('InvalidNonce', exchange, symbol);
# await test ('OrderNotFound', exchange, symbol);
# await test ('InvalidOrder', exchange, symbol);
# await test ('InsufficientFunds', exchange, symbol, balance); # danger zone - won't execute with non-empty balance
# }
tests = {
'signIn': [],
'fetchBalance': [],
'fetchAccounts': [],
'fetchTransactionFees': [],
'fetchTradingFees': [],
'fetchStatus': [],
'fetchOrders': [symbol],
'fetchOpenOrders': [symbol],
'fetchClosedOrders': [symbol],
'fetchMyTrades': [symbol],
'fetchLeverageTiers': [[symbol]],
'fetchLedger': [code],
'fetchTransactions': [code],
'fetchDeposits': [code],
'fetchWithdrawals': [code],
'fetchBorrowInterest': [code, symbol],
'cancelAllOrders': [symbol],
'fetchCanceledOrders': [symbol],
'fetchMarginModes': [symbol],
'fetchPosition': [symbol],
'fetchDeposit': [code],
'createDepositAddress': [code],
'fetchDepositAddress': [code],
'fetchDepositAddresses': [code],
'fetchDepositAddressesByNetwork': [code],
'fetchBorrowRateHistory': [code],
'fetchLedgerEntry': [code],
}
if self.ws_tests:
tests = {
'watchBalance': [code],
'watchMyTrades': [symbol],
'watchOrders': [symbol],
'watchPosition': [symbol],
'watchPositions': [symbol],
}
market = exchange.market(symbol)
is_spot = market['spot']
if not self.ws_tests:
if is_spot:
tests['fetchCurrencies'] = []
else:
# derivatives only
tests['fetchPositions'] = [symbol] # this test fetches all positions for 1 symbol
tests['fetchPosition'] = [symbol]
tests['fetchPositionRisk'] = [symbol]
tests['setPositionMode'] = [symbol]
tests['setMarginMode'] = [symbol]
tests['fetchOpenInterestHistory'] = [symbol]
tests['fetchFundingRateHistory'] = [symbol]
tests['fetchFundingHistory'] = [symbol]
# const combinedTests = exchange.deepExtend (this.publicTests, privateTests);
await self.run_tests(exchange, tests, False)
async def test_proxies(self, exchange):
# these tests should be synchronously executed, because of conflicting nature of proxy settings
proxy_test_name = self.proxy_test_file_name
# todo: temporary skip for sync py
if self.ext == 'py' and is_sync():
return
# try proxy several times
max_retries = 3
exception = None
for j in range(0, max_retries):
try:
await self.test_method(proxy_test_name, exchange, [], True)
return # if successfull, then end the test
except Exception as e:
exception = e
await exchange.sleep(j * 1000)
# if exception was set, then throw it
if exception is not None:
error_message = '[TEST_FAILURE] Failed ' + proxy_test_name + ' : ' + exception_message(exception)
# temporary comment the below, because c# transpilation failure
# throw new Exchange Error (errorMessage.toString ());
dump('[TEST_WARNING]' + str(error_message))
async def start_test(self, exchange, symbol):
# we do not need to test aliases
if exchange.alias:
return
if self.sandbox or get_exchange_prop(exchange, 'sandbox'):
exchange.set_sandbox_mode(True)
try:
result = await self.load_exchange(exchange)
if not result:
if not is_sync():
await close(exchange)
return
# if (exchange.id === 'binance') {
# # we test proxies functionality just for one random exchange on each build, because proxy functionality is not exchange-specific, instead it's all done from base methods, so just one working sample would mean it works for all ccxt exchanges
# # await this.testProxies (exchange);
# }
await self.test_exchange(exchange, symbol)
if not is_sync():
await close(exchange)
except Exception as e:
if not is_sync():
await close(exchange)
raise e
def assert_static_error(self, cond, message, calculated_output, stored_output, key=None):
# -----------------------------------------------------------------------------
# --- Init of static tests functions------------------------------------------
# -----------------------------------------------------------------------------
calculated_string = json_stringify(calculated_output)
stored_string = json_stringify(stored_output)
error_message = message
if key is not None:
error_message = '[' + key + ']'
error_message += ' computed: ' + stored_string + ' stored: ' + calculated_string
assert cond, error_message
def load_markets_from_file(self, id):
# load markets from file
# to make this test as fast as possible
# and basically independent from the exchange
# so we can run it offline
filename = get_root_dir() + './ts/src/test/static/markets/' + id + '.json'
content = io_file_read(filename)
return content
def load_currencies_from_file(self, id):
filename = get_root_dir() + './ts/src/test/static/currencies/' + id + '.json'
content = io_file_read(filename)
return content
def load_static_data(self, folder, target_exchange=None):
result = {}
if target_exchange:
# read a single exchange
path = folder + target_exchange + '.json'
if not io_file_exists(path):
dump('[WARN] tests not found: ' + path)
return None
result[target_exchange] = io_file_read(path)
return result
files = io_dir_read(folder)
for i in range(0, len(files)):
file = files[i]
exchange_name = file.replace('.json', '')
content = io_file_read(folder + file)
result[exchange_name] = content
return result
def remove_hostnamefrom_url(self, url):
if url is None:
return None
url_parts = url.split('/')
res = ''
for i in range(0, len(url_parts)):
if i > 2:
current = url_parts[i]
if current.find('?') > -1:
# handle urls like this: /v1/account/accounts?AccessK
current_parts = current.split('?')
res += '/'
res += current_parts[0]
break
res += '/'
res += current
return res
def urlencoded_to_dict(self, url):
result = {}
parts = url.split('&')
for i in range(0, len(parts)):
part = parts[i]
key_value = part.split('=')
keys_length = len(key_value)
if keys_length != 2:
continue
key = key_value[0]
value = key_value[1]
if (value is not None) and ((value.startswith('[')) or (value.startswith('{'))):
# some exchanges might return something like this: timestamp=1699382693405&batchOrders=[{\"symbol\":\"LTCUSDT\",\"side\":\"BUY\",\"newClientOrderI
value = json_parse(value)
result[key] = value
return result
def assert_new_and_stored_output_inner(self, exchange, skip_keys, new_output, stored_output, strict_type_check=True, asserting_key=None):
if is_null_value(new_output) and is_null_value(stored_output):
return True
if not new_output and not stored_output:
return True
if (isinstance(stored_output, dict)) and (isinstance(new_output, dict)):
stored_output_keys = list(stored_output.keys())
new_output_keys = list(new_output.keys())
stored_keys_length = len(stored_output_keys)
new_keys_length = len(new_output_keys)
self.assert_static_error(stored_keys_length == new_keys_length, 'output length mismatch', stored_output, new_output)
# iterate over the keys
for i in range(0, len(stored_output_keys)):
key = stored_output_keys[i]
if exchange.in_array(key, skip_keys):
continue
if not (exchange.in_array(key, new_output_keys)):
self.assert_static_error(False, 'output key missing: ' + key, stored_output, new_output)
stored_value = stored_output[key]
new_value = new_output[key]
self.assert_new_and_stored_output(exchange, skip_keys, new_value, stored_value, strict_type_check, key)
elif isinstance(stored_output, list) and (isinstance(new_output, list)):
stored_array_length = len(stored_output)
new_array_length = len(new_output)
self.assert_static_error(stored_array_length == new_array_length, 'output length mismatch', stored_output, new_output)
for i in range(0, len(stored_output)):
stored_item = stored_output[i]
new_item = new_output[i]
self.assert_new_and_stored_output(exchange, skip_keys, new_item, stored_item, strict_type_check)
else:
# built-in types like strings, numbers, booleans
sanitized_new_output = None if (not new_output) else new_output # we store undefined as nulls in the json file so we need to convert it back
sanitized_stored_output = None if (not stored_output) else stored_output
new_output_string = str(sanitized_new_output) if sanitized_new_output else 'undefined'
stored_output_string = str(sanitized_stored_output) if sanitized_stored_output else 'undefined'
message_error = 'output value mismatch:' + new_output_string + ' != ' + stored_output_string
if strict_type_check and (self.lang != 'C#'):
# upon building the request we want strict type check to make sure all the types are correct
# when comparing the response we want to allow some flexibility, because a 50.0 can be equal to 50 after saving it to the json file
self.assert_static_error(sanitized_new_output == sanitized_stored_output, message_error, stored_output, new_output, asserting_key)
else:
is_computed_bool = (isinstance(sanitized_new_output, bool))
is_stored_bool = (isinstance(sanitized_stored_output, bool))
is_computed_string = (isinstance(sanitized_new_output, str))
is_stored_string = (isinstance(sanitized_stored_output, str))
is_computed_undefined = (sanitized_new_output is None)
is_stored_undefined = (sanitized_stored_output is None)
should_be_same = (is_computed_bool == is_stored_bool) and (is_computed_string == is_stored_string) and (is_computed_undefined == is_stored_undefined)
self.assert_static_error(should_be_same, 'output type mismatch', stored_output, new_output, asserting_key)
is_boolean = is_computed_bool or is_stored_bool
is_string = is_computed_string or is_stored_string
is_undefined = is_computed_undefined or is_stored_undefined # undefined is a perfetly valid value
if is_boolean or is_string or is_undefined:
if self.lang == 'C#':
# tmp c# number comparsion
is_number = False
try:
exchange.parse_to_numeric(sanitized_new_output)
is_number = True
except Exception as e:
# if we can't parse it to number, then it's not a number
is_number = False
if is_number:
self.assert_static_error(exchange.parse_to_numeric(sanitized_new_output) == exchange.parse_to_numeric(sanitized_stored_output), message_error, stored_output, new_output, asserting_key)
return True
else:
self.assert_static_error(convert_ascii(new_output_string) == convert_ascii(stored_output_string), message_error, stored_output, new_output, asserting_key)
return True
else:
self.assert_static_error(convert_ascii(new_output_string) == convert_ascii(stored_output_string), message_error, stored_output, new_output, asserting_key)
return True
else:
if self.lang == 'C#':
stringified_new_output = exchange.number_to_string(sanitized_new_output)
stringified_stored_output = exchange.number_to_string(sanitized_stored_output)
self.assert_static_error(str(stringified_new_output) == str(stringified_stored_output), message_error, stored_output, new_output, asserting_key)
else:
numeric_new_output = exchange.parse_to_numeric(new_output_string)
numeric_stored_output = exchange.parse_to_numeric(stored_output_string)
self.assert_static_error(numeric_new_output == numeric_stored_output, message_error, stored_output, new_output, asserting_key)
return True # c# requ
def assert_new_and_stored_output(self, exchange, skip_keys, new_output, stored_output, strict_type_check=True, asserting_key=None):
try:
return self.assert_new_and_stored_output_inner(exchange, skip_keys, new_output, stored_output, strict_type_check, asserting_key)
except Exception as e:
if self.info:
error_message = self.var_to_string(new_output) + '(calculated)' + ' != ' + self.var_to_string(stored_output) + '(stored)'
dump('[TEST_FAILURE_DETAIL]' + error_message)
raise e
def var_to_string(self, obj=None):
new_string = None
if obj is None:
new_string = 'undefined'
elif is_null_value(obj):
new_string = 'null'
else:
new_string = json_stringify(obj)
return new_string
def assert_static_request_output(self, exchange, type, skip_keys, stored_url, request_url, stored_output, new_output):
if stored_url != request_url:
# remove the host part from the url
first_path = self.remove_hostnamefrom_url(stored_url)
second_path = self.remove_hostnamefrom_url(request_url)
self.assert_static_error(first_path == second_path, 'url mismatch', first_path, second_path)
# body (aka storedOutput and newOutput) is not defined and information is in the url
# example: "https://open-api.bingx.com/openApi/spot/v1/trade/order?quoteOrderQty=5&side=BUY&symbol=LTC-USDT×tamp=1698777135343&type=MARKET&signature=d55a7e4f7f9dbe56c4004c9f3ab340869d3cb004e2f0b5b861e5fbd1762fd9a0
if (stored_output is None) and (new_output is None):
if (stored_url is not None) and (request_url is not None):
stored_url_parts = stored_url.split('?')
new_url_parts = request_url.split('?')
stored_url_query = exchange.safe_value(stored_url_parts, 1)
new_url_query = exchange.safe_value(new_url_parts, 1)
if (stored_url_query is None) and (new_url_query is None):
# might be a get request without any query parameters
# example: https://api.gateio.ws/api/v4/delivery/usdt/positions
return
stored_url_params = self.urlencoded_to_dict(stored_url_query)
new_url_params = self.urlencoded_to_dict(new_url_query)
self.assert_new_and_stored_output(exchange, skip_keys, new_url_params, stored_url_params)
return
if type == 'json' and (stored_output is not None) and (new_output is not None):
if isinstance(stored_output, str):
stored_output = json_parse(stored_output)
if isinstance(new_output, str):
new_output = json_parse(new_output)
elif type == 'urlencoded' and (stored_output is not None) and (new_output is not None):
stored_output = self.urlencoded_to_dict(stored_output)
new_output = self.urlencoded_to_dict(new_output)
elif type == 'both':
if stored_output.startswith('{') or stored_output.startswith('['):
stored_output = json_parse(stored_output)
new_output = json_parse(new_output)
else:
stored_output = self.urlencoded_to_dict(stored_output)
new_output = self.urlencoded_to_dict(new_output)
self.assert_new_and_stored_output(exchange, skip_keys, new_output, stored_output)
def assert_static_response_output(self, exchange, skip_keys, computed_result, stored_result):
self.assert_new_and_stored_output(exchange, skip_keys, computed_result, stored_result, False)
def sanitize_data_input(self, input):
# remove nulls and replace with unefined instead
if input is None:
return None
new_input = []
for i in range(0, len(input)):
current = input[i]
if is_null_value(current):
new_input.append(None)
else:
new_input.append(current)
return new_input
async def test_request_statically(self, exchange, method, data, type, skip_keys):
output = None
request_url = None
try:
if not is_sync():
await call_exchange_method_dynamically(exchange, method, self.sanitize_data_input(data['input']))
else:
call_exchange_method_dynamically_sync(exchange, method, self.sanitize_data_input(data['input']))
except Exception as e:
if not (isinstance(e, InvalidProxySettings)):
raise e
output = exchange.last_request_body
request_url = exchange.last_request_url
try:
call_output = exchange.safe_value(data, 'output')
self.assert_static_request_output(exchange, type, skip_keys, data['url'], request_url, call_output, output)
except Exception as e:
self.request_tests_failed = True
error_message = '[' + self.lang + '][STATIC_REQUEST]' + '[' + exchange.id + ']' + '[' + method + ']' + '[' + data['description'] + ']' + str(e)
dump('[TEST_FAILURE]' + error_message)
async def test_response_statically(self, exchange, method, skip_keys, data):
expected_result = exchange.safe_value(data, 'parsedResponse')
mocked_exchange = set_fetch_response(exchange, data['httpResponse'])
try:
if not is_sync():
unified_result = await call_exchange_method_dynamically(exchange, method, self.sanitize_data_input(data['input']))
self.assert_static_response_output(mocked_exchange, skip_keys, unified_result, expected_result)
else:
unified_result_sync = call_exchange_method_dynamically_sync(exchange, method, self.sanitize_data_input(data['input']))
self.assert_static_response_output(mocked_exchange, skip_keys, unified_result_sync, expected_result)
except Exception as e:
self.response_tests_failed = True
error_message = '[' + self.lang + '][STATIC_RESPONSE]' + '[' + exchange.id + ']' + '[' + method + ']' + '[' + data['description'] + ']' + str(e)
dump('[TEST_FAILURE]' + error_message)
set_fetch_response(exchange, None) # reset state
def init_offline_exchange(self, exchange_name):
markets = self.load_markets_from_file(exchange_name)
currencies = self.load_currencies_from_file(exchange_name)
exchange = init_exchange(exchange_name, {
'markets': markets,
'currencies': currencies,
'enableRateLimit': False,
'rateLimit': 1,
'httpProxy': 'http://fake:8080',
'httpsProxy': 'http://fake:8080',
'apiKey': 'key',
'secret': 'secretsecret',
'password': 'password',
'walletAddress': 'wallet',
'privateKey': '0xff3bdd43534543d421f05aec535965b5050ad6ac15345435345435453495e771',
'uid': 'uid',
'token': 'token',
'login': 'login',
'accountId': 'accountId',
'accounts': [{
'id': 'myAccount',
'code': 'USDT',
}, {
'id': 'myAccount',
'code': 'USDC',
}],
'options': {
'enableUnifiedAccount': True,
'enableUnifiedMargin': False,
'accessToken': 'token',
'expires': 999999999999999,
'leverageBrackets': {},
},
})
exchange.currencies = currencies # not working in python if assigned in the config dict
return exchange
async def test_exchange_request_statically(self, exchange_name, exchange_data, test_name=None):
# instantiate the exchange and make sure that we sink the requests to avoid an actual request
exchange = self.init_offline_exchange(exchange_name)
global_options = exchange.safe_dict(exchange_data, 'options', {})
# read apiKey/secret from the test file
api_key = exchange.safe_string(exchange_data, 'apiKey')
if api_key:
exchange.apiKey = str(api_key)
secret = exchange.safe_string(exchange_data, 'secret')
if secret:
exchange.secret = str(secret)
private_key = exchange.safe_string(exchange_data, 'privateKey')
if private_key:
exchange.privateKey = str(private_key)
wallet_address = exchange.safe_string(exchange_data, 'walletAddress')
if wallet_address:
exchange.walletAddress = str(wallet_address)
accounts = exchange.safe_list(exchange_data, 'accounts')
if accounts:
exchange.accounts = accounts
# exchange.options = exchange.deepExtend (exchange.options, globalOptions); # custom options to be used in the tests
exchange.extend_exchange_options(global_options)
methods = exchange.safe_value(exchange_data, 'methods', {})
methods_names = list(methods.keys())
for i in range(0, len(methods_names)):
method = methods_names[i]
results = methods[method]
for j in range(0, len(results)):
result = results[j]
old_exchange_options = exchange.options # snapshot options;
test_exchange_options = exchange.safe_value(result, 'options', {})
# exchange.options = exchange.deepExtend (oldExchangeOptions, testExchangeOptions); # custom options to be used in the tests
exchange.extend_exchange_options(exchange.deep_extend(old_exchange_options, test_exchange_options))
description = exchange.safe_value(result, 'description')
if (test_name is not None) and (test_name != description):
continue
is_disabled = exchange.safe_bool(result, 'disabled', False)
if is_disabled:
continue
disabled_string = exchange.safe_string(result, 'disabled', '')
if disabled_string != '':
continue
is_disabled_c_sharp = exchange.safe_bool(result, 'disabledCS', False)
if is_disabled_c_sharp and (self.lang == 'C#'):
continue
type = exchange.safe_string(exchange_data, 'outputType')
skip_keys = exchange.safe_value(exchange_data, 'skipKeys', [])
await self.test_request_statically(exchange, method, result, type, skip_keys)
# reset options
# exchange.options = exchange.deepExtend (oldExchangeOptions, {});
exchange.extend_exchange_options(exchange.deep_extend(old_exchange_options, {}))
if not is_sync():
await close(exchange)
return True # in c# methods that will be used with promiseAll need to return something