How to use _check_and_set_parent method in autotest

Best Python code snippet using autotest_python

mock.py

Source:mock.py Github

copy

Full Screen

...270 return True271 return False272 def __repr__(self):273 return pprint.pformat(list(self))274def _check_and_set_parent(parent, value, name, new_name):275 if not _is_instance_mock(value):276 return False277 if value._mock_name or value._mock_new_name or value._mock_parent is not None or value._mock_new_parent is not None:278 return False279 _parent = parent280 while _parent is not None:281 if _parent is value:282 return False283 _parent = _parent._mock_new_parent284 if new_name:285 value._mock_new_parent = parent286 value._mock_new_name = new_name287 if name:288 value._mock_parent = parent289 value._mock_name = name290 return True291class Base(object):292 _mock_return_value = DEFAULT293 _mock_side_effect = None294 def __init__(self, *args, **kwargs):295 pass296class NonCallableMock(Base):297 def __new__(cls, *args, **kw):298 new = type(cls.__name__, (cls,), {'__doc__': cls.__doc__})299 instance = object.__new__(new)300 return instance301 def __init__(self, spec = None, wraps = None, name = None, spec_set = None, parent = None, _spec_state = None, _new_name = '', _new_parent = None, **kwargs):302 if _new_parent is None:303 _new_parent = parent304 __dict__ = self.__dict__305 __dict__['_mock_parent'] = parent306 __dict__['_mock_name'] = name307 __dict__['_mock_new_name'] = _new_name308 __dict__['_mock_new_parent'] = _new_parent309 if spec_set is not None:310 spec = spec_set311 spec_set = True312 self._mock_add_spec(spec, spec_set)313 __dict__['_mock_children'] = {}314 __dict__['_mock_wraps'] = wraps315 __dict__['_mock_delegate'] = None316 __dict__['_mock_called'] = False317 __dict__['_mock_call_args'] = None318 __dict__['_mock_call_count'] = 0319 __dict__['_mock_call_args_list'] = _CallList()320 __dict__['_mock_mock_calls'] = _CallList()321 __dict__['method_calls'] = _CallList()322 if kwargs:323 self.configure_mock(**kwargs)324 _super(NonCallableMock, self).__init__(spec, wraps, name, spec_set, parent, _spec_state)325 def attach_mock(self, mock, attribute):326 mock._mock_parent = None327 mock._mock_new_parent = None328 mock._mock_name = ''329 mock._mock_new_name = None330 setattr(self, attribute, mock)331 def mock_add_spec(self, spec, spec_set = False):332 self._mock_add_spec(spec, spec_set)333 def _mock_add_spec(self, spec, spec_set):334 _spec_class = None335 if spec is not None and not _is_list(spec):336 if isinstance(spec, ClassTypes):337 _spec_class = spec338 else:339 _spec_class = _get_class(spec)340 spec = dir(spec)341 __dict__ = self.__dict__342 __dict__['_spec_class'] = _spec_class343 __dict__['_spec_set'] = spec_set344 __dict__['_mock_methods'] = spec345 def __get_return_value(self):346 ret = self._mock_return_value347 if self._mock_delegate is not None:348 ret = self._mock_delegate.return_value349 if ret is DEFAULT:350 ret = self._get_child_mock(_new_parent=self, _new_name='()')351 self.return_value = ret352 return ret353 def __set_return_value(self, value):354 if self._mock_delegate is not None:355 self._mock_delegate.return_value = value356 else:357 self._mock_return_value = value358 _check_and_set_parent(self, value, None, '()')359 __return_value_doc = 'The value to be returned when the mock is called.'360 return_value = property(__get_return_value, __set_return_value, __return_value_doc)361 @property362 def __class__(self):363 if self._spec_class is None:364 return type(self)365 return self._spec_class366 called = _delegating_property('called')367 call_count = _delegating_property('call_count')368 call_args = _delegating_property('call_args')369 call_args_list = _delegating_property('call_args_list')370 mock_calls = _delegating_property('mock_calls')371 def __get_side_effect(self):372 sig = self._mock_delegate373 if sig is None:374 return self._mock_side_effect375 return sig.side_effect376 def __set_side_effect(self, value):377 value = _try_iter(value)378 sig = self._mock_delegate379 if sig is None:380 self._mock_side_effect = value381 else:382 sig.side_effect = value383 side_effect = property(__get_side_effect, __set_side_effect)384 def reset_mock(self):385 self.called = False386 self.call_args = None387 self.call_count = 0388 self.mock_calls = _CallList()389 self.call_args_list = _CallList()390 self.method_calls = _CallList()391 for child in self._mock_children.values():392 if isinstance(child, _SpecState):393 continue394 child.reset_mock()395 ret = self._mock_return_value396 if _is_instance_mock(ret) and ret is not self:397 ret.reset_mock()398 def configure_mock(self, **kwargs):399 for arg, val in sorted(kwargs.items(), key=lambda entry: entry[0].count('.')):400 args = arg.split('.')401 final = args.pop()402 obj = self403 for entry in args:404 obj = getattr(obj, entry)405 setattr(obj, final, val)406 def __getattr__(self, name):407 if name == '_mock_methods':408 raise AttributeError(name)409 elif self._mock_methods is not None:410 if name not in self._mock_methods or name in _all_magics:411 raise AttributeError('Mock object has no attribute %r' % name)412 elif _is_magic(name):413 raise AttributeError(name)414 result = self._mock_children.get(name)415 if result is _deleted:416 raise AttributeError(name)417 elif result is None:418 wraps = None419 if self._mock_wraps is not None:420 wraps = getattr(self._mock_wraps, name)421 result = self._get_child_mock(parent=self, name=name, wraps=wraps, _new_name=name, _new_parent=self)422 self._mock_children[name] = result423 elif isinstance(result, _SpecState):424 result = create_autospec(result.spec, result.spec_set, result.instance, result.parent, result.name)425 self._mock_children[name] = result426 return result427 def __repr__(self):428 _name_list = [self._mock_new_name]429 _parent = self._mock_new_parent430 last = self431 dot = '.'432 if _name_list == ['()']:433 dot = ''434 seen = set()435 while _parent is not None:436 last = _parent437 _name_list.append(_parent._mock_new_name + dot)438 dot = '.'439 if _parent._mock_new_name == '()':440 dot = ''441 _parent = _parent._mock_new_parent442 if id(_parent) in seen:443 break444 seen.add(id(_parent))445 _name_list = list(reversed(_name_list))446 _first = last._mock_name or 'mock'447 if len(_name_list) > 1:448 if _name_list[1] not in ('()', '().'):449 _first += '.'450 _name_list[0] = _first451 name = ''.join(_name_list)452 name_string = ''453 if name not in ('mock', 'mock.'):454 name_string = ' name=%r' % name455 spec_string = ''456 if self._spec_class is not None:457 spec_string = ' spec=%r'458 if self._spec_set:459 spec_string = ' spec_set=%r'460 spec_string = spec_string % self._spec_class.__name__461 return "<%s%s%s id='%s'>" % (type(self).__name__,462 name_string,463 spec_string,464 id(self))465 def __dir__(self):466 extras = self._mock_methods or []467 from_type = dir(type(self))468 from_dict = list(self.__dict__)469 if FILTER_DIR:470 from_type = [ e for e in from_type if not e.startswith('_') ]471 from_dict = [ e for e in from_dict if not e.startswith('_') or _is_magic(e) ]472 return sorted(set(extras + from_type + from_dict + list(self._mock_children)))473 def __setattr__(self, name, value):474 if name in _allowed_names:475 return object.__setattr__(self, name, value)476 if self._spec_set and self._mock_methods is not None and name not in self._mock_methods and name not in self.__dict__:477 raise AttributeError("Mock object has no attribute '%s'" % name)478 elif name in _unsupported_magics:479 msg = 'Attempting to set unsupported magic method %r.' % name480 raise AttributeError(msg)481 elif name in _all_magics:482 if self._mock_methods is not None and name not in self._mock_methods:483 raise AttributeError("Mock object has no attribute '%s'" % name)484 if not _is_instance_mock(value):485 setattr(type(self), name, _get_method(name, value))486 original = value487 value = lambda *args, **kw: original(self, *args, **kw)488 else:489 _check_and_set_parent(self, value, None, name)490 setattr(type(self), name, value)491 self._mock_children[name] = value492 else:493 if name == '__class__':494 self._spec_class = value495 return496 if _check_and_set_parent(self, value, name, name):497 self._mock_children[name] = value498 return object.__setattr__(self, name, value)499 def __delattr__(self, name):500 if name in _all_magics and name in type(self).__dict__:501 delattr(type(self), name)502 if name not in self.__dict__:503 return504 if name in self.__dict__:505 object.__delattr__(self, name)506 obj = self._mock_children.get(name, _missing)507 if obj is _deleted:508 raise AttributeError(name)509 if obj is not _missing:510 del self._mock_children[name]...

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