def test_update_on_sale_field_buy_updating_sale_price(): product_helper = ProductsHelper() regular_price = str(random.randint(10, 100)) + '.' + str( random.randint(10, 99)) payload = dict() payload['name'] = generate_random_string(20) payload['type'] = 'simple' payload['regular_price'] = regular_price product_info = product_helper.call_create_product(payload) product_id = product_info['id'] assert not product_info[ 'on_sale'], f"Newly created product should not have 'on_sale = True', Product id: {product_id}" assert not product_info[ 'sale_price'], f"Newly created product should not have value for'sale_price' field" sale_price = float(regular_price) * 0.75 product_helper.call_update_product(product_id, {"sale_price": str(sale_price)}) product_after_update = product_helper.call_retrieve_product(product_id) assert product_after_update['on_sale'], f"Updated 'sale_price' of product, but the 'on_sale' did not set tot 'True'." \ f"Product id: {product_id}" product_helper.call_update_product(product_id, {"sale_price": ''}) product_after_update = product_helper.call_retrieve_product(product_id) assert not product_after_update['on_sale'], f"Updated 'sale_price=''' of product, but the 'on_sale' did not set tot 'False'." \ f"Product id: {product_id}"
def test_list_products_with_filter_after(self): # create data x_days_from_today = 300 _after_created_date = datetime.now().replace( microsecond=0) - timedelta(days=x_days_from_today) after_created_date = _after_created_date.isoformat() payload = dict() payload['after'] = after_created_date # make the call rs_api = ProductsHelper().call_list_products(payload) assert rs_api, f"Empty response for list products with filter" #get data from db db_products = ProductsDAO().get_products_created_after_given_date( after_created_date) #verify response match db assert len(rs_api) == len(db_products), f"List products with filter 'after' returned unexpected number " \ f"of products. Expected: {len(db_products)}, Actual:{len(rs_api)}" ids_in_api = [i['id'] for i in rs_api] ids_in_db = [i['ID'] for i in db_products] idf_diff = list(set(ids_in_api) - set(ids_in_db)) assert not idf_diff, f"List products with filter. Product ids in response mismatch in db."
def test_get_product_by_id(): # get a product (test data) from db rand_product = ProductsDAO().get_random_product_from_db(1) rand_product_id = rand_product[0]['ID'] db_name = rand_product[0]['post_title'] # make the call product_helper = ProductsHelper() rs_api = product_helper.get_product_by_id(rand_product_id) api_name = rs_api['name'] # verify the response assert db_name == api_name, f"Get product by id returned wrong product." \ f"ID: {rand_product_id}," \ f"DB name: {db_name}," \ f"API name: {api_name}."
def my_setup_teardown(): # hard code 50% coupon coupon_code = 'helehf' discount_pct = '50.00' payload = dict() payload['per_page'] = 10 # get a random product for order rand_products = ProductsHelper().call_list_products(payload) rand_product = random.choice(rand_products) info = dict() info['order_helper'] = OrdersHelper() info['coupon_code'] = coupon_code info['discount_pct'] = discount_pct info['product_id'] = rand_product['id'] info['product_price'] = rand_product['price'] return info
def test_update_product_status(): product_helper = ProductsHelper() product_dao = ProductsDAO() rand_products = product_dao.get_random_product_from_db(30) for product in rand_products: product_id = product['ID'] product_data = product_helper.call_retrieve_product(product_id) if product_data['on_sale']: continue else: break else: test_product = random.choices(rand_products) product_id = test_product['ID'] product_helper.call_update_product(product_id, {'sale_price': ''}) new_price = str(random.randint(10, 100)) + '.' + str(random.randint( 10, 99)) payload = dict() payload['regular_price'] = new_price rs_update = product_helper.call_update_product(product_id, payload) assert rs_update['price'] == new_price, f"Update product api call response. Updating the 'regular_price' did not " \ f"update the 'price' field. price field actual {rs_update['price']}, " \ f"price field expected {new_price}" assert rs_update['regular_price'] == new_price, f"Update product api call response. Updating the 'regular_price' did not" \ f"update the 'price' field. price field actual {rs_update['regular_price']}, " \ f"price field expected {new_price}" rs_product = product_helper.call_retrieve_product(product_id) assert rs_product['price'] == new_price, f"Update product api call response. Updating the 'regular_price' did not " \ f"update the 'price' field. Price field actual value {rs_product['price']}," \ f"but expected: {new_price}" assert rs_product['regular_price'] == new_price, f"Update product api call response. Updating the 'regular_price' did not " \ f"update the 'price' field. Price field actual value {rs_product['regular_price']}," \ f"but expected: {new_price}"
def test_create_1_simple_product(): # generate some data payload = dict() payload['name'] = generate_random_string(20) payload['type'] = "simple" payload['regular_price'] = "10.99" # make the call product_rs = ProductsHelper().call_create_product(payload) # verify the response is not empty assert product_rs, f"Create product api response is empty. Payload: {payload}" assert product_rs['name'] == payload['name'], f"Create product api call response has" \ f"unexpected name. Expected: {product_rs['name']}, Actual: {payload['name']}" # verify the product exists in db product_id = product_rs['id'] db_product = ProductsDAO().get_product_by_id(product_id) assert payload['name'] == db_product[0]['post_title'], f"Create product, title in db does not match" \ f"Title in api. DB: {db_product['post_title']}, API: {payload['name']}"
def test_adding_sale_price_should_set_on_sale_flag_true(): product_helper = ProductsHelper() product_dao = ProductsDAO() rand_product = product_dao.get_random_products_that_are_not_on_sale(qty=1) product_id = rand_product[0]['ID'] original_info = product_helper.call_retrieve_product(product_id) assert not original_info['on_sale'], f"Getting test data with 'on_sale=False' but got 'True'." \ f"Unable to use this product for test." sale_price = float(original_info['regular_price']) * 0.75 payload = dict() payload['sale_price'] = str(sale_price) product_helper.call_update_product(product_id, payload=payload) after_info = product_helper.call_retrieve_product(product_id) assert after_info['sale_price'] == str(sale_price), f"Update product 'sale_price' but value did not update." \ f"Product id: {product_id}, Expected sale price: {sale_price}," \ f"Actual sale price: {after_info['sale_price']} "