Best Python code snippet using lettuce-tools_python
rest_utils.py
Source:rest_utils.py
...18 def prepare_plain_data(self, data):19 try:20 data = self.generate_fixed_lenght_params(data)21 data = self.remove_missing_params(data)22 data = self.infere_datatypes(data)23 return data24 except:25 return None26 def prepare_data(self, step, hash_index):27 try:28 data = step.hashes[hash_index]29 return self.prepare_plain_data(data)30 except:31 return None32 def remove_missing_params(self, data):33 """34 Removes al the data elements tagged with the text [MISSING_PARAM] in lettuce35 :param data: Lettuce step hash entry36 """37 try:38 for item in data.keys():39 if "[MISSING_PARAM]" in data[item]:40 del(data[item])41 finally:42 return data43 def generate_fixed_lenght_params(self, data):44 """45 Generate a fixed length data for elements tagged with the text [LENGHT] in lettuce46 :param data: Lettuce step hash entry47 """48 try:49 seeds = {'STRING': 'a', 'INTEGER': 1}50 for item in data.keys():51 if "_WITH_LENGTH_" in data[item]:52 seed, length = data[item][1:len(data[item]) - 1].split("_WITH_LENGTH_")53 data[item] = seeds[seed] * int(length)54 finally:55 return data56 def infere_datatypes(self, data):57 """58 Transformes data from string to primitive type59 :param data: Data to be transformed60 """61 """ Separate the process of lists, dicts and plain items"""62 try:63 if isinstance(data, dict):64 for item in data:65 try:66 data[item] = int(data[item])67 except:68 try:69 if "[TRUE]" in data[item]:70 data[item] = True71 if "[FALSE]" in data[item]:72 data[item] = False73 else:74 data[item] = float(data[item])75 except:76 continue77 if isinstance(data, list):78 for item in range(1, len(data)):79 try:80 data[item] = int(data[item])81 except:82 try:83 if "[TRUE]" in data[item]:84 data[item] = True85 if "[FALSE]" in data[item]:86 data[item] = False87 else:88 data[item] = float(data[item])89 except:90 continue91 if not isinstance(data, list) and not isinstance(data, dict):92 """ Assumption of single item """93 try:94 data = int(data)95 except:96 try:97 if "[TRUE]" in data:98 data = True99 if "[FALSE]" in data:100 data = False101 else:102 data = float(data)103 except:104 return data105 finally:106 return data107 def generate_attributes(self, attributes_keys, attributes_values):108 """109 Generates the instances attributes dictionary from a list of keys and values in the lettuce step110 :param key: Attributes keys111 :param data: Attributes values112 """113 attributes = {}114 try:115 if "[MALFORMED]" in attributes_keys:116 attributes = [""]117 return attributes118 if "ARRAY_WITH_ITEMS_" in attributes_keys:119 length = attributes_keys[1:len(attributes_keys) - 1].split("_WITH_ITEMS_")[1]120 fixed_attributes = {}121 for value in range(0, int(length)):122 fixed_attributes[str(value)] = str(value)123 return fixed_attributes124 else:125 try:126 attributes = dict(zip(attributes_keys.split(","),\127 attributes_values.split(",")))128 return self.infere_datatypes(attributes)129 except:130 #attributes are a single value that has been infered. Just zip it131 attributes = dict(zip([attributes_keys], [attributes_values]))132 return self.infere_datatypes(attributes)133 except:134 return attributes135 def request_the_resource(self, step, url_template, params_index=-1, headers_index=-1):136 """137 Send a GET request with params to the URL provided, and store the138 result in the world (status code and JSON received, if any).139 :param step: Current Lettuce step.140 :param url_template: Template of the URL where to send the request.141 :param headers_index: Index of the headers information within the step.142 :param params_index: Index of the params information within the step.143 """144 """Get the URL with values from the template."""145 url = self._get_url_with_values(url_template)146 """Get the params to be sent, if any."""...
dataset_utils.py
Source:dataset_utils.py
...11 """12 try:13 data = self.generate_fixed_length_params(data)14 data = self.remove_missing_params(data)15 data = self.infere_datatypes(data)16 return data17 except:18 return None19 def remove_missing_params(self, data):20 """21 Removes all the data elements tagged with the text [MISSING_PARAM] in lettuce22 :param data: Lettuce step hash entry23 :return data without not desired params24 """25 try:26 for item in data.keys():27 if "[MISSING_PARAM]" in data[item]:28 del(data[item])29 finally:30 return data31 def generate_fixed_length_param(self, param):32 """33 Generate a fixed length param if the elements matches the expression34 [<type>_WITH_LENGTH_<length>] in lettuce. E.g.: [STRING_WITH_LENGTH_15]35 :param param: Lettuce param36 :return param with the desired length37 """38 try:39 if "_WITH_LENGTH_" in param:40 if "_ARRAY_WITH_LENGTH_" in param:41 seeds = {'STRING': 'a', 'INTEGER': 1}42 seed, length = param[1:-1].split("_ARRAY_WITH_LENGTH_")43 param = list(seeds[seed] for x in xrange(int(length)))44 elif "JSON_WITH_LENGTH_" in param:45 length = int(param[1:-1].split("JSON_WITH_LENGTH_")[1])46 param = dict((str(x), str(x)) for x in xrange(length))47 else:48 seeds = {'STRING': 'a', 'INTEGER': "1"}49 # The chain to be generated can be just a part of param50 start = param.find("[")51 end = param.find("]")52 seed, length = param[start + 1:end].split("_WITH_LENGTH_")53 generated_part = seeds[seed] * int(length)54 placeholder = "[" + seed + "_WITH_LENGTH_" + length + "]"55 param = param.replace(placeholder, generated_part)56 if seed is "INTEGER":57 param = int(param)58 finally:59 return param60 def generate_fixed_length_params(self, data):61 """62 Generate a fixed length data for the elements that match the expression63 [<type>_WITH_LENGTH_<length>] in lettuce. E.g.: [STRING_WITH_LENTGH_15]64 :param data: Lettuce step hash entry65 :return data with the desired params with the desired length66 """67 try:68 for item in data.keys():69 data[item] = self.generate_fixed_length_param(data[item])70 finally:71 return data72 def infere_datatypes(self, data):73 """74 Process the input data and replace the values in string format with the75 the appropriate primitive type, based on its content76 :param data: list of items, dict of items or single item77 :return processed list of items, dict of items or single item78 """79 """ Separate the process of lists, dicts and plain items"""80 try:81 if isinstance(data, dict): # dict of items82 for key in data:83 data[key] = self._get_item_with_type(data[key])84 elif isinstance(data, list): # list of items85 for index in range(len(data)):86 data[index] = self._get_item_with_type(data[index])...
steps.py
Source:steps.py
...13def i_configure_the_mock_in_url_to_answer_with_status_and_body(step, url, status, body):14 url = world.config["mock"]["base_url"] + "/mock_configurations" + url15 headers = dict()16 headers.update({"Content-Type": "application/json"})17 body = dataset_utils.infere_datatypes(body)18 headers.update({"Content-Length": str(len(json.dumps(body)))})19 response_info = dict()20 response_info.update({"status_code": int(status)})21 response_info.update({"headers": headers})22 response_info.update({"body": body})23 requests.post(url, json.dumps(response_info))24@step(u'I request the mock with a GET to (.*)')25def i_request_the_mock_with_a_get_to_url(step, url):26 url = world.config["mock"]["base_url"] + url27 world.res = requests.get(url)28@step(u'I receive (.*) and (.*)')29def i_receive_status_and_body(step, status_code, body):30 assert int(status_code) == world.res.status_code, \31 "Response http code received [%s] is different to the http code expected [%s] " \...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!