def test_get_json_data_as_string(self, project_class, test_utils): _, project = project_class.activate() test_name = test_utils.random_string() json_data_str = '{"foo": "bar"}' test_data.save_json_test_data(project, test_name, json_data_str) json_data = test_data.get_json_test_data_as_string(project, test_name) assert json_data == '{"foo": "bar"}'
def test_save_json_data(self, project_class, test_utils): _, project = project_class.activate() test_name = test_utils.random_string() json_data_str = '[{"foo": "bar"}]' test_data.save_json_test_data(project, test_name, json_data_str) json_data = test_data.get_json_test_data(project, test_name) assert json_data == [{'foo': 'bar'}]
def test_get_json_data_not_list_not_dict(self, project_class, test_utils): """not a list and not a dict""" _, project = project_class.activate() test_name = test_utils.random_string() json_data_str = '"not a dict"' test_data.save_json_test_data(project, test_name, json_data_str) json_data = test_data.get_json_test_data(project, test_name) assert json_data == []
def test_get_json_data_list_of_not_dicts(self, project_class, test_utils): """an element in the list is not a dict""" _, project = project_class.activate() test_name = test_utils.random_string() json_data_str = '[{"foo": "bar"}, "not a dict"]' test_data.save_json_test_data(project, test_name, json_data_str) json_data = test_data.get_json_test_data(project, test_name) assert json_data == []
def test_get_json_data_list_of_dicts(self, project_class, test_utils): """json dict is return as a list of dict""" _, project = project_class.activate() test_name = test_utils.random_string() json_data_str = '[{"foo": "bar"}, {"foo": "etc"}]' test_data.save_json_test_data(project, test_name, json_data_str) json_data = test_data.get_json_test_data(project, test_name) assert json_data == [{'foo': 'bar'}, {'foo': 'etc'}]
def test_get_test_data__csv_and_json(self, project_class, test_utils): """Only csv is returned""" _, project = project_class.activate() test_name = test_utils.create_random_test(project) csv_data = [{'a': ' b'}] json_data = '[{"c": "d"}]' test_data.save_csv_test_data(project, test_name, csv_data) test_data.save_json_test_data(project, test_name, json_data) data = test_data.get_parsed_test_data(project, test_name) assert data == csv_data
def test_get_test_data__json_and_internal(self, project_class, test_utils): """Only json is returned""" _, project = project_class.activate() test_name = test_utils.create_random_test(project) json_data = '[{"c": "d"}]' test_data.save_json_test_data(project, test_name, json_data) test_content = "data = {'e': 'f'}" with open(test.Test(project, test_name).path, 'w+') as f: f.write(test_content) data = test_data.get_parsed_test_data(project, test_name) assert data == json.loads(json_data) # remove json data, internal data is now returned test_data.remove_json_data_if_present(project, test_name) data = test_data.get_parsed_test_data(project, test_name) assert data == [{'e': 'f'}]
def test_get_test_data(self, project_class, test_utils): _, project = project_class.activate() test_name = test_utils.create_random_test(project) csv_data = [{'a': ' b'}] json_data = '[{"c": "d"}]' test_data.save_csv_test_data(project, test_name, csv_data) test_data.save_json_test_data(project, test_name, json_data) test_content = "data = {'e': 'f'}" with open(test.Test(project, test_name).path, 'w+') as f: f.write(test_content) data = test_data.get_test_data(project, test_name) expected = { 'csv': csv_data, 'json': json_data, 'internal': "data = {\n 'e': 'f',\n}" } assert data == expected
def edit_test_code(project, test_name, content, test_data=None): path = Test(project, test_name).path with open(path, 'w', encoding='utf-8') as f: f.write(content) # save test data if test_data is not None: if test_data['csv'] is not None: test_data_module.save_csv_test_data(project, test_name, test_data['csv']) test_data_module.remove_json_data_if_present(project, test_name) else: test_data_module.remove_csv_if_present(project, test_name) if test_data['json'] is not None and test_data['json'].strip(): test_data_module.save_json_test_data(project, test_name, test_data['json']) else: test_data_module.remove_json_data_if_present(project, test_name)
def test_save_json_data_invalid_json(self, project_class, test_utils): _, project = project_class.activate() test_name = test_utils.random_string() json_data_str = '[{"foo": "bar"]' test_data.save_json_test_data(project, test_name, json_data_str) assert not os.path.isfile(test_data.json_file_path(project, test_name))
def edit_test(project, test_name, description, pages, steps, test_data, tags, skip=False): """Save test contents to file""" def _format_description(description): """Format description string to store in test.""" description = description.replace('"', '\\"').replace("'", "\\'") if '\n' in description: desc_lines = description.split('\n') formatted_description = 'description = \'\'\'' for line in desc_lines: formatted_description = formatted_description + '\n' + line formatted_description = formatted_description + '\'\'\'' else: formatted_description = f"description = '{description}'" return formatted_description def _format_tags_string(tags): tags_string = '' for tag in tags: tags_string = tags_string + " '" + tag + "'," tags_string = "[{}]".format(tags_string.strip()[:-1]) return tags_string def _format_page_string(pages): """Format page object string to store in test.""" po_string = '' for page in pages: po_string = po_string + " '" + page + "',\n" + " " * 8 po_string = "[{}]".format(po_string.strip()[:-1]) return po_string def _format_data(test_data): result = '[\n' for data_set in test_data: result += ' {\n' for key, value in data_set.items(): if not value: value = "''" result += f" '{key}': {value},\n" result += ' },\n' result += ']' return result def _format_steps(steps): step_lines = [] for step in steps: if step['type'] == 'function-call': step_action = step['action'].replace(' ', '_') param_str = ', '.join(step['parameters']) step_lines.append(f' {step_action}({param_str})') else: lines = step['code'].splitlines() for line in lines: step_lines.append(f' {line}') return '\n'.join(step_lines) def _print_extra_blank_line(): nonlocal extra_blank_line if extra_blank_line: test_.append('') extra_blank_line = False def _order_hooks(test_hooks): """Put the test hooks in a predefined order""" ordered = {} order_list = [ 'before_test', 'setup', 'before_each', 'after_each', 'after_test', 'teardown' ] for hook_name in order_list: if hook_name in test_hooks: ordered[hook_name] = test_hooks[hook_name] return ordered path = Test(project, test_name).path settings = settings_manager.get_project_settings(project) test_ = [] if not settings['implicit_actions_import']: test_.append('from golem import actions') if not settings['implicit_page_import']: if pages and not settings['implicit_actions_import']: test_.append('') for page in pages: split = page.split('.') top = split.pop() parents = '.'.join(split) parents = f'.{parents}' if parents else '' test_.append( f'from projects.{project}.pages{parents} import {top}') extra_blank_line = False if not settings['implicit_actions_import'] or not settings[ 'implicit_page_import']: extra_blank_line = True if description: _print_extra_blank_line() test_.append('') test_.append(_format_description(description)) if tags: _print_extra_blank_line() test_.append('') test_.append(f'tags = {_format_tags_string(tags)}') if pages and settings['implicit_page_import']: _print_extra_blank_line() test_.append('') test_.append(f'pages = {_format_page_string(pages)}') if test_data['csv'] is not None: test_data_module.save_csv_test_data(project, test_name, test_data['csv']) else: test_data_module.remove_csv_if_present(project, test_name) if test_data['json'] is not None and test_data['json'].strip(): test_data_module.save_json_test_data(project, test_name, test_data['json']) else: test_data_module.remove_json_data_if_present(project, test_name) if test_data['internal'] is not None and test_data['internal'].strip(): _print_extra_blank_line() test_.append('') test_.append(test_data['internal']) if skip: _print_extra_blank_line() test_.append('') if type(skip) is str: skip = f"'{skip}'" test_.append(f'skip = {skip}') if steps['hooks']: ordered_hooks = _order_hooks(steps['hooks']) for hook, hook_steps in ordered_hooks.items(): if hook_steps: test_.append('') test_.append('') test_.append(f'def {hook}(data):') test_.append(_format_steps(hook_steps)) for test_function_name, test_function_steps in steps['tests'].items(): test_.append('') test_.append('') test_.append(f'def {test_function_name}(data):') if test_function_steps: test_.append(_format_steps(test_function_steps)) else: test_.append(' pass') test_.append('') with open(path, 'w', encoding='utf-8') as f: f.write('\n'.join(test_))