From d0c752b99f3391d58d9ba84a968c6788f9191817 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Sat, 4 Feb 2023 01:18:58 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- conftest.py | 47 +++++++-------- tests/nodes.py | 4 +- tests/template.py | 74 +++++++---------------- tests/test_module2.py | 68 ++++++++++----------- tests/test_module3.py | 70 +++++++++++----------- tests/test_module4.py | 134 +++++++++++++++++++++++------------------- tests/test_module5.py | 96 ++++++++++++++++-------------- 7 files changed, 243 insertions(+), 250 deletions(-) diff --git a/conftest.py b/conftest.py index 5e5a81e9..8bb3104e 100644 --- a/conftest.py +++ b/conftest.py @@ -31,9 +31,7 @@ def __init__(self, file_name, nodes): } if file_name is not None: - path = lambda root, fn: root / "{}.py".format(fn) - # if file_name == "menu" or file_name == "stats": - # full_path = path(ext, file_name) + path = lambda root, fn: root / f"{fn}.py" if file_name == "sensor": full_path = path(Path.cwd(), file_name) else: @@ -85,22 +83,19 @@ def last_line(self): @property def message(self): - return "{} on or around line {} in `{}`.".format( - self.data["message"], self.data["start_pos"], self.data["full_path"] - ) + return f'{self.data["message"]} on or around line {self.data["start_pos"]} in `{self.data["full_path"]}`.' def match(self, template): return Parser(None, list(filter(Query(template).match, self.nodes))) def execute(self, expr): result = Tree(self.nodes).execute(expr) - if isinstance(result, (generator, chain, map)): - process = list(result) - return ( - Parser(None, process[0]) if len(process) == 1 else Parser(None, process) - ) - else: + if not isinstance(result, (generator, chain, map)): return Parser(None, result) + process = list(result) + return ( + Parser(None, process[0]) if len(process) == 1 else Parser(None, process) + ) ex = execute @@ -117,10 +112,16 @@ def assign_(self): return Parser(None, [flatten(self.execute("$.body[@.type is 'Assign']").n)]) def def_args_(self, name): - return Parser(None, [flatten(self.execute("$.body[@.type is 'FunctionDef' and @.name is '{}']".format( - name + return Parser( + None, + [ + flatten( + self.execute( + f"$.body[@.type is 'FunctionDef' and @.name is '{name}']" + ).n + ) + ], ) - ).n)]) def assigns(self): return Parser( @@ -133,16 +134,12 @@ def globals(self, name): def defines(self, name): return self.execute( - "$.body[@.type is 'FunctionDef' and @.name is '{}'].(name, args, body, decorator_list)".format( - name - ) + f"$.body[@.type is 'FunctionDef' and @.name is '{name}'].(name, args, body, decorator_list)" ) def class_(self, name): return self.execute( - "$.body[@.type is 'ClassDef' and @.name is '{}'].(name, args, body)".format( - name - ) + f"$.body[@.type is 'ClassDef' and @.name is '{name}'].(name, args, body)" ) def decorators(self): @@ -155,9 +152,7 @@ def returns_call(self): return Parser(None, [flatten(self.execute("$.body[@.type is 'Return']").n)]) def method(self, name): - return self.execute( - "$..body[@.type is 'FunctionDef' and @.name is '{}']".format(name) - ) + return self.execute(f"$..body[@.type is 'FunctionDef' and @.name is '{name}']") def has_arg(self, name, pos=0): nodes = self.execute("$.args.args.arg").n @@ -173,9 +168,7 @@ def for_(self): def from_imports(self, mod, alias): nodes = self.execute( - "$.body[@.type is 'ImportFrom' and @.module is '{}'].names..name".format( - mod - ) + f"$.body[@.type is 'ImportFrom' and @.module is '{mod}'].names..name" ).n return alias in (nodes if isinstance(nodes, list) else [nodes]) diff --git a/tests/nodes.py b/tests/nodes.py index b4e2a4e6..c469ae91 100644 --- a/tests/nodes.py +++ b/tests/nodes.py @@ -169,9 +169,9 @@ def recurse(t, parent_key=""): recurse(t[i], parent_key + sep + str(i) if parent_key else str(i)) elif isinstance(t, dict): for k, v in t.items(): - if k == "n" or k == "s": + if k in ["n", "s"]: k = "value" - if v == "Str" or v == "NameConstant" or v == "Num": + if v in ["Str", "NameConstant", "Num"]: v = "Constant" recurse(v, parent_key + sep + k if parent_key else k) else: diff --git a/tests/template.py b/tests/template.py index 4fd59bf6..e91c61d0 100644 --- a/tests/template.py +++ b/tests/template.py @@ -47,8 +47,7 @@ def process_directory(self, directory): for match in self.query_file(filepath): yield filepath, match except SyntaxError as e: - warnings.warn( - "Failed to parse {}:\n{}".format(filepath, e)) + warnings.warn(f"Failed to parse {filepath}:\n{e}") class TemplateTransformer(ast.NodeTransformer): @@ -79,11 +78,8 @@ def must_not_exist(self, node, path): raise TemplateMismatch(path, node, "empty") def visit_Name(self, node): - if node.id == self.__WILDCARD_NAME: + if node.id in [self.__WILDCARD_NAME, self.__MULTIWILDCARD_NAME]: return self.must_exist - elif node.id == self.__MULTIWILDCARD_NAME: - return self.must_exist - return NameOrAttr(node.id) def transform_wildcard(self, node, attrname): @@ -136,11 +132,7 @@ def visit_arguments(self, node): ) break else: - if node.args: - args = self.visit_list(node.args) - else: - args = self.must_not_exist - + args = self.visit_list(node.args) if node.args else self.must_not_exist defaults = [ (a.arg, self.visit(d)) for a, d in zip(node.args[-len(node.defaults):], node.defaults) @@ -148,10 +140,7 @@ def visit_arguments(self, node): ] if node.vararg is None: - if positional_final_wildcard: - vararg = None - else: - vararg = self.must_not_exist + vararg = None if positional_final_wildcard else self.must_not_exist else: vararg = self.visit(node.vararg) @@ -167,10 +156,7 @@ def visit_arguments(self, node): ) or any(a.arg == self.__MULTIWILDCARD_NAME for a in node.kwonlyargs) if node.kwarg is None: - if koa_subset: - kwarg = None - else: - kwarg = self.must_not_exist + kwarg = None if koa_subset else self.must_not_exist else: kwarg = self.visit(node.kwarg) @@ -243,9 +229,7 @@ def kwargs_checker(sample_keywords, path): assert_ast_equal( sample_kwargs[k.arg], k.value, path + [k.arg]) else: - raise TemplateMismatch( - path, "(missing)", "keyword arg %s" % k.arg - ) + raise TemplateMismatch(path, "(missing)", f"keyword arg {k.arg}") if template_keywords: node.keywords = kwargs_checker @@ -342,14 +326,12 @@ def __init__(self, front=None, back=None): def __radd__(self, other): if not isinstance(other, list): - raise TypeError( - "Cannot add {} and Middle objects".format(type(other))) + raise TypeError(f"Cannot add {type(other)} and Middle objects") return Middle(other + self.front, self.back) def __add__(self, other): if not isinstance(other, list): - raise TypeError( - "Cannot add Middle and {} objects".format(type(other))) + raise TypeError(f"Cannot add Middle and {type(other)} objects") return Middle(self.front, self.back + other) def __call__(self, sample_list, path): @@ -379,9 +361,7 @@ def __init__(self, path, got, expected): self.got = got def __str__(self): - return ("Mismatch at {}.\n" "Found : {}\n" "Expected: {}").format( - format_path(self.path), self.got, self.expected - ) + return f"Mismatch at {format_path(self.path)}.\nFound : {self.got}\nExpected: {self.expected}" class TemplateNodeTypeMismatch(TemplateMismatch): @@ -391,23 +371,17 @@ def __str__(self): if isinstance(self.expected, ast.AST) else self.expected ) - return "At {}, found {} node instead of {}".format( - format_path(self.path), type(self.got).__name__, expected - ) + return f"At {format_path(self.path)}, found {type(self.got).__name__} node instead of {expected}" class TemplateNodeListMismatch(TemplateMismatch): def __str__(self): - return "At {}, found {} node(s) instead of {}".format( - format_path(self.path), len(self.got), len(self.expected) - ) + return f"At {format_path(self.path)}, found {len(self.got)} node(s) instead of {len(self.expected)}" class TemplatePlainListMismatch(TemplateMismatch): def __str__(self): - return ("At {}, lists differ.\nFound : {}\nExpected: {}").format( - format_path(self.path), self.got, self.expected - ) + return f"At {format_path(self.path)}, lists differ.\nFound : {self.got}\nExpected: {self.expected}" class TemplatePlainObjMismatch(TemplateMismatch): @@ -471,8 +445,7 @@ def __call__(self, sample_node, path): sample_arg, sample_dflt = sample_kwonlyargs[argname] except KeyError: raise TemplateMismatch( - path + - ["kwonlyargs"], "(missing)", "keyword arg %s" % argname + path + ["kwonlyargs"], "(missing)", f"keyword arg {argname}" ) else: assert_ast_equal( @@ -486,8 +459,7 @@ def __call__(self, sample_node, path): if not self.koa_subset: template_kwarg_names = {k.arg for k, d in self.kwonly_args_dflts} - excess_names = set(sample_kwonlyargs) - template_kwarg_names - if excess_names: + if excess_names := set(sample_kwonlyargs) - template_kwarg_names: raise TemplateMismatch( path + ["kwonlyargs"], excess_names, "(not present in template)" @@ -503,7 +475,7 @@ def format_path(path): if isinstance(part, int): formed.append("[%d]" % part) else: - formed.append("." + part) + formed.append(f".{part}") return "".join(formed) @@ -540,11 +512,10 @@ def assert_ast_equal(sample, template, path=None): template_field[0]) ): check_node_list(field_path, sample_field, template_field) - else: - if sample_field != template_field: - raise TemplatePlainListMismatch( - field_path, sample_field, template_field - ) + elif sample_field != template_field: + raise TemplatePlainListMismatch( + field_path, sample_field, template_field + ) elif isinstance(template_field, ast.AST): assert_ast_equal(sample_field, template_field, field_path) @@ -552,10 +523,9 @@ def assert_ast_equal(sample, template, path=None): elif callable(template_field): template_field(sample_field, field_path) - else: - if sample_field != template_field: - raise TemplatePlainObjMismatch( - field_path, sample_field, template_field) + elif sample_field != template_field: + raise TemplatePlainObjMismatch( + field_path, sample_field, template_field) def is_ast_equal(sample, template): diff --git a/tests/test_module2.py b/tests/test_module2.py index ca27940c..4e390f00 100644 --- a/tests/test_module2.py +++ b/tests/test_module2.py @@ -27,9 +27,9 @@ def test_house_info_create_class_module2(parse): my_file = parse(test_file) assert my_file.success, my_file.message - + # my_class = house_info.class_(test_class) - my_class = my_file.query("class {}(): ??".format(test_class)) + my_class = my_file.query(f"class {test_class}(): ??") assert ( my_class.exists() ), "Have you created a class called `{0}` in the `{1}` file?".format(test_class, test_file) @@ -37,9 +37,9 @@ def test_house_info_create_class_module2(parse): my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a constructor called `{}`?".format(test_method) - - + ), f"Are you defining a constructor called `{test_method}`?" + + my_class_arguments = ( my_class.def_args_(test_method).match( { @@ -60,9 +60,9 @@ def test_house_info_create_class_module2(parse): my_class_arguments ), """Are you defining a constructor `{0}` for the `{1}` class? Are you declaring the correct name and number of parameters?""".format(test_method, test_class) - + # debug_test_case(my_method) - + # Check for assignment test_code = ( my_method.assign_().match( @@ -95,9 +95,9 @@ def test_house_info_get_data_by_area_module2(parse): my_file = parse(test_file) assert my_file.success, my_file.message - + # my_class = house_info.class_(test_class) - my_class = my_file.query("class {}(): ??".format(test_class)) + my_class = my_file.query(f"class {test_class}(): ??") assert ( my_class.exists() ), "Have you created a class called `{0}` in the `{1}` file?".format(test_class, test_file) @@ -105,8 +105,8 @@ def test_house_info_get_data_by_area_module2(parse): my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case(my_method) my_class_arguments = ( @@ -137,7 +137,7 @@ def test_house_info_get_data_by_area_module2(parse): ), """Are you defining a method `{0}` for the `{1}` class? Are you declaring the correct name and number of parameters?""".format(test_method, test_class) - + test_code = ( my_method.assign_().match( { @@ -170,9 +170,9 @@ def test_house_info_get_data_by_area_loop_module2(parse): my_file = parse(test_file) assert my_file.success, my_file.message - + # my_class = house_info.class_(test_class) - my_class = my_file.query("class {}(): ??".format(test_class)) + my_class = my_file.query(f"class {test_class}(): ??") assert ( my_class.exists() ), "Have you created a class called `{0}` in the `{1}` file?".format(test_class, test_file) @@ -180,8 +180,8 @@ def test_house_info_get_data_by_area_loop_module2(parse): my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case(my_method) test_code = ( @@ -227,7 +227,7 @@ def test_house_info_get_data_by_area_loop_module2(parse): ), """The length of your filter data when calling `get_data_by_area` is incorrect. Your call should have the first argument as `id`, and the second argument as `rec_area=2 Check the logic inside your loop""" - + home_temp = home_info.get_data_by_area("id", rec_area=-999) assert ( len(home_temp) == 0 @@ -249,9 +249,9 @@ def test_house_info_get_data_by_date_module2(parse): my_file = parse(test_file) assert my_file.success, my_file.message - + # my_class = house_info.class_(test_class) - my_class = my_file.query("class {}(): ??".format(test_class)) + my_class = my_file.query(f"class {test_class}(): ??") assert ( my_class.exists() ), "Have you created a class called `{0}` in the `{1}` file?".format(test_class, test_file) @@ -259,8 +259,8 @@ def test_house_info_get_data_by_date_module2(parse): my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case(my_method) house_info_import = my_file.from_imports( @@ -299,7 +299,7 @@ def test_house_info_get_data_by_date_module2(parse): ), """Are you defining a method `get_data_by_date` with the correct name and number of parameters? Are you setting the third parameter's default value to `date.today()`?""" - + # debug_test_case(my_method) test_code = ( @@ -331,9 +331,9 @@ def test_house_info_get_data_by_date_loop_module2(parse): my_file = parse(test_file) assert my_file.success, my_file.message - + # my_class = house_info.class_(test_class) - my_class = my_file.query("class {}(): ??".format(test_class)) + my_class = my_file.query(f"class {test_class}(): ??") assert ( my_class.exists() ), "Have you created a class called `{0}` in the `{1}` file?".format(test_class, test_file) @@ -341,10 +341,10 @@ def test_house_info_get_data_by_date_loop_module2(parse): my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case(my_method) - + # assert False first_for = ( my_method.for_().match( @@ -427,7 +427,7 @@ def test_house_info_get_data_by_date_loop_module2(parse): Your call should have the first argument as `id`, and the second argument as `datetime` object with the format: "%m/%d/%Y" Check the logic inside your loop""" - + rec_date = datetime.strptime("5/9/2020", "%m/%d/%Y") home_temp = home_info.get_data_by_date("id", rec_date) assert ( @@ -448,16 +448,18 @@ def test_sensor_app_house_info_by_area_module2(parse): # print("\nHouse sensor records for area {} = {}".format(test_area, len(recs))) test_file = "sensor_app" - + my_file = parse(test_file) assert my_file.success, my_file.message - + my_file_import = my_file.from_imports( "house_info", "HouseInfo") - assert my_file_import, "Are you importing `HouseInfo` from `house_info` in `{}` file".format(test_file) + assert ( + my_file_import + ), f"Are you importing `HouseInfo` from `house_info` in `{test_file}` file" # debug_test_case(my_file) - + test_code = ( my_file.assign_().match( { @@ -478,7 +480,7 @@ def test_sensor_app_house_info_by_area_module2(parse): ), """Are you creating an instance of the class `HouseInfo` with `data` list as the initialization argument for the constructor? """ - + test_code = ( my_file.assign_().match( { diff --git a/tests/test_module3.py b/tests/test_module3.py index 5d7371b9..d8c9f968 100644 --- a/tests/test_module3.py +++ b/tests/test_module3.py @@ -7,13 +7,15 @@ def test_temperature_import_module3(parse): # from house_info import HouseInfo test_file = "temperature_info" - + my_file = parse(test_file) assert my_file.success, my_file.message my_file_import = my_file.from_imports( "house_info", "HouseInfo") - assert my_file_import, "Are you importing `HouseInfo` from `house_info` in `{}` file".format(test_file) + assert ( + my_file_import + ), f"Are you importing `HouseInfo` from `house_info` in `{test_file}` file" @pytest.mark.test_temperature_create_class_module3 @@ -39,10 +41,10 @@ def test_temperature_create_class_module3(parse): my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case(my_method) - + my_class_arguments = ( my_class.def_args_(test_method).match( { @@ -65,7 +67,7 @@ def test_temperature_create_class_module3(parse): my_class_arguments ), """Are you defining a method `{0}` for the `{1}` class? Are you declaring the correct name and number of parameters?""".format(test_method, test_class) - + # Check for assignment test_code = ( my_method.assign_().match( @@ -93,7 +95,7 @@ def test_temperature_convert_loop_module3(parse): parent_class = "HouseInfo" test_class = "TemperatureData" test_method = "_convert_data" - + my_file = parse(test_file) assert my_file.success, my_file.message @@ -106,8 +108,8 @@ def test_temperature_convert_loop_module3(parse): my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case(my_method) test_code = ( @@ -152,7 +154,7 @@ def test_temperature_convert_loop_module3(parse): test_code ), """Inside your loop, are you converting `rec` value to integer `base=10` Are you appending it to `recs` list?""" - + test_code= ( my_method.returns_call().match( { @@ -165,19 +167,19 @@ def test_temperature_convert_loop_module3(parse): ) assert ( test_code - ), """Are you returning `recs` list from the `{}` method?""".format(test_method) + ), f"""Are you returning `recs` list from the `{test_method}` method?""" @pytest.mark.test_temperature_by_area_method_module3 def test_temperature_by_area_method_module3(parse): # def get_data_by_area(self, rec_area=0): # data = super().get_data_by_area("temperature", rec_area) - + test_file = "temperature_info" parent_class = "HouseInfo" test_class = "TemperatureData" test_method = "get_data_by_area" - + my_file = parse(test_file) assert my_file.success, my_file.message @@ -188,12 +190,12 @@ def test_temperature_by_area_method_module3(parse): Is your class inheritings the properties of the `{1}` class?""".format(test_class, parent_class) # debug_test_case_class(my_class, test_method) - + my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case(my_method) my_class_arguments = ( @@ -252,12 +254,12 @@ def test_temperature_by_area_method_module3(parse): def test_temperature_by_area_method_return_module3(parse): # ... # return self._convert_data(recs) - + test_file = "temperature_info" parent_class = "HouseInfo" test_class = "TemperatureData" test_method = "get_data_by_area" - + my_file = parse(test_file) assert my_file.success, my_file.message @@ -268,12 +270,12 @@ def test_temperature_by_area_method_return_module3(parse): Is your class inheritings the properties of the `{1}` class?""".format(test_class, parent_class) # debug_test_case_class(my_class, test_method) - + my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case(my_method) test_code = ( @@ -302,19 +304,21 @@ def test_temperature_by_date_method_module3(parse): # from datetime import date # def get_data_by_date(self, rec_date=date.today()): # recs = super().get_data_by_date("temperature", rec_date) - + test_file = "temperature_info" parent_class = "HouseInfo" test_class = "TemperatureData" test_method = "get_data_by_date" - + my_file = parse(test_file) assert my_file.success, my_file.message my_file_import = my_file.from_imports( "datetime", "date") - assert my_file_import, "Are you importing `date` from `datetime` in `{}`".format(test_file) - + assert ( + my_file_import + ), f"Are you importing `date` from `datetime` in `{test_file}`" + my_class = my_file.query("class {0}({1}): ??".format(test_class, parent_class)) assert ( my_class.exists() @@ -322,12 +326,12 @@ def test_temperature_by_date_method_module3(parse): Is your class inheritings the properties of the `{1}` class?""".format(test_class, parent_class) # debug_test_case_class(my_class, test_method) - + my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case(my_method) my_class_arguments = ( @@ -390,12 +394,12 @@ def test_temperature_by_date_method_module3(parse): def test_temperature_by_date_method_return_module3(parse): # ... # return self._convert_data(recs) - + test_file = "temperature_info" parent_class = "HouseInfo" test_class = "TemperatureData" test_method = "get_data_by_date" - + my_file = parse(test_file) assert my_file.success, my_file.message @@ -406,12 +410,12 @@ def test_temperature_by_date_method_return_module3(parse): Is your class inheritings the properties of the `{1}` class?""".format(test_class, parent_class) # debug_test_case_class(my_class, test_method) - + my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case(my_method) test_code = ( diff --git a/tests/test_module4.py b/tests/test_module4.py index 00777bf3..d33e5e7c 100644 --- a/tests/test_module4.py +++ b/tests/test_module4.py @@ -19,8 +19,10 @@ def test_humidity_create_class_module4(parse): my_file_import = my_file.from_imports( "house_info", "HouseInfo") - assert my_file_import, "Are you importing `HouseInfo` from `house_info` in `{}` file".format(test_file) - + assert ( + my_file_import + ), f"Are you importing `HouseInfo` from `house_info` in `{test_file}` file" + my_class = my_file.query("class {0}({1}): ??".format(test_class, parent_class)) assert ( my_class.exists() @@ -30,10 +32,10 @@ def test_humidity_create_class_module4(parse): my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case(my_method) - + my_class_arguments = ( my_class.def_args_(test_method).match( { @@ -56,7 +58,7 @@ def test_humidity_create_class_module4(parse): my_class_arguments ), """Are you defining a method `{0}` for the `{1}` class? Are you declaring the correct name and number of parameters?""".format(test_method, test_class) - + # Check for assignment test_code = ( my_method.assign_().match( @@ -84,7 +86,7 @@ def test_humidity_convert_loop_module4(parse): parent_class = "HouseInfo" test_class = "HumidityData" test_method = "_convert_data" - + my_file = parse(test_file) assert my_file.success, my_file.message @@ -97,8 +99,8 @@ def test_humidity_convert_loop_module4(parse): my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case(my_method) test_code = ( @@ -144,7 +146,7 @@ def test_humidity_convert_loop_module4(parse): ), """Inside your loop, are you converting `rec` value to `float()` and multiplying it by `100`? Are you appending it to `recs` list?""" - + test_code= ( my_method.returns_call().match( { @@ -157,7 +159,7 @@ def test_humidity_convert_loop_module4(parse): ) assert ( test_code - ), """Are you returning `recs` list from the `{}` method?""".format(test_method) + ), f"""Are you returning `recs` list from the `{test_method}` method?""" @pytest.mark.test_humidity_by_area_method_module4 @@ -165,12 +167,12 @@ def test_humidity_by_area_method_module4(parse): # def get_data_by_area(self, rec_area=0): # data = super().get_data_by_area("humidity", rec_area) # return self._convert_data(recs) - + test_file = "humidity_info" parent_class = "HouseInfo" test_class = "HumidityData" test_method = "get_data_by_area" - + my_file = parse(test_file) assert my_file.success, my_file.message @@ -181,12 +183,12 @@ def test_humidity_by_area_method_module4(parse): Is your class inheritings the properties of the `{1}` class?""".format(test_class, parent_class) # debug_test_case_class(my_class, test_method) - + my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case(my_method) my_class_arguments = ( @@ -269,19 +271,21 @@ def test_humidity_by_date_method_module4(parse): # def get_data_by_date(self, rec_date=date.today()): # recs = super().get_data_by_date("humidity", rec_date) # return self._convert_data(recs) - + test_file = "humidity_info" parent_class = "HouseInfo" test_class = "HumidityData" test_method = "get_data_by_date" - + my_file = parse(test_file) assert my_file.success, my_file.message my_file_import = my_file.from_imports( "datetime", "date") - assert my_file_import, "Are you importing `date` from datetime in `{}`".format(test_file) - + assert ( + my_file_import + ), f"Are you importing `date` from datetime in `{test_file}`" + my_class = my_file.query("class {0}({1}): ??".format(test_class, parent_class)) assert ( my_class.exists() @@ -289,12 +293,12 @@ def test_humidity_by_date_method_module4(parse): Is your class inheritings the properties of the `{1}` class?""".format(test_class, parent_class) # debug_test_case_class(my_class, test_method) - + my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case(my_method) my_class_arguments = ( @@ -386,14 +390,16 @@ def test_sensor_app_temp_info_by_area_module4(parse): test_file = "sensor_app" test_class = "HumidityData" - + my_file = parse(test_file) assert my_file.success, my_file.message my_file_import = my_file.from_imports( "humidity_info", "HumidityData") - assert my_file_import, "Are you importing `HumidityData` from `humidity_info` in `{}`".format(test_file) - + assert ( + my_file_import + ), f"Are you importing `HumidityData` from `humidity_info` in `{test_file}`" + # debug_test_case(my_file) test_code = ( @@ -416,7 +422,7 @@ def test_sensor_app_temp_info_by_area_module4(parse): ), """Are you creating an instance of the '{}' class called `humidity_data`? Are you passing `data` as the initialization argument for the constructor? """.format(test_class) - + test_code = ( my_file.assign_().match( { @@ -444,7 +450,9 @@ def test_sensor_app_temp_info_by_area_module4(parse): my_file_import = my_file.from_imports( "statistics", "mean") - assert my_file_import, "Are you importing `mean` from `statistics` in `{}`".format(test_file) + assert ( + my_file_import + ), f"Are you importing `mean` from `statistics` in `{test_file}`" @pytest.mark.test_sensor_app_temp_info_by_date_module4 @@ -505,8 +513,10 @@ def test_particle_create_class_module4(parse): my_file_import = my_file.from_imports( "house_info", "HouseInfo") - assert my_file_import, "Are you importing `HouseInfo` from `house_info` in `{}` file".format(test_file) - + assert ( + my_file_import + ), f"Are you importing `HouseInfo` from `house_info` in `{test_file}` file" + my_class = my_file.query("class {0}({1}): ??".format(test_class, parent_class)) assert ( my_class.exists() @@ -516,10 +526,10 @@ def test_particle_create_class_module4(parse): my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case(my_method) - + my_class_arguments = ( my_class.def_args_(test_method).match( { @@ -542,7 +552,7 @@ def test_particle_create_class_module4(parse): my_class_arguments ), """Are you defining a method `{0}` for the `{1}` class? Are you declaring the correct name and number of parameters?""".format(test_method, test_class) - + # Check for assignment test_code = ( my_method.assign_().match( @@ -570,7 +580,7 @@ def test_particle_convert_loop_module4(parse): parent_class = "HouseInfo" test_class = "ParticleData" test_method = "_convert_data" - + my_file = parse(test_file) assert my_file.success, my_file.message @@ -583,8 +593,8 @@ def test_particle_convert_loop_module4(parse): my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case(my_method) test_code = ( @@ -625,7 +635,7 @@ def test_particle_convert_loop_module4(parse): test_code ), """Inside your loop, are you converting `rec` value to `float()` Are you appending it to `recs` list?""" - + test_code= ( my_method.returns_call().match( { @@ -638,7 +648,7 @@ def test_particle_convert_loop_module4(parse): ) assert ( test_code - ), """Are you returning `recs` list from the `{}` method?""".format(test_method) + ), f"""Are you returning `recs` list from the `{test_method}` method?""" @pytest.mark.test_particle_by_area_and_date_methods_module4 @@ -651,12 +661,12 @@ def test_particle_by_area_and_date_methods_module4(parse): # def get_data_by_date(self, rec_date=date.today()): # recs = super().get_data_by_date("particle", rec_date) # return self._convert_data(recs) - + test_file = "particle_count_info" parent_class = "HouseInfo" test_class = "ParticleData" test_method = "get_data_by_area" - + my_file = parse(test_file) assert my_file.success, my_file.message @@ -667,12 +677,12 @@ def test_particle_by_area_and_date_methods_module4(parse): Is your class inheritings the properties of the `{1}` class?""".format(test_class, parent_class) # debug_test_case_class(my_class, test_method) - + my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case(my_method) my_class_arguments = ( @@ -750,11 +760,13 @@ def test_particle_by_area_and_date_methods_module4(parse): # Now test the get_data_by_date method test_method = "get_data_by_date" - + my_file_import = my_file.from_imports( "datetime", "date") - assert my_file_import, "Are you importing `date` from datetime in `{}`".format(test_file) - + assert ( + my_file_import + ), f"Are you importing `date` from datetime in `{test_file}`" + my_class = my_file.query("class {0}({1}): ??".format(test_class, parent_class)) assert ( my_class.exists() @@ -764,8 +776,8 @@ def test_particle_by_area_and_date_methods_module4(parse): my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + my_class_arguments = ( my_class.def_args_(test_method).match( { @@ -865,11 +877,11 @@ def test_particle_get_concentration_method_module4(parse): my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case_class(my_class, test_method) # debug_test_case(my_method) - + my_class_arguments = ( my_class.def_args_(test_method).match( { @@ -892,7 +904,7 @@ def test_particle_get_concentration_method_module4(parse): my_class_arguments ), """Are you defining a method `{0}` for the `{1}` class? Are you declaring the correct name and number of parameters?""".format(test_method, test_class) - + # Check for assignment test_code = ( my_method.assign_().match( @@ -954,10 +966,10 @@ def test_particle_get_concentration_for_module4(parse): my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case(my_method) - + # Check for assignment test_code = ( my_method.for_().match( @@ -1004,13 +1016,15 @@ def test_sensor_app_particle_info_by_area_module4(parse): test_file = "sensor_app" test_class = "ParticleData" - + my_file = parse(test_file) assert my_file.success, my_file.message my_file_import = my_file.from_imports( "particle_count_info", "ParticleData") - assert my_file_import, "Are you importing `ParticleData` from `particle_count_info` in `{}`".format(test_file) + assert ( + my_file_import + ), f"Are you importing `ParticleData` from `particle_count_info` in `{test_file}`" # debug_test_case(my_file) @@ -1034,7 +1048,7 @@ def test_sensor_app_particle_info_by_area_module4(parse): ), """Are you creating an instance of the '{}' class called `particle_data`? Are you passing `data` as the initialization argument for the constructor? """.format(test_class) - + test_code = ( my_file.assign_().match( { @@ -1059,7 +1073,7 @@ def test_sensor_app_particle_info_by_area_module4(parse): ), """Are you setting `recs` to the method call `get_data_by_area` from the `particle_data` object? Are you passing `rec_area=test_area` as the only argument to the method? """ - + test_code = ( my_file.assign_().match( { diff --git a/tests/test_module5.py b/tests/test_module5.py index 232b0d3b..6140f31d 100644 --- a/tests/test_module5.py +++ b/tests/test_module5.py @@ -19,8 +19,10 @@ def test_energy_create_class_module5(parse): my_file_import = my_file.from_imports( "house_info", "HouseInfo") - - assert my_file_import, "Are you importing `HouseInfo` from `house_info` in `{}` file".format(test_file) + + assert ( + my_file_import + ), f"Are you importing `HouseInfo` from `house_info` in `{test_file}` file" my_class = my_file.query("class {0}({1}): ??".format(test_class, parent_class)) assert ( my_class.exists() @@ -29,7 +31,7 @@ def test_energy_create_class_module5(parse): # debug_test_case_class(my_class, test_method) - + test_code = ( my_class.assign_().match( { @@ -77,16 +79,16 @@ def test_energy_get_energy_method_module5(parse): my_class.exists() ), """Have you created a class called `{0}`? Is your class inheritings the properties of the `{1}` class?""".format(test_class, parent_class) - + # debug_test_case_class(my_class, test_method) my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case(my_method) - + my_class_arguments = ( my_class.def_args_(test_method).match( { @@ -109,7 +111,7 @@ def test_energy_get_energy_method_module5(parse): my_class_arguments ), """Are you defining a method `{0}` for the `{1}` class? Are you declaring the correct name and number of parameters?""".format(test_method, test_class) - + # Check for assignment test_code = ( my_method.assign_().match( @@ -156,7 +158,7 @@ def test_energy_get_energy_method_module5(parse): assert ( test_code ), """Are you converting `energy` by "anding it" with `self.ENERGY_BITS`?""" - + test_code = ( my_method.assign_().match( { @@ -176,7 +178,7 @@ def test_energy_get_energy_method_module5(parse): assert ( test_code ), """Are you converting `energy` by shifting the bits to the right 4 positions?""" - + test_code = ( my_method.returns_call().match( { @@ -189,7 +191,7 @@ def test_energy_get_energy_method_module5(parse): ) assert ( test_code - ), """Are you returning `energy` from the `{}` method?""".format(test_method) + ), f"""Are you returning `energy` from the `{test_method}` method?""" @pytest.mark.test_energy_convert_method_module5 @@ -210,8 +212,10 @@ def test_energy_convert_method_module5(parse): my_file_import = my_file.from_imports( "house_info", "HouseInfo") - - assert my_file_import, "Are you importing `HouseInfo` from `house_info` in `{}` file".format(test_file) + + assert ( + my_file_import + ), f"Are you importing `HouseInfo` from `house_info` in `{test_file}` file" my_class = my_file.query("class {0}({1}): ??".format(test_class, parent_class)) assert ( my_class.exists() @@ -223,10 +227,10 @@ def test_energy_convert_method_module5(parse): my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case(my_method) - + my_class_arguments = ( my_class.def_args_(test_method).match( { @@ -249,7 +253,7 @@ def test_energy_convert_method_module5(parse): my_class_arguments ), """Are you defining a method `{0}` for the `{1}` class? Are you declaring the correct name and number of parameters?""".format(test_method, test_class) - + # Check for assignment test_code = ( my_method.assign_().match( @@ -264,7 +268,7 @@ def test_energy_convert_method_module5(parse): assert ( test_code ), "Are you creating a variable called `recs` set equal to an empty list?" - + test_code = ( my_method.for_().match( { @@ -305,7 +309,7 @@ def test_energy_convert_method_module5(parse): test_code ), """Inside your loop, are you converting `rec` value through `self._rec_energy()` method? Are you appending it to `recs` list?""" - + test_code= ( my_method.returns_call().match( { @@ -318,7 +322,7 @@ def test_energy_convert_method_module5(parse): ) assert ( test_code - ), """Are you returning `recs` list from the `{}` method?""".format(test_method) + ), f"""Are you returning `recs` list from the `{test_method}` method?""" @pytest.mark.test_energy_by_area_and_date_methods_module5 @@ -331,12 +335,12 @@ def test_energy_by_area_and_date_methods_module5(parse): # def get_data_by_date(self, rec_date=date.today()): # recs = super().get_data_by_date("energy_usage", rec_date) # return self._convert_data(recs) - + test_file = "energy_info" parent_class = "HouseInfo" test_class = "EnergyData" test_method = "get_data_by_area" - + my_file = parse(test_file) assert my_file.success, my_file.message @@ -347,12 +351,12 @@ def test_energy_by_area_and_date_methods_module5(parse): Is your class inheritings the properties of the `{1}` class?""".format(test_class, parent_class) # debug_test_case_class(my_class, test_method) - + my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case(my_method) my_class_arguments = ( @@ -429,16 +433,18 @@ def test_energy_by_area_and_date_methods_module5(parse): # Now test get_data_by_date test_method = "get_data_by_date" - + my_file_import = my_file.from_imports( "datetime", "date") - assert my_file_import, "Are you importing `date` from datetime in `{}`".format(test_file) - + assert ( + my_file_import + ), f"Are you importing `date` from datetime in `{test_file}`" + my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case(my_method) my_class_arguments = ( @@ -524,19 +530,21 @@ def test_energy_calculate_usage_method_module5(parse): # def calculate_energy_usage(self, data): # total_energy = sum([field * self.ENERGY_PER_BULB for field in data]) # return total_energy - + test_file = "energy_info" parent_class = "HouseInfo" test_class = "EnergyData" test_method = "calculate_energy_usage" - + my_file = parse(test_file) assert my_file.success, my_file.message my_file_import = my_file.from_imports( "datetime", "date") - assert my_file_import, "Are you importing `date` from datetime in `{}`".format(test_file) - + assert ( + my_file_import + ), f"Are you importing `date` from datetime in `{test_file}`" + my_class = my_file.query("class {0}({1}): ??".format(test_class, parent_class)) assert ( my_class.exists() @@ -544,12 +552,12 @@ def test_energy_calculate_usage_method_module5(parse): Is your class inheritings the properties of the `{1}` class?""".format(test_class, parent_class) # debug_test_case_class(my_class, test_method) - + my_method = my_file.class_(test_class).method(test_method) assert ( my_method.exists() - ), "Are you defining a method called `{}`?".format(test_method) - + ), f"Are you defining a method called `{test_method}`?" + # debug_test_case(my_method) my_class_arguments = ( @@ -611,7 +619,7 @@ def test_energy_calculate_usage_method_module5(parse): Are you setting `field * self.ENERGY_PER_BULB` as you `list comprehension` expression? Are you iterating over `data` in your `list comprehension`? """ - + test_code = ( my_method.returns_call().match( { @@ -624,7 +632,7 @@ def test_energy_calculate_usage_method_module5(parse): ) assert ( test_code - ), """Are you returning a `total_energy` from the `{}` method?""".format(test_method) + ), f"""Are you returning a `total_energy` from the `{test_method}` method?""" @pytest.mark.test_sensor_app_energy_info_by_area_module5 @@ -639,13 +647,15 @@ def test_sensor_app_energy_info_by_area_module5(parse): test_file = "sensor_app" test_class = "EnergyData" - + my_file = parse(test_file) assert my_file.success, my_file.message my_file_import = my_file.from_imports( "energy_info", "EnergyData") - assert my_file_import, "Are you importing `EnergyData` from `energy_info` in `{}`".format(test_file) + assert ( + my_file_import + ), f"Are you importing `EnergyData` from `energy_info` in `{test_file}`" # debug_test_case(my_file) @@ -669,7 +679,7 @@ def test_sensor_app_energy_info_by_area_module5(parse): ), """Are you creating an instance of the '{}' class called `energy_data` Are you passing `data` as the initialization argument for the constructor? """.format(test_class) - + test_code = ( my_file.assign_().match( { @@ -694,7 +704,7 @@ def test_sensor_app_energy_info_by_area_module5(parse): ), """Are you setting `recs` to the method call `get_data_by_area` from the `energy_data` object? Are you passing `rec_area=test_area` as the only argument to the method? """ - + test_code = ( my_file.assign_().match( {