def test_heart_beat_update_data(self, prepare_mongo_heart_report): logger = log.Log(sys._getframe().f_code.co_name) logger.info("test begining") # 1.clear mongo mg = prepare_mongo_heart_report # 2.send post request two times with different data req_url = 'http://%s' % conf.apihost_test api_url = '%s%s' % (req_url, http_heart_beat.data["url"]) header = http_heart_beat.data["header"] data_req1 = http_heart_beat.data["heart_beat_update_data"]["request"][ 0] data_req2 = http_heart_beat.data["heart_beat_update_data"]["request"][ 1] data_resp = http_heart_beat.data["heart_beat_update_data"]["response"] data_req1['time'] = common.current_time_iso() data_req2['time'] = common.current_time_iso() response_tmp = request.post_request(api_url, data_req1, header) response = request.post_request(api_url, data_req2, header) # 3.assert response info http_assert(response, data_resp) # 4.assert mongo data mg_data = mg.find() db_jdxx_assert(mg_data, data_req2, logger, db_counts=1)
def test_block_reported_relay_chain_null_value_chain_type( self, prepare_mongo_block_report): logger = log.Log(sys._getframe().f_code.co_name) logger.info("test begining") # 1.clear mongo mgdbs = prepare_mongo_block_report # 2.send post request req_url = 'http://%s' % conf.apihost_test api_url = '%s%s' % (req_url, http_block_reported.data["url"]) header = http_block_reported.data["header"] data_req = http_block_reported.data[ "block_reported_null_value_chain_type"]["request"] data_resp = http_block_reported.data[ "block_reported_null_value_chain_type"]["response"] data_req['time'] = common.current_time_iso() response = request.post_request(api_url, data_req, header) # 3.assert response info assert test.assert_code(response['code'], 200) assert test.assert_body_code_ge(response['body'], data_resp['code']) # assert test.assert_body(response['body'], 'data', data_resp['data']) assert test.assert_time(response['time_consuming'], 100) # 4.assert mongo data mg_sslsj_data = mgdbs['sslsj'].find() mg_lslqksj_data = mgdbs['lslqksj'].find() mg_kfpjyzsj_data = mgdbs['kfpjyzsj'].find() mg_jysj_data = mgdbs['jysj'].find() assert len(mg_sslsj_data) == 0 assert len(mg_lslqksj_data) == 0 assert len(mg_kfpjyzsj_data) == 0 assert len(mg_jysj_data) == 0
def test_block_reported_beacon_chain(self, prepare_mongo_block_report): logger = log.Log(sys._getframe().f_code.co_name) logger.info("test begining") # 1.clear mongo mgdbs = prepare_mongo_block_report # 2.send post request req_url = 'http://%s' % conf.apihost_test api_url = '%s%s' % (req_url, http_block_reported.data["url"]) header = http_block_reported.data["header"] data_req = http_block_reported.data["block_reported_beacon_chain"][ "request"] data_resp = http_block_reported.data["block_reported_beacon_chain"][ "response"] data_req['time'] = common.current_time_iso() response = request.post_request(api_url, data_req, header) # 3.assert response info http_assert(response, data_resp) # 4.assert mongo data mg_sslsj_data = mgdbs['sslsj'].find() mg_lslqksj_data = mgdbs['lslqksj'].find() db_sslsj_assert(mg_sslsj_data, copy.deepcopy(data_req), logger, db_counts=1) db_lslqksj_assert(mg_lslqksj_data, copy.deepcopy(data_req), logger, db_counts=1)
def test_heart_beat_null_value_chain_type(self, prepare_mongo_heart_report): logger = log.Log(sys._getframe().f_code.co_name) logger.info("test begining") # 1.clear mongo mg = prepare_mongo_heart_report # 2.send post request req_url = 'http://%s' % conf.apihost_test api_url = '%s%s' % (req_url, http_heart_beat.data["url"]) header = http_heart_beat.data["header"] data_req = http_heart_beat.data["heart_beat_null_value_chain_type"][ "request"] data_resp = http_heart_beat.data["heart_beat_null_value_chain_type"][ "response"] data_req['time'] = common.current_time_iso() response = request.post_request(api_url, data_req, header) # 3.assert response info assert test.assert_code(response['code'], 200) assert test.assert_body_code_ge(response['body'], data_resp['code']) assert test.assert_time(response['time_consuming'], 100) # 4.assert mongo data mg_data = mg.find() assert test.assert_db_counts(mg_data, 0)
def test_generate_cmd_single_request_then_send_post_request(self, prepare_mongo_heart_report): logger = log.Log(sys._getframe().f_code.co_name) logger.info("test begining") # 1.prepare mongo data (send post request: heart_beat_shard_chain) # prepare mongo mg = prepare_mongo_heart_report # 2.send 1 shard chain post request req_url = 'http://%s' % conf.apihost_test api_url = '%s%s' % (req_url, http_heart_beat.data["url"]) header = http_heart_beat.data["header"] root_data = copy.deepcopy(ws_generate_cmd.data["generate_cmd_single_request_shard_chain"]) data_req0 = root_data["post_request"][0] data_req0['time'] = common.current_time_iso() request.post_request(api_url, data_req0, header) # 3.send ws request and receive ws_url = 'ws://%s' % conf.wshost_test ws = mwebsocket.WebSocket(ws_url) ws_send_data = root_data['ws_request'] ws_expc_data_resp = root_data['ws_response'] ws.send(ws_send_data) time.sleep(3) ws_resp_data = ws.receive() # 4.send 1 shard chain post request again http_resp_data = request.post_request(api_url, data_req0, header) assert test.assert_code(http_resp_data['code'], 200) assert test.assert_body(http_resp_data['body'], 'code', 200) assert test.assert_body(http_resp_data['body'], 'message', 'OK') assert test.assert_body(http_resp_data['body'], 'data', ws_send_data['body']['cmd']) assert test.assert_time(http_resp_data['time_consuming'], 100)
def test_generate_cmd_single_request_shard_chain(self, prepare_mongo_heart_report): logger = log.Log(sys._getframe().f_code.co_name) logger.info("test begining") # 1.prepare mongo data (send post request: heart_beat_shard_chain) # prepare mongo mg = prepare_mongo_heart_report # send 2 post request req_url = 'http://%s' % conf.apihost_test api_url = '%s%s' % (req_url, http_heart_beat.data["url"]) header = http_heart_beat.data["header"] root_data = copy.deepcopy(ws_generate_cmd.data["generate_cmd_single_request_shard_chain"]) data_req0 = root_data["post_request"][0] data_req1 = root_data["post_request"][1] data_req0['time'] = common.current_time_iso() data_req1['time'] = common.current_time_iso() request.post_request(api_url, data_req0, header) request.post_request(api_url, data_req1, header) # assert db has 2 collections assert test.assert_db_counts(mg.find(), 2) # 2.send ws request and receive ws_url = 'ws://%s' % conf.wshost_test ws = mwebsocket.WebSocket(ws_url) ws_send_data = root_data['ws_request'] ws_expc_data_resp = root_data['ws_response'] ws.send(ws_send_data) time.sleep(3) ws_resp_data = ws.receive() # 3.assert response info assert test.assert_text(ws_resp_data['uri'], ws_expc_data_resp['uri']) assert common.dict_key_not_exist(ws_resp_data, 'error') assert test.assert_text(ws_resp_data['body'], {}) assert test.assert_text(ws_resp_data['msgId'], ws_expc_data_resp['msgId']) # 4.assert mongo data mg_data = mg.find() ws_expc_data_db = ws_send_data ws_expc_data_db['body']['cmd']['params']['amount'] = ws_expc_data_db['body']['cmd']['params']['amount'] / len(mg_data) assert test.assert_text(mg_data[0]['cmd'], ws_expc_data_db['body']['cmd']) assert test.assert_text(mg_data[1]['cmd'], ws_expc_data_db['body']['cmd'])
def test_generate_cmd_multi_request_after_15s_shard_chain(self, prepare_mongo_heart_report): logger = log.Log(sys._getframe().f_code.co_name) logger.info("test begining") # 1.prepare mongo data (send post request: heart_beat_shard_chain) # prepare mongo mg = prepare_mongo_heart_report # send 1 post request req_url = 'http://%s' % conf.apihost_test api_url = '%s%s' % (req_url, http_heart_beat.data["url"]) header = http_heart_beat.data["header"] data_req0 = ws_generate_cmd.data["generate_cmd_multi_request_after_15s_shard_chain"]["post_request"][0] data_req0['time'] = common.current_time_iso() request.post_request(api_url, data_req0, header) # assert db has 1 collections assert test.assert_db_counts(mg.find(), 1) # 2.send ws request and receive ws_url = 'ws://%s' % conf.wshost_test ws = mwebsocket.WebSocket(ws_url) data = ws_generate_cmd.data['generate_cmd_multi_request_after_15s_shard_chain'] ws_send_data0 = data['ws_request'][0] ws_expc_data_resp0 = data['ws_response'][0] ws_send_data1 = data['ws_request'][1] ws_expc_data_resp1 = data['ws_response'][1] # 3. ws send and receive # first time ws.send(ws_send_data0) time.sleep(3) ws_resp_data0 = ws.receive() # second time time.sleep(13) ws.send(ws_send_data1) time.sleep(3) ws_resp_data1 = ws.receive() # 3_1.assert response info assert test.assert_text(ws_resp_data0['uri'], ws_expc_data_resp0['uri']) assert test.assert_text(ws_resp_data0['body'], {}) assert common.dict_key_not_exist(ws_resp_data0, 'error') assert test.assert_text(ws_resp_data0['msgId'], ws_expc_data_resp0['msgId']) assert test.assert_text(ws_resp_data1['uri'], ws_expc_data_resp1['uri']) assert test.assert_text(ws_resp_data1['body'], {}) assert common.dict_key_not_exist(ws_resp_data1, 'error') assert test.assert_text(ws_resp_data1['msgId'], ws_expc_data_resp1['msgId']) # 4.assert mongo data mg_data = mg.find() ws_expc_data_db = ws_send_data0 assert test.assert_text(mg_data[0]['cmd'], ws_expc_data_db['body']['cmd'])
def _gen_data_public(self, lock_hash): if self.data['hash'] == '': self.data['father'] = '' else: self.data['father'] = self.data['hash'] self.data['hash'] = self._gen_hash() self.data['height'] = self._height_count() if self.data['time'] == '': self.data['interval'] = 0 self.data['time'] = common.current_time_iso() else: now = common.current_time_iso() interval = common.isostr_to_datetime( now) - common.isostr_to_datetime(self.data['time']) # self.data['interval'] = (interval.days * 24 * 3600 + interval.seconds) * 1000000 + interval.microseconds self.data['interval'] = float( '%.3f' % float(interval.days * 24 * 3600 + interval.seconds + interval.microseconds * 0.000001)) # self.data['interval'] = interval.days * 24 * 3600 + interval.seconds + interval.microseconds * 0.000001 self.data['time'] = now self.data['lockHash'] = lock_hash
def test_block_reported_relay_chain_update_data( self, prepare_mongo_block_report): logger = log.Log(sys._getframe().f_code.co_name) logger.info("test begining") # 1.clear mongo mgdbs = prepare_mongo_block_report # 2.send post request req_url = 'http://%s' % conf.apihost_test api_url = '%s%s' % (req_url, http_block_reported.data["url"]) header = http_block_reported.data["header"] data_req1 = http_block_reported.data["block_reported_update_data"][ "request"][0] data_resp_expc = http_block_reported.data[ "block_reported_update_data"]["response"] data_req1['time'] = common.current_time_iso() # data_req1['nodeId'] = common.base58_encode(data_req1['nodeId']) data_req2 = http_block_reported.data["block_reported_update_data"][ "request"][1] data_req2['time'] = common.current_time_iso() logger.info(f'send post request 1st time: {api_url}') response_tmp = request.post_request(api_url, data_req1, header) logger.info(f'send post request 2nd time: {api_url}') response = request.post_request(api_url, data_req2, header) # 3.assert response info http_assert(response, data_resp_expc) # 4.assert mongo data mg_sslsj_data = mgdbs['sslsj'].find() mg_lslqksj_data = mgdbs['lslqksj'].find() db_sslsj_assert(mg_sslsj_data, copy.deepcopy(data_req2), logger, db_counts=1) db_lslqksj_assert(mg_lslqksj_data, copy.deepcopy(data_req2), logger, db_counts=1)
def test_get_real_chain_block_info_with_wrong_post_request(self, prepare_mongo_block_report): logger = log.Log(sys._getframe().f_code.co_name) logger.info("test begining") # 1.creat websocket collection ws_url = 'ws://%s' % conf.wshost_test ws = mwebsocket.WebSocket(ws_url) # 2.book news subscription root_data = copy.deepcopy(ws_get_block_info_realtime.data['get_real_chain_block_info_with_wrong_post_request']) ws_send_data = root_data['ws_request'][0] ws_expc_data_resp = root_data['ws_response'][0]['body'] ws.send(ws_send_data) ws.receive() time.sleep(3) # 3.send wrong post request after subscription # prepare mongo mgdbs = prepare_mongo_block_report # 3_2.send post request req_url = 'http://%s' % conf.apihost_test api_url = '%s%s' % (req_url, http_block_reported.data["url"]) header = http_block_reported.data["header"] data_req = root_data["post_request"] data_req0 = data_req[0] data_req0['time'] = common.current_time_iso() ws_wait_time = 15 post_wait_time = 6 # ws_send_data = data['ws_request'][0] t1 = threading.Thread(target=self.ws_recv, name='ws_recv', args=(ws,)) t2 = threading.Thread(target=self.ws_send_for_subscription, name='ws_send', args=(ws, ws_send_data, ws_wait_time)) t3 = threading.Thread(target=self.ws_count, name='ws_count', args=(ws_wait_time,)) t4 = threading.Thread(target=self.http_post_request, name='post_request', args=(api_url, data_req0, header, post_wait_time)) t1.start() t2.start() t3.start() t4.start() t1.join() t2.join() t3.join() t4.join() logger.info(f'===ws_recv spendtime: {TestWSAPITest.spendtime}') logger.info(f'===ws_recv get response: {TestWSAPITest.ws_recv_data}') assert test.assert_text(TestWSAPITest.spendtime, ws_wait_time) assert test.assert_text(TestWSAPITest.ws_recv_data, root_data['ws_response'][0])
def test_get_real_chain_block_info_with_no_sub(self): logger = log.Log(sys._getframe().f_code.co_name) logger.info("test begining") # 1.creat websocket collection ws_url = 'ws://%s' % conf.wshost_test ws = mwebsocket.WebSocket(ws_url) # 2.send 1 post request before subscription req_url = 'http://%s' % conf.apihost_test api_url = '%s%s' % (req_url, http_block_reported.data["url"]) header = http_block_reported.data["header"] root_data = copy.deepcopy(ws_get_block_info_realtime.data["get_real_chain_block_info"]) data_req = root_data["post_request"] data_req0 = data_req[0] data_req0['time'] = common.current_time_iso() # for no subscription ws_wait_time = 10 post_wait_time = 6 ws_send_data = root_data['ws_request'][0] t1 = threading.Thread(target=self.ws_recv, name='ws_recv', args=(ws,)) t2 = threading.Thread(target=self.ws_send_for_subscription, name='ws_send', args=(ws, ws_send_data, ws_wait_time)) t3 = threading.Thread(target=self.ws_count, name='ws_count', args=(ws_wait_time,)) t4 = threading.Thread(target=self.http_post_request, name='post_request', args=(api_url, data_req0, header, post_wait_time)) t1.start() t2.start() t3.start() t4.start() t1.join() t2.join() t3.join() t4.join() logger.info(f'===ws_recv spendtime: {TestWSAPITest.spendtime}') logger.info(f'===ws_recv get response: {TestWSAPITest.ws_recv_data}') assert test.assert_text(TestWSAPITest.spendtime, ws_wait_time) assert test.assert_key_not_exist(TestWSAPITest.ws_recv_data, 'error') assert test.assert_text(TestWSAPITest.ws_recv_data, root_data['ws_response'][-1])
x = self.mg_col.insert_one(data) self.log.info(f'mongo db [{self.db}][{self.col}] inserted data:{data}') return x.inserted_id else: raise Exception(self.log.error('please insert a list type data with elements of dict. e.g. [{"a":1, "b":2}]')) if __name__ == '__main__': # mg_db = mg_client[db_list[0]] # mg_col = mg_db["test_fangchao"] addr = 'mongodb://*****:*****@118.24.168.230:27017/poolwebtest' addr2 = 'mongodb://*****:*****@118.24.168.230:27017,118.24.168.230:27018,118.24.168.230:27019/pynxtest' mg = Mongo(addr2, 'pynxtest2', 'nodes') print(mg.dbs) print(mg.cols) print() print(mg.find(limit=0)) data = { "keyId" : "a-2", "lastTime" : common.current_time_iso(), "number" : "2", "type" : "a", } mg.insert_col_data_one(data) # mg_db = mg.mg_client['pynxtest'] # mg_col = mg_db['node'] # print(list(mg_col.find()))
def test_get_real_chain_block_info_with_sub(self, prepare_mongo_block_report): logger = log.Log(sys._getframe().f_code.co_name) logger.info("test begining") # 1.creat websocket collection ws_url = 'ws://%s' % conf.wshost_test ws = mwebsocket.WebSocket(ws_url) # 2.clear mongo database mgdbs = prepare_mongo_block_report # 3.news subscription root_data = copy.deepcopy(ws_get_block_info_realtime.data['get_real_chain_block_info']) ws_send_data = root_data['ws_request'][0] ws_expc_data_resp_tmp = root_data['ws_response'] ws.send(ws_send_data) ws.receive() time.sleep(3) # 4.send post request after subscription req_url = 'http://%s' % conf.apihost_test api_url = '%s%s' % (req_url, http_block_reported.data["url"]) header = http_block_reported.data["header"] data_req = ws_get_block_info_realtime.data["get_real_chain_block_info"]["post_request"] logger.info(f'debug data_req: {data_req}') data_req0 = data_req[0] data_req0['time'] = common.current_time_iso() data_req1 = data_req[1] data_req1['time'] = common.current_time_iso() data_req2 = data_req[2] data_req2['time'] = common.current_time_iso() ws_wait_time = 12 ws_recv_wait_time = 9 ws_send_data = root_data['ws_request'][0] t1 = threading.Thread(target=self.ws_recv_multi, name='ws_recv', args=(ws, ws_recv_wait_time)) t2 = threading.Thread(target=self.ws_send_for_subscription, name='ws_send', args=(ws, ws_send_data, ws_wait_time)) t3 = threading.Thread(target=self.ws_count, name='ws_count', args=(ws_wait_time,)) t4 = threading.Thread(target=self.http_post_request, name='post_request', args=(api_url, data_req0, header, 2)) t5 = threading.Thread(target=self.http_post_request, name='post_request', args=(api_url, data_req1, header, 2)) t6 = threading.Thread(target=self.http_post_request, name='post_request', args=(api_url, data_req2, header, 5)) t1.start() t2.start() t3.start() t4.start() t5.start() t6.start() t1.join() t2.join() t3.join() t4.join() t5.join() t6.join() logger.info(f'===ws_recv spendtime: {TestWSAPITest.spendtime}') logger.info(f'===ws_recv get response: {TestWSAPITest.ws_recv_data}') logger.info(f'===ws_recv get response: {TestWSAPITest.ws_recv_data1}') logger.info(f'===ws_recv get response: {TestWSAPITest.ws_recv_data2}') # websocket server change send time to 1/5pers,so cancel this check(wait time must be the same as spend time) # assert test.assert_text(TestWSAPITest.spendtime, ws_recv_wait_time) assert test.assert_text(TestWSAPITest.ws_recv_data['event'], ws_send_data['event']) # 5.check ws recv time.sleep(3) ws_resp_data = TestWSAPITest.ws_recv_data['body'] logger.info(f'ws_resp_data :{ws_resp_data}') # 6.assert response info ws_expc_data_resp = [] for i in ws_expc_data_resp_tmp[0:-1]: ws_expc_data_resp.append(i['body'][0]) # 响应体中信息 与 post 请求的信息一致 assert test.assert_db_counts(ws_resp_data, 1) for i in ws_expc_data_resp: # clear dict key (time) logger.info(i) i.pop('time') for ws_resp_data_i in ws_resp_data: ws_resp_data_i.pop('time') if ws_resp_data_i in ws_expc_data_resp: logger.info("Case Assert success : response data -> %s <- in %s" % (ws_resp_data_i, ws_expc_data_resp)) else: logger.error("Case Assert failed : response data -> \n%s \n<- not in \n%s" % (ws_resp_data_i, ws_expc_data_resp)) raise # 7.assert mongo data # 数据库中信息 与 post 请求的信息一致 mg_data = mgdbs['lslqksj'].find() ws_expc_data_db = data_req logger.info(f'debug data_req: {data_req}') assert test.assert_db_counts(mg_data, len(ws_expc_data_db)) for i in ws_expc_data_db: # clear dict key (time) logger.info(i) i.pop('time') i['miner'] = i['nodeId'] # change key name: 'nodeId' to 'miner' i.pop('nodeId') for mg_data_tmp in mg_data: mg_data_tmp.pop('time') mg_data_tmp.pop('updateTime') for i in range(len(ws_expc_data_db)): if ws_expc_data_db[i]['hash'] == mg_data_tmp['hash']: if mg_data_tmp == ws_expc_data_db[i]: logger.info("Case Assert success : database info: %s == ws_expc_data_resp: %s" % (mg_data_tmp, ws_expc_data_db[i])) elif i == len(ws_expc_data_db) - 1: logger.info("Case Assert failed : database info: \n%s \n!= \nws_expc_data_resp: \n%s" % (mg_data_tmp, ws_expc_data_db[i])) raise