def test_01(self): """ 正常访问 """ info_dict = account_info(user=self.buyer, session=self.session, sda_id=sda_id, price=1000000000) info_user_id = info_dict["userId"] info_sda_id = info_dict["sdaId"] # 验证返回的sda_id和user_id是否正确 assert_list([info_user_id, info_sda_id], [self.user_id, sda_id])
def test_02(self): """ 限价空单委托,正常下单。 """ logger.info("用例编号:106-2----限价空单10倍杠杆委托,正常下单。") num = 1000000000 ConnectMysql(_type=mysql_type).sda_clear_open_empty_close_multi_order(user_id=self.user_id, contract_id=sda_id, status=2) ConnectMysql(_type=mysql_type).sda_clear_open_multi_close_open_order(user_id=self.user_id, contract_id=sda_id, status=2) ConnectRedis(_type=redis_type).sda_clear_order(sda_id=sda_id) ConnectRedis(_type=redis_type, db=4).sda_clear_user_balance(user_id=self.user_id, keys=sda_id) ConnectMysql(_type=mysql_type).sda_update_user_balance(user_id=self.user_id, sda_id=sda_id, sda_balance=990000000000) # 查询当前股价 stock_price_dict = market_info_get(user=self.buyer, session=self.session, sda_id=sda_id) now_stock_price = stock_price_dict["stockPrice"] price = int(int(now_stock_price) * 0.95) # 下委托前查询可用余额 info_dict = account_info(user=self.buyer, session=self.session, sda_id=sda_id, price=1000000000) before_balance = info_dict["balance"] # 下限价空单委托 order_resp = self.session.post(url=base+sda_order_create_url, data=get_sda_order_create_param(sda_id=sda_id, order_type=空单, order_price_type=限价, lever="10", order_price=price, order_num=num,)) msg = JMESPathExtractor().extract(query="MSG", body=order_resp.text) status = JMESPathExtractor().extract(query="STATUS", body=order_resp.text) order_id = JMESPathExtractor().extract(query="OBJECT.orderId", body=order_resp.text) sync_id = JMESPathExtractor().extract(query="syncLockKey", body=order_resp.text) # 查询当前委托 # time.sleep(1) get_open_dict = query_order_get_open(user=self.buyer, session=self.session, order_id=order_id) order_quantity = get_open_dict["orderQuantity"] # 委托数量 order_price = get_open_dict["orderPrice"] # 委托价格 order_status = get_open_dict["orderStatus"] # 委托状态 # 查询委托后可用余额 after_info_dict = account_info_sync(sync_id=sync_id, user=self.buyer, session=self.session, sda_id=sda_id, price=1000000000) after_balance = after_info_dict["balance"] flag_one = assert_one(int(before_balance), int(after_balance) + int(price * num / 10 / 100000000)) flag_two = assert_list([int(order_quantity), int(order_price), order_status], [num, price, "0"]) flag_three = assert_list([200, "SUCCESS", "0"], [order_resp.status_code, msg, status]) self.assertListEqual([True, True, True], [flag_one, flag_two, flag_three])
def test_06(self): """ 时间空单,用户可用余额为0,验证是否下单成功。 :return: """ deal_price = 100000000 deal_num = 100000000 ConnectMysql(_type=mysql_type).sda_clear_open_empty_close_multi_order( user_id=self.user_id, contract_id=sda_id, status=2) ConnectMysql(_type=mysql_type).sda_clear_open_multi_close_open_order( user_id=self.user_id, contract_id=sda_id, status=2) ConnectRedis(_type=redis_type).sda_clear_order(sda_id=sda_id) ConnectRedis(_type=redis_type, db=4).sda_clear_user_balance(user_id=self.user_id, keys=sda_id) ConnectMysql(_type=mysql_type).sda_update_user_balance( user_id=self.user_id, sda_id=sda_id, sda_balance=0) # min_max = ConnectMysql(_type=mysql_type).sda_query_contract_min(sda_id=sda_id) order_resp = self.session.post(url=base + sda_order_create_url, data=get_sda_order_create_param( sda_id=sda_id, order_type=order_type.空单, order_price_type=order_type.市价, order_num=deal_num)) msg = JMESPathExtractor().extract(query="MSG", body=order_resp.text) status = JMESPathExtractor().extract(query="STATUS", body=order_resp.text) logger.info("用户:{0}--接口:{1}---状态:{2}---返回信息:{3}".format( self.buyer, sda_order_create_url, order_resp.status_code, order_resp.json())) flag = assert_list([200, "可建仓余额不足", "1"], [order_resp.status_code, msg, status]) self.assertTrue(flag)
def test_03(self): """ 用户余额正常,超额划转资金,验证接口状态、STATUS、MSG """ logger.info( "用例编号:2-3------------资金划转接口测试类,用户余额正常,超额划转资金,验证接口状态、STATUS、MSG") ConnectMysql(_type=mysql_type).update_balance_value( user_mail=self.buyer, currency_id=currency_id, balance_value=100000000000) deposit_resp = self.session.post(url=base + sda_account_deposit_url, data=get_sda_account_deposit_param( sda_id=sda_id, amount=200000000000)) logger.info("用户:{0}---接口:{1}---状态:{2}---返回信息:{3}".format( self.buyer, sda_account_deposit_url, deposit_resp.status_code, deposit_resp.json())) status = JMESPathExtractor().extract(query="STATUS", body=deposit_resp.text) msg = JMESPathExtractor().extract(query="MSG", body=deposit_resp.text) # _object = JMESPathExtractor().extract(query="OBJECT", body=deposit_resp.text) flag = assert_list([200, "1", "平台账户余额不足"], [deposit_resp.status_code, status, msg]) self.assertTrue(flag)
def test_01(self): """ 下限价多单单,下单数量大于可用余额 """ logger.info("用例编号:22-1---下限价多单,下单数量大于可用余额") sda_balance = 9000000000000 price = 100000000 num = sda_balance + 100000000 ConnectMysql(_type=mysql_type).sda_clear_open_empty_close_multi_order( user_id=self.user_id, contract_id=sda_id, status=2) ConnectMysql(_type=mysql_type).sda_clear_open_multi_close_open_order( user_id=self.user_id, contract_id=sda_id, status=2) ConnectRedis(_type=redis_type).sda_clear_order(sda_id=sda_id) ConnectRedis(_type=redis_type, db=4).sda_clear_user_balance(user_id=self.user_id, keys=sda_id) ConnectMysql(_type=mysql_type).sda_update_user_balance( user_id=self.user_id, sda_id=sda_id, sda_balance=sda_balance) # 查询可用余额 info_dict = account_info(user=self.buyer, session=self.session, sda_id=sda_id) before_balance = info_dict["balance"] # 下限价委托 order_resp = self.session.post(url=base + sda_order_create_url, data=get_sda_order_create_param( sda_id=sda_id, order_type=多单, order_price_type=限价, order_price=price, order_num=num, )) msg = JMESPathExtractor().extract(query="MSG", body=order_resp.text) status = JMESPathExtractor().extract(query="STATUS", body=order_resp.text) sync_id = JMESPathExtractor().extract(query="syncLockKey", body=order_resp.text) # order_id = JMESPathExtractor().extract(query="OBJECT.orderId", body=order_resp.text) # 查询当前委托 # get_open_dict = query_order_get_open(user=self.buyer, session=self.session, order_id=order_id) # order_quantity = get_open_dict["orderQuantity"] # 委托数量 # order_price = get_open_dict["orderPrice"] # 委托价格 # order_status = get_open_dict["orderStatus"] # 委托状态 # 查询下委托后的可用余额 after_info_dict = account_info_sync(sync_id=sync_id, user=self.buyer, session=self.session, sda_id=sda_id, price=1000000000) after_balance = after_info_dict["balance"] flag_one = assert_one(int(before_balance), int(after_balance)) flag_three = assert_list([200, "可建仓余额不足", "1"], [order_resp.status_code, msg, status]) self.assertListEqual([True, True], [flag_one, flag_three])
def test_01(self): """ 限价多单1单,限价空单1单,完全成交,验证合约状态、用户余额 """ logger.info("用例编号:40-1---限价多单N单,限价空单1单,完全成交,验证合约状态、用户余额") range_num = 100 deal_num = 100000000 buy_num = 1000*100000000 # 查询当前股价 stock_price_dict = market_info_get(user=self.buyer, session=self.session, sda_id=sda_id) now_stock_price = stock_price_dict["stockPrice"] deal_price = int(int(now_stock_price) * 0.95) # 下N个多单、空单,完全成交 for i in range(range_num): buy_resp = self.session.post(url=base+sda_order_create_url, data=get_sda_order_create_param( sda_id=sda_id, order_type=多单, order_price_type=限价, order_price=deal_price,order_num=deal_num )) logger.info("用户:{0}--接口:{1}--状态:{2}--类型:多单--返回信息:{3}".format( self.buyer, sda_order_create_url, buy_resp.status_code, buy_resp.json() )) # time.sleep(0.2) # time.sleep(1) sell_resp = self.sell_session.post(url=base+sda_order_create_url, data=get_sda_order_create_param( sda_id=sda_id, order_type=空单, order_price_type=限价, order_price=deal_price,order_num=buy_num )) sell_sync_id = JMESPathExtractor().extract(query="syncLockKey", body=sell_resp.text) logger.info("用户:{0}--接口:{1}--状态:{2}--类型:空单--返回信息:{3}".format( self.seller, sda_order_create_url, sell_resp.status_code, sell_resp.json() )) # time.sleep(10) # 查询当前持仓中买卖方持仓数量、保证金、开仓均价 after_buy_position_dict = query_account_position_get(user=self.buyer, session=self.session, sync_key=sell_sync_id) after_sell_position_dict = query_account_position_get(user=self.seller, session=self.sell_session, sync_key=sell_sync_id) after_buy_sdaAveragePrice = after_buy_position_dict["sdaAveragePrice"] after_buy_sdaCount = after_buy_position_dict["sdaCount"] after_buy_currencyBalancePosition = after_buy_position_dict["currencyBalancePosition"] after_sell_sdaAveragePrice = after_sell_position_dict["sdaAveragePrice"] after_sell_sdaCount = after_sell_position_dict["sdaCount"] after_sell_currencyBalancePosition = after_sell_position_dict["currencyBalancePosition"] # 计算保证金 # buy_currency_balance = make_currency_balance(num=deal_num, price=deal_price) # sell_currency_balance = make_currency_balance(num=deal_num, price=deal_price) # 检验持仓均价,持仓量,保证金 price_flag = assert_one(int(deal_price), int(after_buy_sdaAveragePrice)) num_flag = assert_one(int(deal_num * range_num), int(after_buy_sdaCount)) # after_buy_flag = assert_one(buy_currency_balance, int(after_buy_currencyBalancePosition)) after_sell_flag = assert_list([int(deal_price), int(deal_num * range_num)], [int(after_sell_sdaAveragePrice), int(after_sell_sdaCount)]) self.assertListEqual([True, True, True], [price_flag, num_flag,after_sell_flag])
def test_02(self): """ 市价空单5倍杠杆委托,正常下单。 """ logger.info("用例编号:108-1--市价空单10倍杠杆委托,正常下单。") price = 100000000 num = 100000000 ConnectMysql(_type=mysql_type).sda_clear_open_empty_close_multi_order( user_id=self.user_id, contract_id=sda_id, status=2) ConnectMysql(_type=mysql_type).sda_clear_open_multi_close_open_order( user_id=self.user_id, contract_id=sda_id, status=2) ConnectRedis(_type=redis_type).sda_clear_order(sda_id=sda_id) ConnectRedis(_type=redis_type, db=4).sda_clear_user_balance(user_id=self.user_id, keys=sda_id) ConnectMysql(_type=mysql_type).sda_update_user_balance( user_id=self.user_id, sda_id=sda_id, sda_balance=990000000000) # 下委托前查询可用余额 info_dict = account_info(user=self.buyer, session=self.session, sda_id=sda_id, price=1000000000) before_balance = info_dict["balance"] # 下委托 order_resp = self.session.post(url=base + sda_order_create_url, data=get_sda_order_create_param( sda_id=sda_id, order_type=空单, order_price_type=市价, order_price=price, lever="10", order_num=num)) logger.info("接口:{0}---返回信息:{1}".format(sda_order_create_url, order_resp.json())) msg = JMESPathExtractor().extract(query="MSG", body=order_resp.text) status = JMESPathExtractor().extract(query="STATUS", body=order_resp.text) order_id = JMESPathExtractor().extract(query="OBJECT.orderId", body=order_resp.text) sync_id = JMESPathExtractor().extract(query="syncLockKey", body=order_resp.text) # 下委托后查询可用余额 # time.sleep(1) after_info_dict = account_info_sync(sync_id=sync_id, user=self.buyer, session=self.session, sda_id=sda_id, price=1000000000) after_balance = after_info_dict["balance"] one_flag = assert_one(int(before_balance), int(after_balance)) list_flag = assert_list([200, "SUCCESS", "0"], [order_resp.status_code, msg, status]) self.assertListEqual([True, True], [one_flag, list_flag])
def test_01(self): """ 多单1单,空单1单,多单完全成交,空单部分成交,验证合约状态、用户余额 """ logger.info("用例编号:41-1---限价多单1单,限价空单1单,多单完全成交,空单部分成交,验证合约状态、用户余额") deal_num = 100000000 sell_num = 200000000 # 查询当前股价 stock_price_dict = market_info_get(user=self.buyer, session=self.session, sda_id=sda_id) now_stock_price = stock_price_dict["stockPrice"] stock_unit = int(stock_price_dict["tradeUnit"]) deal_price = int(int(now_stock_price) * 0.95) # 下一单多单、空单,完全成交 buy_resp = self.session.post(url=base+sda_order_create_url, data=get_sda_order_create_param( sda_id=sda_id, order_type=多单, order_price_type=限价, order_price=deal_price, order_num=deal_num )) logger.info("用户:{0}--接口:{1}--状态:{2}--类型:多单--返回信息:{3}".format( self.buyer, sda_order_create_url, buy_resp.status_code, buy_resp.json() )) # time.sleep(2) sell_resp = self.sell_session.post(url=base+sda_order_create_url, data=get_sda_order_create_param( sda_id=sda_id, order_type=空单, order_price_type=限价, order_price=deal_price, order_num=sell_num )) sync_id = JMESPathExtractor().extract(query="syncLockKey", body=sell_resp.text) logger.info("用户:{0}--接口:{1}--状态:{2}--类型:空单--返回信息:{3}".format( self.seller, sda_order_create_url, sell_resp.status_code, sell_resp.json() )) sell_order_id = JMESPathExtractor().extract(query="OBJECT.orderId", body=sell_resp.text) # time.sleep(4) # 查询当前持仓中买卖方持仓数量、保证金、开仓均价 after_buy_position_dict = query_account_position_get(user=self.buyer, session=self.session, sync_key=sync_id) after_sell_position_dict = query_account_position_get(user=self.seller, session=self.sell_session, sync_key=sync_id) after_buy_sdaAveragePrice = after_buy_position_dict["sdaAveragePrice"] after_buy_sdaCount = after_buy_position_dict["sdaCount"] after_buy_currencyBalancePosition = int(after_buy_position_dict["currencyBalancePosition"]) after_sell_sdaAveragePrice = after_sell_position_dict["sdaAveragePrice"] after_sell_sdaCount = after_sell_position_dict["sdaCount"] after_sell_currencyBalancePosition = after_sell_position_dict["currencyBalancePosition"] # 计算保证金 buy_employ_balance = employBalance(price=deal_price, count=deal_num, unit=stock_unit, lever=1) print("buy_employ_balance", buy_employ_balance) print("after_buy") # 检验持仓均价,持仓量,保证金, price_flag = assert_one(int(deal_price), int(after_buy_sdaAveragePrice)) num_flag = assert_one(int(deal_num), int(after_buy_sdaCount)) after_buy_flag = assert_one(buy_employ_balance, after_buy_currencyBalancePosition) after_sell_flag = assert_list([int(deal_price), int(deal_num)], [int(after_sell_sdaAveragePrice), int(after_sell_sdaCount)]) self.assertListEqual([True, True, True, True], [after_buy_flag, price_flag, num_flag,after_sell_flag])
def test_02(self): """ 下限价空单,撤单,验证用户余额,委托保证金余额 """ logger.info("用例编号:7-2--下多单,撤单,验证用户余额,委托保证金余额,") ConnectMysql(_type=mysql_type).sda_clear_open_multi_close_open_order(user_id=self.user_id, contract_id=sda_id, status=2) ConnectMysql(_type=mysql_type).sda_clear_open_empty_close_multi_order(user_id=self.user_id, contract_id=sda_id, status=2) ConnectRedis(_type=redis_type).sda_clear_order(sda_id=sda_id) ConnectRedis(_type=redis_type, db=4).sda_clear_user_balance(user_id=self.user_id, keys=sda_id) ConnectMysql(_type=mysql_type).sda_update_user_balance(user_id=self.user_id, sda_id=sda_id, sda_balance=990000000000000) deal_num = 100000000 # 查询当前股价 stock_price_dict = market_info_get(user=self.buyer, session=self.session, sda_id=sda_id) now_stock_price = stock_price_dict["stockPrice"] price = int(int(now_stock_price) * 0.95) # 下单前查询用户的余额 buy_balance = self.session.post(url=base+sda_account_asset_detail_get_url, data=get_sda_account_asset_detail_get_param()) buy_balance_value = JMESPathExtractor().extract(query="OBJECT.PNLList[0].availableMargin",body=buy_balance.text) buy_entrust_value = JMESPathExtractor().extract(query="OBJECT.PNLList[0].entrustMargin", body=buy_balance.text) logger.info("用户:{0}---接口:{1}---状态:{2}---下单前可用保证金:{3}--委托保证金:{4}".format(self.buyer, sda_account_asset_detail_get_url,buy_balance.status_code, buy_balance_value, buy_entrust_value)) # 下限价空单委托 time.sleep(1) buy_resp = self.session.post(url=base+sda_order_create_url, data=get_sda_order_create_param( sda_id=sda_id,order_type=order_type.空单,order_price_type=order_type.限价,order_price=price,order_num=deal_num )) buy_order_id = JMESPathExtractor().extract(query="OBJECT.orderId", body=buy_resp.text) logger.info("用户:{0}--接口:{1}--状态:{2}--类型:空单--返回信息:{3}".format( self.buyer, sda_order_create_url, buy_resp.status_code, buy_resp.json() )) # 撤单 time.sleep(1) cancel_resp = self.session.post(url=base+sda_order_cancel_url, data=get_sda_order_cancel_param(sda_id=sda_id, order_id=buy_order_id, order_type=空单)) logger.info("撤单接口状态:{0}---返回信息:{1}".format(cancel_resp.status_code, cancel_resp.json())) cancel_order_id = JMESPathExtractor().extract(query="OBJECT.orderId", body=cancel_resp.text) cancel_msg = JMESPathExtractor().extract(query="MSG", body=cancel_resp.text) # 查询用户余额,然后判断下单前后的用户余额 time.sleep(1) after_buy_balance = self.session.post(url=base+sda_account_asset_detail_get_url, data=get_sda_account_asset_detail_get_param()) after_buy_balance_value = JMESPathExtractor().extract(query="OBJECT.PNLList[0].availableMargin",body=after_buy_balance.text) after_buy_entrust_value = JMESPathExtractor().extract(query="OBJECT.PNLList[0].entrustMargin", body=after_buy_balance.text) logger.info("用户:{0}---接口:{1}---状态:{2}---下单后可用保证金:{3}---下单后委托保证金:{4}".format(self.buyer, sda_account_asset_detail_get_url,after_buy_balance.status_code, after_buy_balance_value, after_buy_entrust_value)) order_id_flag = assert_list([buy_order_id, 200, "SUCCESS"], [cancel_order_id, cancel_resp.status_code, cancel_msg]) available_margin_flag = assert_one(int(buy_balance_value), int(after_buy_balance_value)) entrust_margin_flag = assert_one(int(buy_entrust_value), int(after_buy_entrust_value)) self.assertListEqual([True, True, True], [available_margin_flag, entrust_margin_flag, order_id_flag])
def test_01(self): """ 下限价多单,撤单,验证用户余额,委托保证金余额 """ logger.info("用例编号:7-1--下多单,撤单,验证用户余额,委托保证金余额,") ConnectMysql(_type=mysql_type).sda_clear_open_multi_close_open_order(user_id=self.user_id, contract_id=sda_id, status=2) ConnectMysql(_type=mysql_type).sda_clear_open_empty_close_multi_order(user_id=self.user_id, contract_id=sda_id, status=2) ConnectRedis(_type=redis_type).sda_clear_order(sda_id=sda_id) ConnectRedis(_type=redis_type, db=4).sda_clear_user_balance(user_id=self.user_id, keys=sda_id) ConnectMysql(_type=mysql_type).sda_update_user_balance(user_id=self.user_id, sda_id=sda_id, sda_balance=990000000000000) deal_price = 100000000 deal_num = 100000000 # 下委托前查询可用余额 info_dict = account_info(user=self.buyer, session=self.session, sda_id=sda_id, price=1000000000) before_balance = info_dict["balance"] # 下多单限价 buy_resp = self.session.post(url=base+sda_order_create_url, data=get_sda_order_create_param( sda_id=sda_id,order_type=order_type.多单,order_price_type=order_type.限价,order_price=deal_price,order_num=deal_num )) buy_order_id = JMESPathExtractor().extract(query="OBJECT.orderId", body=buy_resp.text) logger.info("用户:{0}--接口:{1}--状态:{2}--类型:多单--返回信息:{3}".format( self.buyer, sda_order_create_url, buy_resp.status_code, buy_resp.json() )) # 撤单 time.sleep(1) cancel_resp = self.session.post(url=base+sda_order_cancel_url, data=get_sda_order_cancel_param(sda_id=sda_id, order_id=buy_order_id, order_type=多单)) logger.info("撤单接口状态:{0}---返回信息:{1}".format(cancel_resp.status_code, cancel_resp.json())) cancel_order_id = JMESPathExtractor().extract(query="OBJECT.orderId", body=cancel_resp.text) sync_id = JMESPathExtractor().extract(query="syncLockKey", body=cancel_resp.text) cancel_msg = JMESPathExtractor().extract(query="MSG", body=cancel_resp.text) # 下委托后查询可用余额 time.sleep(1) after_info_dict = account_info_sync(sync_id=sync_id, user=self.buyer, session=self.session, sda_id=sda_id, price=1000000000) after_balance = after_info_dict["balance"] order_id_flag = assert_list([buy_order_id, 200, "SUCCESS"], [cancel_order_id, cancel_resp.status_code, cancel_msg]) available_margin_flag = assert_one(int(before_balance), int(after_balance) ) self.assertListEqual([True, True], [available_margin_flag, order_id_flag])
def test_04(self): """ 用户可用保证金为0,空单,检查是否下单成功 """ logger.info("用例编号:20-4---用户可用保证金为0,限价空单,检查是否下单成功") deal_num = 100000000 ConnectMysql(_type=mysql_type).sda_clear_open_empty_close_multi_order( user_id=self.user_id, contract_id=sda_id, status=2) ConnectMysql(_type=mysql_type).sda_clear_open_multi_close_open_order( user_id=self.user_id, contract_id=sda_id, status=2) ConnectRedis(_type=redis_type).sda_clear_order(sda_id=sda_id) ConnectRedis(_type=redis_type, db=4).sda_clear_user_balance(user_id=self.user_id, keys=sda_id) ConnectMysql(_type=mysql_type).sda_update_user_balance( user_id=self.user_id, sda_id=sda_id, sda_balance=0) # 查询当前股价 stock_price_dict = market_info_get(user=self.buyer, session=self.session, sda_id=sda_id) now_stock_price = stock_price_dict["stockPrice"] price = int(int(now_stock_price) * 0.95) # min_max = ConnectMysql(_type=mysql_type).sda_query_contract_min(sda_id=sda_id) order_resp = self.session.post(url=base + sda_order_create_url, data=get_sda_order_create_param( sda_id=sda_id, order_type=order_type.空单, order_price_type=order_type.限价, order_price=price, order_num=deal_num)) msg = JMESPathExtractor().extract(query="MSG", body=order_resp.text) status = JMESPathExtractor().extract(query="STATUS", body=order_resp.text) logger.info("用户:{0}--接口:{1}---状态:{2}---返回信息:{3}".format( self.buyer, sda_order_create_url, order_resp.status_code, order_resp.json())) flag = assert_list([200, "该订单将会触发您所持仓位的强平", "1"], [order_resp.status_code, msg, status]) self.assertTrue(flag)
def test_01(self): """ 持仓后验证持仓价值是否返回 :return: """ logger.info("用例编号:202-1,验证持仓价值和开仓价值计算") market_dict = market_info_get(user=self.buyer, session=self.session, sda_id=sda_id) print("now stock price", market_dict["stockPrice"]) print("tradeUnit", market_dict["tradeUnit"]) positions_dict = query_account_position_get(user=self.buyer, session=self.session, sync_key=self.sync_key) # print(positions_dict) print("sdaValuation", positions_dict["sdaValuation"]) print("sdaOpeningValuation", positions_dict["sdaOpeningValuation"]) buy_open_positions_value = open_positions_value( hold_price=self.deal_price, hold_num=self.deal_num, trade_unit=int(market_dict["tradeUnit"])) print("buy_open_positions_value", buy_open_positions_value) buy_positions_value = positions_value( stock_price=int(market_dict["stockPrice"]), hold_num=self.deal_num, trade_unit=int(market_dict["tradeUnit"])) print("buy_positions_value", buy_positions_value) flag = assert_list([ int(positions_dict["sdaValuation"]), int(positions_dict["sdaOpeningValuation"]) ], [int(buy_positions_value), int(buy_open_positions_value)]) self.assertEqual(flag, True)
def test_03(self, lever, lever_two): """ 多单1单,空单1单,多单部分成交,空单完全成交,验证合约状态、用户余额 """ logger.info("用例编号:114-3---限价多单1单,限价空单1单,多单部分成交,空单完全成交,验证合约状态、用户余额") deal_num = 200000000 sell_num = 100000000 # 查询当前股价 stock_price_dict = market_info_get(user=self.buyer, session=self.session, sda_id=sda_id) now_stock_price = stock_price_dict["stockPrice"] deal_price = int(int(now_stock_price) * 0.95) # 下一单多单、空单,完全成交 buy_resp = self.session.post(url=base + sda_order_create_url, data=get_sda_order_create_param( sda_id=sda_id, order_type=多单, order_price_type=限价, lever=lever, order_price=deal_price, order_num=deal_num)) logger.info("用户:{0}--接口:{1}--状态:{2}--类型:多单--返回信息:{3}".format( self.buyer, sda_order_create_url, buy_resp.status_code, buy_resp.json())) buy_order_id = JMESPathExtractor().extract(query="OBJECT.orderId", body=buy_resp.text) # time.sleep(1) sell_resp = self.sell_session.post(url=base + sda_order_create_url, data=get_sda_order_create_param( sda_id=sda_id, order_type=空单, order_price_type=限价, lever=lever_two, order_price=deal_price, order_num=sell_num)) logger.info("用户:{0}--接口:{1}--状态:{2}--类型:空单--返回信息:{3}".format( self.seller, sda_order_create_url, sell_resp.status_code, sell_resp.json())) sell_order_id = JMESPathExtractor().extract(query="OBJECT.orderId", body=sell_resp.text) # time.sleep(1) after_buy_balance = self.session.post( url=base + sda_account_asset_detail_get_url, data=get_sda_account_asset_detail_get_param()) after_sell_balance = self.sell_session.post( url=base + sda_account_asset_detail_get_url, data=get_sda_account_asset_detail_get_param()) after_buy_balance_value = JMESPathExtractor().extract( query="OBJECT.PNLList[0].availableMargin", body=after_buy_balance.text) after_sell_balance_value = JMESPathExtractor().extract( query="OBJECT.PNLList[0].availableMargin", body=after_sell_balance.text) after_buy_entrust_value = JMESPathExtractor().extract( query="OBJECT.PNLList[0].entrustMargin", body=after_buy_balance.text) after_sell_entrust_value = JMESPathExtractor().extract( query="OBJECT.PNLList[0].entrustMargin", body=after_sell_balance.text) # 查询当前持仓中买卖方持仓数量、保证金、开仓均价 time.sleep(4) after_buy_position_dict = query_account_position_get( user=self.buyer, session=self.session) after_sell_position_dict = query_account_position_get( user=self.seller, session=self.sell_session) after_buy_sdaAveragePrice = after_buy_position_dict["sdaAveragePrice"] after_buy_sdaCount = after_buy_position_dict["sdaCount"] after_buy_currencyBalancePosition = after_buy_position_dict[ "currencyBalancePosition"] after_sell_sdaAveragePrice = after_sell_position_dict[ "sdaAveragePrice"] after_sell_sdaCount = after_sell_position_dict["sdaCount"] after_sell_currencyBalancePosition = after_sell_position_dict[ "currencyBalancePosition"] # 计算保证金 # buy_currency_balance = make_currency_balance(num=deal_num, price=deal_price) # sell_currency_balance = make_currency_balance(num=deal_num, price=deal_price) # 检验持仓均价,持仓量,保证金 price_flag = assert_one(int(deal_price), int(after_buy_sdaAveragePrice)) num_flag = assert_one(int(sell_num), int(after_buy_sdaCount)) # after_buy_flag = assert_one(buy_currency_balance, int(after_buy_currencyBalancePosition)) after_sell_flag = assert_list( [int(deal_price), int(sell_num)], [int(after_sell_sdaAveragePrice), int(after_sell_sdaCount)]) self.assertListEqual([True, True, True], [price_flag, num_flag, after_sell_flag])
def test_01(self): """ 正常访问资金划转接口,验证接口状态、status、msg """ amount = 100000000 logger.info("用例编号:2-1---资金划转接口测试类,正常访问资金划转接口,验证接口状态、status、msg") ConnectMysql(_type=mysql_type).update_balance_value( user_mail=self.buyer, currency_id=currency_id, balance_value=90000000000) # 转入前查询bb账户余额 before_bb_account_resp = self.session.post( url=base + get_user_balance_servlet_url, data=get_user_balance_servlet_param(user=self.buyer, currency_id=currency_id)) before_currency_balance = JMESPathExtractor().extract( query="OBJECT.balanceValue", body=before_bb_account_resp.text) logger.info("用户:{0}---币ID:{1}---转入前余额:{2}".format( self.buyer, currency_id, before_currency_balance)) # 转入前查询合约账户余额 info_dict = account_info(user=self.buyer, session=self.session, sda_id=sda_id) before_account_balance = info_dict["balance"] deposit_resp = self.session.post(url=base + sda_account_deposit_url, data=get_sda_account_deposit_param( sda_id=sda_id, amount=amount)) logger.info("用户:{0}---接口:{1}---状态:{2}---正常划转返回信息:{3}".format( self.buyer, sda_account_deposit_url, deposit_resp.status_code, deposit_resp.json())) status = JMESPathExtractor().extract(query="STATUS", body=deposit_resp.text) msg = JMESPathExtractor().extract(query="MSG", body=deposit_resp.text) sync_id = JMESPathExtractor().extract(query="syncLockKey", body=deposit_resp.text) print("msg:", type(msg)) time.sleep(2) # 转入后查询合约账户余额 after_info_dict = account_info_sync(sync_id=sync_id, user=self.buyer, session=self.session, sda_id=sda_id) after_account_balance = after_info_dict["balance"] # 转入后查询BB账户余额 # time.sleep(1) after_bb_account_resp = self.session.post( url=base + get_user_balance_servlet_url, data=get_user_balance_servlet_param(self.buyer, currency_id=currency_id)) after_currency_balance = JMESPathExtractor().extract( query="OBJECT.balanceValue", body=after_bb_account_resp.text) logger.info("用户:{0}--币ID:{1}--转入后余额:{2}".format( self.buyer, currency_id, after_currency_balance)) # _object = JMESPathExtractor().extract(query="OBJECT", body=deposit_resp.text) print("before account balance", before_account_balance) print("amount:", amount) print("after account balance:", after_account_balance) flag = assert_list([200, "0"], [deposit_resp.status_code, status]) flag_1 = assert_one( int(before_currency_balance) - int(amount), int(after_currency_balance)) flag_2 = assert_one( int(before_account_balance) + int(amount), int(after_account_balance)) self.assertListEqual([True, True, True], [flag, flag_1, flag_2])
def test_01(self): """ 大数极限测试类 """ logger.info("用例编号:110-1---大数极限测试类") deal_num = 90000000000*100000000 lever = 100 # 查询当前股价 stock_price_dict = market_info_get(user=self.buyer, session=self.session, sda_id=sda_id) now_stock_price = stock_price_dict["stockPrice"] stock_unit = int(stock_price_dict["tradeUnit"]) deal_price = int(int(now_stock_price) * 1) # deal_price = now_stock_price print("stock price", now_stock_price) before_buy_dict = account_info(user=self.buyer, session=self.session, sda_id=sda_id) before_buy_balance = before_buy_dict["balance"] # 下一单多单、空单,完全成交 buy_resp = self.session.post(url=base+sda_order_create_url, data=get_sda_order_create_param( sda_id=sda_id, order_type=多单, order_price_type=限价, lever=lever, order_price=deal_price,order_num=deal_num )) buy_sync_id = JMESPathExtractor().extract(query="syncLockKey", body=buy_resp.text) logger.info("用户:{0}--接口:{1}--状态:{2}--类型:多单--返回信息:{3}".format( self.buyer, sda_order_create_url, buy_resp.status_code, buy_resp.json() )) # time.sleep(3) sell_resp = self.sell_session.post(url=base+sda_order_create_url, data=get_sda_order_create_param( sda_id=sda_id, order_type=空单, order_price_type=限价, lever=lever, order_price=deal_price,order_num=deal_num )) sell_sync_id = JMESPathExtractor().extract(query="syncLockKey", body=sell_resp.text) logger.info("用户:{0}--接口:{1}--状态:{2}--类型:空单--返回信息:{3}".format( self.seller, sda_order_create_url, sell_resp.status_code, sell_resp.json() )) # time.sleep(5) # 查询当前持仓中买卖方持仓数量、保证金、开仓均价 after_buy_position_dict = query_account_position_get(sync_key=sell_sync_id, user=self.buyer, session=self.session) after_sell_position_dict = query_account_position_get(sync_key=sell_sync_id, user=self.seller, session=self.sell_session) after_buy_sdaAveragePrice = after_buy_position_dict["sdaAveragePrice"] after_buy_sdaCount = after_buy_position_dict["sdaCount"] after_buy_currencyBalancePosition = after_buy_position_dict["currencyBalancePosition"] after_sell_sdaAveragePrice = after_sell_position_dict["sdaAveragePrice"] after_sell_sdaCount = after_sell_position_dict["sdaCount"] after_sell_currencyBalancePosition = after_sell_position_dict["currencyBalancePosition"] # 计算保证金 buy_currency_balance = employBalance(price=deal_price, count=deal_num, unit=stock_unit, lever=lever) # 检验持仓均价,持仓量,保证金 balance_flag = assert_one(int(after_buy_currencyBalancePosition), int(buy_currency_balance)) self.assertTrue(balance_flag) price_flag = assert_one(int(deal_price), int(after_buy_sdaAveragePrice)) num_flag = assert_one(int(deal_num), int(after_buy_sdaCount)) # after_buy_flag = assert_one(buy_currency_balance, int(after_buy_currencyBalancePosition)) after_sell_flag = assert_list([int(deal_price), int(deal_num)], [int(after_sell_sdaAveragePrice), int(after_sell_sdaCount)]) self.assertListEqual([True, True, True], [price_flag, num_flag,after_sell_flag])
def test_01(self): """ 多单,验证合约状态、余额。 """ logger.info("用例编号:20-1---限价多单,验证合约、用户余额。") deal_price = 100000000 deal_num = 100000000 # 更新此合约ID下的所有合约状态为2 ConnectMysql(_type=mysql_type).sda_clear_open_empty_close_multi_order( user_id=self.user_id, contract_id=sda_id, status=2) ConnectMysql(_type=mysql_type).sda_clear_open_multi_close_open_order( user_id=self.user_id, contract_id=sda_id, status=2) ConnectRedis(_type=redis_type).sda_clear_order(sda_id=sda_id) ConnectRedis(_type=redis_type, db=4).sda_clear_user_balance(user_id=self.user_id, keys=sda_id) ConnectMysql(_type=mysql_type).sda_update_user_balance( user_id=self.user_id, sda_id=sda_id, sda_balance=990000000000) # 查询当前合约的unit stock_price_dict = market_info_get(user=self.buyer, session=self.session, sda_id=sda_id) # now_stock_price = int(stock_price_dict["stockPrice"]) stock_unit = int(stock_price_dict["tradeUnit"]) # print("now stock price", now_stock_price) # 下单前查询两个用户的余额 account_dict = account_info(user=self.buyer, session=self.session, sda_id=sda_id) buy_balance = account_dict["balance"] buy_resp = self.session.post(url=base + sda_order_create_url, data=get_sda_order_create_param( sda_id=sda_id, order_type=order_type.多单, order_price_type=order_type.限价, order_price=deal_price, order_num=deal_num)) buy_order_id = JMESPathExtractor().extract(query="OBJECT.orderId", body=buy_resp.text) sync_id = JMESPathExtractor().extract(query="syncLockKey", body=buy_resp.text) logger.info("用户:{0}--接口:{1}--状态:{2}--类型:多单--返回信息:{3}".format( self.buyer, sda_order_create_url, buy_resp.status_code, buy_resp.json())) # 计算委托价值 stock_value = int( stockValue(price=deal_price, count=deal_num, unit=stock_unit)) print("stock_value", stock_value) # 查询当前委托 order_get_open_resp = self.session.post( url=base + sda_order_get_open_url, data=get_sda_order_get_open_param(sda_id=sda_id)) get_order_id = JMESPathExtractor().extract( query="LIST[0].orderId", body=order_get_open_resp.text) order_num = JMESPathExtractor().extract(query="LIST[0].orderQuantity", body=order_get_open_resp.text) order_price = JMESPathExtractor().extract( query="LIST[0].orderPrice", body=order_get_open_resp.text) total_price = int(JMESPathExtractor().extract( query="LIST[0].totalPrice", body=order_get_open_resp.text)) order_flag = assert_list( [buy_order_id, deal_price, deal_num], [get_order_id, int(order_price), int(order_num)]) total_price_flag = assert_one(stock_value, total_price) self.assertTrue(total_price_flag) self.assertTrue(order_flag) after_account_dict = account_info_sync(sync_id=sync_id, user=self.buyer, session=self.session, sda_id=sda_id) after_account_balance = after_account_dict["balance"] flag = assert_one(int(buy_balance), int(after_account_balance) + deal_num) self.assertTrue(flag)
def test_01(self, lever): """ 参数化测试不同杠杆倍数成交,先限价多单,后市价空单 :return: """ logger.info( "用例编号:112-1---参数化测试不同杠杆倍数,5,10,20,50,100,成交,先限价多单,后市价空单,完全成交,验证余额") deal_num = 100000000 # 查询当前股价 stock_price_dict = market_info_get(user=self.buyer, session=self.session, sda_id=sda_id) now_stock_price = stock_price_dict["stockPrice"] deal_price = int(int(now_stock_price) * 0.95) # 下一单多单、空单,完全成交 buy_resp = self.session.post(url=base + sda_order_create_url, data=get_sda_order_create_param( sda_id=sda_id, order_type=多单, order_price_type=限价, lever=lever, order_price=deal_price, order_num=deal_num)) buy_sync_id = JMESPathExtractor().extract(query="syncLockKey", body=buy_resp.text) logger.info("用户:{0}--接口:{1}--状态:{2}--类型:多单--返回信息:{3}".format( self.buyer, sda_order_create_url, buy_resp.status_code, buy_resp.json())) # time.sleep(3) sell_resp = self.sell_session.post(url=base + sda_order_create_url, data=get_sda_order_create_param( sda_id=sda_id, order_type=空单, order_price_type=市价, order_price=deal_price, order_num=deal_num)) sell_sync_id = JMESPathExtractor().extract(query="syncLockKey", body=sell_resp.text) logger.info("用户:{0}--接口:{1}--状态:{2}--类型:空单--返回信息:{3}".format( self.seller, sda_order_create_url, sell_resp.status_code, sell_resp.json())) time.sleep(4) # 查询当前持仓中买卖方持仓数量、保证金、开仓均价 after_buy_position_dict = query_account_position_get( user=self.buyer, session=self.session) after_sell_position_dict = query_account_position_get( user=self.seller, session=self.sell_session) after_buy_sdaAveragePrice = after_buy_position_dict["sdaAveragePrice"] after_buy_sdaCount = after_buy_position_dict["sdaCount"] after_buy_currencyBalancePosition = after_buy_position_dict[ "currencyBalancePosition"] after_sell_sdaAveragePrice = after_sell_position_dict[ "sdaAveragePrice"] after_sell_sdaCount = after_sell_position_dict["sdaCount"] after_sell_currencyBalancePosition = after_sell_position_dict[ "currencyBalancePosition"] # 计算保证金 # buy_currency_balance = make_currency_balance(num=deal_num, price=deal_price) # sell_currency_balance = make_currency_balance(num=deal_num, price=deal_price) # 检验持仓均价,持仓量,保证金 price_flag = assert_one(int(deal_price), int(after_buy_sdaAveragePrice)) num_flag = assert_one(int(deal_num), int(after_buy_sdaCount)) # after_buy_flag = assert_one(buy_currency_balance, int(after_buy_currencyBalancePosition)) after_sell_flag = assert_list( [int(deal_price), int(deal_num)], [int(after_sell_sdaAveragePrice), int(after_sell_sdaCount)]) self.assertListEqual([True, True, True], [price_flag, num_flag, after_sell_flag])
def test_02(self): """ 限价空单1单,50倍杠杆,限价多单1单,1倍杠杆,完全成交,验证合约状态、用户余额 """ logger.info("用例编号:110-2---限价空单1单,50倍杠杆,限价多单1单,1倍杠杆,完全成交,验证合约状态、用户余额") deal_num = 200000000 lever = 50 # 查询当前股价 stock_price_dict = market_info_get(user=self.buyer, session=self.session, sda_id=sda_id) now_stock_price = stock_price_dict["stockPrice"] deal_price = int(int(now_stock_price) * 0.95) # 交易前查询用户余额 before_buy_dict = account_info(user=self.buyer, session=self.session, sda_id=sda_id) before_buy_balance = before_buy_dict["balance"] # 下一单多单、空单,完全成交 buy_resp = self.session.post(url=base+sda_order_create_url, data=get_sda_order_create_param( sda_id=sda_id, order_type=空单, order_price_type=限价, lever=lever, order_price=deal_price,order_num=deal_num )) logger.info("用户:{0}--接口:{1}--状态:{2}--类型:空单--返回信息:{3}".format( self.buyer, sda_order_create_url, buy_resp.status_code, buy_resp.json() )) # time.sleep(3) sell_resp = self.sell_session.post(url=base+sda_order_create_url, data=get_sda_order_create_param( sda_id=sda_id, order_type=多单, order_price_type=限价, order_price=deal_price, order_num=deal_num )) sell_sync_id = JMESPathExtractor().extract(query="syncLockKey", body=sell_resp.text) logger.info("用户:{0}--接口:{1}--状态:{2}--类型:多单--返回信息:{3}".format( self.seller, sda_order_create_url, sell_resp.status_code, sell_resp.json() )) # time.sleep(1) # 查询当前持仓中买卖方持仓数量、保证金、开仓均价 after_buy_position_dict = query_account_position_get(sync_key=sell_sync_id, user=self.buyer, session=self.session) after_sell_position_dict = query_account_position_get(sync_key=sell_sync_id, user=self.seller, session=self.sell_session) after_buy_sdaAveragePrice = after_buy_position_dict["sdaAveragePrice"] after_buy_sdaCount = after_buy_position_dict["sdaCount"] after_buy_currencyBalancePosition = after_buy_position_dict["currencyBalancePosition"] after_sell_sdaAveragePrice = after_sell_position_dict["sdaAveragePrice"] after_sell_sdaCount = after_sell_position_dict["sdaCount"] after_sell_currencyBalancePosition = after_sell_position_dict["currencyBalancePosition"] # 计算保证金 buy_currency_balance = (deal_num * deal_price / 100000000) / lever # sell_currency_balance = make_currency_balance(num=deal_num, price=deal_price) # 检验持仓均价,持仓量,保证金 balance_flag = assert_one(int(after_buy_currencyBalancePosition), int(buy_currency_balance)) self.assertTrue(balance_flag) price_flag = assert_one(int(deal_price), int(after_buy_sdaAveragePrice)) num_flag = assert_one(int(deal_num), int(after_buy_sdaCount)) # after_buy_flag = assert_one(buy_currency_balance, int(after_buy_currencyBalancePosition)) after_sell_flag = assert_list([int(deal_price), int(deal_num)], [int(after_sell_sdaAveragePrice), int(after_sell_sdaCount)]) self.assertListEqual([True, True, True], [price_flag, num_flag,after_sell_flag])