How to use mock_add_spec method in autotest

Best Python code snippet using autotest_python

test_module_record.py

Source:test_module_record.py Github

copy

Full Screen

...18 def test_init(self):19 node = MagicMock()20 node.name = "name"21 node.body = ["body"]22 node.mock_add_spec(ast.Module)23 record = ModuleRecord(node)24 assert record.name == "module"25 assert record.title == ""26 assert record.import_string.value == ""27 def test_create_from_source(self, set_source_path):28 source_path = self.source_path / "path.py"29 source_path.write_text("a = 5\nb = 7")30 import_string = ImportString("my_dir.my_module")31 record = ModuleRecord.create_from_source(source_path, import_string)32 assert isinstance(record, ModuleRecord)33 assert record.import_string == import_string34 assert record.name == "my_module"35 assert record.title == ""36 assert record.source_path == source_path37 assert record.source_lines == ["a = 5", "b = 7"]38 def test_find_record(self):39 node = MagicMock()40 node.name = "name"41 node.body = ["body"]42 node.mock_add_spec(ast.Module)43 record = ModuleRecord(node)44 record.import_string = "module_import"45 record.import_string_map = {"test": "test_object"}46 assert record.find_record("test") == "test_object"47 assert record.find_record("module_import") == record48 assert record.find_record("non_existing") is None49 def test_iter_records(self):50 node = MagicMock()51 node.name = "name"52 node.body = ["body"]53 node.mock_add_spec(ast.Module)54 record = ModuleRecord(node)55 assert list(record.iter_records()) == []56 class_record = MagicMock()57 class_record.name = "class_record"58 class_record.iter_records.return_value = ["class_method"]59 private_class_record = MagicMock()60 private_class_record.name = "private_class_record"61 private_class_record.iter_records.return_value = ["private_class_method"]62 record.class_records = [class_record, private_class_record]63 function_record = MagicMock()64 function_record.name = "function_record"65 private_function_record = MagicMock()66 private_function_record.name = "private_function_record"67 record.function_records = [function_record, private_function_record]68 attribute_record = MagicMock()69 record.attribute_records = [attribute_record]70 assert list(record.iter_records()) == [71 class_record,72 private_class_record,73 function_record,74 private_function_record,75 ]76 record.all_names = ["class_record", "function_record"]77 assert list(record.iter_records()) == [class_record, function_record]78 @patch("handsdown.ast_parser.node_records.module_record.ModuleAnalyzer")79 def test_build_children(self, ModuleAnalyzerMock):80 node = MagicMock()81 node.name = "name"82 node.body = ["body"]83 node.mock_add_spec(ast.Module)84 record = ModuleRecord(node)85 record.import_string = ImportString("my_module")86 record.name = "class_node"87 class_node = MagicMock()88 class_node.name = "ClassNode"89 method_node = MagicMock()90 method_node.name = "class_method"91 method_node.body = ["class_method"]92 method_node.mock_add_spec(ast.FunctionDef)93 class_node.body = [method_node]94 class_node.decorator_list = []95 class_node.bases = []96 class_node.mock_add_spec(ast.ClassDef)97 function_node = MagicMock()98 function_node.decorator_list = []99 function_node.name = "function_node"100 function_node.body = ["function_body"]101 function_node.mock_add_spec(ast.FunctionDef)102 attribute_node = MagicMock()103 attribute_target = MagicMock()104 attribute_target.id = "attribute_target"105 attribute_target.mock_add_spec(ast.Name)106 attribute_node.targets = [attribute_target]107 attribute_node.value = "attribute_value"108 attribute_node.mock_add_spec(ast.Assign)109 import_node = MagicMock()110 import_name = MagicMock()111 import_name.name = "import_name"112 import_name_2 = MagicMock()113 import_name_2.name = "import_name_2"114 import_node.names = [import_name, import_name_2]115 import_node.mock_add_spec(ast.Import)116 ModuleAnalyzerMock().class_nodes = [class_node]117 ModuleAnalyzerMock().function_nodes = [function_node]118 ModuleAnalyzerMock().attribute_nodes = [attribute_node]119 ModuleAnalyzerMock().import_nodes = [import_node]120 assert record.build_children() is None121 assert record.title == "ClassNode"122 assert record.class_records[0].node == class_node123 assert record.function_records[0].node == function_node124 assert record.attribute_records[0].node == attribute_node125 assert record.import_records[0].node == import_node126 assert record.import_records[0].name == "import_name"127 assert record.import_records[1].node == import_node128 assert record.import_records[1].name == "import_name_2"129 assert record.class_records[0].import_string.value == "my_module.ClassNode"130 assert (131 record.class_records[0].method_records[0].import_string.value132 == "my_module.ClassNode.class_method"133 )134 assert record.function_records[0].import_string.value == "my_module.function_node"135 def test_parse(self):136 node = MagicMock()137 node.name = "name"138 node.body = ["body"]139 node.mock_add_spec(ast.Module)140 record = ModuleRecord(node)141 record.source_lines = [142 "# attribute docstring",143 "# attribute docstring 2",144 "# FIXME: ingored",145 "",146 "function_line 1",147 "function_line 2",148 "function body line",149 ]150 attribute_record = MagicMock()151 attribute_record.node.mock_add_spec(ast.Assign)152 attribute_record.node.lineno = 4153 record.attribute_records = [attribute_record]154 method_record = MagicMock()155 method_record.name = "method_record"156 method_record.node.mock_add_spec(ast.FunctionDef)157 method_record_body = MagicMock()158 method_record_body.lineno = 999159 method_record.node.body = [method_record_body]160 method_record.is_classmethod = False161 method_record.is_staticmethod = False162 static_method_record = MagicMock()163 static_method_record.name = "static_method_record"164 static_method_record_body = MagicMock()165 static_method_record_body.lineno = 999166 static_method_record.node.body = [static_method_record_body]167 static_method_record.node.mock_add_spec(ast.FunctionDef)168 static_method_record.is_classmethod = True169 static_method_record.is_staticmethod = False170 class_record = MagicMock()171 class_record.name = "class_record"172 class_record.node.mock_add_spec(ast.ClassDef)173 class_attribute_record = MagicMock()174 class_attribute_record.node.mock_add_spec(ast.Assign)175 class_attribute_record.node.lineno = 999176 class_record.attribute_records = [class_attribute_record]177 class_record.method_records = [method_record, static_method_record]178 record.class_records = [class_record]179 function_record = MagicMock()180 function_record.line_number = 5181 body_mock = MagicMock()182 body_mock.lineno = 7183 function_record.node.body = [body_mock]184 function_record.node.lineno = 7185 function_record.node.mock_add_spec(ast.FunctionDef)186 record.function_records = [function_record]187 assert record.parse() is None188 assert attribute_record.docstring == "attribute docstring\n attribute docstring 2"189 assert class_attribute_record.docstring == ""190 assert method_record.title == "class_record().method_record"191 assert static_method_record.title == "class_record.static_method_record"192 function_record.parse_type_comments.assert_called_once_with(193 ["function_line 1", "function_line 2"]194 )195 def test_render(self):196 node = MagicMock()197 node.name = "name"198 node.body = ["body"]199 node.mock_add_spec(ast.Module)200 record = ModuleRecord(node)201 record.parse()202 record.class_records = ["class_record"]203 record.function_records = ["function_record"]204 record.import_records = ["import_record"]...

Full Screen

Full Screen

test_google_drive_filesystem.py

Source:test_google_drive_filesystem.py Github

copy

Full Screen

...29 assert res.requests[0].directory_id == 'asdfasdsadfasdfasd'30 assert res.is_recursive31 def test_create_a_file(self, mocker):32 google_drive_service = MagicMock(spec=Resource)33 google_drive_service.mock_add_spec(['files'])34 file_resource = MagicMock(spec=Resource)35 file_resource.mock_add_spec(['create'])36 google_drive_service.files.return_value = file_resource37 create_resource = MagicMock(spec=Resource)38 create_resource.mock_add_spec(['execute'])39 file_resource.create.return_value = create_resource40 create_resource.execute.return_value = {41 "id": "asdgasdfasdf",42 "mimeType": "application/text",43 "md5Checksum": "akcklsjfkdlsjfk"44 }45 mocked_upload = MagicMock(spec=MediaFileUpload)46 mocker.patch('googleapiclient.http.MediaFileUpload', return_value=mocked_upload)47 fs = GoogleDriveFilesystem(google_drive_service)48 file = fs.create_file(49 GoogleDriveFile('asdgasdfasdf', 'aa', 'dir', 'application/vnd.google-apps.folder', None),50 'a.txt',51 'tmp.txt'52 )53 assert isinstance(file, GoogleDriveFile)54 file_resource.create.assert_called_once_with(55 body={56 "name": "a.txt",57 "parents": [58 "asdgasdfasdf"59 ]60 },61 media_body=mocked_upload,62 fields='id, mimeType, md5Checksum'63 )64 assert file.file_id == 'asdgasdfasdf'65 assert file.file_path == 'aa/a.txt'66 assert file.file_name == 'a.txt'67 assert file.mime_type == 'application/text'68 assert file.md5_checksum == 'akcklsjfkdlsjfk'69 assert not file.is_dir()70 def test_create_a_directory(self):71 google_drive_service = MagicMock(spec=Resource)72 google_drive_service.mock_add_spec(['files'])73 file_resource = MagicMock(spec=Resource)74 file_resource.mock_add_spec(['create'])75 google_drive_service.files.return_value = file_resource76 create_resource = MagicMock(spec=Resource)77 create_resource.mock_add_spec(['execute'])78 file_resource.create.return_value = create_resource79 create_resource.execute.return_value = {80 "id": "asdgasdfasdf"81 }82 fs = GoogleDriveFilesystem(google_drive_service)83 file = fs.create_directory(84 GoogleDriveFile('asdgasdfasdf', 'aa', 'dir', 'application/vnd.google-apps.folder', None),85 'bb',86 )87 assert isinstance(file, GoogleDriveFile)88 file_resource.create.assert_called_once_with(89 body={90 "name": "bb",91 "mimeType": "application/vnd.google-apps.folder",92 "parents": [93 "asdgasdfasdf"94 ]95 },96 fields='id'97 )98 assert file.file_id == 'asdgasdfasdf'99 assert file.file_path == 'aa/bb'100 assert file.file_name == 'bb'101 assert file.mime_type == 'application/vnd.google-apps.folder'102 assert file.md5_checksum is None103 assert file.is_dir()104 def test_delete_a_file(self):105 google_drive_service = MagicMock(spec=Resource)106 google_drive_service.mock_add_spec(['files'])107 file_resource = MagicMock(spec=Resource)108 file_resource.mock_add_spec(['delete'])109 google_drive_service.files.return_value = file_resource110 delete_resource = MagicMock(spec=Resource)111 delete_resource.mock_add_spec(['execute'])112 file_resource.delete.return_value = delete_resource113 fs = GoogleDriveFilesystem(google_drive_service)114 fs.delete_file('asdfjlasdkfj')115 file_resource.delete.assert_called_once_with(fileId='asdfjlasdkfj')116 def test_get_root_directory(self):117 fs = GoogleDriveFilesystem(MagicMock(spec=Resource))118 file = fs.get_root_dir('asdfasgsdafdasdfas')119 assert file.file_id == 'asdfasgsdafdasdfas'120 assert file.file_path == '.'121 assert file.file_name == 'root'...

Full Screen

Full Screen

test_function_record.py

Source:test_function_record.py Github

copy

Full Screen

...5 def test_init(self):6 node = MagicMock()7 node.name = "name"8 node.body = ["body"]9 node.mock_add_spec(ast.FunctionDef)10 record = FunctionRecord(node, is_method=True)11 assert record.name == "name"12 assert record.title == "name"13 assert record.is_method14 assert not record.is_classmethod15 assert not record.is_staticmethod16 assert record.line_number == 117 record.line_number = 1018 assert record.line_number == 1019 @patch("handsdown.ast_parser.node_records.function_record.FunctionAnalyzer")20 def test_parse(self, FunctionAnalyzerMock):21 node = MagicMock()22 node.name = "name"23 node.body = ["body"]24 node.mock_add_spec(ast.FunctionDef)25 record = FunctionRecord(node, is_method=False)26 argument_1 = MagicMock()27 argument_1.related_names = ["argument_1_related", "argument_1_related_2"]28 argument_2 = MagicMock()29 argument_2.related_names = ["argument_2_related"]30 decorator_1 = MagicMock()31 decorator_1.related_names = ["decorator_1_related", "decorator_1_related_2"]32 decorator_2 = MagicMock()33 decorator_2.mock_add_spec(ast.Name)34 decorator_2.id = "staticmethod"35 decorator_3 = MagicMock()36 decorator_3.mock_add_spec(ast.Name)37 decorator_3.id = "classmethod"38 FunctionAnalyzerMock().argument_records = [argument_1, argument_2]39 FunctionAnalyzerMock().decorator_nodes = [decorator_1, decorator_2, decorator_3]40 FunctionAnalyzerMock().return_type_hint = "return_type_hint"41 record.parse()42 assert record.related_names == {43 "argument_1_related",44 "argument_1_related_2",45 "argument_2_related",46 }47 assert record.is_classmethod48 assert record.is_staticmethod49 @patch("handsdown.ast_parser.node_records.function_record.FunctionAnalyzer")50 def test_parse_type_comments(self, FunctionAnalyzerMock):51 node = MagicMock()52 node.name = "name"53 node.body = ["body"]54 node.mock_add_spec(ast.FunctionDef)55 record = FunctionRecord(node, is_method=False)56 argument_1 = MagicMock()57 argument_1.related_names = ["argument_1_related", "argument_1_related_2"]58 argument_1.line_number = 359 argument_1.type_hint = None60 argument_2 = MagicMock()61 argument_2.related_names = ["argument_2_related"]62 argument_2.line_number = 563 argument_2.type_hint = None64 FunctionAnalyzerMock().argument_records = [argument_1, argument_2]65 FunctionAnalyzerMock().decorator_nodes = []66 FunctionAnalyzerMock().return_type_hint = "return_type_hint"67 record.parse()68 record.line_number = 269 lines = [70 "",71 "",72 "line # type: arg1",73 "line # type:arg2[type, type2]",74 "): # type: (...) -> return",75 ]76 record.parse_type_comments(lines)77 assert argument_1.type_hint.name == "arg1"78 assert argument_2.type_hint.name == "arg2[type, type2]"79 assert record.return_type_hint.name == "return"80 lines = [81 "",82 "",83 "line # type: (extra_arg, new_arg1[type, type2], new_arg2) -> new_return",84 ]85 record.parse_type_comments(lines)86 assert argument_1.type_hint.name == "new_arg1[type, type2]"87 assert argument_2.type_hint.name == "new_arg2"88 assert record.return_type_hint.name == "new_return"89 @patch("handsdown.ast_parser.node_records.function_record.FunctionAnalyzer")90 def test_render(self, FunctionAnalyzerMock):91 node = MagicMock()92 node.name = "name"93 node.body = ["body"]94 node.mock_add_spec(ast.FunctionDef)95 record = FunctionRecord(node, is_method=True)96 argument_1 = MagicMock()97 argument_1.render.return_value = "arg1"98 argument_2 = MagicMock()99 argument_2.render.return_value = "arg3"100 argument_3 = MagicMock()101 argument_3.render.return_value = "arg3"102 decorator_1 = MagicMock()103 decorator_1.mock_add_spec(ast.Name)104 decorator_1.id = "my_deco"105 decorator_1.related_names = ["decorator_1_related", "decorator_1_related_2"]106 decorator_2 = MagicMock()107 decorator_2.mock_add_spec(ast.Name)108 decorator_2.id = "classmethod"109 FunctionAnalyzerMock().argument_records = [argument_1, argument_2, argument_3]110 FunctionAnalyzerMock().decorator_nodes = [decorator_1, decorator_2]111 FunctionAnalyzerMock().return_type_hint = "return_type_hint"112 assert record.render() == "def name()"113 node.mock_add_spec(ast.AsyncFunctionDef)114 record = FunctionRecord(node, is_method=True)...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful