本文整理汇总了Python中mock.MagicMock.pk方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.pk方法的具体用法?Python MagicMock.pk怎么用?Python MagicMock.pk使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mock.MagicMock
的用法示例。
在下文中一共展示了MagicMock.pk方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get_target_metadata
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import pk [as 别名]
def test_get_target_metadata(self):
"""
Test MQTT backend get_target_metadata function
"""
# User objects
user = _create_user('John')
user_metadata = {'id': user.pk, 'name': user.get_full_name()}
self.assertEqual(
json.dumps(user_metadata), json.dumps(get_target_metadata(user)))
# XForm objects
xform = MagicMock()
xform.pk = 1337
xform.title = 'Test Form'
xform.id_string = 'Test_Form_ID'
xform._meta.model_name = XFORM
xform_metadata = {
'id': 1337,
'name': 'Test Form',
'form_id': 'Test_Form_ID'
}
self.assertEqual(
json.dumps(xform_metadata), json.dumps(get_target_metadata(xform)))
# Project objects
project = MagicMock()
project.pk = 7331
project.name = 'Test Project'
project._meta.model_name = PROJECT
project_metadata = {'id': 7331, 'name': 'Test Project'}
self.assertEqual(
json.dumps(project_metadata),
json.dumps(get_target_metadata(project)))
示例2: _mock_get_up
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import pk [as 别名]
def _mock_get_up(pk=None):
result = MagicMock()
result.state = 'uploaded'
if pk == '1111':
result.pk = '1111'
else:
result.pk = '2222'
return result
示例3: _mock_get
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import pk [as 别名]
def _mock_get(pk=None):
result = MagicMock()
if pk == '1111':
result.state = 'published'
result.pk = '1111'
else:
result.state = 'uploaded'
result.pk = '2222'
return result
示例4: test_update_tags
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import pk [as 别名]
def test_update_tags(self, create_dir, pk, tags):
if create_dir:
os.makedirs(self._path)
# Create schema
schema = Schema(id=ID(stored=True, unique=True), tags=KEYWORD(stored=True), named_tags=KEYWORD(stored=True))
# Create index
index = create_in(self._path, schema)
index_writer = index.writer()
index_writer.add_document(id=unicode(pk), tags=unicode('test1 test2'), named_tags=unicode('test1 test2'))
index_writer.commit()
offering = MagicMock()
offering.pk = pk
offering.save = MagicMock()
tag_man = tag_manager.TagManager(index_path=self._path)
tag_man.update_tags(offering, tags)
self.assertEquals(offering.tags, tags)
# Query the index
index = open_dir(self._path)
with index.searcher() as searcher:
query = QueryParser('id', index.schema).parse(unicode(pk))
val = searcher.search(query)
self.assertEquals(len(val), 1)
self.assertEquals(val[0]['id'], unicode(pk))
ret_tags = val[0]['tags'].split(' ')
self.assertEquals(len(tags), len(ret_tags))
for t in tags:
self.assertTrue(t in ret_tags)
示例5: setUp
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import pk [as 别名]
def setUp(self):
sdr_manager.Organization = MagicMock()
org = MagicMock()
org.pk = '1111'
sdr_manager.Organization.objects.filter.return_value = [org]
# Create Order mock
self._order = MagicMock()
self._order.owner_organization = org
self._contract = MagicMock()
self._contract.pricing_model = {
'pay_per_use': [{
'unit': 'invocation'
}]
}
self._contract.correlation_number = 1
self._contract.last_usage = None
sdr_manager.Order = MagicMock()
sdr_manager.Order.objects.get.return_value = self._order
self._order.get_product_contract.return_value = self._contract
self._user = MagicMock()
self._user.is_staff = True
self._user.userprofile.organizations = [{
'organization': '1111'
}]
sdr_manager.User = MagicMock()
sdr_manager.User.objects.get.return_value = self._user
self._timestamp = datetime.strptime('2015-10-20 17:31:57.100', '%Y-%m-%d %H:%M:%S.%f')
示例6: test_create_no_luggage_pk
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import pk [as 别名]
def test_create_no_luggage_pk(self):
'''Should return an InternalError Luggage can\'t be save'''
user = MagicMock()
luggage = MagicMock()
luggage.pk = None
with self.assertRaises(InternalError):
Log.create(user, luggage)
示例7: test_delete_tag_document
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import pk [as 别名]
def test_delete_tag_document(self, name, pk, err_msg=None, ix_path=None):
# Create mock
offering = MagicMock()
offering.pk = pk
# Create indexes
self._create_index_dir()
path = self._path
if ix_path:
from django.conf import settings
path = os.path.join(settings.BASEDIR, ix_path)
# Build the tag manager
tm = tag_manager.TagManager(path)
error = None
try:
tm.delete_tag(offering)
except Exception as e:
error = e
# Check response
if not err_msg:
self.assertEquals(error, None)
# Check that the index does not exists
index = open_dir(self._path)
with index.searcher() as searcher:
query = QueryParser('id', index.schema).parse(unicode(offering.pk))
self.assertEquals(len(searcher.search(query)), 0)
else:
self.assertTrue(isinstance(error, ValueError))
self.assertEquals(unicode(error), err_msg)
示例8: setUp
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import pk [as 别名]
def setUp(self):
views.RSS = MagicMock()
self.rss_object = MagicMock()
views.RSS.objects.all.return_value = [self.rss_object]
# Create request user mock
user_object = MagicMock()
user_object.username = 'test_user'
user_object.pk = '1111'
user_object.is_staff = False
self.request = MagicMock()
self.request.user = user_object
# Create organization mock
self.org_object = MagicMock()
self.org_object.name = 'test_org'
self.org_object.managers = ['1111']
self.org_object.payment_info = {
'number': '4567456745674567'
}
views.Organization = MagicMock()
views.Organization.objects.get.return_value = self.org_object
views.RSSManagerFactory = MagicMock()
self._limits_factory = MagicMock()
views.RSSManagerFactory.return_value = self._limits_factory
self._exp_mock = MagicMock(name="ExpenditureManager")
self._limits_factory.get_expenditure_manager.return_value = self._exp_mock
示例9: test_remove_review
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import pk [as 别名]
def test_remove_review(self, name, user_check, side_effect=None):
# Create Mocks
#if name == 'manager':
# import ipdb; ipdb.set_trace()
rev_object = MagicMock()
rev_object.pk = '333333'
rev_object.user = self.user
rev_object.organization = self.org
self.user.userprofile.rated_offerings = [self.offering.pk]
self.offering.rating = 3.75
self.offering.comments = ['333333', '444444', '555555', '666666']
rev_object.offering = self.offering
rev_object.rating = 3
review_manager.Review = MagicMock()
review_manager.Review.objects.get.return_value = rev_object
if side_effect:
side_effect(self, rev_object)
error = False
try:
rm = review_manager.ReviewManager()
rm.remove_review(self.user, '333333')
except:
error = True
# Check result
self.assertFalse(error)
self.assertFalse('333333' in self.offering)
self.offering.save.assert_called_once_with()
rev_object.delete.assert_called_once_with()
# Check user or organization models
user_check(self)
示例10: test_create_employee_without_current_flight
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import pk [as 别名]
def test_create_employee_without_current_flight(self):
'''Should return an Employee.DoesNotExist error'''
user = MagicMock()
user.employee.current_flight = None
luggage = MagicMock()
luggage.pk = True
with self.assertRaises(FieldError):
Log.create(user, luggage)
示例11: objects_get_or_create
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import pk [as 别名]
def objects_get_or_create(*args, **kwargs):
task = MagicMock()
queue.put({ k: v for k, v in kwargs.items() })
task.pk = kwargs['taskid']
for name, value in kwargs.items():
setattr(task, name, value)
queue.close()
return (task, True)
示例12: _already_owned_bundle
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import pk [as 别名]
def _already_owned_bundle(self):
bundle_offering = MagicMock()
bundle_offering.pk = '111111'
bundle_offering.name = 'Bundle'
bundle_offering.off_id = '6'
self._offering_inst.bundled_offerings = ['111111']
self._customer.userprofile.current_organization.acquired_offerings = ['111111']
ordering_management.Offering.objects.get.side_effect = [self._offering_inst, bundle_offering]
示例13: test_create_review
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import pk [as 别名]
def test_create_review(self, review, exp_rating, side_effect=None, org_comment=False, err_type=None, err_msg=None):
# Create review mock
review_object = MagicMock()
rev = MagicMock()
rev.pk = '111111'
review_object.objects.create.return_value = rev
review_manager.Review = review_object
# Create review manager
rm = review_manager.ReviewManager()
if side_effect:
side_effect(self)
exception = None
try:
rm.create_review(self.user, self.offering, review)
except Exception as e:
exception = e
# Check calls and objects
if not err_type:
self.assertEquals(exception, None)
if not self.offering.open:
review_manager.Purchase.objects.get.assert_called_once_with(
owner_organization=self.org,
offering=self.offering
)
review_object.objects.create.assert_called_once_with(
user=self.user,
organization=self.org,
offering=self.offering,
timestamp=self.datetime,
title=review['title'],
comment=review['comment'],
rating=review['rating']
)
self.assertEquals(self.offering.rating, exp_rating)
self.assertEquals(self.offering.comments[0], rev.pk)
if not org_comment:
self.assertTrue(self.offering.pk in self.user.userprofile.rated_offerings)
self.user.userprofile.save.assert_called_once_with()
else:
self.assertTrue({'user': self.user.pk, 'offering': self.offering.pk} in self.org.rated_offerings)
self.org.save.assert_called_once_with()
else:
self.assertTrue(isinstance(exception, err_type))
self.assertEquals(unicode(e), err_msg)
示例14: _mock_resource
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import pk [as 别名]
def _mock_resource(self, info, provider):
# Mock resource model
resource = MagicMock()
resource.pk = info['pk']
resource.provider = provider
resource.version = info['version']
resource.content_type = info['content_type']
resource.state = info['state']
resource.download_link = info['download_link']
resource.resource_type = info['type']
resource.meta_info = {}
resource.get_url.return_value = info['download_link']
resource.get_uri.return_value = info['uri']
return resource
示例15: setUp
# 需要导入模块: from mock import MagicMock [as 别名]
# 或者: from mock.MagicMock import pk [as 别名]
def setUp(self):
# Create request user mock
user_object = MagicMock()
user_object.username = 'test_user'
user_object.pk = '1111'
user_object.is_staff = False
self.request = MagicMock()
self.request.user = user_object
# Create organization mock
self.org_object = MagicMock()
self.org_object.name = 'test_org'
self.org_object.managers = ['1111']
self.org_object.payment_info = {
'number': '4567456745674567'
}
views.Organization = MagicMock()
views.Organization.objects.get.return_value = self.org_object