How to use called_with method in Sure

Best Python code snippet using sure_python

test_callbacks.py

Source:test_callbacks.py Github

copy

Full Screen

1from __future__ import absolute_import2import unittest3import uuid4import typing5from callbacks import supports_callbacks6called_with = []7def callback(*args, **kwargs):8 called_with.append((args, kwargs))9@supports_callbacks()10def foo(bar, baz: typing.Union[int, str] = 'bone', *, qux=None):11 if qux is None:12 return bar, baz13 else:14 return bar, baz, qux15called_order = []16def cb1(*args, **kwargs):17 called_order.append('cb1')18def cb2(*args, **kwargs):19 called_order.append('cb2')20def cb3(*args, **kwargs):21 called_order.append('cb3')22class TestCallbackDecorator(unittest.TestCase):23 def setUp(self):24 while called_with:25 called_with.pop()26 while called_order:27 called_order.pop()28 foo.remove_callbacks()29 def test_with_defaults(self):30 result = foo(10, 20)31 self.assertEqual(result, (10, 20))32 self.assertEqual(len(called_with), 0)33 foo.add_callback(callback)34 result = foo(10, 20)35 self.assertEqual(result, (10, 20))36 self.assertEqual(len(called_with), 1)37 self.assertEqual(called_with[0], (tuple(), {}))38 result = foo(10, baz=20)39 self.assertEqual(result, (10, 20))40 self.assertEqual(len(called_with), 2)41 self.assertEqual(called_with[1], (tuple(), {}))42 def test_raises(self):43 self.assertRaises(ValueError, foo.add_callback, callback, priority='boo')44 def test_duplicate_label(self):45 foo.add_callback(callback, label='a')46 self.assertRaises(RuntimeError, foo.add_callback, callback, label='a')47 def test_remove_raises(self):48 foo.add_callback(callback)49 foo.add_callback(callback, label='good_label')50 self.assertRaises(RuntimeError, foo.remove_callback, 'bad_label')51 self.assertRaises(RuntimeError, foo.remove_callbacks, ['bad_label', 'good_label'])52 self.assertEqual(len(foo.callbacks), 1)53 def test_callbacks_info(self):54 foo.add_pre_callback(callback, label='a')55 foo.add_pre_callback(callback, label='b', takes_target_args=True)56 foo.add_post_callback(callback, label='c', priority=1.1,57 takes_target_result=True)58 foo.add_exception_callback(callback, label='d')59 expected_string =\60''' Label priority order type takes args takes result61 a 0.0 0 pre False N/A62 b 0.0 1 pre True N/A63 c 1.1 0 post False True64 d 0.0 0 exception False N/A'''65 self.assertEqual(expected_string, foo._callbacks_info)66 def test_with_takes_target_args(self):67 result = foo(10, 20)68 self.assertEqual(result, (10, 20))69 self.assertEqual(len(called_with), 0)70 foo.add_callback(callback, takes_target_args=True)71 result = foo(10, 20)72 self.assertEqual(result, (10, 20))73 self.assertEqual(len(called_with), 1)74 self.assertEqual(called_with[0], ((10, 20), {}))75 result = foo(10, baz=20)76 self.assertEqual(result, (10, 20))77 self.assertEqual(len(called_with), 2)78 self.assertEqual(called_with[1], ((10, ), {'baz':20}))79 def test_with_takes_target_result(self):80 result = foo(10, 20)81 self.assertEqual(result, (10, 20))82 self.assertEqual(len(called_with), 0)83 foo.add_callback(callback, takes_target_result=True)84 result = foo(10, 20)85 self.assertEqual(result, (10, 20))86 self.assertEqual(len(called_with), 1)87 self.assertEqual(called_with[0], (((10, 20), ), {}))88 result = foo(10, baz=20)89 self.assertEqual(result, (10, 20))90 self.assertEqual(len(called_with), 2)91 self.assertEqual(called_with[1], (((10, 20), ), {}))92 def test_with_takes_target_result_and_args(self):93 result = foo(10, 20)94 self.assertEqual(result, (10, 20))95 self.assertEqual(len(called_with), 0)96 foo.add_callback(callback, takes_target_result=True,97 takes_target_args=True)98 result = foo(10, 20)99 self.assertEqual(result, (10, 20))100 self.assertEqual(len(called_with), 1)101 self.assertEqual(called_with[0], (((10, 20), 10, 20), {}))102 result = foo(10, baz=20)103 self.assertEqual(result, (10, 20))104 self.assertEqual(len(called_with), 2)105 self.assertEqual(called_with[1], (((10, 20), 10), {'baz':20}))106 def test_before(self):107 result = foo(10, 20)108 self.assertEqual(result, (10, 20))109 self.assertEqual(len(called_with), 0)110 foo.add_pre_callback(callback)111 result = foo(10, 20)112 self.assertEqual(result, (10, 20))113 self.assertEqual(len(called_with), 1)114 self.assertEqual(called_with[0], (tuple(), {}))115 result = foo(10, baz=20)116 self.assertEqual(result, (10, 20))117 self.assertEqual(len(called_with), 2)118 self.assertEqual(called_with[1], (tuple(), {}))119 def test_before_with_target_args(self):120 result = foo(10, 20)121 self.assertEqual(result, (10, 20))122 self.assertEqual(len(called_with), 0)123 foo.add_pre_callback(callback, takes_target_args=True)124 result = foo(10, 20)125 self.assertEqual(result, (10, 20))126 self.assertEqual(len(called_with), 1)127 self.assertEqual(called_with[0], ((10, 20), {}))128 result = foo(10, baz=20)129 self.assertEqual(result, (10, 20))130 self.assertEqual(len(called_with), 2)131 self.assertEqual(called_with[1], ((10, ), {'baz':20}))132 def test_multiple_before(self):133 result = foo(10, 20)134 self.assertEqual(result, (10, 20))135 self.assertEqual(len(called_order), 0)136 foo.add_pre_callback(cb1)137 foo.add_pre_callback(cb2)138 foo.add_pre_callback(cb3)139 result = foo(10, 20)140 self.assertEqual(result, (10, 20))141 self.assertEqual(called_order, ['cb1','cb2','cb3'])142 def test_multiple_before_priority(self):143 result = foo(10, 20)144 self.assertEqual(result, (10, 20))145 self.assertEqual(len(called_order), 0)146 foo.add_pre_callback(cb1)147 foo.add_pre_callback(cb2, priority=1)148 foo.add_pre_callback(cb3, priority=1)149 result = foo(10, 20)150 self.assertEqual(result, (10, 20))151 self.assertEqual(called_order, ['cb2','cb3','cb1'])152 def test_multiple(self):153 result = foo(10, 20)154 self.assertEqual(result, (10, 20))155 self.assertEqual(len(called_order), 0)156 foo.add_callback(cb1)157 foo.add_callback(cb2)158 foo.add_callback(cb3)159 result = foo(10, 20)160 self.assertEqual(result, (10, 20))161 self.assertEqual(called_order, ['cb1','cb2','cb3'])162 def test_multiple_priority(self):163 result = foo(10, 20)164 self.assertEqual(result, (10, 20))165 self.assertEqual(len(called_order), 0)166 foo.add_callback(cb1)167 foo.add_callback(cb2, priority=1)168 foo.add_callback(cb3, priority=1)169 result = foo(10, 20)170 self.assertEqual(result, (10, 20))171 self.assertEqual(called_order, ['cb2','cb3','cb1'])172 def test_remove_callback(self):173 result = foo(10, 20)174 self.assertEqual(result, (10, 20))175 self.assertEqual(len(called_order), 0)176 foo.add_callback(cb1)177 label = foo.add_callback(cb2)178 foo.add_callback(cb3)179 result = foo(10, 20)180 self.assertEqual(result, (10, 20))181 self.assertEqual(called_order, ['cb1','cb2','cb3'])182 foo.remove_callback(label)183 result = foo(10, 20)184 self.assertEqual(result, (10, 20))185 self.assertEqual(called_order, ['cb1','cb2','cb3', 'cb1', 'cb3'])186 def test_remove_callbacks(self):187 result = foo(10, 20)188 self.assertEqual(result, (10, 20))189 self.assertEqual(len(called_order), 0)190 l1 = foo.add_callback(cb1)191 l2 = foo.add_callback(cb2)192 foo.add_callback(cb3)193 result = foo(10, 20)194 self.assertEqual(result, (10, 20))195 self.assertEqual(called_order, ['cb1','cb2','cb3'])196 foo.remove_callbacks([l1, l2])197 result = foo(10, 20)198 self.assertEqual(result, (10, 20))199 self.assertEqual(called_order, ['cb1','cb2','cb3', 'cb3'])200 foo.remove_callbacks()201 result = foo(10, 20)202 self.assertEqual(result, (10, 20))203 self.assertEqual(called_order, ['cb1','cb2','cb3', 'cb3'])204 def test_labels(self):205 result = foo(10, 20)206 self.assertEqual(result, (10, 20))207 self.assertEqual(len(called_order), 0)208 l1 = foo.add_callback(cb1, label=1)209 l2 = foo.add_pre_callback(cb2, label=2)210 l3 = foo.add_callback(cb3)211 self.assertEqual(l1, 1)212 self.assertEqual(l2, 2)...

Full Screen

Full Screen

test_exceptions.py

Source:test_exceptions.py Github

copy

Full Screen

1from __future__ import absolute_import2import unittest3from callbacks import supports_callbacks4called_with = []5called_order = []6class foo_error(RuntimeError):7 def __eq__(self, other):8 return isinstance(other, self.__class__)9class c5_error(ValueError):10 def __eq__(self, other):11 return isinstance(other, self.__class__)12def c1():13 called_order.append('c1')14def c2(exception):15 called_with.append((exception,))16 called_order.append('c2')17 raise exception18def c3(*args, **kwargs):19 called_with.append((args, kwargs))20 called_order.append('c3')21def c4(exception, *args, **kwargs):22 called_with.append((exception, args, kwargs))23 called_order.append('c4')24 return 'c4 returned this'25def c5(exception, *args, **kwargs):26 called_with.append((exception, args, kwargs))27 called_order.append('c5')28 raise c5_error29@supports_callbacks30def foo(bar, baz='bone'):31 raise foo_error32 return (bar, baz)33class TestExceptions(unittest.TestCase):34 def setUp(self):35 while called_with:36 called_with.pop()37 while called_order:38 called_order.pop()39 foo.remove_callbacks()40 def test_c1(self):41 foo.add_exception_callback(c1)42 self.assertRaises(foo_error, foo, 1, baz=2)43 self.assertEqual(len(called_with), 0)44 def test_c2(self):45 foo.add_exception_callback(c2, handles_exception=True)46 self.assertRaises(foo_error, foo, 1, baz=2)47 self.assertEqual(len(called_with), 1)48 self.assertTrue(isinstance(called_with[0][0], foo_error))49 def test_c3(self):50 foo.add_exception_callback(c3, takes_target_args=True)51 self.assertRaises(foo_error, foo, 1, baz=2)52 self.assertEqual(len(called_with), 1)53 self.assertEqual(called_with[0][0], (1,))54 self.assertEqual(called_with[0][1], {'baz':2})55 def test_c2_and_3(self):56 foo.add_exception_callback(c2, handles_exception=True)57 foo.add_exception_callback(c3, takes_target_args=True)58 self.assertRaises(foo_error, foo, 1, baz=2)59 self.assertEqual(len(called_with), 2)60 self.assertTrue(isinstance(called_with[0][0], foo_error))61 self.assertEqual(called_with[1][0], (1,))62 self.assertEqual(called_with[1][1], {'baz':2})63 def test_c4(self):64 foo.add_exception_callback(c4, handles_exception=True,65 takes_target_args=True)66 result = foo(1, baz=2)67 self.assertEqual('c4 returned this', result)68 self.assertEqual(len(called_with), 1)69 expected_called_with = [70 (foo_error(), (1,), {'baz':2})71 ]72 self.assertEqual(expected_called_with, called_with)73 def test_c4_without_takes_args(self):74 foo.add_exception_callback(c4, handles_exception=True)75 result = foo(1, baz=2)76 self.assertEqual('c4 returned this', result)77 expected_called_with = [(foo_error(), tuple(), {})]78 self.assertEqual(expected_called_with, called_with)79 def test_c5(self):80 foo.add_exception_callback(c5, handles_exception=True)81 self.assertRaises(c5_error, foo, 1, baz=2)82 expected_called_with = [(foo_error(), tuple(), {})]83 self.assertEqual(expected_called_with, called_with)84 def test_c5_takes_target_args(self):85 foo.add_exception_callback(c5, takes_target_args=True, handles_exception=True)86 self.assertRaises(c5_error, foo, 1, baz=2)87 self.assertEqual(len(called_with), 1)88 expected_called_with = [(foo_error(), (1,), {'baz': 2})]89 self.assertEqual(expected_called_with, called_with)90 def test_c3_and_c4_and_c5(self):91 foo.add_exception_callback(c3, priority=0.1, takes_target_args=True)92 foo.add_exception_callback(c5, priority=0.2, handles_exception=True)93 foo.add_exception_callback(c4, priority=0.3, takes_target_args=True,94 handles_exception=True)95 result = foo(1, baz=2)96 self.assertEqual('c4 returned this', result)97 self.assertEqual(['c4', 'c3'], called_order)98 self.assertEqual(len(called_with), 2)99 expected_called_with = [100 (foo_error(), (1,), {'baz':2}),101 ((1,), {'baz':2}),102 ]103 self.assertEqual(expected_called_with, called_with)104 def test_pre_post1(self):105 foo.add_pre_callback(c3, takes_target_args=True)106 foo.add_post_callback(c3, takes_target_args=True)107 foo.add_exception_callback(c4, takes_target_args=True,108 handles_exception=True)109 result = foo(1, baz=2)110 self.assertEqual('c4 returned this', result)111 self.assertEqual(['c3', 'c4', 'c3'], called_order)112 self.assertEqual(len(called_with), 3)113 expected_called_with = [114 ((1,), {'baz':2}),115 (foo_error(), (1,), {'baz':2}),116 ((1,), {'baz':2}),117 ]118 self.assertEqual(expected_called_with, called_with)119 def test_pre_post2(self):120 foo.add_pre_callback(c3, takes_target_args=True)121 foo.add_post_callback(c3, takes_target_args=True)122 foo.add_exception_callback(c5, handles_exception=True)123 self.assertRaises(c5_error, foo, 1, baz=2)124 self.assertEqual(['c3', 'c5'], called_order)125 self.assertEqual(len(called_with), 2)126 expected_called_with = [((1,), {'baz': 2}), (foo_error(), (), {})]...

Full Screen

Full Screen

test_localfs.py

Source:test_localfs.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2"""3Tests the localfs tokens interface.4"""5from __future__ import absolute_import, print_function, unicode_literals6import os7import salt.exceptions8import salt.tokens.localfs9import salt.utils.files10from tests.support.helpers import with_tempdir11from tests.support.mock import patch12from tests.support.unit import TestCase13class CalledWith(object):14 def __init__(self, func, called_with=None):15 self.func = func16 if called_with is None:17 self.called_with = []18 else:19 self.called_with = called_with20 def __call__(self, *args, **kwargs):21 self.called_with.append((args, kwargs))22 return self.func(*args, **kwargs)23class WriteTokenTest(TestCase):24 @with_tempdir()25 def test_write_token(self, tmpdir):26 """27 Validate tokens put in place with an atomic move28 """29 opts = {"token_dir": tmpdir}30 fopen = CalledWith(salt.utils.files.fopen)31 rename = CalledWith(os.rename)32 with patch("salt.utils.files.fopen", fopen), patch("os.rename", rename):33 tdata = salt.tokens.localfs.mk_token(opts, {})34 assert "token" in tdata35 t_path = os.path.join(tmpdir, tdata["token"])36 temp_t_path = "{}.tmp".format(t_path)37 assert len(fopen.called_with) == 1, len(fopen.called_with)38 assert fopen.called_with == [((temp_t_path, "w+b"), {})], fopen.called_with39 assert len(rename.called_with) == 1, len(rename.called_with)40 assert rename.called_with == [((temp_t_path, t_path), {})], rename.called_with41class TestLocalFS(TestCase):42 def setUp(self):43 # Default expected data44 self.expected_data = {"this": "is", "some": "token data"}45 @with_tempdir()46 def test_get_token_should_return_token_if_exists(self, tempdir):47 opts = {"token_dir": tempdir}48 tok = salt.tokens.localfs.mk_token(opts=opts, tdata=self.expected_data,)[49 "token"50 ]51 actual_data = salt.tokens.localfs.get_token(opts=opts, tok=tok)52 self.assertDictEqual(self.expected_data, actual_data)53 @with_tempdir()54 def test_get_token_should_raise_SaltDeserializationError_if_token_file_is_empty(55 self, tempdir56 ):57 opts = {"token_dir": tempdir}58 tok = salt.tokens.localfs.mk_token(opts=opts, tdata=self.expected_data,)[59 "token"60 ]61 with salt.utils.files.fopen(os.path.join(tempdir, tok), "w") as f:62 f.truncate()63 with self.assertRaises(salt.exceptions.SaltDeserializationError) as e:64 salt.tokens.localfs.get_token(opts=opts, tok=tok)65 @with_tempdir()66 def test_get_token_should_raise_SaltDeserializationError_if_token_file_is_malformed(67 self, tempdir68 ):69 opts = {"token_dir": tempdir}70 tok = salt.tokens.localfs.mk_token(opts=opts, tdata=self.expected_data,)[71 "token"72 ]73 with salt.utils.files.fopen(os.path.join(tempdir, tok), "w") as f:74 f.truncate()75 f.write("this is not valid msgpack data")76 with self.assertRaises(salt.exceptions.SaltDeserializationError) as e:...

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 Sure 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