本文整理汇总了Python中warlock.model_factory函数的典型用法代码示例。如果您正苦于以下问题:Python model_factory函数的具体用法?Python model_factory怎么用?Python model_factory使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了model_factory函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_resolver
def test_resolver(self):
from jsonschema import RefResolver
dirname = os.path.dirname(__file__)
schemas_path = 'file://' + os.path.join(dirname, 'schemas/')
resolver = RefResolver(schemas_path, None)
country_schema_file = \
open(os.path.join(dirname, 'schemas/') + 'country.json')
person_schema_file = \
open(os.path.join(dirname, 'schemas/') + 'person.json')
country_schema = json.load(country_schema_file)
person_schema = json.load(person_schema_file)
Country = warlock.model_factory(country_schema, resolver)
Person = warlock.model_factory(person_schema, resolver)
england = Country(
name="England",
population=53865800,
overlord=Person(
title="Queen",
firstname="Elizabeth",
lastname="Windsor"
)
)
expected = {
'name': 'England',
'population': 53865800,
'overlord': {
'title': 'Queen',
'lastname': 'Windsor',
'firstname': 'Elizabeth'
}
}
self.assertEqual(england, expected)
示例2: test_naming
def test_naming(self):
Country = warlock.model_factory(fixture)
self.assertEqual(Country.__name__, 'Country')
Country2 = warlock.model_factory(fixture, name='Country2')
self.assertEqual(Country2.__name__, 'Country2')
nameless = warlock.model_factory(nameless_fixture)
self.assertEqual(nameless.__name__, 'Model')
nameless2 = warlock.model_factory(nameless_fixture, name='Country3')
self.assertEqual(nameless2.__name__, 'Country3')
示例3: test_recursive_models
def test_recursive_models(self):
Parent = warlock.model_factory(parent_fixture)
Child = warlock.model_factory(child_fixture)
mom = Parent(name='Abby', children=[])
teenager = Child(age=15, mother=mom)
toddler = Child(age=3, mother=mom)
mom.children = [teenager, toddler]
self.assertEqual(mom.children[0].age, 15)
self.assertEqual(mom.children[1].age, 3)
示例4: test_BaseModel_setattr
def test_BaseModel_setattr(self):
model_class = model_factory(self._test_schema, BaseModel)
model = model_class(**self._test_values)
self.assertRaises(AttributeError, getattr, model, 'test')
model.test = 'FOO'
self.assertEqual('FOO', model.test)
self.assertRaises(AttributeError, getattr, model.json_object, 'test')
示例5: create_one_image
def create_one_image(attrs=None):
"""Create a fake image.
:param Dictionary attrs:
A dictionary with all attrbutes of image
:return:
A FakeResource object with id, name, owner, protected,
visibility and tags attrs
"""
attrs = attrs or {}
# Set default attribute
image_info = {
'id': str(uuid.uuid4()),
'name': 'image-name' + uuid.uuid4().hex,
'owner': 'image-owner' + uuid.uuid4().hex,
'protected': bool(random.choice([0, 1])),
'visibility': random.choice(['public', 'private']),
'tags': [uuid.uuid4().hex for r in range(2)],
}
# Overwrite default attributes if there are some attributes set
image_info.update(attrs)
# Set up the schema
model = warlock.model_factory(
IMAGE_schema,
schemas.SchemaBasedModel,
)
return model(**image_info)
示例6: test_BaseModel
def test_BaseModel(self):
model_class = model_factory(self._test_schema, BaseModel)
model = model_class(**self._test_values)
model.test = 'foo'
self.assertEqual('FOO', model.type)
self.assertEqual('0001', model.id)
示例7: test_patch_alter_value
def test_patch_alter_value(self):
Country = warlock.model_factory(fixture)
sweden = Country(name='Sweden', population=9379116)
sweden['name'] = 'Finland'
self.assertEqual(
sweden.patch,
'[{"path": "/name", "value": "Finland", "op": "replace"}]')
示例8: test_patch_drop_attribute
def test_patch_drop_attribute(self):
Country = warlock.model_factory(fixture)
sweden = Country(name='Sweden', population=9379116)
del sweden['name']
self.assertEqual(
json.loads(sweden.patch),
json.loads('[{"path": "/name", "op": "remove"}]'))
示例9: test_deepcopy
def test_deepcopy(self):
"""Make sure we aren't leaking references."""
Mixmaster = warlock.model_factory(complex_fixture)
mike = Mixmaster(sub={'foo': 'mike'})
self.assertEquals(mike.sub['foo'], 'mike')
mike_1 = mike.copy()
mike_1['sub']['foo'] = 'james'
self.assertEquals(mike.sub['foo'], 'mike')
mike_2 = dict(mike.iteritems())
mike_2['sub']['foo'] = 'james'
self.assertEquals(mike.sub['foo'], 'mike')
mike_2 = dict(mike.items())
mike_2['sub']['foo'] = 'james'
self.assertEquals(mike.sub['foo'], 'mike')
mike_3_sub = list(mike.itervalues())[0]
mike_3_sub['foo'] = 'james'
self.assertEquals(mike.sub['foo'], 'mike')
mike_3_sub = list(mike.values())[0]
mike_3_sub['foo'] = 'james'
self.assertEquals(mike.sub['foo'], 'mike')
示例10: unvalidated_model
def unvalidated_model(self):
"""A model which does not validate the image against the v2 schema."""
schema = self.schema_client.get('image')
warlock_model = warlock.model_factory(schema.raw(),
schemas.SchemaBasedModel)
warlock_model.validate = lambda *args, **kwargs: None
return warlock_model
示例11: test_items
def test_items(self):
Country = warlock.model_factory(fixture)
sweden = Country(name='Sweden', population=9379116)
self.assertEqual(set(list(sweden.iteritems())),
set([('name', 'Sweden'), ('population', 9379116)]))
self.assertEqual(set(sweden.items()),
set([('name', 'Sweden'), ('population', 9379116)]))
示例12: test_forbidden_methods
def test_forbidden_methods(self):
Country = warlock.model_factory(fixture)
sweden = Country(name='Sweden', population=9379116)
exc = warlock.InvalidOperation
self.assertRaises(exc, sweden.clear)
self.assertRaises(exc, sweden.pop, 0)
self.assertRaises(exc, sweden.popitem)
示例13: test_changes
def test_changes(self):
Country = warlock.model_factory(fixture)
sweden = Country(name='Sweden', population=9379116)
self.assertEqual(sweden.changes, {})
sweden['name'] = 'Finland'
self.assertEqual(sweden.changes, {'name': 'Finland'})
sweden['name'] = 'Norway'
self.assertEqual(sweden.changes, {'name': 'Norway'})
示例14: model
def model(**item):
schema = self.schemas.get('scene')
wrapped_model = warlock.model_factory(
schema.raw(),
schemas.SceneSchemaModel)
model = wrapped_model(**item)
model.controller = self.scenes
return model
示例15: test_dict_syntax
def test_dict_syntax(self):
Country = warlock.model_factory(fixture)
sweden = Country(name='Sweden', population=9379116)
sweden['name'] = 'Finland'
self.assertEqual('Finland', sweden['name'])
del sweden['name']
self.assertRaises(AttributeError, getattr, sweden, 'name')