本文整理汇总了Python中mkt.collections.serializers.CollectionSerializer.to_native方法的典型用法代码示例。如果您正苦于以下问题:Python CollectionSerializer.to_native方法的具体用法?Python CollectionSerializer.to_native怎么用?Python CollectionSerializer.to_native使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mkt.collections.serializers.CollectionSerializer
的用法示例。
在下文中一共展示了CollectionSerializer.to_native方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestCollectionSerializer
# 需要导入模块: from mkt.collections.serializers import CollectionSerializer [as 别名]
# 或者: from mkt.collections.serializers.CollectionSerializer import to_native [as 别名]
class TestCollectionSerializer(CollectionDataMixin, amo.tests.TestCase):
def setUp(self):
self.collection = Collection.objects.create(**self.collection_data)
self.serializer = CollectionSerializer()
def test_to_native(self, apps=None):
if apps:
for app in apps:
self.collection.add_app(app)
else:
apps = []
data = self.serializer.to_native(self.collection)
for name, value in self.collection_data.iteritems():
eq_(self.collection_data[name], data[name])
self.assertSetEqual(data.keys(), ['id', 'name', 'description', 'apps',
'collection_type'])
for order, app in enumerate(apps):
eq_(data['apps'][order]['slug'], app.app_slug)
def test_to_native_with_apps(self):
apps = [amo.tests.app_factory() for n in xrange(1, 5)]
self.test_to_native(apps=apps)
示例2: TestCollectionSerializer
# 需要导入模块: from mkt.collections.serializers import CollectionSerializer [as 别名]
# 或者: from mkt.collections.serializers.CollectionSerializer import to_native [as 别名]
class TestCollectionSerializer(CollectionDataMixin, amo.tests.TestCase):
def setUp(self):
self.collection = Collection.objects.create(**self.collection_data)
self.serializer = CollectionSerializer()
def test_to_native(self, apps=None):
if apps:
for app in apps:
self.collection.add_app(app)
else:
apps = []
data = self.serializer.to_native(self.collection)
for name, value in self.collection_data.iteritems():
eq_(self.collection_data[name], data[name])
self.assertSetEqual(
data.keys(),
[
"id",
"name",
"description",
"apps",
"collection_type",
"category",
"region",
"carrier",
"author",
"is_public",
],
)
for order, app in enumerate(apps):
eq_(data["apps"][order]["slug"], app.app_slug)
def test_to_native_with_apps(self):
apps = [amo.tests.app_factory() for n in xrange(1, 5)]
self.test_to_native(apps=apps)
示例3: TestCollectionSerializer
# 需要导入模块: from mkt.collections.serializers import CollectionSerializer [as 别名]
# 或者: from mkt.collections.serializers.CollectionSerializer import to_native [as 别名]
class TestCollectionSerializer(CollectionDataMixin, amo.tests.TestCase):
def setUp(self):
minimal_context = {"request": RequestFactory().get("/whatever")}
self.collection = Collection.objects.create(**self.collection_data)
self.serializer = CollectionSerializer(self.collection, context=minimal_context)
def test_metadata_is_serialized_to_json(self):
ok_(json.dumps(self.serializer.metadata()))
def test_to_native(self, apps=None):
if apps:
for app in apps:
self.collection.add_app(app)
else:
apps = []
data = self.serializer.to_native(self.collection)
for name, value in self.collection_data.iteritems():
eq_(self.collection_data[name], data[name])
self.assertSetEqual(
data.keys(),
[
"apps",
"author",
"background_color",
"carrier",
"category",
"collection_type",
"default_language",
"description",
"id",
"image",
"is_public",
"name",
"region",
"slug",
"text_color",
],
)
for order, app in enumerate(apps):
eq_(data["apps"][order]["slug"], app.app_slug)
return data
def test_to_native_operator(self):
self.collection.update(collection_type=COLLECTIONS_TYPE_OPERATOR)
data = self.serializer.to_native(self.collection)
ok_("can_be_hero" in data.keys())
@patch("mkt.collections.serializers.build_id", "bbbbbb")
def test_image(self):
data = self.serializer.to_native(self.collection)
eq_(data["image"], None)
self.collection.update(has_image=True)
data = self.serializer.to_native(self.collection)
self.assertApiUrlEqual(data["image"], "/rocketfuel/collections/%s/image.png?bbbbbb" % self.collection.pk)
def test_wrong_default_language_serialization(self):
# The following is wrong because we only accept the 'en-us' form.
data = {"default_language": u"en_US"}
serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
eq_(serializer.is_valid(), False)
ok_("default_language" in serializer.errors)
def test_translation_deserialization(self):
data = {"name": u"¿Dónde está la biblioteca?"}
serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
eq_(serializer.errors, {})
ok_(serializer.is_valid())
def test_translation_deserialization_multiples_locales(self):
data = {"name": {"fr": u"Chat grincheux…", "en-US": u"Grumpy Cat..."}}
serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
eq_(serializer.errors, {})
ok_(serializer.is_valid())
def test_empty_choice_deserialization(self):
# Build data from existing object.
data = self.serializer.to_native(self.collection)
data.pop("id")
# Emulate empty values passed via POST.
data.update({"carrier": "", "region": ""})
instance = self.serializer.from_native(data, None)
eq_(self.serializer.errors, {})
ok_(self.serializer.is_valid())
eq_(instance.region, None)
eq_(instance.carrier, None)
def test_to_native_with_apps(self):
apps = [amo.tests.app_factory() for n in xrange(1, 5)]
data = self.test_to_native(apps=apps)
keys = data["apps"][0].keys()
ok_("name" in keys)
ok_("id" in keys)
def validate(self, **kwargs):
return self.serializer.validate(kwargs)
def test_validation_operatorshelf_category(self):
category = Category.objects.create(name="BastaCorp", slug="basta", type=amo.ADDON_WEBAPP)
#.........这里部分代码省略.........
示例4: TestCollectionSerializer
# 需要导入模块: from mkt.collections.serializers import CollectionSerializer [as 别名]
# 或者: from mkt.collections.serializers.CollectionSerializer import to_native [as 别名]
class TestCollectionSerializer(CollectionDataMixin, amo.tests.TestCase):
def setUp(self):
minimal_context = {
'request': RequestFactory().get('/whatever')
}
self.collection = Collection.objects.create(**self.collection_data)
self.serializer = CollectionSerializer(self.collection,
context=minimal_context)
def test_to_native(self, apps=None):
if apps:
for app in apps:
self.collection.add_app(app)
else:
apps = []
data = self.serializer.to_native(self.collection)
for name, value in self.collection_data.iteritems():
eq_(self.collection_data[name], data[name])
self.assertSetEqual(data.keys(), [
'apps', 'author', 'background_color', 'carrier', 'category',
'collection_type', 'default_language', 'description', 'id',
'image', 'is_public', 'name', 'region', 'slug', 'text_color'
])
for order, app in enumerate(apps):
eq_(data['apps'][order]['slug'], app.app_slug)
return data
def test_to_native_operator(self):
self.collection.update(collection_type=COLLECTIONS_TYPE_OPERATOR)
data = self.serializer.to_native(self.collection)
ok_('can_be_hero' in data.keys())
def test_image(self):
data = self.serializer.to_native(self.collection)
eq_(data['image'], None)
self.collection.update(has_image=True)
data = self.serializer.to_native(self.collection)
self.assertApiUrlEqual(data['image'],
'/rocketfuel/collections/%s/image.png' % self.collection.pk)
def test_wrong_default_language_serialization(self):
# The following is wrong because we only accept the 'en-us' form.
data = {'default_language': u'en_US'}
serializer = CollectionSerializer(instance=self.collection, data=data,
partial=True)
eq_(serializer.is_valid(), False)
ok_('default_language' in serializer.errors)
def test_translation_deserialization(self):
data = {
'name': u'¿Dónde está la biblioteca?'
}
serializer = CollectionSerializer(instance=self.collection, data=data,
partial=True)
eq_(serializer.errors, {})
ok_(serializer.is_valid())
def test_translation_deserialization_multiples_locales(self):
data = {
'name': {
'fr': u'Chat grincheux…',
'en-US': u'Grumpy Cat...'
}
}
serializer = CollectionSerializer(instance=self.collection, data=data,
partial=True)
eq_(serializer.errors, {})
ok_(serializer.is_valid())
def test_empty_choice_deserialization(self):
# Build data from existing object.
data = self.serializer.to_native(self.collection)
data.pop('id')
# Emulate empty values passed via POST.
data.update({'carrier': '', 'region': ''})
instance = self.serializer.from_native(data, None)
eq_(self.serializer.errors, {})
ok_(self.serializer.is_valid())
eq_(instance.region, None)
eq_(instance.carrier, None)
def test_to_native_with_apps(self):
apps = [amo.tests.app_factory() for n in xrange(1, 5)]
data = self.test_to_native(apps=apps)
keys = data['apps'][0].keys()
ok_('name' in keys)
ok_('id' in keys)
def validate(self, **kwargs):
return self.serializer.validate(kwargs)
def test_validation_operatorshelf_category(self):
category = Category.objects.create(name='BastaCorp', slug='basta',
type=amo.ADDON_WEBAPP)
ok_(self.validate(collection_type=COLLECTIONS_TYPE_BASIC,
category=category))
ok_(self.validate(collection_type=COLLECTIONS_TYPE_OPERATOR))
#.........这里部分代码省略.........
示例5: TestCollectionSerializer
# 需要导入模块: from mkt.collections.serializers import CollectionSerializer [as 别名]
# 或者: from mkt.collections.serializers.CollectionSerializer import to_native [as 别名]
class TestCollectionSerializer(CollectionDataMixin, amo.tests.TestCase):
def setUp(self):
minimal_context = {
'request': RequestFactory().get('/whatever')
}
self.collection = Collection.objects.create(**self.collection_data)
self.serializer = CollectionSerializer(context=minimal_context)
def test_to_native(self, apps=None):
if apps:
for app in apps:
self.collection.add_app(app)
else:
apps = []
data = self.serializer.to_native(self.collection)
for name, value in self.collection_data.iteritems():
eq_(self.collection_data[name], data[name])
self.assertSetEqual(data.keys(), ['apps', 'author', 'background_color',
'carrier', 'category',
'collection_type', 'default_language',
'description', 'id', 'image',
'is_public', 'name', 'region',
'slug', 'text_color'])
for order, app in enumerate(apps):
eq_(data['apps'][order]['slug'], app.app_slug)
return data
def test_image(self):
data = self.serializer.to_native(self.collection)
eq_(data['image'], None)
try:
os.makedirs(os.path.dirname(self.collection.image_path()))
except OSError:
pass
open(self.collection.image_path(), 'w').write("some data")
data = self.serializer.to_native(self.collection)
eq_(data['image'], reverse('collection-image-detail',
kwargs={'pk': self.collection.id}))
def test_wrong_default_language_serialization(self):
# The following is wrong because we only accept the 'en-us' form.
data = {'default_language': u'en_US'}
serializer = CollectionSerializer(instance=self.collection, data=data,
partial=True)
eq_(serializer.is_valid(), False)
ok_('default_language' in serializer.errors)
def test_translation_deserialization(self):
data = {
'name': u'¿Dónde está la biblioteca?'
}
serializer = CollectionSerializer(instance=self.collection, data=data,
partial=True)
eq_(serializer.errors, {})
ok_(serializer.is_valid())
def test_translation_deserialization_multiples_locales(self):
data = {
'name': {
'fr': u'Chat grincheux…',
'en-US': u'Grumpy Cat...'
}
}
serializer = CollectionSerializer(instance=self.collection, data=data,
partial=True)
eq_(serializer.errors, {})
ok_(serializer.is_valid())
def test_empty_choice_deserialization(self):
# Build data from existing object.
data = self.serializer.to_native(self.collection)
data.pop('id')
# Emulate empty values passed via POST.
data.update({'carrier': '', 'region': ''})
instance = self.serializer.from_native(data, None)
eq_(self.serializer.errors, {})
ok_(self.serializer.is_valid())
eq_(instance.region, None)
eq_(instance.carrier, None)
def test_to_native_with_apps(self):
apps = [amo.tests.app_factory() for n in xrange(1, 5)]
data = self.test_to_native(apps=apps)
keys = data['apps'][0].keys()
ok_('name' in keys)
ok_('id' in keys)
示例6: TestCollectionSerializer
# 需要导入模块: from mkt.collections.serializers import CollectionSerializer [as 别名]
# 或者: from mkt.collections.serializers.CollectionSerializer import to_native [as 别名]
class TestCollectionSerializer(CollectionDataMixin, amo.tests.TestCase):
def setUp(self):
minimal_context = {"request": RequestFactory().get("/whatever")}
minimal_context["request"].user = AnonymousUser()
self.collection = Collection.objects.create(**self.collection_data)
self.serializer = CollectionSerializer(self.collection, context=minimal_context)
def test_metadata_is_serialized_to_json(self):
ok_(json.dumps(self.serializer.metadata()))
def test_to_native(self, apps=None):
if apps:
for app in apps:
self.collection.add_app(app)
else:
apps = []
data = self.serializer.to_native(self.collection)
for name, value in self.collection_data.iteritems():
eq_(self.collection_data[name], data[name])
self.assertSetEqual(
data.keys(),
[
"apps",
"author",
"background_color",
"carrier",
"category",
"collection_type",
"default_language",
"description",
"id",
"image",
"is_public",
"name",
"region",
"slug",
"text_color",
],
)
for order, app in enumerate(apps):
eq_(data["apps"][order]["slug"], app.app_slug)
return data
def test_to_native_operator(self):
self.collection.update(collection_type=COLLECTIONS_TYPE_OPERATOR)
data = self.serializer.to_native(self.collection)
ok_("can_be_hero" in data.keys())
@override_settings(STATIC_URL="https://testserver-cdn/")
def test_image(self):
data = self.serializer.to_native(self.collection)
eq_(data["image"], None)
self.collection.update(image_hash="bbbbbb")
data = self.serializer.to_native(self.collection)
self.assertApiUrlEqual(
data["image"],
"/rocketfuel/collections/%s/image.png?bbbbbb" % self.collection.pk,
scheme="https",
netloc="testserver-cdn",
)
def test_wrong_default_language_serialization(self):
# The following is wrong because we only accept the 'en-us' form.
data = {"default_language": u"en_US"}
serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
eq_(serializer.is_valid(), False)
ok_("default_language" in serializer.errors)
def test_name_required(self):
data = {"description": u"some description", "collection_type": u"1"}
serializer = CollectionSerializer(instance=self.collection, data=data)
eq_(serializer.is_valid(), False)
ok_("name" in serializer.errors)
def test_name_cannot_be_empty(self):
data = {"name": u""}
serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
eq_(serializer.is_valid(), False)
ok_("name" in serializer.errors)
eq_(serializer.errors["name"], [u"The field must have a length of at least 1 characters."])
def test_name_all_locales_cannot_be_empty(self):
data = {"name": {"fr": u"", "en-US": u""}}
serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
eq_(serializer.is_valid(), False)
ok_("name" in serializer.errors)
def test_name_one_locale_must_be_non_empty(self):
data = {"name": {"fr": u"", "en-US": u"Non-Empty Name"}}
serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
eq_(serializer.is_valid(), True)
def test_translation_deserialization(self):
data = {"name": u"¿Dónde está la biblioteca?"}
serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
eq_(serializer.errors, {})
ok_(serializer.is_valid())
def test_translation_deserialization_multiples_locales(self):
#.........这里部分代码省略.........
示例7: TestCollectionSerializer
# 需要导入模块: from mkt.collections.serializers import CollectionSerializer [as 别名]
# 或者: from mkt.collections.serializers.CollectionSerializer import to_native [as 别名]
class TestCollectionSerializer(CollectionDataMixin, amo.tests.TestCase):
def setUp(self):
minimal_context = {
'request': RequestFactory().get('/whatever')
}
minimal_context['request'].user = AnonymousUser()
self.collection = Collection.objects.create(**self.collection_data)
self.serializer = CollectionSerializer(self.collection,
context=minimal_context)
def test_metadata_is_serialized_to_json(self):
ok_(json.dumps(self.serializer.metadata()))
def test_to_native(self, apps=None):
if apps:
for app in apps:
self.collection.add_app(app)
else:
apps = []
data = self.serializer.to_native(self.collection)
for name, value in self.collection_data.iteritems():
eq_(self.collection_data[name], data[name])
self.assertSetEqual(data.keys(), [
'apps', 'author', 'background_color', 'carrier', 'category',
'collection_type', 'default_language', 'description', 'id',
'image', 'is_public', 'name', 'region', 'slug', 'text_color'
])
for order, app in enumerate(apps):
eq_(data['apps'][order]['slug'], app.app_slug)
return data
def test_to_native_operator(self):
self.collection.update(collection_type=COLLECTIONS_TYPE_OPERATOR)
data = self.serializer.to_native(self.collection)
ok_('can_be_hero' in data.keys())
@override_settings(STATIC_URL='https://testserver-cdn/')
def test_image(self):
data = self.serializer.to_native(self.collection)
eq_(data['image'], None)
self.collection.update(image_hash='bbbbbb')
data = self.serializer.to_native(self.collection)
self.assertApiUrlEqual(data['image'],
'/rocketfuel/collections/%s/image.png?bbbbbb' % self.collection.pk,
scheme='https', netloc='testserver-cdn')
def test_wrong_default_language_serialization(self):
# The following is wrong because we only accept the 'en-us' form.
data = {'default_language': u'en_US'}
serializer = CollectionSerializer(instance=self.collection, data=data,
partial=True)
eq_(serializer.is_valid(), False)
ok_('default_language' in serializer.errors)
def test_name_required(self):
data = {
'description': u'some description',
'collection_type': u'1'
}
serializer = CollectionSerializer(instance=self.collection, data=data)
eq_(serializer.is_valid(), False)
ok_('name' in serializer.errors)
def test_name_cannot_be_empty(self):
data = {
'name': u''
}
serializer = CollectionSerializer(instance=self.collection, data=data,
partial=True)
eq_(serializer.is_valid(), False)
ok_('name' in serializer.errors)
eq_(serializer.errors['name'],
[u'The field must have a length of at least 1 characters.'])
def test_name_all_locales_cannot_be_empty(self):
data = {
'name': {
'fr': u'',
'en-US': u''
}
}
serializer = CollectionSerializer(instance=self.collection, data=data,
partial=True)
eq_(serializer.is_valid(), False)
ok_('name' in serializer.errors)
def test_name_one_locale_must_be_non_empty(self):
data = {
'name': {
'fr': u'',
'en-US': u'Non-Empty Name'
}
}
serializer = CollectionSerializer(instance=self.collection, data=data,
partial=True)
eq_(serializer.is_valid(), True)
def test_translation_deserialization(self):
#.........这里部分代码省略.........
示例8: TestCollectionSerializer
# 需要导入模块: from mkt.collections.serializers import CollectionSerializer [as 别名]
# 或者: from mkt.collections.serializers.CollectionSerializer import to_native [as 别名]
class TestCollectionSerializer(CollectionDataMixin, amo.tests.TestCase):
def setUp(self):
self.collection = Collection.objects.create(**self.collection_data)
self.serializer = CollectionSerializer()
def test_to_native(self, apps=None):
if apps:
for app in apps:
self.collection.add_app(app)
else:
apps = []
data = self.serializer.to_native(self.collection)
for name, value in self.collection_data.iteritems():
eq_(self.collection_data[name], data[name])
self.assertSetEqual(
data.keys(),
[
"id",
"name",
"description",
"apps",
"collection_type",
"category",
"region",
"carrier",
"author",
"slug",
"is_public",
"default_language",
],
)
for order, app in enumerate(apps):
eq_(data["apps"][order]["slug"], app.app_slug)
return data
def test_wrong_default_language_serialization(self):
# The following is wrong because we only accept the 'en-us' form.
data = {"default_language": u"en_US"}
serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
eq_(serializer.is_valid(), False)
ok_("default_language" in serializer.errors)
def test_translation_deserialization(self):
data = {"name": u"¿Dónde está la biblioteca?"}
serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
eq_(serializer.errors, {})
ok_(serializer.is_valid())
def test_translation_deserialization_multiples_locales(self):
data = {"name": {"fr": u"Chat grincheux…", "en-US": u"Grumpy Cat..."}}
serializer = CollectionSerializer(instance=self.collection, data=data, partial=True)
eq_(serializer.errors, {})
ok_(serializer.is_valid())
def test_to_native_with_apps(self):
apps = [amo.tests.app_factory() for n in xrange(1, 5)]
data = self.test_to_native(apps=apps)
keys = data["apps"][0].keys()
ok_("name" in keys)
ok_("id" in keys)
示例9: TestCollectionSerializer
# 需要导入模块: from mkt.collections.serializers import CollectionSerializer [as 别名]
# 或者: from mkt.collections.serializers.CollectionSerializer import to_native [as 别名]
class TestCollectionSerializer(CollectionDataMixin, amo.tests.TestCase):
def setUp(self):
self.collection = Collection.objects.create(**self.collection_data)
self.serializer = CollectionSerializer()
def test_to_native(self, apps=None):
if apps:
for app in apps:
self.collection.add_app(app)
else:
apps = []
data = self.serializer.to_native(self.collection)
for name, value in self.collection_data.iteritems():
eq_(self.collection_data[name], data[name])
self.assertSetEqual(data.keys(), ['apps', 'author', 'background_color',
'carrier', 'category',
'collection_type', 'default_language',
'description', 'id', 'is_public',
'name', 'region', 'slug',
'text_color'])
for order, app in enumerate(apps):
eq_(data['apps'][order]['slug'], app.app_slug)
return data
def test_wrong_default_language_serialization(self):
# The following is wrong because we only accept the 'en-us' form.
data = {'default_language': u'en_US'}
serializer = CollectionSerializer(instance=self.collection, data=data,
partial=True)
eq_(serializer.is_valid(), False)
ok_('default_language' in serializer.errors)
def test_translation_deserialization(self):
data = {
'name': u'¿Dónde está la biblioteca?'
}
serializer = CollectionSerializer(instance=self.collection, data=data,
partial=True)
eq_(serializer.errors, {})
ok_(serializer.is_valid())
def test_translation_deserialization_multiples_locales(self):
data = {
'name': {
'fr': u'Chat grincheux…',
'en-US': u'Grumpy Cat...'
}
}
serializer = CollectionSerializer(instance=self.collection, data=data,
partial=True)
eq_(serializer.errors, {})
ok_(serializer.is_valid())
def test_to_native_with_apps(self):
apps = [amo.tests.app_factory() for n in xrange(1, 5)]
data = self.test_to_native(apps=apps)
keys = data['apps'][0].keys()
ok_('name' in keys)
ok_('id' in keys)