def test_search_text_valid_iso2(self): """ Testing the service for valid results with search text as any valid ISO2CODE """ # Fetch test data and read the ISO2CODE from the first record of the result assert self.fetch_data_and_validate() assert len(self.test_data_list) > 0 item = self.test_data_list[0] iso2code = item['alpha2_code'] # Create URL to test with ISO2CODE # Fetch status code and content from get method url2 = self.url + iso2code status_code, content = self.service.get(url2) assert status_code == HTTPStatus.OK # Check if the JSON format of content is valid # Read the message returned in the content validate(instance=content, schema=Schemas.schema_all) resp = content['RestResponse']['messages'] assert len(resp) == 1 resp_msg = resp[0] # Read the number of records returned total_no_of_records = CommonUtilities.fetch_total_records(resp_msg) assert total_no_of_records != -1 # Verify if the length of results list is same as the no mentioned in messages # Verify if the result list contains the iso2code as text in name,iso2code or iso3code assert len(content['RestResponse']['result']) == int(total_no_of_records) assert self.search_text(content['RestResponse']['result'], iso2code)
def test_get_all_post_method(self): """ Testing the webservice for HTTP POST Method Expectation: Webservice should return HTTP BAD REQUEST """ rand = CommonUtilities.generate_random_string(5) status_code, content = self.service.post(self.url, rand) assert status_code == HTTPStatus.BAD_REQUEST
def test_get_all_invalid_request(self): """ Testing the webservice for an invalid request with additional path information in the URI Expectation: Webservice should return HTTP BAD REQUEST """ rand = CommonUtilities.generate_random_string(5) url = self.url + '/' + rand status_code, content = self.service.get(url) assert status_code == HTTPStatus.BAD_REQUEST
def test_get_iso2_post_method(self): """ Testing to query records with a random invalid iso2Code using HTTP Post Expectation: Service should return a HTTP Bad Request """ # Create invalid ISO2CODE with random string rand = CommonUtilities.generate_random_string(5) url = self.url + rand # Fetch the status code from POST method status_code, content = self.service.post(url, rand) assert status_code == HTTPStatus.BAD_REQUEST
def test_get_all_valid_with_query_params(self): """ Testing the webservice for a valid request with additional query parameters Expectation: Webservice should return HTTP Status 200 OK and a valid json with all records in the content """ rand = CommonUtilities.generate_random_string(5) url = self.url + '?' + rand + '=' + rand status_code, content = self.service.get(url) assert status_code == HTTPStatus.OK # Check if the JSON format is valid validate(instance=content, schema=Schemas.schema_all) # Read the message returned in the content resp = content['RestResponse']['messages'] assert len(resp) == 1 resp_msg = resp[0] # Read the number of records returned total_no_of_records = CommonUtilities.fetch_total_records(resp_msg) assert total_no_of_records != -1 assert len( content['RestResponse']['result']) == int(total_no_of_records)
def test_get_iso3_invalid(self): """ Testing to query records with randomly chosen invalid iso3Codes Expectation: Service should return a valid json as per the defined schema with zero records """ # Create invalid ISO3CODE with random number # Fetch the status code and content from get method rand = CommonUtilities.generate_random_string(5) url = self.url + rand status_code, content = self.service.get(url) assert status_code == HTTPStatus.OK # Check if the JSON format of content is valid # Read the message returned in the content validate(instance=content, schema=Schemas.schema_iso_invalid) resp = content['RestResponse']['messages'] assert len(resp) == 1 resp_msg = resp[0] # Check if we get 'no matching' message assert StringConstants.no_matching in resp_msg
def test_search_text_empty_string(self): """ Testing to query records with empty search text Expectation: Service should return all records """ status_code, content = self.service.get(self.url) assert status_code == HTTPStatus.OK # Check if the JSON format of content is valid as it will return the full list # Read the message returned in the content validate(instance=content, schema=Schemas.schema_all) resp = content['RestResponse']['messages'] assert len(resp) == 1 resp_msg = resp[0] # Read the number of records returned total_no_of_records = CommonUtilities.fetch_total_records(resp_msg) assert total_no_of_records != -1 # Verify the total no of records matches in message and result list assert len(content['RestResponse']['result']) == int(total_no_of_records)
def test_search_text_invalid(self): """ Testing the service with invalid text Expectation: Service should handle the request gracefully and return no matching records """ # Create invalid text with random number # Fetch the status code and content from get method rand = CommonUtilities.generate_random_string() url = self.url + rand status_code, content = self.service.get(url) assert status_code == HTTPStatus.OK # Check if the JSON format of content is valid # Read the message returned in the content validate(instance=content, schema=Schemas.schema_search_invalid) resp = content['RestResponse']['messages'] assert len(resp) == 1 resp_msg = resp[0] # Check if we get 'no matching' message and result list is empty assert StringConstants.no_matching in resp_msg assert not content['RestResponse']['result']
def fetch_data_and_validate(self): """ Method to fetch, validate and store all records for future test cases Return: (bool): return True if method is successful in fetching the test data, false otherwise """ # Fetch data for all records that will be used as test data in further methods # Check if the JSON format of test_content is valid status_code, test_content = self.service.get(self.url_testdata) validate(instance=test_content, schema=Schemas.schema_all) self.test_data_list = test_content['RestResponse']['result'] # Read the message returned in the content resp = test_content['RestResponse']['messages'] if len(resp) == 1: resp_msg = resp[0] else: return False # Read the number of records returned self.total_no_of_records = CommonUtilities.fetch_total_records(resp_msg) if self.total_no_of_records != -1: return True else: return False
def test_generate_random_string_length(self): length = 10 str1 = CommonUtilities.generate_random_string(length) assert len(str1) == length
def test_fetch_total_records_invalid(self): num_records = CommonUtilities.fetch_total_records("Total records") assert num_records is None
def test_fetch_total_records_valid(self): num_records = CommonUtilities.fetch_total_records("Total [4] records") assert int(num_records) == 4