def testFunctionWithDefaults(self): def f(a, b=None): pass mock = Mock() f2 = mocksignature(f, mock) f2(3) mock.assert_called_with(3, None) mock.reset_mock() f2(1, 7) mock.assert_called_with(1, 7) mock.reset_mock() f2(b=1, a=7) mock.assert_called_with(7, 1) mock.reset_mock() a = object() def f(a=a): pass f2 = mocksignature(f, mock) f2() mock.assert_called_with(a)
def setUp(self): abstract_node_class = mocksignature(AbstractNode, AbstractNode) selector_class = mocksignature(StaticSelector, StaticSelector) self.abstract_node = abstract_node_class(self) self.abstract_node.selector = selector_class(self) self.root_node = Node(pattern='', parent=None, abstract_node=self.abstract_node)
def test_create_event_process(self): """ Test creating an event process """ # process_definition = ProcessDefinition(name='test') mock = Mock() mock.definition = 'a_definition' mock.detail = 'event_process_detail' mock_id = 'a_process_definition_id' self.mock_rr_client.read = Mock() self.mock_rr_client.read.return_value = mock self.mock_rr_client.update = Mock() self.mock_rr_client.update = mocksignature(self.mock_rr_client.update) self.mock_rr_client.find_objects = Mock() self.mock_rr_client.find_objects.return_value = ['stream_id_1'], 'obj_assoc_1' self.mock_pd_client.schedule_process = Mock() self.mock_pd_client.schedule_process.return_value = 'process_id' self.mock_rr_client.create_association = mocksignature(self.mock_rr_client.create_association) self.mock_dams_client.register_event_process = mocksignature(self.mock_dams_client.register_event_process) pid = self.event_management.create_event_process(process_definition_id=mock_id, event_types=['type_1', 'type_2'], sub_types=['subtype_1', 'subtype_2'], origins=['or_1', 'or_2'], origin_types=['t1', 't2'], out_data_products={'conductivity': 'id1'} )
def setUp(self): def config_get_args(str1, str2): if str2 == 'api_root': return sentinel.api_root else: return sentinel.promo_code self.config = Mock() self.config.get = config_get_args def testfun_nopromo(api_root, user): pass def testfun_promo(api_root, user, promo_code): pass self.testfun_nopromo_mock = Mock() self.testfun_nopromo = mocksignature(testfun_nopromo, self.testfun_nopromo_mock) self.testfun_nopromo.return_value = sentinel.testfun_nopromo self.testfun_promo_mock = Mock() self.testfun_promo = mocksignature(testfun_promo, self.testfun_promo_mock) self.testfun_promo.return_value = sentinel.testfun_promo
def testFunctionWithVarArgsAndKwargs(self): def f(a, b=None, *args, **kwargs): return (a, b, args, kwargs) f2 = mocksignature(f, f) self.assertEqual(f2(3, 4, 5, x=6, y=9), (3, 4, (5, ), { 'x': 6, 'y': 9 })) self.assertEqual(f2(3, x=6, y=9, b='a'), (3, 'a', (), { 'x': 6, 'y': 9 })) def f(*args): pass g = mocksignature(f) g.mock.return_value = 3 self.assertEqual(g(1, 2, 'many'), 3) self.assertEqual(g(), 3) self.assertRaises(TypeError, lambda: g(a=None)) def f(**kwargs): pass g = mocksignature(f) g.mock.return_value = 3 self.assertEqual(g(), 3) self.assertEqual(g(a=None, b=None), 3) self.assertRaises(TypeError, lambda: g(None))
def testIntrospection(self): def f(a, *args, **kwargs): pass f2 = mocksignature(f, f) self.assertEqual(inspect.getargspec(f), inspect.getargspec(f2)) def f(a, b=None, c=3, d=object()): pass f2 = mocksignature(f, f) self.assertEqual(inspect.getargspec(f), inspect.getargspec(f2))
def test_create_event_type(self): """ Test creating an event """ event_type = EventType(name="Test event type") self.mock_rr_client.create = mocksignature(self.mock_rr_client.create) self.mock_rr_client.create.return_value = ('event_type_id','rev_1') self.mock_dams_client.register_process = mocksignature(self.mock_dams_client.register_process) event_type_id = self.event_management.create_event_type(event_type) self.assertEquals(event_type_id, 'event_type_id') self.mock_rr_client.create.assert_called_once_with(event_type)
def test_load_cards(self): fbb = [u"Foo", u"Bar", u"Baz"] parse = mock.mocksignature(p.parse) for l, f in itertools.izip_longest(p.load_cards(S, _parse=parse), fbb): self.assertEqual(list(parse.mock.call_args[0][0]), [f] * 3)
def test_create_event_process(self): """ Test creating an event process """ process_definition = ProcessDefinition(name='test') process_definition.definition = '' rrc = ResourceRegistryServiceClient(node = self.container.node) process_definition_id = rrc.create(process_definition) self.mock_rr_client.find_objects = Mock() self.mock_rr_client.find_objects.return_value = ['stream_id_1'], 'obj_assoc_1' # self.mock_pd_client.schedule_process = Mock() # self.mock_pd_client.schedule_process.return_value = 'process_id' self.mock_rr_client.create_association = mocksignature(self.mock_rr_client.create_association) pid = self.event_management.create_event_process(process_definition_id=process_definition_id, event_types=['type_1', 'type_2'], sub_types=['subtype_1', 'subtype_2'], origins=['or_1', 'or_2'], origin_types=['t1', 't2'], out_data_products={'conductivity': 'id1'} )
def test_side_effect_from_existing_mock(self): mock = Mock(side_effect=KeyError) func = mocksignature(f, mock) self.assertRaises(KeyError, func, 1, 2, 3) mock.side_effect = NameError self.assertRaises(NameError, func, 1, 2, 3)
def test_return_value_from_existing_mock(self): mock = Mock(return_value='foo') func = mocksignature(f, mock) self.assertEqual(func(1, 2, 3), 'foo') mock.return_value = 'bar' self.assertEqual(func(1, 2, 3), 'bar')
def test_mocksignature_with_reserved_arg(self): def f(_mock_): pass self.assertRaises(AssertionError, lambda: mocksignature(f)) def f(_mock_=None): pass self.assertRaises(AssertionError, lambda: mocksignature(f)) def f(*_mock_): pass self.assertRaises(AssertionError, lambda: mocksignature(f)) def f(**_mock_): pass self.assertRaises(AssertionError, lambda: mocksignature(f))
def testMockSignatureWithReservedArg(self): def f(_mock_): pass self.assertRaises(AssertionError, lambda: mocksignature(f)) def f(_mock_=None): pass self.assertRaises(AssertionError, lambda: mocksignature(f)) def f(*_mock_): pass self.assertRaises(AssertionError, lambda: mocksignature(f)) def f(**_mock_): pass self.assertRaises(AssertionError, lambda: mocksignature(f))
def _create_service_mock(self, service_name): # set self.clients if not already set clients = Mock(name='clients') base_service = get_service_registry().get_service_base(service_name) # Save it to use in test_verify_service self.base_service = base_service self.addCleanup(delattr, self, 'base_service') dependencies = base_service.dependencies for dep_name in dependencies: dep_service = get_service_registry().get_service_base(dep_name) # Force mock service to use interface mock_service = Mock(name='clients.%s' % dep_name, spec=dep_service) setattr(clients, dep_name, mock_service) # set self.dep_name for convenience setattr(self, dep_name, mock_service) self.addCleanup(delattr, self, dep_name) iface = list(implementedBy(dep_service))[0] names_and_methods = iface.namesAndDescriptions() for func_name, _ in names_and_methods: mock_func = mocksignature(getattr(dep_service, func_name), mock=Mock(name='clients.%s.%s' % (dep_name, func_name)), skipfirst=True) setattr(mock_service, func_name, mock_func) return clients
def test_get_pod_files(): mock_os_listdir = Mock(return_value = ['foo', 'bar', 'baz']) mock_pod_executable_file_p = mocksignature(barker.pod.executable_file_p) mock_pod_executable_file_p.return_value = True with patch.object(barker.pod.os, 'listdir', mock_os_listdir): with patch.object(barker.pod, "executable_file_p", mock_pod_executable_file_p): assert_equals(list(barker.pod.get_pod_files('/barker-tests')), ['/barker-tests/foo', '/barker-tests/bar', '/barker-tests/baz'])
def test_delete_event_process(self): """ Test deleting an event process """ self.mock_rr_client.delete = mocksignature(self.mock_rr_client.delete) self.event_management.delete_event_process('event_process_id') self.mock_rr_client.delete.assert_called_once_with('event_process_id')
def test_delete_event_type(self): """ Test updating an event """ self.mock_rr_client.delete = mocksignature(self.mock_rr_client.delete) self.event_management.delete_event_type(event_type_id='event_type_id') self.mock_rr_client.delete.assert_called_once_with('event_type_id')
def test_get_nodes(self): selector = ModelSelector(projection="%(label)s", model_class=Thing) abstract_node_class = mocksignature(AbstractNode, AbstractNode) abstract_node = abstract_node_class(self) nodes = selector.get_nodes(abstract_node) self.assertIsInstance(nodes, list) self.assertGreater(len(nodes), 0)
def testMockSignatureClass(self): MockedSomething = mocksignature(Something) result = MockedSomething(5, 23) self.assertIs(result, MockedSomething.mock.return_value) MockedSomething(1) MockedSomething.mock.assert_caled_with(1, 10) self.assertRaises(TypeError, MockedSomething)
def test_update_event_process_definition(self): """ Test updating an event process definition """ process_definition_id = 'an id' process_def = ProcessDefinition() self.mock_rr_client.read = mocksignature(self.mock_rr_client.read) self.mock_rr_client.read.return_value = process_def self.mock_rr_client.update = mocksignature(self.mock_rr_client.update) self.event_management.update_event_process_definition( event_process_definition_id =process_definition_id, version='version', event_types=['t1', 't2'], sub_types=['t3'], origin_types=['or1', 'or2']) self.mock_rr_client.update.assert_called_once_with(process_def)
def test_read_event_process_definition(self): """ Test reading an event process definition """ event_process_def = Mock() self.mock_rr_client.read = mocksignature(self.mock_rr_client.read) self.mock_rr_client.read.return_value = event_process_def result = self.event_management.read_event_process_definition() self.assertEquals(result, event_process_def)
def testMockSignatureCallable(self): mocked_something = mocksignature(something) result = mocked_something(5, 23) self.assertIs(result, mocked_something.mock.return_value) mocked_something(1) mocked_something.mock.assert_caled_with(1, 5) self.assertRaises(TypeError, mocked_something)
def test_read_event_process(self): """ Test reading an event process """ event_process = Mock() self.mock_rr_client.read = mocksignature(self.mock_rr_client.read) self.mock_rr_client.read.return_value = event_process result = self.event_management.read_event_process('event_process_id') self.assertEquals(result, event_process)
def test_update_event_type(self): """ Test updating an event """ event_type = EventType(name="Test event type") self.mock_rr_client.update = mocksignature(self.mock_rr_client.update) # self.mock_rr_client.update.return_value = ('event_type_id','rev_1') self.event_management.update_event_type(event_type) self.mock_rr_client.update.assert_called_once_with(event_type)
def test_get_pod_files(): mock_os_listdir = Mock(return_value=['foo', 'bar', 'baz']) mock_pod_executable_file_p = mocksignature(barker.pod.executable_file_p) mock_pod_executable_file_p.return_value = True with patch.object(barker.pod.os, 'listdir', mock_os_listdir): with patch.object(barker.pod, "executable_file_p", mock_pod_executable_file_p): assert_equals(list(barker.pod.get_pod_files('/barker-tests')), [ '/barker-tests/foo', '/barker-tests/bar', '/barker-tests/baz' ])
def test_load_pod_dir(): mock_os_listdir = Mock(return_value = ['foo', 'bar', 'baz']) mock_pod_executable_file_p = mocksignature(barker.pod.executable_file_p) mock_pod_executable_file_p.return_value = True mock_eventlet_greenpool = Mock() mock_imap = mock_eventlet_greenpool.return_value.imap mock_imap.return_value = FAKE_POD_OUTPUT.iteritems() with patch.object(barker.pod.os, 'listdir', mock_os_listdir): with patch.object(barker.pod, "executable_file_p", mock_pod_executable_file_p): with patch.object(barker.pod.eventlet, 'GreenPool', mock_eventlet_greenpool): assert_equals(barker.pod.load_pod_dir(), FAKE_POD_OUTPUT)
def testMethod(self): class Foo(object): def method(self, a, b): pass f = Foo() mock = Mock() mock.return_value = 3 f.method = mocksignature(f.method, mock) self.assertEqual(f.method('foo', 'bar'), 3) mock.assert_called_with('foo', 'bar')
def testFunctionWithoutExplicitMock(self): def f(a): pass f2 = mocksignature(f) self.assertIsInstance(f2.mock, Mock) self.assertRaises(TypeError, f2) f2.mock.return_value = 3 self.assertEqual(f2('foo'), 3) f2.mock.assert_called_with('foo')
def test_read_event_type(self): """ Test reading an event """ event_type = Mock() self.mock_rr_client.read = mocksignature(self.mock_rr_client.read) self.mock_rr_client.read.return_value = event_type result = self.event_management.read_event_type(event_type_id='event_type_id') self.assertEquals(result, event_type) self.mock_rr_client.read.assert_called_once_with('event_type_id', '')
def test_read_node_contents(self): test_fh = open('/etc/passwd') selector = StaticSelector(projection='', contentfile_path=test_fh.name) abs_node = AbstractNode(selector=selector) node = mocksignature(Node, Node) node.is_leaf = True contents = abs_node.read_node_contents(node) self.assertIsInstance(contents, str) self.assertEqual(contents, test_fh.read())
def test_transforms(self): length = 1 packet = self._get_new_ctd_packet("STR_ID", length) self.tx_L0.process(packet) self.tx_L0.cond_publisher.publish = mocksignature( self.tx_L0.cond_publisher.publish) self.tx_L0.cond_publisher.publish.return_value = '' self.tx_L0.temp_publisher.publish = mocksignature( self.tx_L0.cond_publisher.publish) self.tx_L0.temp_publisher.publish.return_value = '' self.tx_L0.pres_publisher.publish = mocksignature( self.tx_L0.cond_publisher.publish) self.tx_L0.pres_publisher.publish.return_value = '' L0_cond = self.tx_L0.cond_publisher.publish.call_args[0][0] L0_temp = self.tx_L0.temp_publisher.publish.call_args[0][0] L0_pres = self.tx_L0.pres_publisher.publish.call_args[0][0] log.debug("L0 cond: %s" % L0_cond) log.debug("L0 temp: %s" % L0_temp) log.debug("L0 pres: %s" % L0_pres) L1_cond = self.tx_L1_C.execute(L0_cond) log.debug("L1 cond: %s" % L1_cond) L1_temp = self.tx_L1_T.execute(L0_temp) log.debug("L1 temp: %s" % L1_temp) L1_pres = self.tx_L1_P.execute(L0_pres) log.debug("L1 pres: %s" % L1_pres) L2_sal = self.tx_L2_S.execute(packet) log.debug("L2 sal: %s" % L2_sal) L2_dens = self.tx_L2_D.execute(packet) log.debug("L2 dens: %s" % L2_dens)
def test_transforms(self): length = 1 packet = self.px_ctd._get_new_ctd_packet("STR_ID", length) log.info("Packet: %s" % packet) self.tx_L0.process(packet) self.tx_L0.cond_publisher.publish = mocksignature(self.tx_L0.cond_publisher.publish) self.tx_L0.cond_publisher.publish.return_value = '' self.tx_L0.temp_publisher.publish = mocksignature(self.tx_L0.cond_publisher.publish) self.tx_L0.temp_publisher.publish.return_value = '' self.tx_L0.pres_publisher.publish = mocksignature(self.tx_L0.cond_publisher.publish) self.tx_L0.pres_publisher.publish.return_value = '' L0_cond = self.tx_L0.cond_publisher.publish.call_args[0][0] L0_temp = self.tx_L0.temp_publisher.publish.call_args[0][0] L0_pres = self.tx_L0.pres_publisher.publish.call_args[0][0] log.info("L0 cond: %s" % L0_cond) log.info("L0 temp: %s" % L0_temp) log.info("L0 pres: %s" % L0_pres) L1_cond = self.tx_L1_C.execute(L0_cond) log.info("L1 cond: %s" % L1_cond) L1_temp = self.tx_L1_T.execute(L0_temp) log.info("L1 temp: %s" % L1_temp) L1_pres = self.tx_L1_P.execute(L0_pres) log.info("L1 pres: %s" % L1_pres) L2_sal = self.tx_L2_S.execute(packet) log.info("L2 sal: %s" % L2_sal) L2_dens = self.tx_L2_D.execute(packet) log.info("L2 dens: %s" % L2_dens)
def test_load_pod_subset(): mock_os_listdir = Mock(return_value = ['foo', 'bar', 'baz']) mock_pod_executable_file_p = mocksignature(barker.pod.executable_file_p) mock_pod_executable_file_p.return_value = True mock_eventlet_greenpool = Mock() mock_imap = mock_eventlet_greenpool.return_value.imap mock_imap.return_value = FAKE_POD_SUBSET.iteritems() with patch.object(barker.pod.os, 'listdir', mock_os_listdir): with patch.object(barker.pod, "executable_file_p", mock_pod_executable_file_p): with patch.object(barker.pod.eventlet, 'GreenPool', mock_eventlet_greenpool): assert_equals(barker.pod.load_pod_dir(filter_fn=barker.pod.get_pod_filter(["test-pod1.sh", "test-pod3.rb"])), FAKE_POD_SUBSET)
def test_assert_called_with(self): func = mocksignature(f) self.assertRaises(AssertionError, func.assert_called_with) self.assertRaises(AssertionError, func.assert_called_once_with) func(1, 2, 3) func.assert_called_with(1, 2, 3) self.assertRaises(AssertionError, func.assert_called_with, 4, 5, 6) func.assert_called_once_with(1, 2, 3) self.assertRaises(AssertionError, func.assert_called_once_with, 4, 5, 6)
def test_create_event_process_definition(self): """ Test creating an event process definition """ process_definition = Mock() self.mock_pd_client.create_process_definition=mocksignature(self.mock_pd_client.create_process_definition) self.mock_pd_client.create_process_definition.return_value='procdef_id' self.mock_pd_client.schedule_process=mocksignature(self.mock_pd_client.schedule_process) self.mock_pd_client.schedule_process.return_value='pid' procdef_id = self.event_management.create_event_process_definition(version='version', module ='module', class_name='class_name', uri='uri', arguments='arguments', event_types=['t1', 't2'], sub_types=['t3'], origin_types=['or1', 'or2']) self.assertEquals(procdef_id, 'procdef_id')
def testFunction(self): def f(a): pass mock = Mock() f2 = mocksignature(f, mock) self.assertIs(f2.mock, mock) self.assertRaises(TypeError, f2) mock.return_value = 3 self.assertEqual(f2('foo'), 3) mock.assert_called_with('foo') f2.mock.assert_called_with('foo')
def test_mock_calls_mocksignature(self): def f(a, b): pass obj = Iter() obj.f = f funcs = [mocksignature(f), create_autospec(f), create_autospec(obj).f] for func in funcs: func(1, 2) func(3, 4) self.assertEqual(func.mock_calls, [call(1, 2), call(3, 4)])
def testFunctionWithVarArgsAndKwargs(self): def f(a, b=None, *args, **kwargs): return (a, b, args, kwargs) f2 = mocksignature(f, f) self.assertEqual(f2(3, 4, 5, x=6, y=9), (3, 4, (5, ), { 'x': 6, 'y': 9 })) self.assertEqual(f2(3, x=6, y=9, b='a'), (3, 'a', (), { 'x': 6, 'y': 9 }))
def test_assert_any_call(self): for mock in Mock(), mocksignature(lambda *args, **kwargs: None): mock(1, 2) mock(a=3) mock(1, b=6) mock.assert_any_call(1, 2) mock.assert_any_call(a=3) mock.assert_any_call(1, b=6) self.assertRaises(AssertionError, mock.assert_any_call) self.assertRaises(AssertionError, mock.assert_any_call, 1, 3) self.assertRaises(AssertionError, mock.assert_any_call, a=4)