Best Python code snippet using avocado_python
unittest_deprecated.py
Source:unittest_deprecated.py
1import astroid2from pylint.checkers import BaseChecker, DeprecatedMixin3from pylint.interfaces import UNDEFINED, IAstroidChecker4from pylint.testutils import CheckerTestCase, Message5class _DeprecatedChecker(DeprecatedMixin, BaseChecker):6 __implements__ = (IAstroidChecker,)7 name = "deprecated"8 def deprecated_methods(self):9 return {"deprecated_func", ".Deprecated.deprecated_method"}10 def deprecated_modules(self):11 return {"deprecated_module"}12 def deprecated_arguments(self, method):13 if method == "myfunction1":14 # def myfunction1(arg1, deprecated_arg1='spam')15 return ((1, "deprecated_arg1"),)16 if method == "myfunction2":17 # def myfunction2(arg1, deprecated_arg1, arg2='foo', deprecated_arg2='spam'))18 return ((1, "deprecated_arg1"), (3, "deprecated_arg2"))19 if method == "myfunction3":20 # def myfunction1(arg1, *, deprecated_arg1='spam')21 return ((None, "deprecated_arg1"),)22 if method == ".MyClass.mymethod1":23 # def mymethod1(self, arg1, deprecated_arg1=None)24 return ((1, "deprecated_arg1"),)25 if method == ".MyClass.mymethod2":26 # def mymethod2(self, arg1, deprecated_arg1='bar', arg2='foo', deprecated_arg2='spam'))27 return ((1, "deprecated_arg1"), (3, "deprecated_arg2"))28 if method == ".MyClass.mymethod3":29 # def mymethod1(self, arg1, *, deprecated_arg1=None)30 return ((None, "deprecated_arg1"),)31 return ()32class TestDeprecatedChecker(CheckerTestCase):33 CHECKER_CLASS = _DeprecatedChecker34 def test_deprecated_function(self):35 # Tests detecting deprecated function36 node = astroid.extract_node(37 """38 def deprecated_func():39 pass40 deprecated_func()41 """42 )43 with self.assertAddsMessages(44 Message(45 msg_id="deprecated-method",46 args=("deprecated_func",),47 node=node,48 confidence=UNDEFINED,49 )50 ):51 self.checker.visit_call(node)52 def test_deprecated_method(self):53 # Tests detecting deprecated method54 node = astroid.extract_node(55 """56 class Deprecated:57 def deprecated_method():58 pass59 d = Deprecated()60 d.deprecated_method()61 """62 )63 with self.assertAddsMessages(64 Message(65 msg_id="deprecated-method",66 args=("deprecated_method",),67 node=node,68 confidence=UNDEFINED,69 )70 ):71 self.checker.visit_call(node)72 def test_no_message(self):73 # Tests not raising error when no deprecated functions/methods are present.74 node = astroid.extract_node(75 """76 class MyClass:77 def mymethod():78 pass79 MyClass().mymethod()80 def myfunc():81 pass82 myfunc()83 """84 )85 with self.assertNoMessages():86 self.checker.visit_call(node)87 def test_function_deprecated_arg(self):88 # Tests raising error when calling function with deprecated argument89 node = astroid.extract_node(90 """91 def myfunction1(arg1, deprecated_arg1='spam'):92 pass93 myfunction1(None, 'deprecated')94 """95 )96 with self.assertAddsMessages(97 Message(98 msg_id="deprecated-argument",99 args=("deprecated_arg1", "myfunction1"),100 node=node,101 confidence=UNDEFINED,102 )103 ):104 self.checker.visit_call(node)105 def test_function_deprecated_kwarg(self):106 # Tests raising error when calling function with deprecated keyword argument107 node = astroid.extract_node(108 """109 def myfunction1(arg1, deprecated_arg1='spam'):110 pass111 myfunction1(None, deprecated_arg1='deprecated')112 """113 )114 with self.assertAddsMessages(115 Message(116 msg_id="deprecated-argument",117 args=("deprecated_arg1", "myfunction1"),118 node=node,119 confidence=UNDEFINED,120 )121 ):122 self.checker.visit_call(node)123 def test_function_deprecated_not_used(self):124 # Tests raising error when calling function without deprecated argument125 node = astroid.extract_node(126 """127 def myfunction1(arg1, deprecated_arg1='spam'):128 pass129 myfunction1(None)130 """131 )132 with self.assertNoMessages():133 self.checker.visit_call(node)134 def test_function_deprecated_kwarg_only(self):135 # Tests raising error when calling function with deprecated keyword only argument136 node = astroid.extract_node(137 """138 def myfunction3(arg1, *, deprecated_arg1='spam'):139 pass140 myfunction3(None, deprecated_arg1='deprecated')141 """142 )143 with self.assertAddsMessages(144 Message(145 msg_id="deprecated-argument",146 args=("deprecated_arg1", "myfunction3"),147 node=node,148 confidence=UNDEFINED,149 )150 ):151 self.checker.visit_call(node)152 def test_method_deprecated_arg(self):153 # Tests raising error when calling method with deprecated argument154 node = astroid.extract_node(155 """156 class MyClass:157 def mymethod1(self, arg1, deprecated_arg1):158 pass159 MyClass().mymethod1(None, 'deprecated')160 """161 )162 with self.assertAddsMessages(163 Message(164 msg_id="deprecated-argument",165 args=("deprecated_arg1", "mymethod1"),166 node=node,167 confidence=UNDEFINED,168 )169 ):170 self.checker.visit_call(node)171 def test_method_deprecated_kwarg(self):172 # Tests raising error when calling method with deprecated keyword argument173 node = astroid.extract_node(174 """175 class MyClass:176 def mymethod1(self, arg1, deprecated_arg1):177 pass178 MyClass().mymethod1(None, deprecated_arg1='deprecated')179 """180 )181 with self.assertAddsMessages(182 Message(183 msg_id="deprecated-argument",184 args=("deprecated_arg1", "mymethod1"),185 node=node,186 confidence=UNDEFINED,187 )188 ):189 self.checker.visit_call(node)190 def test_method_deprecated_not_used(self):191 # Tests raising error when calling method without deprecated argument192 node = astroid.extract_node(193 """194 class MyClass:195 def mymethod1(self, arg1, deprecated_arg1):196 pass197 MyClass().mymethod1(None)198 """199 )200 with self.assertNoMessages():201 self.checker.visit_call(node)202 def test_method_deprecated_kwarg_only(self):203 # Tests raising error when calling method with deprecated keyword only argument204 node = astroid.extract_node(205 """206 class MyClass:207 def mymethod3(self, arg1, *, deprecated_arg1):208 pass209 MyClass().mymethod3(None, deprecated_arg1='deprecated')210 """211 )212 with self.assertAddsMessages(213 Message(214 msg_id="deprecated-argument",215 args=("deprecated_arg1", "mymethod3"),216 node=node,217 confidence=UNDEFINED,218 )219 ):220 self.checker.visit_call(node)221 def test_function_deprecated_arg_kwargs(self):222 # Tests raising error when calling function with deprecated argument223 # and keyword argument224 node = astroid.extract_node(225 """226 def myfunction2(arg1, deprecated_arg1, arg2='foo', deprecated_arg2='spam'):227 pass228 myfunction2(None, 'deprecated', deprecated_arg2='deprecated')229 """230 )231 with self.assertAddsMessages(232 Message(233 msg_id="deprecated-argument",234 args=("deprecated_arg1", "myfunction2"),235 node=node,236 confidence=UNDEFINED,237 ),238 Message(239 msg_id="deprecated-argument",240 args=("deprecated_arg2", "myfunction2"),241 node=node,242 confidence=UNDEFINED,243 ),244 ):245 self.checker.visit_call(node)246 def test_function_deprecated_kwarg_kwarg(self):247 # Tests raising error when calling function with deprecated keyword arguments248 node = astroid.extract_node(249 """250 def myfunction2(arg1, deprecated_arg1, arg2='foo', deprecated_arg2='spam'):251 pass252 myfunction2(None, deprecated_arg1='deprecated', deprecated_arg2='deprecated')253 """254 )255 with self.assertAddsMessages(256 Message(257 msg_id="deprecated-argument",258 args=("deprecated_arg1", "myfunction2"),259 node=node,260 confidence=UNDEFINED,261 ),262 Message(263 msg_id="deprecated-argument",264 args=("deprecated_arg2", "myfunction2"),265 node=node,266 confidence=UNDEFINED,267 ),268 ):269 self.checker.visit_call(node)270 def test_method_deprecated_arg_kwargs(self):271 # Tests raising error when calling method with deprecated argument272 # and keyword argument273 node = astroid.extract_node(274 """275 class MyClass:276 def mymethod2(self, arg1, deprecated_arg1, arg2='foo', deprecated_arg2='spam'):277 pass278 MyClass().mymethod2(None, 'deprecated', deprecated_arg2='deprecated')279 """280 )281 with self.assertAddsMessages(282 Message(283 msg_id="deprecated-argument",284 args=("deprecated_arg1", "mymethod2"),285 node=node,286 confidence=UNDEFINED,287 ),288 Message(289 msg_id="deprecated-argument",290 args=("deprecated_arg2", "mymethod2"),291 node=node,292 confidence=UNDEFINED,293 ),294 ):295 self.checker.visit_call(node)296 def test_method_deprecated_kwarg_kwarg(self):297 # Tests raising error when calling method with deprecated keyword arguments298 node = astroid.extract_node(299 """300 class MyClass:301 def mymethod2(self, arg1, deprecated_arg1, arg2='foo', deprecated_arg2='spam'):302 pass303 MyClass().mymethod2(None, deprecated_arg1='deprecated', deprecated_arg2='deprecated')304 """305 )306 with self.assertAddsMessages(307 Message(308 msg_id="deprecated-argument",309 args=("deprecated_arg1", "mymethod2"),310 node=node,311 confidence=UNDEFINED,312 ),313 Message(314 msg_id="deprecated-argument",315 args=("deprecated_arg2", "mymethod2"),316 node=node,317 confidence=UNDEFINED,318 ),319 ):320 self.checker.visit_call(node)321 def test_deprecated_module(self):322 # Tests detecting deprecated module323 node = astroid.extract_node(324 """325 import deprecated_module326 """327 )328 with self.assertAddsMessages(329 Message(330 msg_id="deprecated-module",331 args="deprecated_module",332 node=node,333 confidence=UNDEFINED,334 )335 ):336 self.checker.visit_import(node)337 def test_deprecated_module_from(self):338 # Tests detecting deprecated module339 node = astroid.extract_node(340 """341 from deprecated_module import myfunction342 """343 )344 with self.assertAddsMessages(345 Message(346 msg_id="deprecated-module",347 args="deprecated_module",348 node=node,349 confidence=UNDEFINED,350 )351 ):...
db_query_string_format_plugin_tests.py
Source:db_query_string_format_plugin_tests.py
1import astroid2import pylint.testutils3from baseplate.lint import db_query_string_format_plugin4class TestNoCQLStringFormatChecker(pylint.testutils.CheckerTestCase):5 CHECKER_CLASS = db_query_string_format_plugin.NoDbQueryStringFormatChecker6 def test_finds_variable_binop_string_format_query(self):7 assign_node_a, call_node_b = astroid.extract_node(8 """9 query = "select * from %s" % "users" #@10 cassandra_session.execute(query) #@11 """12 )13 self.checker.visit_assign(assign_node_a)14 self.checker.visit_call(call_node_b)15 self.assertAddsMessages(16 pylint.testutils.Message(msg_id="database-query-string-format", node=call_node_b)17 )18 def test_finds_variable_call_string_format_query(self):19 assign_node_a, call_node_b = astroid.extract_node(20 """21 query = "select * from %s".format("users") #@22 cassandra_session.execute(query) #@23 """24 )25 self.checker.visit_assign(assign_node_a)26 self.checker.visit_call(call_node_b)27 self.assertAddsMessages(28 pylint.testutils.Message(msg_id="database-query-string-format", node=call_node_b)29 )30 def test_finds_binop_string_format_query(self):31 call_node_a = astroid.extract_node(32 """33 cassandra_session.execute("select * from %s" % "users") #@34 """35 )36 self.checker.visit_call(call_node_a)37 self.assertAddsMessages(38 pylint.testutils.Message(msg_id="database-query-string-format", node=call_node_a)39 )40 def test_finds_call_string_format_query(self):41 call_node_a = astroid.extract_node(42 """43 cassandra_session.execute("select * from %s".format("users")) #@44 """45 )46 self.checker.visit_call(call_node_a)47 self.assertAddsMessages(48 pylint.testutils.Message(msg_id="database-query-string-format", node=call_node_a)49 )50 def test_ignores_variable_non_string_format_query(self):51 assign_node_a, call_node_b = astroid.extract_node(52 """53 query = "select * from users" #@54 cassandra_session.execute(query) #@55 """56 )57 with self.assertNoMessages():58 self.checker.visit_assign(assign_node_a)59 self.checker.visit_call(call_node_b)60 def test_ignores_non_string_format_query(self):61 call_node_a = astroid.extract_node(62 """63 cassandra_session.execute("select * from users") #@64 """65 )66 with self.assertNoMessages():67 self.checker.visit_call(call_node_a)68 def test_ignores_no_argument(self):69 call_node_a = astroid.extract_node(70 """71 kb.execute() #@72 """73 )74 with self.assertNoMessages():75 self.checker.visit_call(call_node_a)76 def test_variable_reset(self):77 assign_node_a, assign_node_b, call_node_c = astroid.extract_node(78 """79 query = "select * from %s" % "users" #@80 query = "select * from users" #@81 cassandra_session.execute(query) #@82 """83 )84 with self.assertNoMessages():85 self.checker.visit_assign(assign_node_a)86 self.checker.visit_assign(assign_node_b)87 self.checker.visit_call(call_node_c)88 def test_func_variable_reset(self):89 func_node, assign_node_a, assign_node_b, call_node_c = astroid.extract_node(90 """91 def test(cassandra_session): #@92 query = "select * from %s" % "users" #@93 cassandra_session.execute(query)94 def other_test(cassandra_session):95 query = "select * from users" #@96 cassandra_session.execute(query) #@97 """98 )99 with self.assertNoMessages():100 self.checker.visit_assign(assign_node_a)101 self.checker.leave_functiondef(func_node)102 self.checker.visit_assign(assign_node_b)103 self.checker.visit_call(call_node_c)104 def test_class_variable_reset(self):105 class_node, assign_node_a, assign_node_b, call_node_c = astroid.extract_node(106 """107 class Test(): #@108 query = "select * from %s" % "users" #@109 cassandra_session.execute(query)110 query = "select * from users" #@111 cassandra_session.execute(query) #@112 """113 )114 with self.assertNoMessages():115 self.checker.visit_assign(assign_node_a)116 self.checker.leave_classdef(class_node)117 self.checker.visit_assign(assign_node_b)118 self.checker.visit_call(call_node_c)119 def test_module_variable_reset(self):120 mod_node, assign_node_a, assign_node_b, call_node_c = astroid.extract_node(121 """122 import test #@123 query = "select * from %s" % "users" #@124 cassandra_session.execute(query)125 import other_test126 query = "select * from users" #@127 cassandra_session.execute(query) #@128 """129 )130 with self.assertNoMessages():131 self.checker.visit_assign(assign_node_a)132 self.checker.leave_module(mod_node)133 self.checker.visit_assign(assign_node_b)...
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!!