def test_adding_sale_price_should_set_on_sale_flag_true(): """ When the sale price of a product is updated, then it should set the field 'on_sale' = True """ # first get a product from db that is not on sale product_helper = ProductsHelper() product_dao = ProductsDAO() rand_product = product_dao.get_random_products_that_are_not_on_sale(1) product_id = rand_product[0]['ID'] # first check the status is False to start with 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'. Unable to use this product for test." # update the sale price of the product sale_price = float( original_info['regular_price']) * 0.75 # sale is 75% of original payload = dict() payload['sale_price'] = str(sale_price) product_helper.call_update_product(product_id, payload=payload) # get the product sale price is updated after_info = product_helper.call_retrieve_product(product_id) assert after_info['sale_price'] == str(sale_price), f"Updated 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']}"
def test_create_1_simple_product(): # generate data for the product payload = dict() payload['name'] = generate_random_string(20) payload['type'] = "simple" payload['regular_price'] = "10.99" # make the call product_helper_obj = ProductHelper() product_response = product_helper_obj.call_create_product( json.dumps(payload)) # verify response is not empty assert product_response, f"Create product API response is empty. Payload is {payload}" assert product_response['name'] == payload['name'], f"Create product API response has unexpected name" \ f"Expected is {payload['name']}" \ f"Actual is {product_response['name']}" # verify the product exists in db products_dao_obj = ProductsDAO() sql_response = products_dao_obj.get_product_by_id(product_response['id']) # match the information in the db with the information in the api call assert payload['name'] == sql_response[0]['post_title'], f"Product name from payload is different from database response" \ f"Payload name is {payload['name']}" \ f"In response from db is {sql_response['name']}" import pdb pdb.set_trace()
def my_orders_smoke_setup(): product_dao = ProductsDAO() rand_product = product_dao.get_random_product_from_db(1) product_id = rand_product[0]['ID'] order_helper = OrdersHelper() info = {'product_id': product_id, 'order_helper': order_helper} return info
def test_update_regular_price_should_update_price(): """ Verifies updating the 'regular_price' field should automatically update the 'price' field. """ # create helper objects and get random product from db product_helper = ProductsHelper() product_dao = ProductsDAO() # for this test the 'sale_price' of the product must be empty. If product has sale price, updating the 'regular_price' # does not update the 'price'. So get a bunch of products and loop untill you find one that is not on sale. If all in # the list are on sale then take random one and update the sale price 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: # take a random product and make it not on sale by setting sale_price='' test_product = random.choice(rand_products) product_id = test_product['ID'] product_helper.call_update_product(product_id, {'sale_price': ''}) # make the update to 'regular_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=payload) # verify the response has the 'price' and 'regular_price' has updated and 'sale_price' is not updated 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 value {rs_update['price']}," \ f"but expected: {new_price}" assert rs_update['regular_price'] == new_price, f"Update product api call response. Updating the 'regular_price' did not " \ f"update in the response. Actual response 'regular_price'={rs_update['price']}," \ f"but expected: {new_price}" # get the product after the update and verify response 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. Actual 'regular_price'={rs_product['price']}," \ f"but expected: {new_price}"
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() # tmp_date = datetime.now() - timedelta(days=x_days_from_today) # after_created_date = tmp_date.strftime('%Y-%M-%dT%H:%m:%S') # make the call payload = dict() payload['after'] = after_created_date rs_api = ProductsHelper().call_list_products(payload) assert rs_api, f"Empty response for 'list products with filer" # 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 of products." \ f"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] ids_diff = list(set(ids_in_api) - set(ids_in_db)) assert not ids_diff, f"List products with filter, product ids in response mismatch in db."
def test_get_products_by_id(): # Get an existing product from db products_dao_obj = ProductsDAO() random_product = products_dao_obj.get_random_product_from_db() random_product_name = random_product[0]['post_name'] random_product_id = random_product[0]['ID'] # Make the call from the api # Get the product with this id prod_helper_obj = ProductHelper() response_product = prod_helper_obj.get_product_by_id(random_product_id) response_product_sku = response_product['sku'] response_product_name = response_product['name'] # Verify the response assert random_product_name.lower() == response_product_name.lower(), f"Get product from db does not match product" \ f"from api using id." \ f"Product from db:" \ f"{random_product_name}" \ f"Product from API:" \ f"{response_product_name}"
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. Id: {rand_product_id}" \ f"Db name: {db_name}, Api name: {api_name}"
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: {payload['name']}, Actual: {product_rs['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']}"