Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit 5222994

Browse files
committed
feat(api): Add send_transfer regression test
1 parent 9877230 commit 5222994

File tree

1 file changed

+107
-5
lines changed

1 file changed

+107
-5
lines changed

tests/regression/runner.py

Lines changed: 107 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import unittest
77
import statistics
88
import time
9+
import random
10+
import logging
911

1012
DEBUG_FLAG = False
1113
if len(sys.argv) == 2:
@@ -29,6 +31,11 @@
2931
TIMES_TOTAL = 2
3032
else:
3133
TIMES_TOTAL = 100
34+
TIMES_TOTAL = 100
35+
LEN_TAG = 27
36+
LEN_ADDR = 81
37+
LEN_MSG_SIGN = 2187
38+
tryte_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ9"
3239

3340

3441
def eval_stat(time_cost, func_name):
@@ -73,15 +80,20 @@ def test_mam_recv_msg(self):
7380
eval_stat(time_cost, "mam recv message")
7481

7582

76-
def is_addr_trytes(trytes):
77-
tryte_alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ9"
78-
if len(trytes) != 81:
83+
def gen_rand_trytes(tryte_len):
84+
trytes = ""
85+
for i in range(tryte_len):
86+
trytes = trytes + tryte_alphabet[random.randint(0, 26)]
87+
return trytes
88+
89+
90+
def valid_trytes(trytes, trytes_len):
91+
if len(trytes) != trytes_len:
7992
return False
8093

8194
for char in trytes:
8295
if char not in tryte_alphabet:
8396
return False
84-
8597
return True
8698

8799

@@ -115,6 +127,7 @@ def API(get_query, get_data=None, post_data=None):
115127
return None
116128
if not response:
117129
response = ""
130+
time.sleep(2)
118131
return response
119132

120133

@@ -170,7 +183,7 @@ def test_mam_recv_msg(self):
170183
4. Not existing bundle hash (address)
171184
"""
172185
test_cases = [
173-
"QBFXQETKSHDYPFUDO9ILVCAVQIXOHXKCECZYFLPBNVIX9JUXQZJE9URQEEUWPWYZOIACTCGZX9IDIODCA",
186+
"BDIQXTDSGAWKCEPEHLRBSLDEFLXMX9ZOTUZW9JAIGZBFKPICXPEO9LLVTNIFGFDWWHEQNZXJZ9F9HTXD9",
174187
"", "生れてすみません"
175188
]
176189
expect_cases = [
@@ -187,6 +200,95 @@ def test_mam_recv_msg(self):
187200
for i in range(len(test_cases)):
188201
self.assertTrue(expect_cases[i] in response[i])
189202

203+
def test_send_transfer(self):
204+
# cmd
205+
# 0. positive value, tryte maessage, tryte tag, tryte address
206+
# 1. zero value, tryte message, tryte tag, tryte address
207+
# 2. chinese value, tryte message, tryte tag, tryte address
208+
# 3. zero value, chinese message, tryte tag, tryte address
209+
# 4. zero value, tryte message, chinese tag, tryte address
210+
# 5. negative value, tryte maessage, tryte tag, tryte address
211+
# 6. no value, tryte maessage, tryte tag, tryte address
212+
# 7. zero value, no maessage, tryte tag, tryte address
213+
# 8. zero value, tryte maessage, no tag, tryte address
214+
# 9. zero value, tryte maessage, tryte tag, no address
215+
# 10. zero value, tryte maessage, tryte tag, unicode address
216+
rand_msg = gen_rand_trytes(30)
217+
rand_tag = gen_rand_trytes(27)
218+
rand_addr = gen_rand_trytes(81)
219+
test_cases = [
220+
[420, rand_msg, rand_tag, rand_addr],
221+
[0, rand_msg, rand_tag, rand_addr],
222+
[
223+
"生而為人, 我很抱歉".encode(encoding='utf-8'), rand_msg, rand_tag,
224+
rand_addr
225+
], [0, "生而為人, 我很抱歉".encode(encoding='utf-8'), rand_tag, rand_addr],
226+
[0, rand_msg, "生而為人, 我很抱歉".encode(encoding='utf-8'), rand_addr],
227+
[-5, rand_msg, rand_tag, rand_addr],
228+
[None, rand_msg, rand_tag, rand_addr],
229+
[0, None, rand_tag, rand_addr], [0, rand_msg, None, rand_addr],
230+
[0, rand_msg, rand_tag, None], [0, rand_msg, rand_tag, "我思故我在"]
231+
]
232+
233+
response = []
234+
for i in range(len(test_cases)):
235+
logging.debug("testing case = " + str(test_cases[i]))
236+
post_data = {
237+
"value": test_cases[i][0],
238+
"message": test_cases[i][1],
239+
"tag": test_cases[i][2],
240+
"address": test_cases[i][3]
241+
}
242+
logging.debug("post_data = " + repr(post_data))
243+
post_data_json = json.dumps(post_data)
244+
response.append(API("/transaction/", post_data=post_data_json))
245+
246+
if DEBUG_FLAG == True:
247+
for i in range(len(response)):
248+
logging.debug("send transfer i = " + str(i) + ", response = " +
249+
response[i])
250+
251+
pass_case = [0, 1, 2]
252+
for i in range(len(response)):
253+
if i in pass_case:
254+
logging.debug("i = " + str(i) + ", response = " + response[i])
255+
res_split = response[i].split(", ")
256+
res_json = json.loads(res_split[0])
257+
258+
# we only send zero tx at this moment
259+
self.assertEqual(0, res_json["value"])
260+
self.assertTrue(valid_trytes(res_json["tag"], LEN_TAG))
261+
self.assertTrue(valid_trytes(res_json["address"], LEN_ADDR))
262+
self.assertTrue(
263+
valid_trytes(res_json["trunk_transaction_hash"], LEN_ADDR))
264+
self.assertTrue(
265+
valid_trytes(res_json["branch_transaction_hash"],
266+
LEN_ADDR))
267+
self.assertTrue(valid_trytes(res_json["bundle_hash"],
268+
LEN_ADDR))
269+
self.assertTrue(valid_trytes(res_json["hash"], LEN_ADDR))
270+
self.assertTrue(
271+
valid_trytes(res_json["signature_and_message_fragment"],
272+
LEN_MSG_SIGN))
273+
else:
274+
self.assertTrue(EMPTY_REPLY in response[i]
275+
or MSG_STATUS_CODE_404 in response[i])
276+
277+
# Time Statistics
278+
time_cost = []
279+
rand_msg = gen_rand_trytes(30)
280+
rand_tag = gen_rand_trytes(27)
281+
rand_addr = gen_rand_trytes(81)
282+
for i in range(TIMES_TOTAL):
283+
start_time = time.time()
284+
post_data = "{\"value\":0,\"message\":\"" + str(
285+
rand_msg) + "\",\"tag\":\"" + str(
286+
rand_tag) + "\",\"address\":\"" + str(rand_addr) + "\"}"
287+
API("/transaction/", post_data=post_data)
288+
time_cost.append(time.time() - start_time)
289+
290+
eval_stat(time_cost, "send transfer")
291+
190292

191293
"""
192294
API List

0 commit comments

Comments
 (0)