def test_validation_iteration_events_are_fired_when_validate_is_called_explicitly( ): max_epochs = 5 num_batches = 3 validation_data = _create_mock_data_loader(max_epochs, num_batches) trainer = Trainer(training_data=[None], validation_data=validation_data, training_update_function=MagicMock(), validation_inference_function=MagicMock(return_value=1)) mock_manager = Mock() iteration_started = Mock() trainer.add_event_handler(TrainingEvents.VALIDATION_ITERATION_STARTED, iteration_started) iteration_complete = Mock() trainer.add_event_handler(TrainingEvents.VALIDATION_ITERATION_COMPLETED, iteration_complete) mock_manager.attach_mock(iteration_started, 'iteration_started') mock_manager.attach_mock(iteration_complete, 'iteration_complete') assert iteration_started.call_count == 0 assert iteration_complete.call_count == 0 trainer.validate() assert iteration_started.call_count == num_batches assert iteration_complete.call_count == num_batches
def test_start_stop_order(self): s = Spawnable() f1, f1_stop = Mock(), Mock() f2, f2_stop = Mock(), Mock() f3 = Mock() f4, f4_stop = Mock(), Mock() stop_parent = Mock() stop_parent.attach_mock(f1_stop, "f1_stop") stop_parent.attach_mock(f2_stop, "f2_stop") stop_parent.attach_mock(f4_stop, "f4_stop") s.add_spawn_function(f1, f1_stop) s.add_spawn_function(f2, f2_stop) s.add_spawn_function(f3) s.add_spawn_function(f4, f4_stop) process = Mock() s.start(process) self.assertEquals([call(f1), call(f2), call(f3), call(f4)], process.spawn.call_args_list) s.stop() self.assertEquals( [call.f4_stop(), call.f2_stop(), call.f1_stop()], stop_parent.method_calls)
def test_publish_trim(self): # Arrange subscriptionmanager.__manager__ = None redis_mock = Mock() publish_mock = Mock() redis_mock.attach_mock(publish_mock, "publish") manager = GetManager() manager.trc = redis_mock condition1 = {"id": "test1"} fields = ["name", "file"] resource = { "id": "test1", "name": "test1", "file": "AnnaK", "value": "Tols" } channel1 = manager.createChannel(conditions=condition1, collection="test", fields=fields) # Act manager.publish(resource, "test") # Assert self.assertEqual(json.loads(redis_mock.publish.call_args[0][1]), { "name": "test1", "file": "AnnaK" }) manager.subscriptions = []
def test_publish_in(self): # Arrange subscriptionmanager.__manager__ = None redis_mock = Mock() publish_mock = Mock() redis_mock.attach_mock(publish_mock, "publish") manager = GetManager() manager.trc = redis_mock condition1 = {"id": {"in": [10, 3]}} resource1 = {"id": 10, "data": "stuff"} resource2 = {"id": 5, "data": "stuff_also"} resource3 = {"id": 3, "data": "stuff_more"} channel1 = manager.createChannel(conditions=condition1, collection="test_coll1", fields=None) # Act manager.publish(resource1, "test_coll1") manager.publish(resource2, "test_coll1") manager.publish(resource3, "test_coll1") # Assert self.assertEqual(redis_mock.publish.call_count, 2) self.assertEqual(redis_mock.publish.call_args[0][0], channel1) self.assertEqual( json.loads(redis_mock.publish.call_args_list[0][0][1]), resource1) self.assertEqual( json.loads(redis_mock.publish.call_args_list[1][0][1]), resource3) manager.subscriptions = []
def it_can_unmarshal_relationships(self): # test data -------------------- reltype = 'http://reltype' # mockery ---------------------- pkg_reader = Mock(name='pkg_reader') pkg_reader.iter_srels.return_value = ( ('/', Mock(name='srel1', rId='rId1', reltype=reltype, target_partname='partname1', is_external=False)), ('/', Mock(name='srel2', rId='rId2', reltype=reltype, target_ref='target_ref_1', is_external=True)), ('partname1', Mock(name='srel3', rId='rId3', reltype=reltype, target_partname='partname2', is_external=False)), ('partname2', Mock(name='srel4', rId='rId4', reltype=reltype, target_ref='target_ref_2', is_external=True)), ) pkg = Mock(name='pkg') parts = {} for num in range(1, 3): name = 'part%d' % num part = Mock(name=name) parts['partname%d' % num] = part pkg.attach_mock(part, name) # exercise --------------------- Unmarshaller._unmarshal_relationships(pkg_reader, pkg, parts) # verify ----------------------- expected_pkg_calls = [ call.load_rel(reltype, parts['partname1'], 'rId1', False), call.load_rel(reltype, 'target_ref_1', 'rId2', True), call.part1.load_rel(reltype, parts['partname2'], 'rId3', False), call.part2.load_rel(reltype, 'target_ref_2', 'rId4', True), ] assert pkg.mock_calls == expected_pkg_calls
def test_iteration_events_are_fired(): max_epochs = 5 num_batches = 3 data = _create_mock_data_loader(max_epochs, num_batches) engine = Engine(MagicMock(return_value=1)) mock_manager = Mock() iteration_started = Mock() engine.add_event_handler(Events.ITERATION_STARTED, iteration_started) iteration_complete = Mock() engine.add_event_handler(Events.ITERATION_COMPLETED, iteration_complete) mock_manager.attach_mock(iteration_started, 'iteration_started') mock_manager.attach_mock(iteration_complete, 'iteration_complete') state = engine.run(data, max_epochs=max_epochs) assert iteration_started.call_count == num_batches * max_epochs assert iteration_complete.call_count == num_batches * max_epochs expected_calls = [] for i in range(max_epochs * num_batches): expected_calls.append(call.iteration_started(engine)) expected_calls.append(call.iteration_complete(engine)) assert mock_manager.mock_calls == expected_calls
def test_act_must_abort_hooks_after_exception(self): # ie. after a hook raises an exception, subsequent hooks must NOT be run # Create three plugins, and setup hook methods on it plugin1 = _make_mock_plugin("plugin1") setattr(plugin1, "on_" + self.my_event.name, Mock()) plugin2 = _make_mock_plugin("plugin2") setattr(plugin2, "on_" + self.my_event.name, Mock()) plugin3 = _make_mock_plugin("plugin3") setattr(plugin3, "on_" + self.my_event.name, Mock()) # Create a parent mock and attach child mocks to help assert order of the calls # https://stackoverflow.com/questions/32463321/how-to-assert-method-call-order-with-python-mock parent_mock = Mock() parent_mock.attach_mock(plugin1.on_my_event, "plugin1_hook") parent_mock.attach_mock(plugin2.on_my_event, "plugin2_hook") parent_mock.attach_mock(plugin3.on_my_event, "plugin3_hook") self.sam_plugins.register(plugin1) self.sam_plugins.register(plugin2) self.sam_plugins.register(plugin3) # setup plugin2 to raise exception plugin2.on_my_event.side_effect = IOError # Call the act method with self.assertRaises(IOError): self.sam_plugins.act(self.my_event) # Since Plugin2 raised the exception, plugin3's hook must NEVER be called parent_mock.assert_has_calls( [call.plugin1_hook(), call.plugin2_hook()])
def get_client_and_mock(): rest_call = Mock() rest_call.return_value = {"success": True} rest = Mock() rest.attach_mock(rest_call, "request") client = snowflake.connector.telemetry.TelemetryClient(rest, 2) return (client, rest_call)
def test_publish_equal(self): # Arrange subscriptionmanager.__manager__ = None redis_mock = Mock() publish_mock = Mock() redis_mock.attach_mock(publish_mock, "publish") manager = GetManager() manager.trc = redis_mock condition1 = {"id": {"equal": "test1"}} resource = {"id": "test1", "data": "stuff"} channel1 = manager.createChannel(conditions=condition1, collection="test_coll1", fields=None) channel2 = manager.createChannel(conditions=condition1, collection="test_coll2", fields=None) # Act manager.publish(resource, "test_coll1") # Assert self.assertEqual(redis_mock.publish.call_count, 1) self.assertEqual(redis_mock.publish.call_args[0][0], channel1) self.assertEqual(json.loads(redis_mock.publish.call_args[0][1]), resource) manager.subscriptions = []
def test_act_must_invoke_plugins_in_sequence(self): # Create three plugins, and setup hook methods on it plugin1 = _make_mock_plugin("plugin1") setattr(plugin1, "on_" + self.my_event.name, Mock()) plugin2 = _make_mock_plugin("plugin2") setattr(plugin2, "on_" + self.my_event.name, Mock()) plugin3 = _make_mock_plugin("plugin3") setattr(plugin3, "on_" + self.my_event.name, Mock()) # Create a parent mock and attach child mocks to help assert order of the calls # https://stackoverflow.com/questions/32463321/how-to-assert-method-call-order-with-python-mock parent_mock = Mock() parent_mock.attach_mock(plugin1.on_my_event, "plugin1_hook") parent_mock.attach_mock(plugin2.on_my_event, "plugin2_hook") parent_mock.attach_mock(plugin3.on_my_event, "plugin3_hook") self.sam_plugins.register(plugin1) self.sam_plugins.register(plugin2) self.sam_plugins.register(plugin3) # Call the act method self.sam_plugins.act(self.my_event) # Verify calls were made in the specific sequence parent_mock.assert_has_calls( [call.plugin1_hook(), call.plugin2_hook(), call.plugin3_hook()])
def test_order_of_materialization(): """Make sure that the materialize process happens in order. 1. Resolve the configs 4. Validate the configs 5. Validate the configset 6. Write the configs """ mock_manager = Mock() config = Mock(output=TestSimpleSchema(a=1, b=2)) mock_manager.attach_mock(config, "config") configset_modifier = create_and_attach_mock(mock_manager, "configset_modifier") configset_validator = create_and_attach_mock(mock_manager, "configset_validator") configset = ConfigSet( configs=[config], configset_modifiers=[configset_modifier], configset_validators=[configset_validator], ) configset.materialize() assert mock_manager.mock_calls == [ call.config.resolve(), call.configset_modifier([config.output]), call.config.validate(), call.configset_validator([config.output]), call.config.write(), ]
def get_client_and_mock(): rest_call = Mock() rest_call.return_value = {u'success': True} rest = Mock() rest.attach_mock(rest_call, 'request') client = TelemetryClient(rest, 2) return (client, rest_call)
def test_validation_iteration_events_are_fired(): max_epochs = 5 num_batches = 3 validation_data = _create_mock_data_loader(max_epochs, num_batches) trainer = Trainer(training_data=[None], validation_data=validation_data, training_update_function=MagicMock(return_value=1), validation_inference_function=MagicMock(return_value=1)) mock_manager = Mock() iteration_started = Mock() trainer.add_event_handler(TrainingEvents.VALIDATION_ITERATION_STARTED, iteration_started) iteration_complete = Mock() trainer.add_event_handler(TrainingEvents.VALIDATION_ITERATION_COMPLETED, iteration_complete) mock_manager.attach_mock(iteration_started, 'iteration_started') mock_manager.attach_mock(iteration_complete, 'iteration_complete') trainer.run(max_epochs=max_epochs) assert iteration_started.call_count == num_batches * max_epochs assert iteration_complete.call_count == num_batches * max_epochs expected_calls = [] for i in range(max_epochs * num_batches): expected_calls.append(call.iteration_started(trainer)) expected_calls.append(call.iteration_complete(trainer)) assert mock_manager.mock_calls == expected_calls
def next(self): inmate = Mock() for method_name in Inmates.INMATE_DETAILS_METHOD_NAMES: new_method = Mock() new_method.return_value = getattr(self, '_Inmates__%s' % method_name)() inmate.attach_mock(new_method, method_name) self.__created_inmates.append(inmate) return inmate
def test_attaching__attach_named__propogated(): parent = Mock() child = Mock(name='named') assert mock_name(parent) is None assert mock_name(child) == 'named' parent.attach_mock(child, 'child') # attach (API) assert mock_name(child) == 'mock.child' # renamed child.foo() assert parent.mock_calls == [call.child.foo()] # propogated
def get_mocked_hierarchy(self): oxygen = Mock() oxygen.name = "O1-" hydrogen = Mock() hydrogen.name = "H+" project = Mock() project.atom_types = [oxygen, hydrogen] phase = Mock() phase.attach_mock(project, 'project') component = Mock() component.attach_mock(phase, 'phase') return oxygen, hydrogen, component, phase, project
def test_dict_and_string(self, mock_json, mock_markdown): """Test st.write with dict.""" manager = Mock() manager.attach_mock(mock_json, 'json') manager.attach_mock(mock_markdown, 'markdown') st.write('here is a dict', {'a': 1, 'b': 2}, ' and that is all') expected_calls = [ call.markdown('here is a dict'), call.json({'a': 1, 'b': 2}), call.markdown(' and that is all'), ] self.assertEqual(manager.mock_calls, expected_calls)
def test_dict_and_string(self, mock_json, mock_markdown): """Test st.write with dict.""" manager = Mock() manager.attach_mock(mock_json, "json") manager.attach_mock(mock_markdown, "markdown") st.write("here is a dict", {"a": 1, "b": 2}, " and that is all") expected_calls = [ call.markdown("here is a dict", unsafe_allow_html=False), call.json({"a": 1, "b": 2}), call.markdown(" and that is all", unsafe_allow_html=False), ] self.assertEqual(manager.mock_calls, expected_calls)
def test_init(self, adapter, logger): session = Mock(spec='keystoneauth1.session.Session') session.attach_mock(Mock(), 'auth') session.auth.auth_url = 'http://127.0.0.1' obj = client.Client(session=session) self.assertIn(call(service_type=u'ostf', session=session), adapter.mock_calls) logger.assert_has_calls((call.info( 'Initialization of NailgunClient using shared session \n' '(auth_url={})'.format(session.auth.auth_url)), )) self.assertIn('ostf', dir(obj))
class InOrder(object): def __init__(self, *mock_instances): self.mock_manager = Mock() self.instances_names = {} for index, mock_instance in enumerate(mock_instances): mock_name = 'm_{}'.format(index) self.instances_names[mock_instance] = mock_name self.mock_manager.attach_mock(mock_instance, mock_name) self.next_index = None def verify(self, mock_instance, verification=ONLY_ONCE_PREDICATE): return InOrderVerifier(self, mock_instance, verification)
def prepare_session(): session = Mock(spec='keystoneauth1.session.Session') session.attach_mock(Mock(), 'auth') session.auth.auth_url = 'http://127.0.0.1' get = Mock(name='get') post = Mock(name='post') put = Mock(name='put') delete = Mock(name='delete') session.attach_mock(get, 'get') session.attach_mock(post, 'post') session.attach_mock(put, 'put') session.attach_mock(delete, 'delete') return session
def test_add_remote_adds(mock_repo): """ GIVEN GitRepo initialized with a path and repo WHEN remote.add is called with a name and url THEN a TRUE status is returned WITH update called """ remote_mock = Mock() update_mock = Mock() remote_mock.attach_mock(update_mock, 'update') mock_repo.create_remote.return_value = remote_mock git_util = GitRepo('./', mock_repo) assert git_util.remote.add('rdo', 'http://rdoproject.org') is True assert update_mock.called is True
def mock_repo(): """repo mock fixture""" repo_mock = Mock() repo_git_mock = Mock() repo_mock.attach_mock(repo_git_mock, 'git') remote = Mock(spec=git.Remote) remote.configure_mock(name="origin", url="http://example.com") remote_list = IterableList("name") remote_list.extend([remote]) repo_mock.remotes = remote_list repo_mock.branches = [] return repo_mock
def rels_elm(self, request): """ Return a rels_elm mock that will be returned from CT_Relationships.new() """ # create rels_elm mock with a .xml property rels_elm = Mock(name='rels_elm') xml = PropertyMock(name='xml') type(rels_elm).xml = xml rels_elm.attach_mock(xml, 'xml') rels_elm.reset_mock() # to clear attach_mock call # patch CT_Relationships to return that rels_elm patch_ = patch.object(CT_Relationships, 'new', return_value=rels_elm) patch_.start() request.addfinalizer(patch_.stop) return rels_elm
def test_progress_init(self, build_progress_mock, write_to_file_mock, tmp_path, test_config, test_progress): with open(test_progress, 'r') as f: expected_xml = f.read() paths.OUTPUT_DIR = tmp_path.as_posix() build_progress_mock.return_value = et.fromstring(expected_xml) mock_manager = Mock() mock_manager.attach_mock(build_progress_mock, 'managed_build_progress') mock_manager.attach_mock(write_to_file_mock, 'managed_write_to_file') progress = Progress(config_file=test_config, config=load_json(test_config)) expected_calls = [call.managed_build_progress(load_json(test_config), test_config), call.managed_write_to_file()] assert mock_manager.mock_calls == expected_calls expected_lxml = et.fromstring(expected_xml) current_lxml = progress.progress_xml_content assert self.elements_equal(current_lxml, expected_lxml)
def test_deploy_constraint(self): fake_client = Mock(wraps=fake_juju_client()) fake_client.attach_mock(Mock(), 'deploy') fake_client.attach_mock(Mock(), 'wait_for_workloads') charm_name = 'test-constraint' charm_series = 'xenial' constraints = Constraints(mem='10GB') with temp_dir() as charm_dir: charm = os.path.join(charm_dir, charm_series, charm_name) deploy_constraint(fake_client, constraints, charm, charm_series, charm_dir) fake_client.deploy.assert_called_once_with( charm, series=charm_series, repository=charm_dir, constraints=str(constraints)) fake_client.wait_for_workloads.assert_called_once_with()
def test_aggregate_data_end_default_no_subject(self, python, aggregate_subjects, android_test_plugin_handler): android_test_plugin_handler.pluginParams = {'experiment_aggregation': 'default'} mock_profiler = Mock() mock_manager = Mock() mock_manager.attach_mock(mock_profiler.aggregate_end, 'end') mock_manager.attach_mock(aggregate_subjects, 'subjects') android_test_plugin_handler.subject_aggregated_default = False android_test_plugin_handler.subject_aggregated = False android_test_plugin_handler.currentProfiler = mock_profiler android_test_plugin_handler.aggregate_data_end('fake/dir/') expected_call_order = "[call.subjects('fake/dir/data'),\n call.end('fake/dir/data', " \ "'fake/dir/Aggregated_Results_Android1.csv')]" assert expected_call_order == str(mock_manager.mock_calls) mock_profiler.aggregate_end.assert_called_once_with('fake/dir/data', 'fake/dir/Aggregated_Results_Android1.csv') assert python.call_count == 0 aggregate_subjects.assert_called_once()
def test_0105_run_initialized_reporters_only_when_not_spinning(self): from invenio_checker.clients.master import StatusMaster from invenio_checker.api import create_task, run_task, create_reporter # Given a task with a reporter task_data = TestApi.task_data new_task = create_task(task_data) new_reporter = create_reporter(TestApi.reporter_data) Query = get_Query(task_data) self.create_records(small_rng) # ..while tracking the reporter's initialization reporterA = reimport_module('tests.demo_package.checkerext.reporters.reporterA') reporterA.get_reporter = MagicMock() # ..as well as calls to the task conflict resolver conflict_resolver = Mock(side_effect=({MagicMock(uuid=1)}, {})) mock_manager = Mock() mock_manager.attach_mock(reporterA.get_reporter, 'get_reporter') mock_manager.attach_mock(conflict_resolver, 'conflict_resolver') with patch('invenio_checker.models.CheckerRule.filepath', filepath_without_class): with patch('invenio_checker.models.Query', Query): with patch('invenio_checker.models.CheckerReporter.module', reporterA): with patch('invenio_checker.conftest.conftest_checker._worker_conflicts_with_currently_running', conflict_resolver): task_id = run_task(task_data['name']) # (Better safe than (very) sorry) execution = CheckerRuleExecution.query.filter(CheckerRuleExecution.uuid == task_id).one() assert execution.status == StatusMaster.completed # Ensure that the reporter was not initialized before no conflicts were remaining from ..conftest import contains_sublist assert contains_sublist( [call[0] for call in mock_manager.mock_calls], # func names [ 'conflict_resolver', 'conflict_resolver', 'get_reporter', ] )
def test_manager_mock(self): class Foo(object): one = 'one' two = 'two' manager = Mock() p1 = patch.object(Foo, 'one') p2 = patch.object(Foo, 'two') mock_one = p1.start() self.addCleanup(p1.stop) mock_two = p2.start() self.addCleanup(p2.stop) manager.attach_mock(mock_one, 'one') manager.attach_mock(mock_two, 'two') Foo.two() Foo.one() self.assertEqual(manager.mock_calls, [call.two(), call.one()])
def test_add_remote_update_fails(mock_repo): """ GIVEN GitRepo initialized with a path and repo WHEN remote.add is called with a name and url AND the remote update fails with an exception THEN a False status is returned WITH delete_remote called """ remote_mock = Mock() delete_mock = Mock() update_mock = Mock(side_effect=git.CommandError('update')) remote_mock.attach_mock(update_mock, 'update') mock_repo.attach_mock(delete_mock, 'delete_remote') mock_repo.create_remote.return_value = remote_mock git_util = GitRepo('./', mock_repo) assert git_util.remote.add('rdo', 'http://rdoproject.org') is False assert update_mock.called is True delete_mock.assert_called_once_with(remote_mock)