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

Commit f0e7975

Browse files
committed
fix(reg): Save APIs request response as dict
The http content and status code of API's request responses were saved in a list which has less readability. Now they were saved in a dictionary, so we can use the content and status code with key `content` and `status_code` respectively. Remove the redundent part of if statement
1 parent 63fb5f8 commit f0e7975

File tree

1 file changed

+81
-96
lines changed

1 file changed

+81
-96
lines changed

tests/regression/runner.py

Lines changed: 81 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ def valid_trytes(trytes, trytes_len):
6868

6969
def API(get_query, get_data=None, post_data=None):
7070
try:
71-
response = []
71+
response = {}
7272
if get_data is not None:
7373
r = requests.get(str(url + get_query + get_data), timeout=TIMEOUT)
74-
response = [r.text, str(r.status_code)]
74+
response = {"content": r.text, "status_code": str(r.status_code)}
7575

7676
elif post_data is not None:
7777
command = "curl " + str(
@@ -85,7 +85,10 @@ def API(get_query, get_data=None, post_data=None):
8585
stderr=subprocess.PIPE)
8686
out, err = p.communicate()
8787
curl_response = str(out.decode('ascii'))
88-
response = curl_response.split(", ")
88+
response = {
89+
"content": curl_response.split(", ")[0],
90+
"status_code": curl_response.split(", ")[1]
91+
}
8992
else:
9093
logging.error("Wrong request method")
9194
response = None
@@ -139,16 +142,17 @@ def test_mam_send_msg(self):
139142

140143
for i in range(len(response)):
141144
logging.debug("send msg i = " + str(i) + ", res = " +
142-
response[i][0] + ", status code = " + response[i][1])
145+
response[i]["content"] + ", status code = " +
146+
response[i]["status_code"])
143147

144148
for i in range(len(response)):
145149
if i in pass_case:
146-
res_json = json.loads(response[i][0])
150+
res_json = json.loads(response[i]["content"])
147151
self.assertTrue(valid_trytes(res_json["channel"], LEN_ADDR))
148152
self.assertTrue(valid_trytes(res_json["bundle_hash"],
149153
LEN_ADDR))
150154
else:
151-
self.assertEqual(STATUS_CODE_500, response[i][1])
155+
self.assertEqual(STATUS_CODE_500, response[i]["status_code"])
152156

153157
# Time Statistics
154158
payload = "Who are we? Just a speck of dust within the galaxy?"
@@ -187,9 +191,9 @@ def test_mam_recv_msg(self):
187191
pass_case = [0]
188192
for i in range(len(test_cases)):
189193
if i in pass_case:
190-
self.assertTrue(expect_cases[i] in response[i][0])
194+
self.assertTrue(expect_cases[i] in response[i]["content"])
191195
else:
192-
self.assertEqual(STATUS_CODE_405, response[i][1])
196+
self.assertEqual(STATUS_CODE_405, response[i]["status_code"])
193197

194198
# Time Statistics
195199
# send a MAM message and use it as the the message to be searched
@@ -198,7 +202,7 @@ def test_mam_recv_msg(self):
198202
post_data_json = json.dumps(post_data)
199203
response = API("/mam/", post_data=post_data_json)
200204

201-
res_json = json.loads(response[0])
205+
res_json = json.loads(response["content"])
202206
bundle_hash = res_json["bundle_hash"]
203207

204208
time_cost = []
@@ -255,12 +259,13 @@ def test_send_transfer(self):
255259

256260
for i in range(len(response)):
257261
logging.debug("send transfer i = " + str(i) + ", res = " +
258-
response[i][0] + ", status code = " + response[i][1])
262+
response[i]["content"] + ", status code = " +
263+
response[i]["status_code"])
259264

260265
pass_case = [0, 1, 2, 3]
261266
for i in range(len(response)):
262267
if i in pass_case:
263-
res_json = json.loads(response[i][0])
268+
res_json = json.loads(response[i]["content"])
264269

265270
# we only send zero tx at this moment
266271
self.assertEqual(0, res_json["value"])
@@ -278,9 +283,9 @@ def test_send_transfer(self):
278283
valid_trytes(res_json["signature_and_message_fragment"],
279284
LEN_MSG_SIGN))
280285
elif i == 4:
281-
self.assertEqual(EMPTY_REPLY, response[i][1])
286+
self.assertEqual(EMPTY_REPLY, response[i]["status_code"])
282287
else:
283-
self.assertEqual(STATUS_CODE_404, response[i][1])
288+
self.assertEqual(STATUS_CODE_404, response[i]["status_code"])
284289

285290
# Time Statistics
286291
time_cost = []
@@ -332,8 +337,9 @@ def test_find_transactions_by_tag(self):
332337

333338
for i in range(len(transaction_response)):
334339
logging.debug("find transactions by tag i = " + str(i) +
335-
", tx_res = " + transaction_response[i][0] +
336-
", status code = " + transaction_response[i][1])
340+
", tx_res = " + transaction_response[i]["content"] +
341+
", status code = " +
342+
transaction_response[i]["status_code"])
337343

338344
response = []
339345
for t_case in test_cases:
@@ -345,17 +351,19 @@ def test_find_transactions_by_tag(self):
345351

346352
for i in range(len(response)):
347353
logging.debug("find transactions by tag i = " + str(i) +
348-
", res = " + response[i][0] + ", status code = " +
349-
response[i][1])
354+
", res = " + response[i]["content"] +
355+
", status code = " + response[i]["status_code"])
350356
pass_case = [0, 1]
351357
for i in range(len(response)):
352358
if i in pass_case:
353-
tx_res_json = json.loads(transaction_response[i][0])
354-
res_json = json.loads(response[i][0])
359+
expected_tx_json = json.loads(
360+
transaction_response[i]["content"])
361+
res_json = json.loads(response[i]["content"])
355362

356-
self.assertEqual(tx_res_json["hash"], res_json["hashes"][0])
363+
self.assertEqual(expected_tx_json["hash"],
364+
res_json["hashes"][0])
357365
else:
358-
self.assertEqual(STATUS_CODE_400, response[i][1])
366+
self.assertEqual(STATUS_CODE_400, response[i]["status_code"])
359367

360368
# Time Statistics
361369
time_cost = []
@@ -390,9 +398,10 @@ def test_get_transactions_object(self):
390398

391399
logging.debug("sent_transaction_obj = " +
392400
", sent_transaction_obj[0] = " +
393-
sent_transaction_obj[0] +
394-
", sent_transaction_obj[1] = " + sent_transaction_obj[1])
395-
sent_transaction_obj_json = json.loads(sent_transaction_obj[0])
401+
sent_transaction_obj["content"] +
402+
", sent_transaction_obj[1] = " +
403+
sent_transaction_obj["status_code"])
404+
sent_transaction_obj_json = json.loads(sent_transaction_obj["content"])
396405
sent_transaction_hash = sent_transaction_obj_json["hash"]
397406
test_cases = [
398407
sent_transaction_hash, sent_transaction_hash[0:19],
@@ -406,16 +415,17 @@ def test_get_transactions_object(self):
406415

407416
for i in range(len(response)):
408417
logging.debug("get transactions object i = " + str(i) +
409-
", res = " + repr(response[i][0]) +
410-
", status code = " + repr(response[i][1]))
418+
", res = " + repr(response[i]["content"]) +
419+
", status code = " +
420+
repr(response[i]["status_code"]))
411421

412422
pass_case = [0]
413423
for i in range(len(response)):
414424
if i in pass_case:
415-
res_json = json.loads(response[i][0])
425+
res_json = json.loads(response[i]["content"])
416426
self.assertEqual(sent_transaction_hash, res_json["hash"])
417427
else:
418-
self.assertEqual(STATUS_CODE_405, response[i][1])
428+
self.assertEqual(STATUS_CODE_405, response[i]["status_code"])
419429

420430
# Time Statistics
421431
time_cost = []
@@ -467,11 +477,11 @@ def test_find_transactions_obj_by_tag(self):
467477
])
468478
else:
469479
transaction_response.append(
470-
API("/transaction/", post_data=post_data_json))
480+
[API("/transaction/", post_data=post_data_json)])
471481

472482
for i in range(len(transaction_response)):
473483
logging.debug("find transactions obj by tag i = " + str(i) +
474-
", tx_res = " + repr(transaction_response[i][0]))
484+
", tx_res = " + repr(transaction_response[i]))
475485

476486
response = []
477487
for t_case in test_cases:
@@ -481,92 +491,67 @@ def test_find_transactions_obj_by_tag(self):
481491
for i in range(len(response)):
482492
logging.debug("find transactions obj by tag i = " + str(i) +
483493
", res = " + repr(response[i]))
484-
494+
pass_case = [0, 1, 2]
485495
for i in range(len(response)):
486-
if i == 0 or i == 1:
487-
tx_res_json = json.loads(transaction_response[i][0])
488-
res_tx_json = json.loads(response[i][0])
489-
res_json = res_tx_json["transactions"][0]
490-
491-
self.assertEqual(tx_res_json["hash"], res_json["hash"])
492-
self.assertEqual(tx_res_json["signature_and_message_fragment"],
493-
res_json["signature_and_message_fragment"])
494-
self.assertEqual(tx_res_json["address"], res_json["address"])
495-
self.assertEqual(tx_res_json["value"], res_json["value"])
496-
self.assertEqual(tx_res_json["obsolete_tag"],
497-
res_json["obsolete_tag"])
498-
self.assertEqual(tx_res_json["timestamp"],
499-
res_json["timestamp"])
500-
self.assertEqual(tx_res_json["last_index"],
501-
res_json["last_index"])
502-
self.assertEqual(tx_res_json["bundle_hash"],
503-
res_json["bundle_hash"])
504-
self.assertEqual(tx_res_json["trunk_transaction_hash"],
505-
res_json["trunk_transaction_hash"])
506-
self.assertEqual(tx_res_json["branch_transaction_hash"],
507-
res_json["branch_transaction_hash"])
508-
self.assertEqual(tx_res_json["tag"], res_json["tag"])
509-
self.assertEqual(tx_res_json["attachment_timestamp"],
510-
res_json["attachment_timestamp"])
511-
self.assertEqual(
512-
tx_res_json["attachment_timestamp_lower_bound"],
513-
res_json["attachment_timestamp_lower_bound"])
514-
self.assertEqual(
515-
tx_res_json["attachment_timestamp_upper_bound"],
516-
res_json["attachment_timestamp_upper_bound"])
517-
self.assertEqual(tx_res_json["nonce"], res_json["nonce"])
518-
elif i == 2:
519-
res_tx_json = json.loads(response[i][0])
496+
if i in pass_case:
497+
res_tx_json = json.loads(response[i]["content"])
520498
res_json = []
521-
tx_res_json = []
499+
expected_tx_json = []
522500
for j in range(len(transaction_response[i])):
523501
res_json.append(res_tx_json["transactions"][j])
524-
tx_res_json.append(
525-
json.loads(transaction_response[i][j][0]))
526-
527-
for j in range(len(tx_res_json)):
502+
expected_tx_json.append(
503+
json.loads(transaction_response[i][j]["content"]))
504+
505+
for j in range(len(expected_tx_json)):
528506
case_examined_equal = False
529507
for k in range(len(res_json)):
530-
if tx_res_json[j]["hash"] == res_json[k]["hash"]:
508+
if expected_tx_json[j]["hash"] == res_json[k]["hash"]:
531509
self.assertEqual(
532-
tx_res_json[j]["signature_and_message_fragment"],
510+
expected_tx_json[j]
511+
["signature_and_message_fragment"],
533512
res_json[k]["signature_and_message_fragment"])
534-
self.assertEqual(tx_res_json[j]["address"],
535-
res_json[k]["address"])
536-
self.assertEqual(tx_res_json[j]["value"],
537-
res_json[k]["value"])
538-
self.assertEqual(tx_res_json[j]["obsolete_tag"],
539-
res_json[k]["obsolete_tag"])
540-
self.assertEqual(tx_res_json[j]["timestamp"],
541-
res_json[k]["timestamp"])
542-
self.assertEqual(tx_res_json[j]["last_index"],
543-
res_json[k]["last_index"])
544-
self.assertEqual(tx_res_json[j]["bundle_hash"],
545-
res_json[k]["bundle_hash"])
513+
self.assertEqual(expected_tx_json[j]["address"],
514+
res_json[k]["address"])
515+
self.assertEqual(expected_tx_json[j]["value"],
516+
res_json[k]["value"])
517+
self.assertEqual(
518+
expected_tx_json[j]["obsolete_tag"],
519+
res_json[k]["obsolete_tag"])
520+
self.assertEqual(expected_tx_json[j]["timestamp"],
521+
res_json[k]["timestamp"])
522+
self.assertEqual(expected_tx_json[j]["last_index"],
523+
res_json[k]["last_index"])
524+
self.assertEqual(
525+
expected_tx_json[j]["bundle_hash"],
526+
res_json[k]["bundle_hash"])
546527
self.assertEqual(
547-
tx_res_json[j]["trunk_transaction_hash"],
528+
expected_tx_json[j]["trunk_transaction_hash"],
548529
res_json[k]["trunk_transaction_hash"])
549530
self.assertEqual(
550-
tx_res_json[j]["branch_transaction_hash"],
531+
expected_tx_json[j]["branch_transaction_hash"],
551532
res_json[k]["branch_transaction_hash"])
552-
self.assertEqual(tx_res_json[j]["tag"],
553-
res_json[k]["tag"])
533+
self.assertEqual(expected_tx_json[j]["tag"],
534+
res_json[k]["tag"])
554535
self.assertEqual(
555-
tx_res_json[j]["attachment_timestamp"],
536+
expected_tx_json[j]["attachment_timestamp"],
556537
res_json[k]["attachment_timestamp"])
557538
self.assertEqual(
558-
tx_res_json[j]["attachment_timestamp_lower_bound"],
559-
res_json[k]["attachment_timestamp_lower_bound"])
539+
expected_tx_json[j]
540+
["attachment_timestamp_lower_bound"],
541+
res_json[k]
542+
["attachment_timestamp_lower_bound"])
560543
self.assertEqual(
561-
tx_res_json[j]["attachment_timestamp_upper_bound"],
562-
res_json[k]["attachment_timestamp_upper_bound"])
563-
self.assertEqual(tx_res_json[j]["nonce"],
564-
res_json[k]["nonce"])
544+
expected_tx_json[j]
545+
["attachment_timestamp_upper_bound"],
546+
res_json[k]
547+
["attachment_timestamp_upper_bound"])
548+
self.assertEqual(expected_tx_json[j]["nonce"],
549+
res_json[k]["nonce"])
565550
case_examined_equal = True
566551
break
567552
self.assertTrue(case_examined_equal)
568553
else:
569-
self.assertEqual(STATUS_CODE_400, response[i][1])
554+
self.assertEqual(STATUS_CODE_400, response[i]["status_code"])
570555

571556
# Time Statistics
572557
time_cost = []

0 commit comments

Comments
 (0)