How to use create_mock method in autotest

Best Python code snippet using autotest_python

test_topological_order.py

Source:test_topological_order.py Github

copy

Full Screen

...5 from catkin_pkg.topological_order import topological_order_packages, _PackageDecorator, \6 _sort_decorated_packages7except ImportError as e:8 raise ImportError('Please adjust your PYTHONPATH before running this test: %s' % str(e))9def create_mock(name, build_depends, run_depends, path):10 m = Mock()11 m.name = name12 m.build_depends = build_depends13 m.buildtool_depends = []14 m.run_depends = run_depends15 m.test_depends = []16 m.group_depends = []17 m.exports = []18 m.path = path19 return m20class TopologicalOrderTest(unittest.TestCase):21 def test_topological_order_packages(self):22 mc = create_mock('c', [], [], 'pc')23 md = create_mock('d', [], [], 'pd')24 ma = create_mock('a', [mc], [md], 'pa')25 mb = create_mock('b', [ma], [], 'pb')26 packages = {ma.path: ma,27 mb.path: mb,28 mc.path: mc,29 md.path: md}30 ordered_packages = topological_order_packages(packages, blacklisted=['c'])31 # d before b because of the run dependency from a to d32 # a before d only because of alphabetic order, a run depend on d should not influence the order33 self.assertEqual(['pa', 'pd', 'pb'], [path for path, _ in ordered_packages])34 ordered_packages = topological_order_packages(packages, whitelisted=['a', 'b', 'c'])35 # c before a because of the run dependency from a to c36 self.assertEqual(['pc', 'pa', 'pb'], [path for path, _ in ordered_packages])37 def test_topological_order_packages_with_duplicates(self):38 pkg1 = create_mock('pkg', [], [], 'path/to/pkg1')39 pkg2_dep = create_mock('pkg_dep', [], [], 'path/to/pkg2_dep')40 pkg2 = create_mock('pkg', [pkg2_dep], [], 'path/to/pkg2')41 if hasattr(self, 'assertRaisesRegexp'):42 with self.assertRaisesRegexp(RuntimeError, 'Two packages with the same name "pkg" in the workspace'):43 topological_order_packages({44 pkg1.path: pkg1,45 pkg2_dep.path: pkg2_dep,46 pkg2.path: pkg2,47 })48 def test_package_decorator_init(self):49 mockproject = Mock()50 mockexport = Mock()51 mockexport.tagname = 'message_generator'52 mockexport.content = 'foolang'53 mockproject.exports = [mockexport]54 pd = _PackageDecorator(mockproject, 'foo/bar')55 self.assertEqual(mockproject.name, pd.name)56 self.assertEqual('foo/bar', pd.path)57 self.assertFalse(pd.is_metapackage)58 self.assertEqual(mockexport.content, pd.message_generator)59 self.assertIsNotNone(str(pd))60 def test_calculate_depends_for_topological_order(self):61 def create_mock(name, run_depends):62 m = Mock()63 m.name = name64 m.build_depends = []65 m.buildtool_depends = []66 m.run_depends = run_depends67 m.group_depends = []68 m.exports = []69 return m70 mockproject1 = _PackageDecorator(create_mock('n1', []), 'p1')71 mockproject2 = _PackageDecorator(create_mock('n2', []), 'p2')72 mockproject3 = _PackageDecorator(create_mock('n3', []), 'p3')73 mockproject4 = _PackageDecorator(create_mock('n4', []), 'p4')74 mockproject5 = _PackageDecorator(create_mock('n5', [mockproject4]), 'p5')75 mockproject6 = _PackageDecorator(create_mock('n6', [mockproject5]), 'p6')76 mockproject7 = _PackageDecorator(create_mock('n7', []), 'p7')77 mockproject = Mock()78 mockproject.build_depends = [mockproject1, mockproject2]79 mockproject.buildtool_depends = [mockproject3, mockproject6]80 mockproject.run_depends = [mockproject7]81 mockproject.test_depends = []82 mockproject.group_depends = []83 mockproject.exports = []84 pd = _PackageDecorator(mockproject, 'foo/bar')85 # 2 and 3 as external dependencies86 packages = {mockproject1.name: mockproject1,87 mockproject4.name: mockproject4,88 mockproject5.name: mockproject5,89 mockproject6.name: mockproject6}90 pd.calculate_depends_for_topological_order(packages)91 self.assertEqual(set([mockproject1.name, mockproject4.name, mockproject5.name, mockproject6.name]),92 pd.depends_for_topological_order)93 def test_sort_decorated_packages(self):94 projects = {}95 sprojects = _sort_decorated_packages(projects)96 self.assertEqual([], sprojects)97 def create_mock(path):98 m = Mock()99 m.path = path100 m.depends_for_topological_order = set()101 m.message_generator = False102 return m103 mock1 = create_mock('mock1')104 mock2 = create_mock('mock2')105 mock3 = create_mock('mock3')106 mock3.message_generator = True107 projects = {'mock3': mock3, 'mock2': mock2, 'mock1': mock1}108 sprojects = _sort_decorated_packages(projects)109 # mock3 first since it is a message generator110 # mock1 before mock2 due to alphabetic order111 self.assertEqual(['mock3', 'mock1', 'mock2'], [path for path, _ in sprojects])112 def test_sort_decorated_packages_favoring_message_generators(self):113 def create_mock(path):114 m = Mock()115 m.path = path116 m.depends_for_topological_order = set()117 m.message_generator = False118 return m119 mock1 = create_mock('mock1')120 mock2 = create_mock('mock2')121 mock3 = create_mock('mock3')122 mock3.depends_for_topological_order = set(['mock2'])123 mock3.message_generator = True124 projects = {'mock3': mock3, 'mock2': mock2, 'mock1': mock1}125 sprojects = _sort_decorated_packages(projects)126 # mock2 first since it is the dependency of a message generator127 # mock3 since it is a message generator128 # mock1 last, although having no dependencies and being first in alphabetic order129 self.assertEqual(['mock2', 'mock3', 'mock1'], [path for path, _ in sprojects])130 def test_sort_decorated_packages_cycles(self):131 def create_mock(path, depend):132 m = Mock()133 m.path = path134 m.depends_for_topological_order = set([depend])135 m.message_generator = False136 return m137 # creating a cycle for cycle detection138 mock1 = create_mock('mock1', 'mock2')139 mock2 = create_mock('mock2', 'mock3')140 mock3 = create_mock('mock3', 'mock4')141 mock4 = create_mock('mock4', 'mock2')142 projects = {'mock3': mock3, 'mock2': mock2, 'mock1': mock1, 'mock4': mock4}143 sprojects = _sort_decorated_packages(projects)144 self.assertEqual([[None, 'mock2, mock3, mock4']], sprojects)145 # remove cycle146 mock4.depends_for_topological_order = set()147 sprojects = _sort_decorated_packages(projects)148 # mock4 first since it has no dependencies149 # than mock3 since it only had mock4 as a dependency150 # than mock2 since it only had mock3 as a dependency151 # than mock1 since it only had mock2 as a dependency152 self.assertEqual(['mock4', 'mock3', 'mock2', 'mock1'], [path for path, _ in sprojects])153 def test_topological_order_packages_with_underlay(self):154 def create_mock(name, build_depends, path):155 m = Mock()156 m.name = name157 m.build_depends = build_depends158 m.buildtool_depends = []159 m.run_depends = []160 m.test_depends = []161 m.group_depends = []162 m.exports = []163 m.path = path164 return m165 mc = create_mock('c', [], 'pc')166 mb = create_mock('b', [mc], 'pb')167 ma = create_mock('a', [mb], 'pa')168 packages = {ma.path: ma,169 mc.path: mc}170 underlay_packages = {mb.path: mb}171 ordered_packages = topological_order_packages(packages, underlay_packages=underlay_packages)172 # c before a because of the indirect dependency via b which is part of an underlay173 self.assertEqual(['pc', 'pa'], [path for path, _ in ordered_packages])174 def test_topological_order_packages_cycles(self):175 def create_mock(name, build_depends, path):176 m = Mock()177 m.name = name178 m.build_depends = build_depends179 m.buildtool_depends = []180 m.test_depends = []181 m.run_depends = []182 m.group_depends = []183 m.exports = []184 m.path = path185 return m186 mc = create_mock('c', [], 'pc')187 mb = create_mock('b', [mc], 'pb')188 ma = create_mock('a', [mb], 'pa')189 mc.build_depends = [ma]190 packages = {ma.path: ma,191 mb.math: mb,192 mc.path: mc}193 ordered_packages = topological_order_packages(packages)...

Full Screen

Full Screen

test_models.py

Source:test_models.py Github

copy

Full Screen

1import datetime2import mock3import pytest4import pytz5from django_dynamic_fixture import G6from freezegun import freeze_time7from appointment.models import Appointment, AppointmentStatus8from client.models import Client9from salon.models import Salon, Stylist10class TestAppointment(object):11 @pytest.mark.django_db12 @freeze_time('2018-11-24 10:00:00 EST')13 @mock.patch('appointment.models.build_oauth_http_object_from_tokens')14 @mock.patch('appointment.models.create_calendar_event')15 def test_create_stylist_google_calendar_event(self, create_mock, build_http_mock):16 salon: Salon = G(Salon, timezone=pytz.timezone('America/New_York'))17 stylist: Stylist = G(Stylist, salon=salon)18 appointment: Appointment = G(19 Appointment, status=AppointmentStatus.CANCELLED_BY_CLIENT,20 stylist=stylist, stylist_google_calendar_id='some_id'21 )22 appointment.create_stylist_google_calendar_event()23 build_http_mock.assert_not_called()24 create_mock.assert_not_called()25 create_mock.return_value = 'new_event_id'26 stylist.google_access_token = 'access_token'27 stylist.google_refresh_token = 'refresh_token'28 stylist.google_integration_added_at = pytz.timezone('America/New_York').localize(29 datetime.datetime(2018, 11, 23, 0, 0)30 )31 stylist.save()32 appointment.create_stylist_google_calendar_event()33 build_http_mock.assert_not_called()34 create_mock.assert_not_called()35 appointment.create_stylist_google_calendar_event()36 build_http_mock.assert_not_called()37 create_mock.assert_not_called()38 appointment.stylist_google_calendar_id = None39 appointment.save()40 appointment.create_stylist_google_calendar_event()41 build_http_mock.assert_not_called()42 create_mock.assert_not_called()43 appointment.status = AppointmentStatus.NEW44 appointment.save()45 appointment.create_stylist_google_calendar_event()46 build_http_mock.assert_called_once_with(47 access_token=stylist.google_access_token,48 refresh_token=stylist.google_refresh_token,49 model_object_to_update=stylist,50 access_token_field='google_access_token'51 )52 assert(create_mock.call_count == 1)53 assert(create_mock.call_args[1]['start_at'] == appointment.datetime_start_at)54 assert(55 create_mock.call_args[1]['end_at'] ==56 appointment.datetime_start_at + appointment.stylist.service_time_gap)57 appointment.refresh_from_db()58 assert(appointment.stylist_google_calendar_id == 'new_event_id')59 @pytest.mark.django_db60 @freeze_time('2018-11-24 10:00:00 EST')61 @mock.patch('appointment.models.build_oauth_http_object_from_tokens')62 @mock.patch('appointment.models.cancel_calendar_event')63 def test_cancel_stylist_google_calendar_event(self, cancel_mock, build_http_mock):64 salon: Salon = G(Salon, timezone=pytz.timezone('America/New_York'))65 stylist: Stylist = G(66 Stylist, salon=salon, google_access_token='access_token',67 google_refresh_token='refresh_token'68 )69 appointment: Appointment = G(70 Appointment, status=AppointmentStatus.NEW,71 stylist=stylist, stylist_google_calendar_id='some_id'72 )73 appointment.cancel_stylist_google_calendar_event()74 build_http_mock.assert_not_called()75 cancel_mock.assert_not_called()76 appointment.status = AppointmentStatus.CANCELLED_BY_CLIENT77 appointment.save()78 appointment.cancel_stylist_google_calendar_event()79 build_http_mock.assert_called_once_with(80 access_token=stylist.google_access_token,81 refresh_token=stylist.google_refresh_token,82 model_object_to_update=stylist,83 access_token_field='google_access_token'84 )85 assert(cancel_mock.call_count == 1)86 assert (cancel_mock.call_args[1]['event_id'] == 'some_id')87 @pytest.mark.django_db88 @freeze_time('2018-11-24 10:00:00 EST')89 @mock.patch('appointment.models.build_oauth_http_object_from_tokens')90 @mock.patch('appointment.models.create_calendar_event')91 def test_create_client_google_calendar_event(self, create_mock, build_http_mock):92 salon: Salon = G(Salon, timezone=pytz.timezone('America/New_York'))93 stylist = G(Stylist, salon=salon)94 client: Client = G(Client)95 appointment: Appointment = G(96 Appointment, status=AppointmentStatus.CANCELLED_BY_CLIENT,97 stylist=stylist, stylist_google_calendar_id='some_id',98 client=client99 )100 appointment.create_client_google_calendar_event()101 build_http_mock.assert_not_called()102 create_mock.assert_not_called()103 create_mock.return_value = 'new_event_id'104 client.google_access_token = 'access_token'105 client.google_refresh_token = 'refresh_token'106 client.google_integration_added_at = pytz.timezone('America/New_York').localize(107 datetime.datetime(2018, 11, 23, 0, 0)108 )109 client.save()110 appointment.create_client_google_calendar_event()111 build_http_mock.assert_not_called()112 create_mock.assert_not_called()113 appointment.create_client_google_calendar_event()114 build_http_mock.assert_not_called()115 create_mock.assert_not_called()116 appointment.client_google_calendar_id = None117 appointment.save()118 appointment.create_client_google_calendar_event()119 build_http_mock.assert_not_called()120 create_mock.assert_not_called()121 appointment.status = AppointmentStatus.NEW122 appointment.save()123 appointment.create_client_google_calendar_event()124 build_http_mock.assert_called_once_with(125 access_token=client.google_access_token,126 refresh_token=client.google_refresh_token,127 model_object_to_update=client,128 access_token_field='google_access_token'129 )130 assert(create_mock.call_count == 1)131 assert(create_mock.call_args[1]['start_at'] == appointment.datetime_start_at)132 assert(133 create_mock.call_args[1]['end_at'] ==134 appointment.datetime_start_at + appointment.stylist.service_time_gap)135 appointment.refresh_from_db()136 assert(appointment.client_google_calendar_id == 'new_event_id')137 @pytest.mark.django_db138 @freeze_time('2018-11-24 10:00:00 EST')139 @mock.patch('appointment.models.build_oauth_http_object_from_tokens')140 @mock.patch('appointment.models.cancel_calendar_event')141 def test_cancel_client_google_calendar_event(self, cancel_mock, build_http_mock):142 salon: Salon = G(Salon, timezone=pytz.timezone('America/New_York'))143 stylist: Stylist = G(144 Stylist, salon=salon145 )146 client: Client = G(147 Client, google_access_token='access_token', google_refresh_token='refresh_token')148 appointment: Appointment = G(149 Appointment, status=AppointmentStatus.NEW, client=client,150 stylist=stylist, client_google_calendar_id='some_id'151 )152 appointment.cancel_client_google_calendar_event()153 build_http_mock.assert_not_called()154 cancel_mock.assert_not_called()155 appointment.status = AppointmentStatus.CANCELLED_BY_CLIENT156 appointment.save()157 appointment.cancel_client_google_calendar_event()158 build_http_mock.assert_called_once_with(159 access_token=client.google_access_token,160 refresh_token=client.google_refresh_token,161 model_object_to_update=client,162 access_token_field='google_access_token'163 )164 assert(cancel_mock.call_count == 1)...

Full Screen

Full Screen

test_environment.py

Source:test_environment.py Github

copy

Full Screen

...37class Failure(Layer):38 user_exception = True39 def run(self, metadata):40 raise FailureException()41def create_mock():42 m = mock.Mock()43 # need to manually set those44 def enter(self):45 self.setup()46 return self47 def exit(self, type, value, traceback):48 self.teardown()49 m.__enter__ = enter50 m.__exit__ = exit51 m.__call__ = mock.Mock()52 return m53def test_exception_return():54 # the last layer is not called, the error is swallowed55 hooks = str(Path(HERE, "data", "hook.py"))56 mach, metadata, env = get_running_env(hooks=hooks)57 last_layer = create_mock()58 env.layers = [create_mock(), Failure(env, mach), last_layer]59 with env:60 env.run(metadata)61 last_layer.assert_not_called()62def test_exception_resume():63 # the last layer is called, the error is swallowed64 hooks = str(Path(HERE, "data", "hook_resume.py"))65 mach, metadata, env = get_running_env(hooks=hooks)66 last_layer = create_mock()67 env.layers = [create_mock(), Failure(env, mach), last_layer]68 with env:69 env.run(metadata)70 last_layer.assert_called()71def test_exception_no_user_exception():72 # the last layer is called, the error is raised73 # because user_exception = False74 hooks = str(Path(HERE, "data", "hook_resume.py"))75 mach, metadata, env = get_running_env(hooks=hooks)76 last_layer = create_mock()77 f = Failure(env, mach)78 f.user_exception = False79 env.layers = [create_mock(), f, last_layer]80 with env, pytest.raises(FailureException):81 env.run(metadata)82 last_layer._call__.assert_not_called()83def test_exception_raised():84 # the error is raised85 hooks = str(Path(HERE, "data", "hook_raises.py"))86 mach, metadata, env = get_running_env(hooks=hooks)87 last_layer = create_mock()88 env.layers = [create_mock(), Failure(env, mach), last_layer]89 with env, pytest.raises(FailureException):90 env.run(metadata)91 last_layer.__call__.assert_not_called()92def test_metrics_last():93 mach, metadata, env = get_running_env()94 system = create_mock()95 browser = create_mock()96 # Check that the metrics layer is entered after97 # other have finished and that the other layers98 # were only called once99 class M:100 def __enter__(self):101 system.setup.assert_called_once()102 browser.setup.assert_called_once()103 system.teardown.assert_called_once()104 browser.teardown.assert_called_once()105 return self106 def __exit__(self, *args, **kw):107 return108 def __call__(self, metadata):109 return...

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