我们从Python开源项目中,提取了以下23个代码示例,用于说明如何使用mock.NonCallableMock()。
def test_query_facts_with_query_and_facts_list(self): mock_pdb = mock.NonCallableMock() mock_pdb.facts = mock.Mock(return_value=[ _FakeNode('alpha', 'foo', 'bar'), ]) node_facts = self._query_facts(mock_pdb, 'foo=bar', ['foo']) mock_pdb.facts.assert_called_once_with(query=json.dumps([ 'and', ['in', 'certname', ['extract', 'certname', ['select_fact_contents', ['and', ['=', 'path', ['foo']], ['=', 'value', 'bar']]]]], ['or', ['=', 'name', 'foo']]])) self.assertEquals(node_facts, { 'alpha': {'foo': 'bar'}, })
def test_query_facts_with_query_and_facts_list_regex(self): mock_pdb = mock.NonCallableMock() mock_pdb.facts = mock.Mock(return_value=[ _FakeNode('alpha', 'foo', 'bar'), ]) node_facts = self._query_facts(mock_pdb, 'foo=bar', ['/foo/']) mock_pdb.facts.assert_called_once_with(query=json.dumps([ 'and', ['in', 'certname', ['extract', 'certname', ['select_fact_contents', ['and', ['=', 'path', ['foo']], ['=', 'value', 'bar']]]]], ['or', ['~', 'name', 'foo']]])) self.assertEquals(node_facts, { 'alpha': {'foo': 'bar'}, })
def test_get_value_from_instance_with_inner_objectfield(self): field = ObjectField(attr='person', properties={ 'first_name': StringField(analyzier='foo'), 'last_name': StringField(), 'aditional': ObjectField(properties={ 'age': IntegerField() }) }) instance = NonCallableMock(person=NonCallableMock( first_name="foo", last_name="bar", aditional=NonCallableMock(age=12) )) self.assertEqual(field.get_value_from_instance(instance), { 'first_name': "foo", 'last_name': "bar", 'aditional': {'age': 12} })
def test_get_value_from_instance_with_none_inner_objectfield(self): field = ObjectField(attr='person', properties={ 'first_name': StringField(analyzier='foo'), 'last_name': StringField(), 'aditional': ObjectField(properties={ 'age': IntegerField() }) }) instance = NonCallableMock(person=NonCallableMock( first_name="foo", last_name="bar", aditional=None )) self.assertEqual(field.get_value_from_instance(instance), { 'first_name': "foo", 'last_name': "bar", 'aditional': {} })
def test_query_facts_with_facts_list_only(self): mock_pdb = mock.NonCallableMock() mock_pdb.facts = mock.Mock(return_value=[ _FakeNode('alpha', 'foo', 'bar'), ]) node_facts = self._query_facts(mock_pdb, '', ['foo']) mock_pdb.facts.assert_called_once_with(query=json.dumps([ 'or', ['=', 'name', 'foo']])) self.assertEquals(node_facts, { 'alpha': {'foo': 'bar'}, })
def test_query_facts_in_raw_mode(self): mock_pdb = mock.NonCallableMock() mock_pdb.facts = mock.Mock(return_value=[ _FakeNode('alpha', 'foo', 'bar'), ]) node_facts = self._query_facts(mock_pdb, 'foo=bar', raw=True) self.assertEquals(node_facts, mock_pdb.facts.return_value)
def test_with_query_and_facts_list(self): mock_pdb = mock.NonCallableMock() mock_pdb.fact_contents = mock.Mock(return_value=[ { 'value': 14, 'certname': 'alpha', 'environment': 'production', 'path': ['system_uptime', 'days'], 'name': 'system_uptime', }, ]) out = self._query_fact_contents( mock_pdb, 'foo=bar', ['system_uptime.days']) mock_pdb.fact_contents.assert_called_once_with(query=json.dumps([ 'and', ['in', 'certname', ['extract', 'certname', ['select_fact_contents', ['and', ['=', 'path', ['foo']], ['=', 'value', 'bar']]]]], ['or', ['=', 'path', ['system_uptime', 'days']]]])) self.assertEquals(out, { 'alpha': {'system_uptime.days': 14}, })
def test_raw_output(self): mock_pdb = mock.NonCallableMock() mock_pdb.fact_contents = mock.Mock(return_value=[ { 'value': 14, 'certname': 'alpha', 'environment': 'production', 'path': ['system_uptime', 'days'], 'name': 'system_uptime', }, ]) out = self._query_fact_contents( mock_pdb, 'foo=bar', ['system_uptime.days'], True) mock_pdb.fact_contents.assert_called_once_with(query=json.dumps([ 'and', ['in', 'certname', ['extract', 'certname', ['select_fact_contents', ['and', ['=', 'path', ['foo']], ['=', 'value', 'bar']]]]], ['or', ['=', 'path', ['system_uptime', 'days']]]])) self.assertEquals(out, mock_pdb.fact_contents.return_value)
def test_notify_recruited(self, recruiter): dummy = mock.NonCallableMock() recruiter.notify_recruited(participant=dummy)
def test_rejects_questionnaire_from_returns_none(self, recruiter): dummy = mock.NonCallableMock() assert recruiter.rejects_questionnaire_from(participant=dummy) is None
def test_get_value_from_instance_attr(self): field = DEDField(attr='attr1') instance = NonCallableMock(attr1="foo", attr2="bar") self.assertEqual(field.get_value_from_instance(instance), "foo")
def test_get_value_from_instance_related_attr(self): field = DEDField(attr='related.attr1') instance = NonCallableMock(attr1="foo", related=NonCallableMock(attr1="bar")) self.assertEqual(field.get_value_from_instance(instance), "bar")
def test_get_value_from_instance_callable(self): field = DEDField(attr='callable') instance = NonCallableMock(callable=Mock(return_value="bar")) self.assertEqual(field.get_value_from_instance(instance), "bar")
def test_get_value_from_instance_related_callable(self): field = DEDField(attr='related.callable') instance = NonCallableMock(related=NonCallableMock( callable=Mock(return_value="bar"), attr1="foo")) self.assertEqual(field.get_value_from_instance(instance), "bar")
def test_get_value_from_none(self): field = DEDField(attr='related.none') instance = NonCallableMock(attr1="foo", related=None) self.assertEqual(field.get_value_from_instance(instance), None)
def test_get_value_from_instance(self): field = ObjectField(attr='person', properties={ 'first_name': StringField(analyzier='foo'), 'last_name': StringField() }) instance = NonCallableMock(person=NonCallableMock( first_name='foo', last_name='bar')) self.assertEqual(field.get_value_from_instance(instance), { 'first_name': "foo", 'last_name': "bar", })
def test_get_value_from_iterable(self): field = ObjectField(attr='person', properties={ 'first_name': StringField(analyzier='foo'), 'last_name': StringField() }) instance = NonCallableMock( person=[ NonCallableMock( first_name="foo1", last_name="bar1" ), NonCallableMock( first_name="foo2", last_name="bar2" ) ] ) self.assertEqual(field.get_value_from_instance(instance), [ { 'first_name': "foo1", 'last_name': "bar1", }, { 'first_name': "foo2", 'last_name': "bar2", } ])
def test_get_value_from_instance(self): instance = NonCallableMock( foo=NonCallableMock(bar=["alpha", "beta", "gamma"]) ) field = ListField(StringField(attr='foo.bar')) self.assertEqual( field.get_value_from_instance(instance), instance.foo.bar)
def test_should_return_as_json(self): with_as_json = NonCallableMock() with_as_json.as_json.return_value = sentinel.as_json result = api.resource.resolve_value(with_as_json) self.assertEqual(result, sentinel.as_json)
def testCalculateDeviceMd5Sums_singlePath(self): test_path = '/storage/emulated/legacy/test/file.dat' device = mock.NonCallableMock() device.adb = mock.NonCallableMock() device.adb.Push = mock.Mock() device_md5sum_output = [ '0123456789abcdeffedcba9876543210 ' '/storage/emulated/legacy/test/file.dat', ] device.RunShellCommand = mock.Mock(return_value=device_md5sum_output) mock_temp_file = mock.mock_open() mock_temp_file.return_value.name = '/tmp/test/script/file.sh' mock_device_temp_file = mock.mock_open() mock_device_temp_file.return_value.name = ( '/data/local/tmp/test/script/file.sh') with mock.patch('tempfile.NamedTemporaryFile', new=mock_temp_file), ( mock.patch('pylib.utils.device_temp_file.DeviceTempFile', new=mock_device_temp_file)): out = md5sum.CalculateDeviceMd5Sums(test_path, device) self.assertEquals(1, len(out)) self.assertTrue('/storage/emulated/legacy/test/file.dat' in out) self.assertEquals('0123456789abcdeffedcba9876543210', out['/storage/emulated/legacy/test/file.dat']) device.adb.Push.assert_called_once_with( '/tmp/test/script/file.sh', '/data/local/tmp/test/script/file.sh') device.RunShellCommand.assert_called_once_with( ['sh', '/data/local/tmp/test/script/file.sh'])
def testCalculateDeviceMd5Sums_list(self): test_path = ['/storage/emulated/legacy/test/file0.dat', '/storage/emulated/legacy/test/file1.dat'] device = mock.NonCallableMock() device.adb = mock.NonCallableMock() device.adb.Push = mock.Mock() device_md5sum_output = [ '0123456789abcdeffedcba9876543210 ' '/storage/emulated/legacy/test/file0.dat', '123456789abcdef00fedcba987654321 ' '/storage/emulated/legacy/test/file1.dat', ] device.RunShellCommand = mock.Mock(return_value=device_md5sum_output) mock_temp_file = mock.mock_open() mock_temp_file.return_value.name = '/tmp/test/script/file.sh' mock_device_temp_file = mock.mock_open() mock_device_temp_file.return_value.name = ( '/data/local/tmp/test/script/file.sh') with mock.patch('tempfile.NamedTemporaryFile', new=mock_temp_file), ( mock.patch('pylib.utils.device_temp_file.DeviceTempFile', new=mock_device_temp_file)): out = md5sum.CalculateDeviceMd5Sums(test_path, device) self.assertEquals(2, len(out)) self.assertTrue('/storage/emulated/legacy/test/file0.dat' in out) self.assertEquals('0123456789abcdeffedcba9876543210', out['/storage/emulated/legacy/test/file0.dat']) self.assertTrue('/storage/emulated/legacy/test/file1.dat' in out) self.assertEquals('123456789abcdef00fedcba987654321', out['/storage/emulated/legacy/test/file1.dat']) device.adb.Push.assert_called_once_with( '/tmp/test/script/file.sh', '/data/local/tmp/test/script/file.sh') device.RunShellCommand.assert_called_once_with( ['sh', '/data/local/tmp/test/script/file.sh'])
def testCalculateDeviceMd5Sums_generator(self): test_path = ('/storage/emulated/legacy/test/file%d.dat' % n for n in xrange(0, 2)) device = mock.NonCallableMock() device.adb = mock.NonCallableMock() device.adb.Push = mock.Mock() device_md5sum_output = [ '0123456789abcdeffedcba9876543210 ' '/storage/emulated/legacy/test/file0.dat', '123456789abcdef00fedcba987654321 ' '/storage/emulated/legacy/test/file1.dat', ] device.RunShellCommand = mock.Mock(return_value=device_md5sum_output) mock_temp_file = mock.mock_open() mock_temp_file.return_value.name = '/tmp/test/script/file.sh' mock_device_temp_file = mock.mock_open() mock_device_temp_file.return_value.name = ( '/data/local/tmp/test/script/file.sh') with mock.patch('tempfile.NamedTemporaryFile', new=mock_temp_file), ( mock.patch('pylib.utils.device_temp_file.DeviceTempFile', new=mock_device_temp_file)): out = md5sum.CalculateDeviceMd5Sums(test_path, device) self.assertEquals(2, len(out)) self.assertTrue('/storage/emulated/legacy/test/file0.dat' in out) self.assertEquals('0123456789abcdeffedcba9876543210', out['/storage/emulated/legacy/test/file0.dat']) self.assertTrue('/storage/emulated/legacy/test/file1.dat' in out) self.assertEquals('123456789abcdef00fedcba987654321', out['/storage/emulated/legacy/test/file1.dat']) device.adb.Push.assert_called_once_with( '/tmp/test/script/file.sh', '/data/local/tmp/test/script/file.sh') device.RunShellCommand.assert_called_once_with( ['sh', '/data/local/tmp/test/script/file.sh'])
def testCalculateDeviceMd5Sums_singlePath_linkerWarning(self): # See crbug/479966 test_path = '/storage/emulated/legacy/test/file.dat' device = mock.NonCallableMock() device.adb = mock.NonCallableMock() device.adb.Push = mock.Mock() device_md5sum_output = [ 'WARNING: linker: /data/local/tmp/md5sum/md5sum_bin: ' 'unused DT entry: type 0x1d arg 0x15db', 'THIS_IS_NOT_A_VALID_CHECKSUM_ZZZ some random text', '0123456789abcdeffedcba9876543210 ' '/storage/emulated/legacy/test/file.dat', ] device.RunShellCommand = mock.Mock(return_value=device_md5sum_output) mock_temp_file = mock.mock_open() mock_temp_file.return_value.name = '/tmp/test/script/file.sh' mock_device_temp_file = mock.mock_open() mock_device_temp_file.return_value.name = ( '/data/local/tmp/test/script/file.sh') with mock.patch('tempfile.NamedTemporaryFile', new=mock_temp_file), ( mock.patch('pylib.utils.device_temp_file.DeviceTempFile', new=mock_device_temp_file)): out = md5sum.CalculateDeviceMd5Sums(test_path, device) self.assertEquals(1, len(out)) self.assertTrue('/storage/emulated/legacy/test/file.dat' in out) self.assertEquals('0123456789abcdeffedcba9876543210', out['/storage/emulated/legacy/test/file.dat']) device.adb.Push.assert_called_once_with( '/tmp/test/script/file.sh', '/data/local/tmp/test/script/file.sh') device.RunShellCommand.assert_called_once_with( ['sh', '/data/local/tmp/test/script/file.sh'])