Skip to content

Commit 7133df9

Browse files
committed
test: fix websocket tests
1 parent c186ff0 commit 7133df9

15 files changed

+460
-544
lines changed

Gemfile.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ PLATFORMS
5656
x86_64-linux
5757

5858
DEPENDENCIES
59-
eventmachine
60-
faye-websocket
61-
openssl
62-
rest-client
63-
rubocop
64-
test-unit
59+
eventmachine (= 1.2.7)
60+
faye-websocket (= 0.11.3)
61+
openssl (= 3.2.0)
62+
rest-client (= 2.1.0)
63+
rubocop (= 1.60.2)
64+
test-unit (= 3.6.2)
6565

6666
BUNDLED WITH
6767
2.3.5

lib/cryptomarket/http_manager.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def make_request(method:, endpoint:, params: nil, public: false)
3535
def make_post_request(method:, endpoint:, params: nil)
3636
uri = URI(@@API_URL + @@API_VERSION + endpoint)
3737
payload = build_payload(params)
38-
do_request(method, uri, payload, build_post_headers(endpoint, payload), is_json: true)
38+
do_request(method, uri, payload, build_post_headers(endpoint, payload))
3939
end
4040

4141
def build_headers(method, endpoint, params, public)
@@ -52,11 +52,10 @@ def build_payload(params)
5252
payload
5353
end
5454

55-
def do_request(method, uri, payload, headers, is_json: false)
55+
def do_request(method, uri, payload, headers)
5656
response = RestClient::Request.execute(
5757
method: method.downcase.to_sym, url: uri.to_s, payload: payload.to_json, headers: headers
5858
)
59-
response[:content_type] = :json if is_json
6059
handle_response(response)
6160
rescue RestClient::ExceptionWithResponse => e
6261
handle_response(e.response)

lib/cryptomarket/websocket/wallet_client.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,18 @@ def get_wallet_balances(callback:)
109109
#
110110
# ==== Params
111111
# +String+ +currency+:: The currency code to query the balance
112-
# +Proc+ +callback+:: A +Proc+ called with an user balance
112+
# +Proc+ +callback+:: A +Proc+ called with a user balance
113113

114114
def get_wallet_balance(currency:, callback:)
115-
request('wallet_balance', callback, { currency: currency })
115+
interceptor = lambda { |err, balance|
116+
unless err.nil?
117+
callback.call(err, nil)
118+
return
119+
end
120+
balance['currency'] = currency
121+
callback.call(err, balance)
122+
}
123+
request('wallet_balance', interceptor, { currency: currency })
116124
end
117125

118126
# Get the transaction history of the account

tests/checker_generator.rb

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# frozen_string_literal: true
2+
3+
def red(msg)
4+
"\e[31m#{msg}\e[0m"
5+
end
6+
7+
def green(msg)
8+
"\e[32m#{msg}\e[0m"
9+
end
10+
11+
class VeredictChecker # rubocop:disable Style/Documentation
12+
def initialize
13+
@veredict = true
14+
@err_msgs = []
15+
end
16+
17+
def good_veredict?
18+
print "\n"
19+
@veredict
20+
end
21+
22+
def err_msg
23+
@err_msgs.to_s
24+
end
25+
26+
def append(veredict, err_msg)
27+
if veredict
28+
print(green('.'))
29+
else
30+
print(red('.'))
31+
end
32+
@err_msgs.append(err_msg) unless veredict
33+
@veredict &&= veredict
34+
end
35+
end
36+
37+
def check_error(err, veredict_checker)
38+
return unless err
39+
40+
puts "err: #{err}"
41+
veredict_checker.append(false, err)
42+
end
43+
44+
def check_val(val, check_fn, veredict_checker)
45+
return unless val
46+
47+
is_good_val = check_fn.call(val)
48+
puts "bad value: #{val}" unless is_good_val
49+
veredict_checker.append(is_good_val, val)
50+
end
51+
52+
def check_list(val_list, check_fn, veredict_checker)
53+
val_list.each { |val| check_val(val, check_fn, veredict_checker) }
54+
end
55+
56+
def check_hash(hash, check_fn, veredict_checker)
57+
hash.each_value { |val| check_val(val, check_fn, veredict_checker) }
58+
end
59+
60+
def check_hash_list(hash, check_fn, veredict_checker)
61+
hash.each_value { |val_list| check_list(val_list, check_fn, veredict_checker) }
62+
end
63+
64+
def gen_result_callback(veredict_checker)
65+
->(err, _result) { check_error(err, veredict_checker) }
66+
end
67+
68+
def gen_check_result_callback(check_fn, veredict_checker)
69+
lambda { |err, result|
70+
check_error(err, veredict_checker)
71+
check_val(result, check_fn, veredict_checker)
72+
}
73+
end
74+
75+
def gen_check_result_list_callback(check_fn, veredict_checker)
76+
lambda { |err, result|
77+
check_error(err, veredict_checker)
78+
check_list(result, check_fn, veredict_checker)
79+
}
80+
end
81+
82+
def gen_check_notification_w_n_type_callback(check_fn, veredict_checker)
83+
->(notification, _notification_type) { check_val(notification, check_fn, veredict_checker) }
84+
end
85+
86+
def gen_check_notification_list_callback(check_fn, veredict_checker)
87+
->(notification) { check_list(notification, check_fn, veredict_checker) }
88+
end
89+
90+
def |(check_fn, veredict_checker)
91+
->(notification, _notification_type) { check_list(notification, check_fn, veredict_checker) }
92+
end
93+
94+
def gen_check_notification_hash_list_callback(check_fn, veredict_checker)
95+
->(notification, _notification_type) { check_hash_list(notification, check_fn, veredict_checker) }
96+
end
97+
98+
def gen_check_notification_hash_callback(check_fn, veredict_checker)
99+
->(notification, _notification_type) { check_hash(notification, check_fn, veredict_checker) }
100+
end

tests/checks.rb

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,7 @@ def defined(a_hash, key)
1919
def good_params(a_hash, fields)
2020
return false if a_hash.nil?
2121

22-
fields.each do |field|
23-
return false unless defined(a_hash, field)
24-
end
25-
true
26-
end
27-
28-
def good_list(check_fn, list)
29-
list.each do |elem|
30-
return false unless check_fn.call(elem)
31-
end
32-
true
33-
end
34-
35-
def good_hash(_check_fn, hash)
36-
hash.each_value do |elem|
37-
return false unless check_fn(elem)
38-
end
22+
fields.each { |field| return false unless defined(a_hash, field) }
3923
true
4024
end
4125

@@ -51,9 +35,7 @@ def good_currency(currency)
5135
])
5236
return false unless good
5337

54-
currency['networks'].each do |level|
55-
return false unless good_network(level)
56-
end
38+
currency['networks'].each { |level| return false unless good_network(level) }
5739
true
5840
end
5941

@@ -85,9 +67,7 @@ def good_price_history(price_history)
8567
good = good_params(price_history, %w[currency history])
8668
return false unless good
8769

88-
price_history['history'].each do |point|
89-
return false unless good_history_point(point)
90-
end
70+
price_history['history'].each { |point| return false unless good_history_point(point) }
9171
true
9272
end
9373

@@ -104,16 +84,11 @@ def self.good_public_trade
10484
end
10585

10686
def good_orderbook(orderbook)
107-
good_orderbook = good_params(orderbook,
108-
%w[timestamp ask bid])
87+
good_orderbook = good_params(orderbook, %w[timestamp ask bid])
10988
return false unless good_orderbook
11089

111-
orderbook['ask'].each do |level|
112-
return false unless good_orderbook_level(level)
113-
end
114-
orderbook['bid'].each do |level|
115-
return false unless good_orderbook_level(level)
116-
end
90+
orderbook['ask'].each { |level| return false unless good_orderbook_level(level) }
91+
orderbook['bid'].each { |level| return false unless good_orderbook_level(level) }
11792
true
11893
end
11994

@@ -131,23 +106,22 @@ def good_order(order)
131106
quantity price quantity_cumulative created_at updated_at])
132107
end
133108

134-
def self.good_trade
109+
def good_trade
135110
lambda { |trade|
136111
good_params(trade, %w[id orderId clientOrderId symbol side quantity
137112
price fee timestamp])
138113
}
139114
end
140115

141-
def good_native_transaction(native_transaction)
116+
def self.good_native_transaction(native_transaction)
142117
good_params(native_transaction, %w[tx_id index currency amount])
143118
end
144119

145-
def good_transaction(transaction)
146-
good = good_params(transaction,
147-
%w[id status type subtype created_at updated_at])
120+
def self.good_transaction(transaction)
121+
good = good_params(transaction, %w[id status type subtype created_at updated_at])
148122
return false unless good
149123

150-
return false if transaction.key?('native') && !good_native_transaction(transaction['native'])
124+
return false if transaction.key?('native') && !Checks.good_native_transaction(transaction['native'])
151125

152126
true
153127
end
@@ -182,12 +156,8 @@ def self.good_orderbook
182156
lambda { |orderbook|
183157
return false unless good_params(orderbook, %w[t s a b])
184158

185-
orderbook['a'].each do |level|
186-
return false unless good_orderbook_level(level)
187-
end
188-
orderbook['b'].each do |level|
189-
return false unless good_orderbook_level(level)
190-
end
159+
orderbook['a'].each { |level| return false unless good_orderbook_level(level) }
160+
orderbook['b'].each { |level| return false unless good_orderbook_level(level) }
191161
true
192162
}
193163
end
@@ -199,18 +169,30 @@ def self.good_orderbook_top
199169
def self.good_price_rate
200170
->(price_rate) { good_params(price_rate, %w[t r]) }
201171
end
202-
end
203172

204-
class VeredictChecker # rubocop:disable Style/Documentation
205-
def initialize
206-
@veredict = true
173+
def self.good_report
174+
lambda { |report|
175+
good_params(report,
176+
%w[id client_order_id symbol side status type time_in_force
177+
quantity price quantity_cumulative created_at updated_at])
178+
}
179+
end
180+
181+
def self.good_balance
182+
->(balance) { good_params(balance, %w[currency available reserved]) }
183+
end
184+
185+
def self.good_commission
186+
->(commission) { good_params(commission, %w[symbol take_rate make_rate]) }
207187
end
208188

209-
def good_veredict?
210-
@veredict
189+
def self.good_transaction
190+
->(transaction) { Checks.good_transaction(transaction) }
211191
end
212192

213-
def append(veredict)
214-
@veredict &&= veredict
193+
def self.print
194+
lambda { |reports|
195+
puts reports
196+
}
215197
end
216198
end

0 commit comments

Comments
 (0)