12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883 |
- # Copyright (C) 2007-2012 Michael Foord & the mock team
- # E-mail: fuzzyman AT voidspace DOT org DOT uk
- # http://www.voidspace.org.uk/python/mock/
-
- import os
- import sys
-
- import six
- import unittest2 as unittest
-
- from mock.tests import support
- from mock.tests.support import SomeClass, is_instance, callable
-
- from mock import (
- NonCallableMock, CallableMixin, patch, sentinel,
- MagicMock, Mock, NonCallableMagicMock, patch,
- DEFAULT, call
- )
- from mock.mock import _patch, _get_target
-
- builtin_string = '__builtin__'
- if six.PY3:
- builtin_string = 'builtins'
- unicode = str
-
- PTModule = sys.modules[__name__]
- MODNAME = '%s.PTModule' % __name__
-
-
- def _get_proxy(obj, get_only=True):
- class Proxy(object):
- def __getattr__(self, name):
- return getattr(obj, name)
- if not get_only:
- def __setattr__(self, name, value):
- setattr(obj, name, value)
- def __delattr__(self, name):
- delattr(obj, name)
- Proxy.__setattr__ = __setattr__
- Proxy.__delattr__ = __delattr__
- return Proxy()
-
-
- # for use in the test
- something = sentinel.Something
- something_else = sentinel.SomethingElse
-
-
- class Foo(object):
- def __init__(self, a):
- pass
- def f(self, a):
- pass
- def g(self):
- pass
- foo = 'bar'
-
- class Bar(object):
- def a(self):
- pass
-
- foo_name = '%s.Foo' % __name__
-
-
- def function(a, b=Foo):
- pass
-
-
- class Container(object):
- def __init__(self):
- self.values = {}
-
- def __getitem__(self, name):
- return self.values[name]
-
- def __setitem__(self, name, value):
- self.values[name] = value
-
- def __delitem__(self, name):
- del self.values[name]
-
- def __iter__(self):
- return iter(self.values)
-
-
-
- class PatchTest(unittest.TestCase):
-
- def assertNotCallable(self, obj, magic=True):
- MockClass = NonCallableMagicMock
- if not magic:
- MockClass = NonCallableMock
-
- self.assertRaises(TypeError, obj)
- self.assertTrue(is_instance(obj, MockClass))
- self.assertFalse(is_instance(obj, CallableMixin))
-
-
- def test_single_patchobject(self):
- class Something(object):
- attribute = sentinel.Original
-
- @patch.object(Something, 'attribute', sentinel.Patched)
- def test():
- self.assertEqual(Something.attribute, sentinel.Patched, "unpatched")
-
- test()
- self.assertEqual(Something.attribute, sentinel.Original,
- "patch not restored")
-
-
- def test_patchobject_with_none(self):
- class Something(object):
- attribute = sentinel.Original
-
- @patch.object(Something, 'attribute', None)
- def test():
- self.assertIsNone(Something.attribute, "unpatched")
-
- test()
- self.assertEqual(Something.attribute, sentinel.Original,
- "patch not restored")
-
-
- def test_multiple_patchobject(self):
- class Something(object):
- attribute = sentinel.Original
- next_attribute = sentinel.Original2
-
- @patch.object(Something, 'attribute', sentinel.Patched)
- @patch.object(Something, 'next_attribute', sentinel.Patched2)
- def test():
- self.assertEqual(Something.attribute, sentinel.Patched,
- "unpatched")
- self.assertEqual(Something.next_attribute, sentinel.Patched2,
- "unpatched")
-
- test()
- self.assertEqual(Something.attribute, sentinel.Original,
- "patch not restored")
- self.assertEqual(Something.next_attribute, sentinel.Original2,
- "patch not restored")
-
-
- def test_object_lookup_is_quite_lazy(self):
- global something
- original = something
- @patch('%s.something' % __name__, sentinel.Something2)
- def test():
- pass
-
- try:
- something = sentinel.replacement_value
- test()
- self.assertEqual(something, sentinel.replacement_value)
- finally:
- something = original
-
-
- def test_patch(self):
- @patch('%s.something' % __name__, sentinel.Something2)
- def test():
- self.assertEqual(PTModule.something, sentinel.Something2,
- "unpatched")
-
- test()
- self.assertEqual(PTModule.something, sentinel.Something,
- "patch not restored")
-
- @patch('%s.something' % __name__, sentinel.Something2)
- @patch('%s.something_else' % __name__, sentinel.SomethingElse)
- def test():
- self.assertEqual(PTModule.something, sentinel.Something2,
- "unpatched")
- self.assertEqual(PTModule.something_else, sentinel.SomethingElse,
- "unpatched")
-
- self.assertEqual(PTModule.something, sentinel.Something,
- "patch not restored")
- self.assertEqual(PTModule.something_else, sentinel.SomethingElse,
- "patch not restored")
-
- # Test the patching and restoring works a second time
- test()
-
- self.assertEqual(PTModule.something, sentinel.Something,
- "patch not restored")
- self.assertEqual(PTModule.something_else, sentinel.SomethingElse,
- "patch not restored")
-
- mock = Mock()
- mock.return_value = sentinel.Handle
- @patch('%s.open' % builtin_string, mock)
- def test():
- self.assertEqual(open('filename', 'r'), sentinel.Handle,
- "open not patched")
- test()
- test()
-
- self.assertNotEqual(open, mock, "patch not restored")
-
-
- def test_patch_class_attribute(self):
- @patch('%s.SomeClass.class_attribute' % __name__,
- sentinel.ClassAttribute)
- def test():
- self.assertEqual(PTModule.SomeClass.class_attribute,
- sentinel.ClassAttribute, "unpatched")
- test()
-
- self.assertIsNone(PTModule.SomeClass.class_attribute,
- "patch not restored")
-
-
- def test_patchobject_with_default_mock(self):
- class Test(object):
- something = sentinel.Original
- something2 = sentinel.Original2
-
- @patch.object(Test, 'something')
- def test(mock):
- self.assertEqual(mock, Test.something,
- "Mock not passed into test function")
- self.assertIsInstance(mock, MagicMock,
- "patch with two arguments did not create a mock")
-
- test()
-
- @patch.object(Test, 'something')
- @patch.object(Test, 'something2')
- def test(this1, this2, mock1, mock2):
- self.assertEqual(this1, sentinel.this1,
- "Patched function didn't receive initial argument")
- self.assertEqual(this2, sentinel.this2,
- "Patched function didn't receive second argument")
- self.assertEqual(mock1, Test.something2,
- "Mock not passed into test function")
- self.assertEqual(mock2, Test.something,
- "Second Mock not passed into test function")
- self.assertIsInstance(mock2, MagicMock,
- "patch with two arguments did not create a mock")
- self.assertIsInstance(mock2, MagicMock,
- "patch with two arguments did not create a mock")
-
- # A hack to test that new mocks are passed the second time
- self.assertNotEqual(outerMock1, mock1, "unexpected value for mock1")
- self.assertNotEqual(outerMock2, mock2, "unexpected value for mock1")
- return mock1, mock2
-
- outerMock1 = outerMock2 = None
- outerMock1, outerMock2 = test(sentinel.this1, sentinel.this2)
-
- # Test that executing a second time creates new mocks
- test(sentinel.this1, sentinel.this2)
-
-
- def test_patch_with_spec(self):
- @patch('%s.SomeClass' % __name__, spec=SomeClass)
- def test(MockSomeClass):
- self.assertEqual(SomeClass, MockSomeClass)
- self.assertTrue(is_instance(SomeClass.wibble, MagicMock))
- self.assertRaises(AttributeError, lambda: SomeClass.not_wibble)
-
- test()
-
-
- def test_patchobject_with_spec(self):
- @patch.object(SomeClass, 'class_attribute', spec=SomeClass)
- def test(MockAttribute):
- self.assertEqual(SomeClass.class_attribute, MockAttribute)
- self.assertTrue(is_instance(SomeClass.class_attribute.wibble,
- MagicMock))
- self.assertRaises(AttributeError,
- lambda: SomeClass.class_attribute.not_wibble)
-
- test()
-
-
- def test_patch_with_spec_as_list(self):
- @patch('%s.SomeClass' % __name__, spec=['wibble'])
- def test(MockSomeClass):
- self.assertEqual(SomeClass, MockSomeClass)
- self.assertTrue(is_instance(SomeClass.wibble, MagicMock))
- self.assertRaises(AttributeError, lambda: SomeClass.not_wibble)
-
- test()
-
-
- def test_patchobject_with_spec_as_list(self):
- @patch.object(SomeClass, 'class_attribute', spec=['wibble'])
- def test(MockAttribute):
- self.assertEqual(SomeClass.class_attribute, MockAttribute)
- self.assertTrue(is_instance(SomeClass.class_attribute.wibble,
- MagicMock))
- self.assertRaises(AttributeError,
- lambda: SomeClass.class_attribute.not_wibble)
-
- test()
-
-
- def test_nested_patch_with_spec_as_list(self):
- # regression test for nested decorators
- @patch('%s.open' % builtin_string)
- @patch('%s.SomeClass' % __name__, spec=['wibble'])
- def test(MockSomeClass, MockOpen):
- self.assertEqual(SomeClass, MockSomeClass)
- self.assertTrue(is_instance(SomeClass.wibble, MagicMock))
- self.assertRaises(AttributeError, lambda: SomeClass.not_wibble)
- test()
-
-
- def test_patch_with_spec_as_boolean(self):
- @patch('%s.SomeClass' % __name__, spec=True)
- def test(MockSomeClass):
- self.assertEqual(SomeClass, MockSomeClass)
- # Should not raise attribute error
- MockSomeClass.wibble
-
- self.assertRaises(AttributeError, lambda: MockSomeClass.not_wibble)
-
- test()
-
-
- def test_patch_object_with_spec_as_boolean(self):
- @patch.object(PTModule, 'SomeClass', spec=True)
- def test(MockSomeClass):
- self.assertEqual(SomeClass, MockSomeClass)
- # Should not raise attribute error
- MockSomeClass.wibble
-
- self.assertRaises(AttributeError, lambda: MockSomeClass.not_wibble)
-
- test()
-
-
- def test_patch_class_acts_with_spec_is_inherited(self):
- @patch('%s.SomeClass' % __name__, spec=True)
- def test(MockSomeClass):
- self.assertTrue(is_instance(MockSomeClass, MagicMock))
- instance = MockSomeClass()
- self.assertNotCallable(instance)
- # Should not raise attribute error
- instance.wibble
-
- self.assertRaises(AttributeError, lambda: instance.not_wibble)
-
- test()
-
-
- def test_patch_with_create_mocks_non_existent_attributes(self):
- @patch('%s.frooble' % builtin_string, sentinel.Frooble, create=True)
- def test():
- self.assertEqual(frooble, sentinel.Frooble)
-
- test()
- self.assertRaises(NameError, lambda: frooble)
-
-
- def test_patchobject_with_create_mocks_non_existent_attributes(self):
- @patch.object(SomeClass, 'frooble', sentinel.Frooble, create=True)
- def test():
- self.assertEqual(SomeClass.frooble, sentinel.Frooble)
-
- test()
- self.assertFalse(hasattr(SomeClass, 'frooble'))
-
-
- def test_patch_wont_create_by_default(self):
- try:
- @patch('%s.frooble' % builtin_string, sentinel.Frooble)
- def test():
- self.assertEqual(frooble, sentinel.Frooble)
-
- test()
- except AttributeError:
- pass
- else:
- self.fail('Patching non existent attributes should fail')
-
- self.assertRaises(NameError, lambda: frooble)
-
-
- def test_patchobject_wont_create_by_default(self):
- try:
- @patch.object(SomeClass, 'ord', sentinel.Frooble)
- def test():
- self.fail('Patching non existent attributes should fail')
-
- test()
- except AttributeError:
- pass
- else:
- self.fail('Patching non existent attributes should fail')
- self.assertFalse(hasattr(SomeClass, 'ord'))
-
-
- def test_patch_builtins_without_create(self):
- @patch(__name__+'.ord')
- def test_ord(mock_ord):
- mock_ord.return_value = 101
- return ord('c')
-
- @patch(__name__+'.open')
- def test_open(mock_open):
- m = mock_open.return_value
- m.read.return_value = 'abcd'
-
- fobj = open('doesnotexists.txt')
- data = fobj.read()
- fobj.close()
- return data
-
- self.assertEqual(test_ord(), 101)
- self.assertEqual(test_open(), 'abcd')
-
-
- def test_patch_with_static_methods(self):
- class Foo(object):
- @staticmethod
- def woot():
- return sentinel.Static
-
- @patch.object(Foo, 'woot', staticmethod(lambda: sentinel.Patched))
- def anonymous():
- self.assertEqual(Foo.woot(), sentinel.Patched)
- anonymous()
-
- self.assertEqual(Foo.woot(), sentinel.Static)
-
-
- def test_patch_local(self):
- foo = sentinel.Foo
- @patch.object(sentinel, 'Foo', 'Foo')
- def anonymous():
- self.assertEqual(sentinel.Foo, 'Foo')
- anonymous()
-
- self.assertEqual(sentinel.Foo, foo)
-
-
- def test_patch_slots(self):
- class Foo(object):
- __slots__ = ('Foo',)
-
- foo = Foo()
- foo.Foo = sentinel.Foo
-
- @patch.object(foo, 'Foo', 'Foo')
- def anonymous():
- self.assertEqual(foo.Foo, 'Foo')
- anonymous()
-
- self.assertEqual(foo.Foo, sentinel.Foo)
-
-
- def test_patchobject_class_decorator(self):
- class Something(object):
- attribute = sentinel.Original
-
- class Foo(object):
- def test_method(other_self):
- self.assertEqual(Something.attribute, sentinel.Patched,
- "unpatched")
- def not_test_method(other_self):
- self.assertEqual(Something.attribute, sentinel.Original,
- "non-test method patched")
-
- Foo = patch.object(Something, 'attribute', sentinel.Patched)(Foo)
-
- f = Foo()
- f.test_method()
- f.not_test_method()
-
- self.assertEqual(Something.attribute, sentinel.Original,
- "patch not restored")
-
-
- def test_patch_class_decorator(self):
- class Something(object):
- attribute = sentinel.Original
-
- class Foo(object):
- def test_method(other_self, mock_something):
- self.assertEqual(PTModule.something, mock_something,
- "unpatched")
- def not_test_method(other_self):
- self.assertEqual(PTModule.something, sentinel.Something,
- "non-test method patched")
- Foo = patch('%s.something' % __name__)(Foo)
-
- f = Foo()
- f.test_method()
- f.not_test_method()
-
- self.assertEqual(Something.attribute, sentinel.Original,
- "patch not restored")
- self.assertEqual(PTModule.something, sentinel.Something,
- "patch not restored")
-
-
- def test_patchobject_twice(self):
- class Something(object):
- attribute = sentinel.Original
- next_attribute = sentinel.Original2
-
- @patch.object(Something, 'attribute', sentinel.Patched)
- @patch.object(Something, 'attribute', sentinel.Patched)
- def test():
- self.assertEqual(Something.attribute, sentinel.Patched, "unpatched")
-
- test()
-
- self.assertEqual(Something.attribute, sentinel.Original,
- "patch not restored")
-
-
- def test_patch_dict(self):
- foo = {'initial': object(), 'other': 'something'}
- original = foo.copy()
-
- @patch.dict(foo)
- def test():
- foo['a'] = 3
- del foo['initial']
- foo['other'] = 'something else'
-
- test()
-
- self.assertEqual(foo, original)
-
- @patch.dict(foo, {'a': 'b'})
- def test():
- self.assertEqual(len(foo), 3)
- self.assertEqual(foo['a'], 'b')
-
- test()
-
- self.assertEqual(foo, original)
-
- @patch.dict(foo, [('a', 'b')])
- def test():
- self.assertEqual(len(foo), 3)
- self.assertEqual(foo['a'], 'b')
-
- test()
-
- self.assertEqual(foo, original)
-
-
- def test_patch_dict_with_container_object(self):
- foo = Container()
- foo['initial'] = object()
- foo['other'] = 'something'
-
- original = foo.values.copy()
-
- @patch.dict(foo)
- def test():
- foo['a'] = 3
- del foo['initial']
- foo['other'] = 'something else'
-
- test()
-
- self.assertEqual(foo.values, original)
-
- @patch.dict(foo, {'a': 'b'})
- def test():
- self.assertEqual(len(foo.values), 3)
- self.assertEqual(foo['a'], 'b')
-
- test()
-
- self.assertEqual(foo.values, original)
-
-
- def test_patch_dict_with_clear(self):
- foo = {'initial': object(), 'other': 'something'}
- original = foo.copy()
-
- @patch.dict(foo, clear=True)
- def test():
- self.assertEqual(foo, {})
- foo['a'] = 3
- foo['other'] = 'something else'
-
- test()
-
- self.assertEqual(foo, original)
-
- @patch.dict(foo, {'a': 'b'}, clear=True)
- def test():
- self.assertEqual(foo, {'a': 'b'})
-
- test()
-
- self.assertEqual(foo, original)
-
- @patch.dict(foo, [('a', 'b')], clear=True)
- def test():
- self.assertEqual(foo, {'a': 'b'})
-
- test()
-
- self.assertEqual(foo, original)
-
-
- def test_patch_dict_with_container_object_and_clear(self):
- foo = Container()
- foo['initial'] = object()
- foo['other'] = 'something'
-
- original = foo.values.copy()
-
- @patch.dict(foo, clear=True)
- def test():
- self.assertEqual(foo.values, {})
- foo['a'] = 3
- foo['other'] = 'something else'
-
- test()
-
- self.assertEqual(foo.values, original)
-
- @patch.dict(foo, {'a': 'b'}, clear=True)
- def test():
- self.assertEqual(foo.values, {'a': 'b'})
-
- test()
-
- self.assertEqual(foo.values, original)
-
-
- def test_name_preserved(self):
- foo = {}
-
- @patch('%s.SomeClass' % __name__, object())
- @patch('%s.SomeClass' % __name__, object(), autospec=True)
- @patch.object(SomeClass, object())
- @patch.dict(foo)
- def some_name():
- pass
-
- self.assertEqual(some_name.__name__, 'some_name')
-
-
- def test_patch_with_exception(self):
- foo = {}
-
- @patch.dict(foo, {'a': 'b'})
- def test():
- raise NameError('Konrad')
- try:
- test()
- except NameError:
- pass
- else:
- self.fail('NameError not raised by test')
-
- self.assertEqual(foo, {})
-
-
- def test_patch_dict_with_string(self):
- @patch.dict('os.environ', {'konrad_delong': 'some value'})
- def test():
- self.assertIn('konrad_delong', os.environ)
-
- test()
-
-
- @unittest.expectedFailure
- def test_patch_descriptor(self):
- # would be some effort to fix this - we could special case the
- # builtin descriptors: classmethod, property, staticmethod
- class Nothing(object):
- foo = None
-
- class Something(object):
- foo = {}
-
- @patch.object(Nothing, 'foo', 2)
- @classmethod
- def klass(cls):
- self.assertIs(cls, Something)
-
- @patch.object(Nothing, 'foo', 2)
- @staticmethod
- def static(arg):
- return arg
-
- @patch.dict(foo)
- @classmethod
- def klass_dict(cls):
- self.assertIs(cls, Something)
-
- @patch.dict(foo)
- @staticmethod
- def static_dict(arg):
- return arg
-
- # these will raise exceptions if patching descriptors is broken
- self.assertEqual(Something.static('f00'), 'f00')
- Something.klass()
- self.assertEqual(Something.static_dict('f00'), 'f00')
- Something.klass_dict()
-
- something = Something()
- self.assertEqual(something.static('f00'), 'f00')
- something.klass()
- self.assertEqual(something.static_dict('f00'), 'f00')
- something.klass_dict()
-
-
- def test_patch_spec_set(self):
- @patch('%s.SomeClass' % __name__, spec_set=SomeClass)
- def test(MockClass):
- MockClass.z = 'foo'
-
- self.assertRaises(AttributeError, test)
-
- @patch.object(support, 'SomeClass', spec_set=SomeClass)
- def test(MockClass):
- MockClass.z = 'foo'
-
- self.assertRaises(AttributeError, test)
- @patch('%s.SomeClass' % __name__, spec_set=True)
- def test(MockClass):
- MockClass.z = 'foo'
-
- self.assertRaises(AttributeError, test)
-
- @patch.object(support, 'SomeClass', spec_set=True)
- def test(MockClass):
- MockClass.z = 'foo'
-
- self.assertRaises(AttributeError, test)
-
-
- def test_spec_set_inherit(self):
- @patch('%s.SomeClass' % __name__, spec_set=True)
- def test(MockClass):
- instance = MockClass()
- instance.z = 'foo'
-
- self.assertRaises(AttributeError, test)
-
-
- def test_patch_start_stop(self):
- original = something
- patcher = patch('%s.something' % __name__)
- self.assertIs(something, original)
- mock = patcher.start()
- try:
- self.assertIsNot(mock, original)
- self.assertIs(something, mock)
- finally:
- patcher.stop()
- self.assertIs(something, original)
-
-
- def test_stop_without_start(self):
- patcher = patch(foo_name, 'bar', 3)
-
- # calling stop without start used to produce a very obscure error
- self.assertRaises(RuntimeError, patcher.stop)
-
-
- def test_patchobject_start_stop(self):
- original = something
- patcher = patch.object(PTModule, 'something', 'foo')
- self.assertIs(something, original)
- replaced = patcher.start()
- try:
- self.assertEqual(replaced, 'foo')
- self.assertIs(something, replaced)
- finally:
- patcher.stop()
- self.assertIs(something, original)
-
-
- def test_patch_dict_start_stop(self):
- d = {'foo': 'bar'}
- original = d.copy()
- patcher = patch.dict(d, [('spam', 'eggs')], clear=True)
- self.assertEqual(d, original)
-
- patcher.start()
- try:
- self.assertEqual(d, {'spam': 'eggs'})
- finally:
- patcher.stop()
- self.assertEqual(d, original)
-
-
- def test_patch_dict_class_decorator(self):
- this = self
- d = {'spam': 'eggs'}
- original = d.copy()
-
- class Test(object):
- def test_first(self):
- this.assertEqual(d, {'foo': 'bar'})
- def test_second(self):
- this.assertEqual(d, {'foo': 'bar'})
-
- Test = patch.dict(d, {'foo': 'bar'}, clear=True)(Test)
- self.assertEqual(d, original)
-
- test = Test()
-
- test.test_first()
- self.assertEqual(d, original)
-
- test.test_second()
- self.assertEqual(d, original)
-
- test = Test()
-
- test.test_first()
- self.assertEqual(d, original)
-
- test.test_second()
- self.assertEqual(d, original)
-
-
- def test_get_only_proxy(self):
- class Something(object):
- foo = 'foo'
- class SomethingElse:
- foo = 'foo'
-
- for thing in Something, SomethingElse, Something(), SomethingElse:
- proxy = _get_proxy(thing)
-
- @patch.object(proxy, 'foo', 'bar')
- def test():
- self.assertEqual(proxy.foo, 'bar')
- test()
- self.assertEqual(proxy.foo, 'foo')
- self.assertEqual(thing.foo, 'foo')
- self.assertNotIn('foo', proxy.__dict__)
-
-
- def test_get_set_delete_proxy(self):
- class Something(object):
- foo = 'foo'
- class SomethingElse:
- foo = 'foo'
-
- for thing in Something, SomethingElse, Something(), SomethingElse:
- proxy = _get_proxy(Something, get_only=False)
-
- @patch.object(proxy, 'foo', 'bar')
- def test():
- self.assertEqual(proxy.foo, 'bar')
- test()
- self.assertEqual(proxy.foo, 'foo')
- self.assertEqual(thing.foo, 'foo')
- self.assertNotIn('foo', proxy.__dict__)
-
-
- def test_patch_keyword_args(self):
- kwargs = {'side_effect': KeyError, 'foo.bar.return_value': 33,
- 'foo': MagicMock()}
-
- patcher = patch(foo_name, **kwargs)
- mock = patcher.start()
- patcher.stop()
-
- self.assertRaises(KeyError, mock)
- self.assertEqual(mock.foo.bar(), 33)
- self.assertIsInstance(mock.foo, MagicMock)
-
-
- def test_patch_object_keyword_args(self):
- kwargs = {'side_effect': KeyError, 'foo.bar.return_value': 33,
- 'foo': MagicMock()}
-
- patcher = patch.object(Foo, 'f', **kwargs)
- mock = patcher.start()
- patcher.stop()
-
- self.assertRaises(KeyError, mock)
- self.assertEqual(mock.foo.bar(), 33)
- self.assertIsInstance(mock.foo, MagicMock)
-
-
- def test_patch_dict_keyword_args(self):
- original = {'foo': 'bar'}
- copy = original.copy()
-
- patcher = patch.dict(original, foo=3, bar=4, baz=5)
- patcher.start()
-
- try:
- self.assertEqual(original, dict(foo=3, bar=4, baz=5))
- finally:
- patcher.stop()
-
- self.assertEqual(original, copy)
-
-
- def test_autospec(self):
- class Boo(object):
- def __init__(self, a):
- pass
- def f(self, a):
- pass
- def g(self):
- pass
- foo = 'bar'
-
- class Bar(object):
- def a(self):
- pass
-
- def _test(mock):
- mock(1)
- mock.assert_called_with(1)
- self.assertRaises(TypeError, mock)
-
- def _test2(mock):
- mock.f(1)
- mock.f.assert_called_with(1)
- self.assertRaises(TypeError, mock.f)
-
- mock.g()
- mock.g.assert_called_with()
- self.assertRaises(TypeError, mock.g, 1)
-
- self.assertRaises(AttributeError, getattr, mock, 'h')
-
- mock.foo.lower()
- mock.foo.lower.assert_called_with()
- self.assertRaises(AttributeError, getattr, mock.foo, 'bar')
-
- mock.Bar()
- mock.Bar.assert_called_with()
-
- mock.Bar.a()
- mock.Bar.a.assert_called_with()
- self.assertRaises(TypeError, mock.Bar.a, 1)
-
- mock.Bar().a()
- mock.Bar().a.assert_called_with()
- self.assertRaises(TypeError, mock.Bar().a, 1)
-
- self.assertRaises(AttributeError, getattr, mock.Bar, 'b')
- self.assertRaises(AttributeError, getattr, mock.Bar(), 'b')
-
- def function(mock):
- _test(mock)
- _test2(mock)
- _test2(mock(1))
- self.assertIs(mock, Foo)
- return mock
-
- test = patch(foo_name, autospec=True)(function)
-
- mock = test()
- self.assertIsNot(Foo, mock)
- # test patching a second time works
- test()
-
- module = sys.modules[__name__]
- test = patch.object(module, 'Foo', autospec=True)(function)
-
- mock = test()
- self.assertIsNot(Foo, mock)
- # test patching a second time works
- test()
-
-
- def test_autospec_function(self):
- @patch('%s.function' % __name__, autospec=True)
- def test(mock):
- function(1)
- function.assert_called_with(1)
- function(2, 3)
- function.assert_called_with(2, 3)
-
- self.assertRaises(TypeError, function)
- self.assertRaises(AttributeError, getattr, function, 'foo')
-
- test()
-
-
- def test_autospec_keywords(self):
- @patch('%s.function' % __name__, autospec=True,
- return_value=3)
- def test(mock_function):
- #self.assertEqual(function.abc, 'foo')
- return function(1, 2)
-
- result = test()
- self.assertEqual(result, 3)
-
-
- def test_autospec_with_new(self):
- patcher = patch('%s.function' % __name__, new=3, autospec=True)
- self.assertRaises(TypeError, patcher.start)
-
- module = sys.modules[__name__]
- patcher = patch.object(module, 'function', new=3, autospec=True)
- self.assertRaises(TypeError, patcher.start)
-
-
- def test_autospec_with_object(self):
- class Bar(Foo):
- extra = []
-
- patcher = patch(foo_name, autospec=Bar)
- mock = patcher.start()
- try:
- self.assertIsInstance(mock, Bar)
- self.assertIsInstance(mock.extra, list)
- finally:
- patcher.stop()
-
-
- def test_autospec_inherits(self):
- FooClass = Foo
- patcher = patch(foo_name, autospec=True)
- mock = patcher.start()
- try:
- self.assertIsInstance(mock, FooClass)
- self.assertIsInstance(mock(3), FooClass)
- finally:
- patcher.stop()
-
-
- def test_autospec_name(self):
- patcher = patch(foo_name, autospec=True)
- mock = patcher.start()
-
- try:
- self.assertIn(" name='Foo'", repr(mock))
- self.assertIn(" name='Foo.f'", repr(mock.f))
- self.assertIn(" name='Foo()'", repr(mock(None)))
- self.assertIn(" name='Foo().f'", repr(mock(None).f))
- finally:
- patcher.stop()
-
-
- def test_tracebacks(self):
- @patch.object(Foo, 'f', object())
- def test():
- raise AssertionError
- try:
- test()
- except:
- err = sys.exc_info()
-
- result = unittest.TextTestResult(None, None, 0)
- traceback = result._exc_info_to_string(err, self)
- self.assertIn('raise AssertionError', traceback)
-
-
- def test_new_callable_patch(self):
- patcher = patch(foo_name, new_callable=NonCallableMagicMock)
-
- m1 = patcher.start()
- patcher.stop()
- m2 = patcher.start()
- patcher.stop()
-
- self.assertIsNot(m1, m2)
- for mock in m1, m2:
- self.assertNotCallable(m1)
-
-
- def test_new_callable_patch_object(self):
- patcher = patch.object(Foo, 'f', new_callable=NonCallableMagicMock)
-
- m1 = patcher.start()
- patcher.stop()
- m2 = patcher.start()
- patcher.stop()
-
- self.assertIsNot(m1, m2)
- for mock in m1, m2:
- self.assertNotCallable(m1)
-
-
- def test_new_callable_keyword_arguments(self):
- class Bar(object):
- kwargs = None
- def __init__(self, **kwargs):
- Bar.kwargs = kwargs
-
- patcher = patch(foo_name, new_callable=Bar, arg1=1, arg2=2)
- m = patcher.start()
- try:
- self.assertIs(type(m), Bar)
- self.assertEqual(Bar.kwargs, dict(arg1=1, arg2=2))
- finally:
- patcher.stop()
-
-
- def test_new_callable_spec(self):
- class Bar(object):
- kwargs = None
- def __init__(self, **kwargs):
- Bar.kwargs = kwargs
-
- patcher = patch(foo_name, new_callable=Bar, spec=Bar)
- patcher.start()
- try:
- self.assertEqual(Bar.kwargs, dict(spec=Bar))
- finally:
- patcher.stop()
-
- patcher = patch(foo_name, new_callable=Bar, spec_set=Bar)
- patcher.start()
- try:
- self.assertEqual(Bar.kwargs, dict(spec_set=Bar))
- finally:
- patcher.stop()
-
-
- def test_new_callable_create(self):
- non_existent_attr = '%s.weeeee' % foo_name
- p = patch(non_existent_attr, new_callable=NonCallableMock)
- self.assertRaises(AttributeError, p.start)
-
- p = patch(non_existent_attr, new_callable=NonCallableMock,
- create=True)
- m = p.start()
- try:
- self.assertNotCallable(m, magic=False)
- finally:
- p.stop()
-
-
- def test_new_callable_incompatible_with_new(self):
- self.assertRaises(
- ValueError, patch, foo_name, new=object(), new_callable=MagicMock
- )
- self.assertRaises(
- ValueError, patch.object, Foo, 'f', new=object(),
- new_callable=MagicMock
- )
-
-
- def test_new_callable_incompatible_with_autospec(self):
- self.assertRaises(
- ValueError, patch, foo_name, new_callable=MagicMock,
- autospec=True
- )
- self.assertRaises(
- ValueError, patch.object, Foo, 'f', new_callable=MagicMock,
- autospec=True
- )
-
-
- def test_new_callable_inherit_for_mocks(self):
- class MockSub(Mock):
- pass
-
- MockClasses = (
- NonCallableMock, NonCallableMagicMock, MagicMock, Mock, MockSub
- )
- for Klass in MockClasses:
- for arg in 'spec', 'spec_set':
- kwargs = {arg: True}
- p = patch(foo_name, new_callable=Klass, **kwargs)
- m = p.start()
- try:
- instance = m.return_value
- self.assertRaises(AttributeError, getattr, instance, 'x')
- finally:
- p.stop()
-
-
- def test_new_callable_inherit_non_mock(self):
- class NotAMock(object):
- def __init__(self, spec):
- self.spec = spec
-
- p = patch(foo_name, new_callable=NotAMock, spec=True)
- m = p.start()
- try:
- self.assertTrue(is_instance(m, NotAMock))
- self.assertRaises(AttributeError, getattr, m, 'return_value')
- finally:
- p.stop()
-
- self.assertEqual(m.spec, Foo)
-
-
- def test_new_callable_class_decorating(self):
- test = self
- original = Foo
- class SomeTest(object):
-
- def _test(self, mock_foo):
- test.assertIsNot(Foo, original)
- test.assertIs(Foo, mock_foo)
- test.assertIsInstance(Foo, SomeClass)
-
- def test_two(self, mock_foo):
- self._test(mock_foo)
- def test_one(self, mock_foo):
- self._test(mock_foo)
-
- SomeTest = patch(foo_name, new_callable=SomeClass)(SomeTest)
- SomeTest().test_one()
- SomeTest().test_two()
- self.assertIs(Foo, original)
-
-
- def test_patch_multiple(self):
- original_foo = Foo
- original_f = Foo.f
- original_g = Foo.g
-
- patcher1 = patch.multiple(foo_name, f=1, g=2)
- patcher2 = patch.multiple(Foo, f=1, g=2)
-
- for patcher in patcher1, patcher2:
- patcher.start()
- try:
- self.assertIs(Foo, original_foo)
- self.assertEqual(Foo.f, 1)
- self.assertEqual(Foo.g, 2)
- finally:
- patcher.stop()
-
- self.assertIs(Foo, original_foo)
- self.assertEqual(Foo.f, original_f)
- self.assertEqual(Foo.g, original_g)
-
-
- @patch.multiple(foo_name, f=3, g=4)
- def test():
- self.assertIs(Foo, original_foo)
- self.assertEqual(Foo.f, 3)
- self.assertEqual(Foo.g, 4)
-
- test()
-
-
- def test_patch_multiple_no_kwargs(self):
- self.assertRaises(ValueError, patch.multiple, foo_name)
- self.assertRaises(ValueError, patch.multiple, Foo)
-
-
- def test_patch_multiple_create_mocks(self):
- original_foo = Foo
- original_f = Foo.f
- original_g = Foo.g
-
- @patch.multiple(foo_name, f=DEFAULT, g=3, foo=DEFAULT)
- def test(f, foo):
- self.assertIs(Foo, original_foo)
- self.assertIs(Foo.f, f)
- self.assertEqual(Foo.g, 3)
- self.assertIs(Foo.foo, foo)
- self.assertTrue(is_instance(f, MagicMock))
- self.assertTrue(is_instance(foo, MagicMock))
-
- test()
- self.assertEqual(Foo.f, original_f)
- self.assertEqual(Foo.g, original_g)
-
-
- def test_patch_multiple_create_mocks_different_order(self):
- # bug revealed by Jython!
- original_f = Foo.f
- original_g = Foo.g
-
- patcher = patch.object(Foo, 'f', 3)
- patcher.attribute_name = 'f'
-
- other = patch.object(Foo, 'g', DEFAULT)
- other.attribute_name = 'g'
- patcher.additional_patchers = [other]
-
- @patcher
- def test(g):
- self.assertIs(Foo.g, g)
- self.assertEqual(Foo.f, 3)
-
- test()
- self.assertEqual(Foo.f, original_f)
- self.assertEqual(Foo.g, original_g)
-
-
- def test_patch_multiple_stacked_decorators(self):
- original_foo = Foo
- original_f = Foo.f
- original_g = Foo.g
-
- @patch.multiple(foo_name, f=DEFAULT)
- @patch.multiple(foo_name, foo=DEFAULT)
- @patch(foo_name + '.g')
- def test1(g, **kwargs):
- _test(g, **kwargs)
-
- @patch.multiple(foo_name, f=DEFAULT)
- @patch(foo_name + '.g')
- @patch.multiple(foo_name, foo=DEFAULT)
- def test2(g, **kwargs):
- _test(g, **kwargs)
-
- @patch(foo_name + '.g')
- @patch.multiple(foo_name, f=DEFAULT)
- @patch.multiple(foo_name, foo=DEFAULT)
- def test3(g, **kwargs):
- _test(g, **kwargs)
-
- def _test(g, **kwargs):
- f = kwargs.pop('f')
- foo = kwargs.pop('foo')
- self.assertFalse(kwargs)
-
- self.assertIs(Foo, original_foo)
- self.assertIs(Foo.f, f)
- self.assertIs(Foo.g, g)
- self.assertIs(Foo.foo, foo)
- self.assertTrue(is_instance(f, MagicMock))
- self.assertTrue(is_instance(g, MagicMock))
- self.assertTrue(is_instance(foo, MagicMock))
-
- test1()
- test2()
- test3()
- self.assertEqual(Foo.f, original_f)
- self.assertEqual(Foo.g, original_g)
-
-
- def test_patch_multiple_create_mocks_patcher(self):
- original_foo = Foo
- original_f = Foo.f
- original_g = Foo.g
-
- patcher = patch.multiple(foo_name, f=DEFAULT, g=3, foo=DEFAULT)
-
- result = patcher.start()
- try:
- f = result['f']
- foo = result['foo']
- self.assertEqual(set(result), set(['f', 'foo']))
-
- self.assertIs(Foo, original_foo)
- self.assertIs(Foo.f, f)
- self.assertIs(Foo.foo, foo)
- self.assertTrue(is_instance(f, MagicMock))
- self.assertTrue(is_instance(foo, MagicMock))
- finally:
- patcher.stop()
-
- self.assertEqual(Foo.f, original_f)
- self.assertEqual(Foo.g, original_g)
-
-
- def test_patch_multiple_decorating_class(self):
- test = self
- original_foo = Foo
- original_f = Foo.f
- original_g = Foo.g
-
- class SomeTest(object):
-
- def _test(self, f, foo):
- test.assertIs(Foo, original_foo)
- test.assertIs(Foo.f, f)
- test.assertEqual(Foo.g, 3)
- test.assertIs(Foo.foo, foo)
- test.assertTrue(is_instance(f, MagicMock))
- test.assertTrue(is_instance(foo, MagicMock))
-
- def test_two(self, f, foo):
- self._test(f, foo)
- def test_one(self, f, foo):
- self._test(f, foo)
-
- SomeTest = patch.multiple(
- foo_name, f=DEFAULT, g=3, foo=DEFAULT
- )(SomeTest)
-
- thing = SomeTest()
- thing.test_one()
- thing.test_two()
-
- self.assertEqual(Foo.f, original_f)
- self.assertEqual(Foo.g, original_g)
-
-
- def test_patch_multiple_create(self):
- patcher = patch.multiple(Foo, blam='blam')
- self.assertRaises(AttributeError, patcher.start)
-
- patcher = patch.multiple(Foo, blam='blam', create=True)
- patcher.start()
- try:
- self.assertEqual(Foo.blam, 'blam')
- finally:
- patcher.stop()
-
- self.assertFalse(hasattr(Foo, 'blam'))
-
-
- def test_patch_multiple_spec_set(self):
- # if spec_set works then we can assume that spec and autospec also
- # work as the underlying machinery is the same
- patcher = patch.multiple(Foo, foo=DEFAULT, spec_set=['a', 'b'])
- result = patcher.start()
- try:
- self.assertEqual(Foo.foo, result['foo'])
- Foo.foo.a(1)
- Foo.foo.b(2)
- Foo.foo.a.assert_called_with(1)
- Foo.foo.b.assert_called_with(2)
- self.assertRaises(AttributeError, setattr, Foo.foo, 'c', None)
- finally:
- patcher.stop()
-
-
- def test_patch_multiple_new_callable(self):
- class Thing(object):
- pass
-
- patcher = patch.multiple(
- Foo, f=DEFAULT, g=DEFAULT, new_callable=Thing
- )
- result = patcher.start()
- try:
- self.assertIs(Foo.f, result['f'])
- self.assertIs(Foo.g, result['g'])
- self.assertIsInstance(Foo.f, Thing)
- self.assertIsInstance(Foo.g, Thing)
- self.assertIsNot(Foo.f, Foo.g)
- finally:
- patcher.stop()
-
-
- def test_nested_patch_failure(self):
- original_f = Foo.f
- original_g = Foo.g
-
- @patch.object(Foo, 'g', 1)
- @patch.object(Foo, 'missing', 1)
- @patch.object(Foo, 'f', 1)
- def thing1():
- pass
-
- @patch.object(Foo, 'missing', 1)
- @patch.object(Foo, 'g', 1)
- @patch.object(Foo, 'f', 1)
- def thing2():
- pass
-
- @patch.object(Foo, 'g', 1)
- @patch.object(Foo, 'f', 1)
- @patch.object(Foo, 'missing', 1)
- def thing3():
- pass
-
- for func in thing1, thing2, thing3:
- self.assertRaises(AttributeError, func)
- self.assertEqual(Foo.f, original_f)
- self.assertEqual(Foo.g, original_g)
-
-
- def test_new_callable_failure(self):
- original_f = Foo.f
- original_g = Foo.g
- original_foo = Foo.foo
-
- def crasher():
- raise NameError('crasher')
-
- @patch.object(Foo, 'g', 1)
- @patch.object(Foo, 'foo', new_callable=crasher)
- @patch.object(Foo, 'f', 1)
- def thing1():
- pass
-
- @patch.object(Foo, 'foo', new_callable=crasher)
- @patch.object(Foo, 'g', 1)
- @patch.object(Foo, 'f', 1)
- def thing2():
- pass
-
- @patch.object(Foo, 'g', 1)
- @patch.object(Foo, 'f', 1)
- @patch.object(Foo, 'foo', new_callable=crasher)
- def thing3():
- pass
-
- for func in thing1, thing2, thing3:
- self.assertRaises(NameError, func)
- self.assertEqual(Foo.f, original_f)
- self.assertEqual(Foo.g, original_g)
- self.assertEqual(Foo.foo, original_foo)
-
-
- def test_patch_multiple_failure(self):
- original_f = Foo.f
- original_g = Foo.g
-
- patcher = patch.object(Foo, 'f', 1)
- patcher.attribute_name = 'f'
-
- good = patch.object(Foo, 'g', 1)
- good.attribute_name = 'g'
-
- bad = patch.object(Foo, 'missing', 1)
- bad.attribute_name = 'missing'
-
- for additionals in [good, bad], [bad, good]:
- patcher.additional_patchers = additionals
-
- @patcher
- def func():
- pass
-
- self.assertRaises(AttributeError, func)
- self.assertEqual(Foo.f, original_f)
- self.assertEqual(Foo.g, original_g)
-
-
- def test_patch_multiple_new_callable_failure(self):
- original_f = Foo.f
- original_g = Foo.g
- original_foo = Foo.foo
-
- def crasher():
- raise NameError('crasher')
-
- patcher = patch.object(Foo, 'f', 1)
- patcher.attribute_name = 'f'
-
- good = patch.object(Foo, 'g', 1)
- good.attribute_name = 'g'
-
- bad = patch.object(Foo, 'foo', new_callable=crasher)
- bad.attribute_name = 'foo'
-
- for additionals in [good, bad], [bad, good]:
- patcher.additional_patchers = additionals
-
- @patcher
- def func():
- pass
-
- self.assertRaises(NameError, func)
- self.assertEqual(Foo.f, original_f)
- self.assertEqual(Foo.g, original_g)
- self.assertEqual(Foo.foo, original_foo)
-
-
- def test_patch_multiple_string_subclasses(self):
- for base in (str, unicode):
- Foo = type('Foo', (base,), {'fish': 'tasty'})
- foo = Foo()
- @patch.multiple(foo, fish='nearly gone')
- def test():
- self.assertEqual(foo.fish, 'nearly gone')
-
- test()
- self.assertEqual(foo.fish, 'tasty')
-
-
- @patch('mock.patch.TEST_PREFIX', 'foo')
- def test_patch_test_prefix(self):
- class Foo(object):
- thing = 'original'
-
- def foo_one(self):
- return self.thing
- def foo_two(self):
- return self.thing
- def test_one(self):
- return self.thing
- def test_two(self):
- return self.thing
-
- Foo = patch.object(Foo, 'thing', 'changed')(Foo)
-
- foo = Foo()
- self.assertEqual(foo.foo_one(), 'changed')
- self.assertEqual(foo.foo_two(), 'changed')
- self.assertEqual(foo.test_one(), 'original')
- self.assertEqual(foo.test_two(), 'original')
-
-
- @patch('mock.patch.TEST_PREFIX', 'bar')
- def test_patch_dict_test_prefix(self):
- class Foo(object):
- def bar_one(self):
- return dict(the_dict)
- def bar_two(self):
- return dict(the_dict)
- def test_one(self):
- return dict(the_dict)
- def test_two(self):
- return dict(the_dict)
-
- the_dict = {'key': 'original'}
- Foo = patch.dict(the_dict, key='changed')(Foo)
-
- foo =Foo()
- self.assertEqual(foo.bar_one(), {'key': 'changed'})
- self.assertEqual(foo.bar_two(), {'key': 'changed'})
- self.assertEqual(foo.test_one(), {'key': 'original'})
- self.assertEqual(foo.test_two(), {'key': 'original'})
-
-
- def test_patch_with_spec_mock_repr(self):
- for arg in ('spec', 'autospec', 'spec_set'):
- p = patch('%s.SomeClass' % __name__, **{arg: True})
- m = p.start()
- try:
- self.assertIn(" name='SomeClass'", repr(m))
- self.assertIn(" name='SomeClass.class_attribute'",
- repr(m.class_attribute))
- self.assertIn(" name='SomeClass()'", repr(m()))
- self.assertIn(" name='SomeClass().class_attribute'",
- repr(m().class_attribute))
- finally:
- p.stop()
-
-
- def test_patch_nested_autospec_repr(self):
- p = patch('mock.tests.support', autospec=True)
- m = p.start()
- try:
- self.assertIn(" name='support.SomeClass.wibble()'",
- repr(m.SomeClass.wibble()))
- self.assertIn(" name='support.SomeClass().wibble()'",
- repr(m.SomeClass().wibble()))
- finally:
- p.stop()
-
-
- def test_mock_calls_with_patch(self):
- for arg in ('spec', 'autospec', 'spec_set'):
- p = patch('%s.SomeClass' % __name__, **{arg: True})
- m = p.start()
- try:
- m.wibble()
-
- kalls = [call.wibble()]
- self.assertEqual(m.mock_calls, kalls)
- self.assertEqual(m.method_calls, kalls)
- self.assertEqual(m.wibble.mock_calls, [call()])
-
- result = m()
- kalls.append(call())
- self.assertEqual(m.mock_calls, kalls)
-
- result.wibble()
- kalls.append(call().wibble())
- self.assertEqual(m.mock_calls, kalls)
-
- self.assertEqual(result.mock_calls, [call.wibble()])
- self.assertEqual(result.wibble.mock_calls, [call()])
- self.assertEqual(result.method_calls, [call.wibble()])
- finally:
- p.stop()
-
-
- def test_patch_imports_lazily(self):
- sys.modules.pop('squizz', None)
-
- p1 = patch('squizz.squozz')
- self.assertRaises(ImportError, p1.start)
-
- squizz = Mock()
- squizz.squozz = 6
- sys.modules['squizz'] = squizz
- p1 = patch('squizz.squozz')
- squizz.squozz = 3
- p1.start()
- p1.stop()
- self.assertEqual(squizz.squozz, 3)
-
-
- def test_patch_propogrates_exc_on_exit(self):
- class holder:
- exc_info = None, None, None
-
- class custom_patch(_patch):
- def __exit__(self, etype=None, val=None, tb=None):
- _patch.__exit__(self, etype, val, tb)
- holder.exc_info = etype, val, tb
- stop = __exit__
-
- def with_custom_patch(target):
- getter, attribute = _get_target(target)
- return custom_patch(
- getter, attribute, DEFAULT, None, False, None,
- None, None, {}
- )
-
- @with_custom_patch('squizz.squozz')
- def test(mock):
- raise RuntimeError
-
- self.assertRaises(RuntimeError, test)
- self.assertIs(holder.exc_info[0], RuntimeError)
- self.assertIsNotNone(holder.exc_info[1],
- 'exception value not propgated')
- self.assertIsNotNone(holder.exc_info[2],
- 'exception traceback not propgated')
-
-
- def test_create_and_specs(self):
- for kwarg in ('spec', 'spec_set', 'autospec'):
- p = patch('%s.doesnotexist' % __name__, create=True,
- **{kwarg: True})
- self.assertRaises(TypeError, p.start)
- self.assertRaises(NameError, lambda: doesnotexist)
-
- # check that spec with create is innocuous if the original exists
- p = patch(MODNAME, create=True, **{kwarg: True})
- p.start()
- p.stop()
-
-
- def test_multiple_specs(self):
- original = PTModule
- for kwarg in ('spec', 'spec_set'):
- p = patch(MODNAME, autospec=0, **{kwarg: 0})
- self.assertRaises(TypeError, p.start)
- self.assertIs(PTModule, original)
-
- for kwarg in ('spec', 'autospec'):
- p = patch(MODNAME, spec_set=0, **{kwarg: 0})
- self.assertRaises(TypeError, p.start)
- self.assertIs(PTModule, original)
-
- for kwarg in ('spec_set', 'autospec'):
- p = patch(MODNAME, spec=0, **{kwarg: 0})
- self.assertRaises(TypeError, p.start)
- self.assertIs(PTModule, original)
-
-
- def test_specs_false_instead_of_none(self):
- p = patch(MODNAME, spec=False, spec_set=False, autospec=False)
- mock = p.start()
- try:
- # no spec should have been set, so attribute access should not fail
- mock.does_not_exist
- mock.does_not_exist = 3
- finally:
- p.stop()
-
-
- def test_falsey_spec(self):
- for kwarg in ('spec', 'autospec', 'spec_set'):
- p = patch(MODNAME, **{kwarg: 0})
- m = p.start()
- try:
- self.assertRaises(AttributeError, getattr, m, 'doesnotexit')
- finally:
- p.stop()
-
-
- def test_spec_set_true(self):
- for kwarg in ('spec', 'autospec'):
- p = patch(MODNAME, spec_set=True, **{kwarg: True})
- m = p.start()
- try:
- self.assertRaises(AttributeError, setattr, m,
- 'doesnotexist', 'something')
- self.assertRaises(AttributeError, getattr, m, 'doesnotexist')
- finally:
- p.stop()
-
-
- def test_callable_spec_as_list(self):
- spec = ('__call__',)
- p = patch(MODNAME, spec=spec)
- m = p.start()
- try:
- self.assertTrue(callable(m))
- finally:
- p.stop()
-
-
- def test_not_callable_spec_as_list(self):
- spec = ('foo', 'bar')
- p = patch(MODNAME, spec=spec)
- m = p.start()
- try:
- self.assertFalse(callable(m))
- finally:
- p.stop()
-
-
- def test_patch_stopall(self):
- unlink = os.unlink
- chdir = os.chdir
- path = os.path
- patch('os.unlink', something).start()
- patch('os.chdir', something_else).start()
-
- @patch('os.path')
- def patched(mock_path):
- patch.stopall()
- self.assertIs(os.path, mock_path)
- self.assertIs(os.unlink, unlink)
- self.assertIs(os.chdir, chdir)
-
- patched()
- self.assertIs(os.path, path)
-
-
- def test_wrapped_patch(self):
- decorated = patch('sys.modules')(function)
- self.assertIs(decorated.__wrapped__, function)
-
-
- def test_wrapped_several_times_patch(self):
- decorated = patch('sys.modules')(function)
- decorated = patch('sys.modules')(decorated)
- self.assertIs(decorated.__wrapped__, function)
-
-
- def test_wrapped_patch_object(self):
- decorated = patch.object(sys, 'modules')(function)
- self.assertIs(decorated.__wrapped__, function)
-
-
- def test_wrapped_patch_dict(self):
- decorated = patch.dict('sys.modules')(function)
- self.assertIs(decorated.__wrapped__, function)
-
-
- def test_wrapped_patch_multiple(self):
- decorated = patch.multiple('sys', modules={})(function)
- self.assertIs(decorated.__wrapped__, function)
-
- def test_stopall_lifo(self):
- stopped = []
- class thing(object):
- one = two = three = None
-
- def get_patch(attribute):
- class mypatch(_patch):
- def stop(self):
- stopped.append(attribute)
- return super(mypatch, self).stop()
- return mypatch(lambda: thing, attribute, None, None,
- False, None, None, None, {})
- [get_patch(val).start() for val in ("one", "two", "three")]
- patch.stopall()
-
- self.assertEqual(stopped, ["three", "two", "one"])
-
-
- def test_special_attrs(self):
- def foo(x=0):
- """TEST"""
- return x
- with patch.object(foo, '__defaults__', (1, )):
- self.assertEqual(foo(), 1)
- self.assertEqual(foo(), 0)
-
- with patch.object(foo, '__doc__', "FUN"):
- self.assertEqual(foo.__doc__, "FUN")
- self.assertEqual(foo.__doc__, "TEST")
-
- with patch.object(foo, '__module__', "testpatch2"):
- self.assertEqual(foo.__module__, "testpatch2")
- self.assertEqual(foo.__module__, __name__)
-
- if hasattr(self.test_special_attrs, '__annotations__'):
- with patch.object(foo, '__annotations__', dict([('s', 1, )])):
- self.assertEqual(foo.__annotations__, dict([('s', 1, )]))
- self.assertEqual(foo.__annotations__, dict())
-
- if hasattr(self.test_special_attrs, '__kwdefaults__'):
- foo = eval("lambda *a, x=0: x")
- with patch.object(foo, '__kwdefaults__', dict([('x', 1, )])):
- self.assertEqual(foo(), 1)
- self.assertEqual(foo(), 0)
-
- if __name__ == '__main__':
- unittest.main()
|