MCPcopy Create free account
hub / github.com/celery/celery / _patching

Class _patching

t/unit/conftest.py:576–623  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

574
575
576class _patching:
577
578 def __init__(self, monkeypatch, request):
579 self.monkeypatch = monkeypatch
580 self.request = request
581
582 def __getattr__(self, name):
583 return getattr(self.monkeypatch, name)
584
585 def __call__(self, path, value=SENTINEL, name=None,
586 new=MagicMock, **kwargs):
587 value = self._value_or_mock(value, new, name, path, **kwargs)
588 self.monkeypatch.setattr(path, value)
589 return value
590
591 def object(self, target, attribute, *args, **kwargs):
592 return _wrap_context(
593 patch.object(target, attribute, *args, **kwargs),
594 self.request)
595
596 def _value_or_mock(self, value, new, name, path, **kwargs):
597 if value is SENTINEL:
598 value = new(name=name or path.rpartition('.')[2])
599 for k, v in kwargs.items():
600 setattr(value, k, v)
601 return value
602
603 def setattr(self, target, name=SENTINEL, value=SENTINEL, **kwargs):
604 # alias to __call__ with the interface of pytest.monkeypatch.setattr
605 if value is SENTINEL:
606 value, name = name, None
607 return self(target, value, name=name)
608
609 def setitem(self, dic, name, value=SENTINEL, new=MagicMock, **kwargs):
610 # same as pytest.monkeypatch.setattr but default value is MagicMock
611 value = self._value_or_mock(value, new, name, dic, **kwargs)
612 self.monkeypatch.setitem(dic, name, value)
613 return value
614
615 def modules(self, *mods):
616 modules = []
617 for mod in mods:
618 mod = mod.split('.')
619 modules.extend(reversed([
620 '.'.join(mod[:-i] if i else mod) for i in range(len(mod))
621 ]))
622 modules = sorted(set(modules))
623 return _wrap_context(module_context_manager(*modules), self.request)
624
625
626def _wrap_context(context, request):

Callers 1

patchingFunction · 0.85

Calls

no outgoing calls

Tested by 1

patchingFunction · 0.68