def test_with_function_and_op(self): """Testing SpyOpMatchAny with function and op""" def do_math(a, b, x=0): return a + b self.agency.spy_on(do_math, op=SpyOpMatchAny([ { 'args': [5, 3], 'op': SpyOpMatchInOrder([ { 'args': [5, 3], 'kwargs': { 'x': 1, }, 'op': SpyOpReturn(123), }, { 'args': [5, 3], 'kwargs': { 'x': 2, }, 'op': SpyOpReturn(456), }, ]), }, ])) self.assertEqual(do_math(a=5, b=3, x=1), 123) self.assertEqual(do_math(a=5, b=3, x=2), 456)
def test_with_unexpected_call(self): """Testing SpyOpMatchInOrder with unexpected call""" obj = MathClass() self.agency.spy_on( obj.do_math, op=SpyOpMatchInOrder([ { 'kwargs': { 'a': 4, 'b': 7, }, }, ])) expected_message = re.escape( "This call to do_math was not passed args=(), " "kwargs={'a': 4, 'b': 7}.\n" "\n" "It was called with:\n" "\n" "args=()\n" "kwargs={'a': 4, 'b': 9}" ) with self.assertRaisesRegexp(AssertionError, expected_message): obj.do_math(a=4, b=9)
def test_setup_with_instance_and_op(self): """Testing SpyOpMatchAny set up with op=SpyOpMatchAny([...]) and op""" obj = MathClass() self.agency.spy_on(obj.do_math, op=SpyOpMatchAny([ { 'kwargs': { 'a': 1, 'b': 2, }, 'op': SpyOpMatchInOrder([ { 'kwargs': { 'a': 1, 'b': 2, 'x': 1, }, 'op': SpyOpReturn(123), }, { 'kwargs': { 'a': 1, 'b': 2, 'x': 2, }, 'op': SpyOpReturn(456), }, ]), }, ])) self.assertEqual(obj.do_math(a=1, b=2, x=1), 123) self.assertEqual(obj.do_math(a=1, b=2, x=2), 456)
def test_with_unbound_method_and_op(self): """Testing SpyOpMatchAny with unbound method and op""" self.agency.spy_on(MathClass.do_math, owner=MathClass, op=SpyOpMatchAny([ { 'kwargs': { 'a': 4, 'b': 3, }, 'op': SpyOpMatchInOrder([ { 'kwargs': { 'a': 4, 'b': 3, 'x': 1, }, 'op': SpyOpReturn(123), }, { 'kwargs': { 'a': 4, 'b': 3, 'x': 2, }, 'op': SpyOpReturn(456), }, ]), }, ])) obj = MathClass() self.assertEqual(obj.do_math(a=4, b=3, x=1), 123) self.assertEqual(obj.do_math(a=4, b=3, x=2), 456)
def test_with_classmethod_and_op(self): """Testing SpyOpMatchAny with classmethod and op""" self.agency.spy_on(MathClass.class_do_math, owner=MathClass, op=SpyOpMatchAny([ { 'kwargs': { 'a': 5, 'b': 3, }, 'op': SpyOpMatchInOrder([ { 'kwargs': { 'a': 5, 'b': 3, 'x': 1, }, 'op': SpyOpReturn(123), }, { 'kwargs': { 'a': 5, 'b': 3, 'x': 2, }, 'op': SpyOpReturn(456), }, ]), }, ])) self.assertEqual(MathClass.class_do_math(a=5, b=3, x=1), 123) self.assertEqual(MathClass.class_do_math(a=5, b=3, x=2), 456)
def test_with_function_and_op(self): """Testing SpyOpMatchInOrder with function and op""" def do_math(a, b): return a + b self.agency.spy_on(do_math, op=SpyOpMatchInOrder([ { 'args': [5, 3], 'op': SpyOpReturn(123), }, ])) self.assertEqual(do_math(5, 3), 123)
def test_with_function(self): """Testing SpyOpMatchInOrder with function""" def do_math(a, b): return a + b self.agency.spy_on(do_math, op=SpyOpMatchInOrder([ { 'args': [5, 3], 'call_fake': lambda a, b: a - b }, ])) self.assertEqual(do_math(5, 3), 2)
def test_with_classmethod(self): """Testing SpyOpMatchInOrder with classmethod""" self.agency.spy_on(MathClass.class_do_math, owner=MathClass, op=SpyOpMatchInOrder([ { 'kwargs': { 'a': 5, 'b': 3, }, 'call_fake': lambda a, b: a - b }, ])) self.assertEqual(MathClass.class_do_math(a=5, b=3), 2)
def test_setup_with_instance(self): """Testing SpyOpMatchInOrder set up with op=SpyOpMatchInOrder([...])""" obj = MathClass() self.agency.spy_on(obj.do_math, op=SpyOpMatchInOrder([ { 'kwargs': { 'a': 1, 'b': 2, }, }, ])) self.assertEqual(obj.do_math(a=1, b=2), 3)
def test_with_unbound_method(self): """Testing SpyOpMatchInOrder with unbound method""" self.agency.spy_on(MathClass.do_math, owner=MathClass, op=SpyOpMatchInOrder([ { 'kwargs': { 'a': 4, 'b': 3, }, }, ])) obj = MathClass() self.assertEqual(obj.do_math(a=4, b=3), 7)
def test_with_expected_calls(self): """Testing SpyOpMatchInOrder with all expected calls""" obj = MathClass() self.agency.spy_on(obj.do_math, op=SpyOpMatchInOrder([ { 'kwargs': { 'a': 4, 'b': 7, }, }, { 'kwargs': { 'a': 2, 'b': 8, }, 'call_original': False, }, { 'kwargs': { 'a': 100, 'b': 200, }, 'op': SpyOpReturn(123), }, { 'kwargs': { 'a': 5, 'b': 9, }, 'call_fake': lambda a, b: a + b + 10, }, { 'call_fake': lambda a, b: 1001, }, ])) values = [ obj.do_math(4, 7), obj.do_math(a=2, b=8), obj.do_math(a=100, b=200), obj.do_math(5, b=9), obj.do_math(a=1, b=1), ] self.assertEqual(values, [11, None, 123, 24, 1001])
def test_with_extra_call(self): """Testing SpyOpMatchInOrder with extra unexpected call""" obj = MathClass() self.agency.spy_on( obj.do_math, op=SpyOpMatchInOrder([ { 'kwargs': { 'a': 4, 'b': 7, }, }, ])) self.assertEqual(obj.do_math(a=4, b=7), 11) expected_message = re.escape( 'do_math was called 2 time(s), but only 1 call(s) were expected.' ) with self.assertRaisesRegexp(UnexpectedCallError, expected_message): obj.do_math(a=4, b=9)