def step_impl(context, user, promotion_name): promotion = Promotion.objects.get(name=promotion_name) response = context.client.get('/mall2/premium_sale/?id=%d' % promotion.id) promotion = response.context['promotion'] if promotion.member_grade_id == 0: member_grade = u'全部会员' else: member_grade = promotion.member_grade_name actual = { "main_product": [], "premium_products": [], "name": promotion.name, "promotion_title": promotion.promotion_title, "start_date": promotion.start_date, "end_date": promotion.end_date, "member_grade": member_grade, "count": promotion.detail['count'], "is_enable_cycle_mode": promotion.detail['is_enable_cycle_mode'] } main_product = {} premium_products = {} for product in promotion.products: actual["main_product"].append(product) for premium_product in promotion.detail["premium_products"]: actual["premium_products"].append(premium_product) expected = json.loads(context.text) expected['start_date'] = bdd_util.get_datetime_str(expected['start_date']) expected['end_date'] = bdd_util.get_datetime_str(expected['end_date']) bdd_util.assert_dict(expected, actual)
def step_impl(context, user): url = '/mall2/api/forbidden_coupon_product/?design_mode=0&version=1' if hasattr(context, 'query_param'): if context.query_param.get('product_name'): url += '&name=' + context.query_param['product_name'] if context.query_param.get('bar_code'): url += '&barCode=' + context.query_param['bar_code'] if context.query_param.get('start_date'): url += '&startDate=' + bdd_util.get_datetime_str( context.query_param['start_date'])[:16] if context.query_param.get('end_date'): url += '&endDate=' + bdd_util.get_datetime_str( context.query_param['end_date'])[:16] if context.query_param.get('status', u'全部') != u'全部': if context.query_param['status'] == u'未开始': status = 1 elif context.query_param['status'] == u'进行中': status = 2 response = context.client.get(url) actual = [] for item in json.loads(response.content)['data']['items']: if item['is_permanant_active']: item['start_date'] = '' item['end_date'] = '' if item['is_permanant_active']: item['is_permanant_active'] = 1 else: item['is_permanant_active'] = 0 actual.append({ 'product_name': item['product']['name'], 'product_price': item['product']['display_price_range'], 'start_date': item['start_date'], 'end_date': item['end_date'], 'status': item['status_name'], 'is_permanant_active': item['is_permanant_active'] }) if context.table: expected = [ forbidden_coupon_product.as_dict() for forbidden_coupon_product in context.table ] else: expected = json.loads(context.text) for item in expected: if item.get('start_date', None): item['start_date'] = bdd_util.get_datetime_str(item['start_date']) if item.get('end_date', None): item['end_date'] = bdd_util.get_datetime_str(item['end_date']) bdd_util.assert_list(expected, actual)
def step_impl(context, user): if context.table: forbidden_coupon_products = [ forbidden_coupon_product.as_dict() for forbidden_coupon_product in context.table ] else: forbidden_coupon_products = json.loads(context.text) for forbidden_coupon_product in forbidden_coupon_products: products = [] if type(forbidden_coupon_product['products']) == list: for product in forbidden_coupon_product['products']: db_product = ProductFactory(name=product['name']) products.append({'id': db_product.id}) else: db_product = ProductFactory( name=forbidden_coupon_product['products']) products.append({'id': db_product.id}) start_date = forbidden_coupon_product.get('start_date', None) end_date = forbidden_coupon_product.get('end_date', None) is_permanant_active = int( forbidden_coupon_product.get('is_permanant_active', '')) if not is_permanant_active: start_date = bdd_util.get_datetime_str(start_date) end_date = bdd_util.get_datetime_str(end_date) else: start_date = None end_date = None data = { 'products': json.dumps(products), 'is_permanant_active': is_permanant_active } if start_date and end_date: data['start_date'] = start_date data['end_date'] = end_date url = '/mall2/api/forbidden_coupon_product/?_method=put' response = context.client.post(url, data) bdd_util.assert_api_call_success(response)
def step_impl(context, webapp_owner_name): for row in context.table: webapp_user_name = row['member_name'] if webapp_user_name[0] == u'-': webapp_user_name = webapp_user_name[1:] #clear last member's info in cookie and context context.execute_steps(u"When 清空浏览器") else: context.execute_steps(u"When 清空浏览器") context.execute_steps(u"When %s访问%s的webapp" % (webapp_user_name, webapp_owner_name)) data = { 'webapp_user_name': webapp_user_name, 'powerme_value': row['powerme_value'], 'parti_time': bdd_util.get_datetime_str(row['parti_time']), 'name': row['name'] } webapp_owner_id = context.webapp_owner_id user = User.objects.get(id=webapp_owner_id) openid = "%s_%s" % (webapp_user_name, user.username) power_me_rule_id = str(__get_power_me_rule_id(data['name'])) member = member_api.get_member_by_openid(openid, context.webapp_id) #先进入微助力页面 response = __get_into_power_me_pages(context, webapp_owner_id, power_me_rule_id, openid) context.power_me_rule_id = power_me_rule_id context.rank_response = __get_power_me_rank_informations( context, webapp_owner_id, power_me_rule_id, openid).content context.execute_steps(u"when %s把%s的微助力活动链接分享到朋友圈" % (webapp_user_name, webapp_owner_name)) powered_member_info = PowerMeParticipance.objects.get( member_id=member.id, belong_to=power_me_rule_id) powered_member_info.update(set__created_at=data['parti_time']) i = 0 webapp_test_user_name = 'test_user_' while i < int(data['powerme_value']): i += 1 context.execute_steps(u"When 清空浏览器") context.execute_steps( u"When %s关注%s的公众号" % (webapp_test_user_name + str(i), webapp_owner_name)) context.execute_steps( u"When %s访问%s的webapp" % (webapp_test_user_name + str(i), webapp_owner_name)) context.execute_steps( u"When %s点击%s分享的微助力活动链接进行助力" % (webapp_test_user_name + str(i), webapp_user_name)) context.execute_steps(u"When 更新助力排名")
def step_impl(context, user): delivery_data = json.loads(context.text) order_no = delivery_data['order_no'] if '-' in order_no: order_no_info = order_no.split('-') if UserProfile.objects.filter(store_name=order_no_info[1]).count() > 0: order_no = '%s^%su' % (order_no_info[0], UserProfile.objects.get( store_name=order_no_info[1]).user_id) else: order_no = '%s^%ss' % (order_no_info[0], Supplier.objects.get( name=order_no_info[1]).id) order_id = Order.objects.get(order_id=order_no).id logistics = delivery_data.get('logistics', 'off') if logistics == u'其他': logistics = delivery_data.get('name') leader_name = delivery_data.get('shipper', '') express_company_name = '' express_number = '' is_update_express = '' if logistics != 'off': express_company_name = logistics express_number = delivery_data['number'] is_update_express = 'false' url = '/mall2/api/delivery/' data = { 'order_id': order_id, 'express_company_name': express_company_name, 'express_number': express_number, 'leader_name': leader_name, 'is_update_express': is_update_express } if logistics == u'其他': data['is_100'] = 'false' response = context.client.post(url, data) if 'date' in delivery_data: OrderOperationLog.objects.filter( order_id=delivery_data['order_no'], action="订单发货").update( created_at=bdd_util.get_datetime_str(delivery_data['date']))
def step_impl(context, webapp_user_name, lottery_time, lottery_name): exlottery_time = bdd_util.get_datetime_str(lottery_time) context.exlottery_detail = { 'webapp_owner_id': context.webapp_owner_id, 'id': context.exlottery_id, 'ex_code': context.exlottery_code } response = app_utils.get_response( context, { "app": "m/apps/exlottery", "resource": "exlottery_prize", "method": "put", "type": "api", "args": context.exlottery_detail }) context.lottery_result = json.loads(response.content) ExlottoryRecord.objects(code=context.exlottery_code).update( created_at=exlottery_time)
def step_impl(context, webapp_owner_name): for row in context.table: webapp_user_name = row['consumer'] if webapp_user_name[0] == u'-': webapp_user_name = webapp_user_name[1:] #先关注再取消关注,模拟非会员购买 duhao 20160407 context.execute_steps(u"When %s关注%s的公众号" % (webapp_user_name, webapp_owner_name)) context.execute_steps(u"When %s取消关注%s的公众号" % (webapp_user_name, webapp_owner_name)) openid = "%s_%s" %(webapp_user_name, webapp_owner_name) soucial_account_id = SocialAccount.objects.get(openid=openid).id member_id = MemberHasSocialAccount.objects.get(account_id=soucial_account_id).member_id Member.objects.filter(id=member_id).update(status=2) #clear last member's info in cookie and context context.execute_steps(u"When 清空浏览器") else: context.execute_steps(u"When 清空浏览器") context.execute_steps(u"When %s访问%s的webapp" % (webapp_user_name, webapp_owner_name)) #购买商品 product_infos = row['product'].strip().split(',') model = None if len(product_infos) == 2: product, count = product_infos elif len(product_infos) == 3: product, model, count = product_infos data = { "date": row['date'].strip(), "products": [{ "name": product, "count": count, "model": model }] } if hasattr(context, 'ship_address'): data.update(context.ship_address) # TODO 统计BDD使用,需要删掉 # purchase_type = u'测试购买' if row['type'] == u'测试' else None # if purchase_type: # data['type'] = purchase_type # TODO 统计BDD使用,需要删掉 # data['ship_name'] = webapp_user_name if row.get('integral', None): tmp = 0 try: tmp = int(row['integral']) except: pass # if tmp > 0: if tmp > 0 and row.get('integral', None): #duhao 20150929 消费积分不能依赖于现获取积分,让integral列可以不填 # 先为会员赋予积分,再使用积分 # TODO 修改成jobs修改bill积分 context.execute_steps(u"When %s获得%s的%s会员积分" % (webapp_user_name, webapp_owner_name, row['integral'])) data['products'][0]['integral'] = tmp if row.get('coupon', '') != '': if ',' in row['coupon']: coupon_name, coupon_id = row['coupon'].strip().split(',') coupon_dict = {} coupon_dict['name'] = coupon_name coupon_dict['coupon_ids'] = [ coupon_id ] coupon_list = [ coupon_dict ] context.coupon_list = coupon_list context.execute_steps(u"when %s领取%s的优惠券" % (webapp_user_name, webapp_owner_name)) else: coupon_id = row['coupon'].strip() data['coupon'] = coupon_id if row.get('weizoom_card', None) and ',' in row['weizoom_card']: card_name, card_pass = row['weizoom_card'].strip().split(',') card_dict = {} card_dict['card_name'] = card_name card_dict['card_pass'] = card_pass data['weizoom_card'] = [ card_dict ] if row.get('integral', '') != '': data['integral'] = int(row.get('integral')) if row.get('date') != '': data['date'] = row.get('date') if row.get('order_id', '') != '': data['order_id'] = row.get('order_id') if row.get('pay_type', '') != '': data['pay_type'] = row.get('pay_type') print("SUB STEP: to buy products, param: {}".format(data)) context.caller_step_purchase_info = data context.execute_steps(u"when %s购买%s的商品" % (webapp_user_name, webapp_owner_name)) order = Order.objects.all().order_by('-id')[0] #支付订单 if row.get('payment_time', '') != '' or row.get('payment', '') == u'支付': pay_type = row.get('pay_type', u'货到付款') if pay_type != '' != u'优惠抵扣': if 'order_id' in data: context.created_order_id = data['order_id'] context.execute_steps(u"when %s使用支付方式'%s'进行支付" % (webapp_user_name, pay_type)) if row.get('payment_time', '') != '': Order.objects.filter(id=order.id).update( payment_time=bdd_util.get_datetime_str(row['payment_time'])) # 操作订单 action = row['action'].strip() if action: actor, operation = action.split(',') context.execute_steps(u"given %s登录系统" % actor) if row.get('delivery_time') or operation == u'完成': step_id = OPERATION2STEPID.get(u'发货', None) context.latest_order_id = order.id context.execute_steps(step_id % actor) if row.get('delivery_time'): log = OrderOperationLog.objects.filter( order_id=order.order_id, action='订单发货').get() log.created_at = bdd_util.get_date(row.get('delivery_time')) log.save() # if operation == u'取消' or operation == u'退款' or operation == u'完成退款': if operation == u'完成退款': # 完成退款的前提是要进行退款操作 step_id = OPERATION2STEPID.get(u'发货', None) context.latest_order_id = order.id context.execute_steps(step_id % actor) step_id = OPERATION2STEPID.get(u'完成', None) context.latest_order_id = order.id context.execute_steps(step_id % actor) step_id = OPERATION2STEPID.get(u'退款', None) context.latest_order_id = order.id context.execute_steps(step_id % actor) # if operation == u'退款': # 完成退款的前提是要进行发货和完成操作 # step_id = OPERATION2STEPID.get(u'发货', None) # context.latest_order_id = order.id # context.execute_steps(step_id % actor) # step_id = OPERATION2STEPID.get(u'完成', None) # context.latest_order_id = order.id # context.execute_steps(step_id % actor) step_id = OPERATION2STEPID.get(operation, None) if step_id: context.latest_order_id = order.id context.execute_steps(step_id % actor) elif operation == u'无操作': # 为了兼容之前默认为取消操作所做的处理 pass else: raise context.caller_step_purchase_info = None
def step_impl(context, webapp_user_name, webapp_owner_name): """最近修改: duhao 20160401 weapp中一些BDD仍然需要购买相关的测试场景,在这里调用apiserver的接口实现购买操作 e.g.: { "order_id": "" # 订单号 "ship_area": "", "ship_name": "bill", "ship_address": "", "ship_tel": "", "customer_message": "", "integral": "10", "integral_money": "10", "weizoom_card": [{"card_name":"", "card_pass": ""}], "coupon": "coupon_1", "date": "" # 下单时间 "products": [ { "count": "", "name": "", "promotion": {"name": ""}, integral: "" },... ] } """ if hasattr(context, 'caller_step_purchase_info') and context.caller_step_purchase_info: args = context.caller_step_purchase_info else: args = json.loads(context.text) def __get_current_promotion_id_for_product(product, member_grade_id): promotion_ids = [r.promotion_id for r in ProductHasPromotion.objects.filter(product_id=product.id)] promotions = Promotion.objects.filter(id__in=promotion_ids, status=PROMOTION_STATUS_STARTED).exclude(type__gt=3) if len(promotions) > 0 and (promotions[0].member_grade_id <= 0 or \ promotions[0].member_grade_id == member_grade_id): # 存在促销信息,且促销设置等级对该会员开放 if promotions[0].type != PROMOTION_TYPE_INTEGRAL_SALE: return promotions[0].id return 0 settings = IntegralStrategySttings.objects.filter(webapp_id=context.webapp_id) integral_each_yuan = settings[0].integral_each_yuan member = bdd_util.get_member_for(webapp_user_name, context.webapp_id) group2integralinfo = dict() if webapp_owner_name == u'订单中': is_order_from_shopping_cart = "true" webapp_owner_id = context.webapp_owner_id product_ids = [] product_counts = [] promotion_ids = [] product_model_names = [] products = context.response.context['order'].products integral = 0 integral_group_items = [] for product in products: product_counts.append(str(product.purchase_count)) product_ids.append(str(product.id)) if hasattr(product, 'promotion'): promotion = Promotion.objects.get(name=product.promotion.name) promotion_ids.append(str(promotion.id)) else: promotion_ids.append(str(__get_current_promotion_id_for_product(product_obj, member.grade_id))) product_model_names.append(_get_product_model_ids_from_name(webapp_owner_id, product.model_name)) if hasattr(product, 'integral') and product.integral > 0: integral += product.integral integral_group_items.append('%s_%s' % (product.id, product.model['name'])) if integral: group2integralinfo['-'.join(integral_group_items)] = { "integral": integral, "money": round(integral / integral_each_yuan, 2) } else: is_order_from_shopping_cart = "false" webapp_owner_id = bdd_util.get_user_id_for(webapp_owner_name) product_ids = [] product_counts = [] product_model_names = [] promotion_ids = [] products = args['products'] # integral = 0 # integral_group_items = [] for product in products: product_counts.append(str(product['count'])) product_name = product['name'] product_obj = Product.objects.get(owner_id=webapp_owner_id, name=product_name) product_ids.append(str(product_obj.id)) if product.has_key('promotion'): promotion = Promotion.objects.get(name=product['promotion']['name']) promotion_ids.append(str(promotion.id)) else: promotion_ids.append(str(__get_current_promotion_id_for_product(product_obj, member.grade_id))) _product_model_name = _get_product_model_ids_from_name(webapp_owner_id, product.get('model', None)) product_model_names.append(_product_model_name) if __has_active_integral_sale(product_obj.id): if 'integral' in product and product['integral'] > 0: # integral += product['integral'] # integral_group_items.append('%s_%s' % (product_obj.id, _product_model_name)) group2integralinfo['%s_%s' % (product_obj.id, _product_model_name)] = { "integral": product['integral'], "money": round(product['integral'] / integral_each_yuan, 2) } # if integral: # group2integralinfo['-'.join(integral_group_items)] = { # "integral": integral, # "money": round(integral / integral_each_yuan, 2) # } order_type = args.get('type', 'object') # 处理中文地区转化为id,如果数据库不存在的地区则自动添加该地区 ship_area = get_area_ids(args.get('ship_area')) data = { "webapp_user_name": webapp_user_name, "webapp_owner_name": webapp_owner_name, #参数中携带webapp_user_name和webapp_owner_name,方便apiserver处理 "woid": webapp_owner_id, "is_order_from_shopping_cart": is_order_from_shopping_cart, "product_ids": '_'.join(product_ids), "promotion_ids": '_'.join(promotion_ids), "product_counts": '_'.join(product_counts), "product_model_names": '$'.join(product_model_names), "ship_name": args.get('ship_name', "未知姓名"), "area": ship_area, "ship_id": 0, "ship_address": args.get('ship_address', "长安大街"), "ship_tel": args.get('ship_tel', "11111111111"), "is_use_coupon": "false", "coupon_id": 0, # "coupon_coupon_id": "", "message": args.get('customer_message', ''), "group2integralinfo": json.JSONEncoder().encode(group2integralinfo), "card_name": '', "card_pass": '', "xa-choseInterfaces": PAYNAME2ID.get(args.get("pay_type",u"微信支付"),-1) } if not group2integralinfo: if 'integral' in args and int(args['integral'] > 0): # 整单积分抵扣 # orderIntegralInfo:{"integral":20,"money":"10.00"}" orderIntegralInfo = dict() orderIntegralInfo['integral'] = args['integral'] if 'integral_money' in args: orderIntegralInfo['money'] = args['integral_money'] else: orderIntegralInfo['money'] = round(int(args['integral'])/integral_each_yuan, 2) data["orderIntegralInfo"] = json.JSONEncoder().encode(orderIntegralInfo) if order_type == u'测试购买': data['order_type'] = PRODUCT_TEST_TYPE else: data['order_type'] = order_type if u'weizoom_card' in args: for card in args[u'weizoom_card']: data['card_name'] += card[u'card_name'] + ',' data['card_pass'] += card[u'card_pass'] + ',' #填充商品积分 # for product_model_id, integral in product_integrals: # data['is_use_integral_%s' % product_model_id] = 'on' # data['integral_%s' % product_model_id] = integral #填充优惠券信息 # 根据优惠券规则名称填充优惠券ID coupon = args.get('coupon', None) if coupon: data['is_use_coupon'] = 'true' data['coupon_id'] = coupon access_token = bdd_util.get_access_token(member.id, webapp_owner_id) openid = bdd_util.get_openid(member.id, webapp_owner_id) url = 'http://api.weapp.com/default/mall/order/?_method=put' data['access_token'] = access_token data['openid'] = openid data['woid'] = webapp_owner_id response = requests.post(url, data=data) #response结果为: {"errMsg": "", "code": 200, "data": {"msg": null, "order_id": "20140620180559"}} response_json = json.loads(response.text) context.response = response_json # raise '----------------debug test----------------------' if response_json['code'] == 200: # context.created_order_id为订单ID context.created_order_id = response_json['data']['order_id'] #访问支付结果链接 pay_url_info = response_json['data']['pay_url_info'] pay_type = pay_url_info['type'] del pay_url_info['type'] # if pay_type == 'cod': # pay_url = 'http://api.weapp.com/wapi/pay/pay_result/?_method=put' # data = { # 'pay_interface_type': pay_url_info['pay_interface_type'], # 'order_id': pay_url_info['order_id'], # 'access_token': access_token # } # pay_response = requests.post(url, data=data) # pay_response_json = json.loads(pay_response.text) #同步更新支付时间 if Order.objects.get(order_id=context.created_order_id).status > ORDER_STATUS_CANCEL and args.has_key('payment_time'): Order.objects.filter(order_id=context.created_order_id).update(payment_time=bdd_util.get_datetime_str(args['payment_time'])) else: context.created_order_id = -1 context.response_json = response_json context.server_error_msg = response_json['innerErrMsg'] print("buy_error----------------------------",context.server_error_msg,response) if context.created_order_id != -1: if 'date' in args: Order.objects.filter(order_id=context.created_order_id).update(created_at=bdd_util.get_datetime_str(args['date'])) if 'order_id' in args: db_order = Order.objects.get(order_id=context.created_order_id) db_order.order_id=args['order_id'] db_order.save() if db_order.origin_order_id <0: for order in Order.objects.filter(origin_order_id=db_order.id): order.order_id = '%s^%s' % (args['order_id'], order.order_id.split('^')[1]) order.save() context.created_order_id = args['order_id'] context.product_ids = product_ids context.product_counts = product_counts context.product_model_names = product_model_names context.webapp_owner_name = webapp_owner_name
def step_impl(context, user): user_id = User.objects.get(username=user) if context.table: promotions = [promotion.as_dict() for promotion in context.table] else: promotions = json.loads(context.text) if type(promotions) == dict: # 处理单个积分应用活动创建 promotions = [promotions] for promotion in promotions: product_names = promotion['product_name'].split(',') product_ids = [] for name in product_names: db_product = Product.objects.get(name=name) product_ids.append({'id': db_product.id}) if 'rules' in promotion: rules = promotion['rules'] for rule in rules: if rule.has_key('member_grade'): rule['member_grade_id'] = __get_member_grade( rule, context.webapp_id) else: rules = [{ "member_grade_id": -1, "discount": promotion.get('discount', 100), "discount_money": promotion.get('discount_money', 0.0) }] data = { 'name': promotion['name'], 'promotion_title': promotion.get('promotion_title', ''), 'member_grade': __get_member_grade(promotion, context.webapp_id), 'products': json.dumps(product_ids), 'rules': json.dumps(rules), 'discount': promotion.get('discount', 100), 'discount_money': promotion.get('discount_money', 0.0), 'integral_price': promotion.get('integral_price', 0.0), 'is_permanant_active': str(promotion.get('is_permanant_active', False)).lower(), } if data['is_permanant_active'] != 'true': data['start_date'] = bdd_util.get_datetime_no_second_str( promotion['start_date']), data['end_date'] = bdd_util.get_datetime_no_second_str( promotion['end_date']), url = '/mall2/api/integral_sale/?_method=put' response = context.client.post(url, data) context.response = response if promotion.get('created_at'): models.Promotion.objects.filter( owner_id=context.webapp_owner_id, name=data['name']).update(created_at=bdd_util.get_datetime_str( promotion['created_at'])) bdd_util.assert_api_call_success(response)
def step_impl(context, user, promotion_type): if promotion_type == u"促销": promotion_type = "all" elif promotion_type == u"限时抢购": promotion_type = "flash_sale" elif promotion_type == u"买赠": promotion_type = "premium_sale" elif promotion_type == u"积分应用": promotion_type = "integral_sale" # elif type == u"优惠券": # type = "coupon" url = '/mall2/api/promotion_list/?design_mode=0&version=1&type=%s' % promotion_type if hasattr(context, 'query_param'): if context.query_param.get('product_name'): url += '&name=' + context.query_param['product_name'] if context.query_param.get('bar_code'): url += '&barCode=' + context.query_param['bar_code'] if context.query_param.get('start_date'): url += '&startDate=' + bdd_util.get_datetime_str( context.query_param['start_date'])[:16] if context.query_param.get('end_date'): url += '&endDate=' + bdd_util.get_datetime_str( context.query_param['end_date'])[:16] if context.query_param.get('status', u'全部') != u'全部': if context.query_param['status'] == u'未开始': status = 1 elif context.query_param['status'] == u'进行中': status = 2 elif context.query_param['status'] == u'已结束': context.client.get(url) #先获取一次数据,使status都更新到正常值 status = 3 url += '&promotionStatus=%s' % status response = context.client.get(url) actual = json.loads(response.content)['data']['items'] # 实际数据 for promotion in actual: if promotion['status'] != u'已结束': # 开启这2项操作(实际上在模板中,此时次2项不含hidden属性)。参考 flash_sales.html promotion['actions'] = [u'详情', u'结束'] else: promotion['actions'] = [u'详情', u'删除'] if promotion['promotionTitle'] != '': # 含有促销标题的 promotion['promotion_title'] = promotion['promotionTitle'] if promotion_type == 'integral_sale': for product in promotion['products']: if product['display_price_range'] != '': product['price'] = product['display_price_range'] else: product['price'] = product['display_price'] if product.get('status') and product['status'] == '在售': product['status'] = '' promotion['is_permanant_active'] = str( promotion['detail']['is_permanant_active']).lower() detail = promotion['detail'] rules = detail['rules'] if len(rules) == 1 and rules[0]['member_grade_id'] < 1: rule = rules[0] rule['member_grade'] = u'全部会员' promotion['discount'] = str(rule['discount']) + '%' promotion['discount_money'] = rule['discount_money'] else: promotion['discount'] = detail['discount'].replace(' ', '') promotion['discount_money'] = detail['discount_money'].replace( ' ', '') for rule in rules: rule['member_grade'] = MemberGrade.objects.get( id=rule['member_grade_id']).name promotion['rules'] = rules else: promotion['product_name'] = promotion['product']['name'] if promotion['product']['display_price_range'] != '': promotion['product_price'] = promotion['product'][ 'display_price_range'] else: promotion['product_price'] = promotion['product'][ 'display_price'] promotion['bar_code'] = promotion['product']['bar_code'] promotion['price'] = promotion['product']['display_price'] promotion['stocks'] = promotion['product']['stocks'] if promotion_type == "flash_sale": promotion['promotion_price'] = promotion['detail'][ 'promotion_price'] else: member_grade_id = Promotion.objects.get( id=promotion['id']).member_grade_id try: promotion['member_grade'] = MemberGrade.objects.get( id=member_grade_id).name except MemberGrade.DoesNotExist: webapp_id = bdd_util.get_webapp_id_for(user) promotion['member_grade'] = MemberGrade.get_default_grade( webapp_id).name expected = [] if context.table: expected = [promotion.as_dict() for promotion in context.table] else: expected = json.loads(context.text) # 转化feature中的格式,与actual一致 for promotion in expected: if promotion_type == 'integral_sale': promotion['is_permanant_active'] = str( promotion.get('is_permanant_active', False)).lower() if promotion.has_key('start_date') and promotion.has_key('end_date'): if promotion.get('is_permanant_active', 'false') == 'true': promotion.pop('start_date') promotion.pop('end_date') else: promotion['start_date'] = bdd_util.get_datetime_str( promotion['start_date']) promotion['end_date'] = bdd_util.get_datetime_str( promotion['end_date']) if promotion.get('created_at'): promotion['created_at'] = bdd_util.get_datetime_str( promotion['created_at']) bdd_util.assert_list(expected, actual)
def step_impl(context, user_name): url = '/mall2/api/promotion_list/?design_mode=0&version=1&type=coupon&count_per_page=10&page=1' if hasattr(context, 'query_param'): if context.query_param.get('name'): url += '&name=' + context.query_param['name'] if context.query_param.get('coupon_id'): url += '&couponId='+ context.query_param['coupon_id'] if context.query_param.get('coupon_code'): url += '&couponId='+ context.query_param['coupon_code'] if context.query_param.get('coupon_promotion_type', None): if context.query_param['coupon_promotion_type'] == u'通用券': coupon_promotion_type = 1 elif context.query_param['coupon_promotion_type'] == u'多商品券': coupon_promotion_type = 2 elif context.query_param['coupon_promotion_type'] == u'全部': coupon_promotion_type = -1 url += '&promotionType=all&couponPromotionType=%s' % coupon_promotion_type if context.query_param.get('start_date'): url += '&startDate='+ bdd_util.get_datetime_str(context.query_param['start_date'])[:16] if context.query_param.get('end_date'): url += '&endDate='+ bdd_util.get_datetime_str(context.query_param['end_date'])[:16] if context.query_param.get('promotion_status', u'全部') != u'全部': if context.query_param['promotion_status'] == u'未开始': status = 1 elif context.query_param['promotion_status'] == u'进行中': status = 2 elif context.query_param['promotion_status'] == u'已过期': context.client.get(url) #先获取一次数据,使status都更新到正常值 status = 3 elif context.query_param['promotion_status'] == u'已失效': status = 5 url += '&promotionStatus=%s' % status response = context.client.get(url) coupon_rules = json.loads(response.content)['data']['items'] actual = [] for coupon_rule in coupon_rules: rule = {} rule["name"] = coupon_rule["name"] rule["money"] = coupon_rule["detail"]["money"] rule["remained_count"] = coupon_rule["detail"]["remained_count"] rule["limit_counts"] = coupon_rule["detail"]["limit_counts"] if coupon_rule["detail"]["limit_counts"] != -1 else "无限" if coupon_rule['status'] == '已结束': rule["remained_count"] = 0 rule["use_count"] = coupon_rule["detail"]["use_count"] rule["start_date"] = coupon_rule["start_date"] rule["end_date"] = coupon_rule["end_date"] rule["get_person_count"] = coupon_rule["detail"]["get_person_count"] rule["get_number"] = coupon_rule["detail"]["get_count"] rule['status'] = coupon_rule['status'] rule['note'] = coupon_rule["detail"]['note'] if coupon_rule["detail"]["limit_product"]: rule["type"] = "多商品券" rule['special_product'] = u"查看专属商品" else: rule["type"] = "通用券" rule['special_product'] = u"全部" actual.append(rule) expected = json.loads(context.text) for item in expected: item["start_date"] = "{} 00:00:00".format(bdd_util.get_date_str(item["start_date"])) item["end_date"] = "{} 00:00:00".format(bdd_util.get_date_str(item["end_date"])) if 'status' in item and item['status'] == "已过期": item['status'] = "已结束" bdd_util.assert_list(expected, actual)