Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_random():
from mixer._compat import string_types
mixer = TypeMixer(Test)
test = mixer.blend(name=mixer.RANDOM)
assert isinstance(test.name, string_types)
test = mixer.blend(name=mixer.RANDOM(int))
assert isinstance(test.name, int)
names = ['john_', 'kenn_', 'lenny_']
test = mixer.blend(name=mixer.RANDOM(*names))
assert test.name in names
def test_typemixer():
class Scheme:
id = int
name = str
money = int
male = bool
prop = Test
mixer = TypeMixer(Scheme)
test = mixer.blend(prop__two=2, prop__one=1, prop__name='sigil', name='RJ')
assert test.male in (True, False)
assert test.name == 'RJ'
assert test.prop.two == 2
assert test.prop.name == 'sigil'
test = mixer.blend(prop__two=4, unknown=lambda: '?')
assert test.prop.two == 4
assert test.unknown == '?'
def test_typemixer_meta():
""" Tests that typemixer is a singleton for current class. """
mixer1 = TypeMixer(Test)
mixer2 = TypeMixer(Test, fake=False)
mixer3 = TypeMixer(Test, fake=False)
assert mixer1 is not mixer2
assert mixer2 is mixer3
def test_typemixer_meta():
""" Tests that typemixer is a singleton for current class. """
mixer1 = TypeMixer(Test)
mixer2 = TypeMixer(Test, fake=False)
mixer3 = TypeMixer(Test, fake=False)
assert mixer1 is not mixer2
assert mixer2 is mixer3
""" Support for Pony ODM.
::
from mixer.backend.pony import mixer
"""
from __future__ import absolute_import
from pony.orm import commit
from .. import mix_types as t
from ..main import TypeMixer as BaseTypeMixer, Mixer as BaseMixer, SKIP_VALUE
class TypeMixer(BaseTypeMixer):
""" TypeMixer for Pony ORM. """
def __load_fields(self):
for attr in self.__scheme._attrs_:
yield attr.column, t.Field(attr, attr.column)
def populate_target(self, values):
""" Populate target. """
return self.__scheme(**dict(values))
def is_required(self, field):
""" Return True is field's value should be defined.
:return bool:
StringField: str,
URLField: t.URL,
UUIDField: t.UUID,
}
generators = {
GenericReferenceField: get_generic_reference,
GeoPointField: faker.coordinates,
LineStringField: get_linestring,
ObjectIdField: get_objectid,
PointField: get_pointfield,
PolygonField: get_polygon,
}
class TypeMixer(BaseTypeMixer):
""" TypeMixer for Mongoengine. """
factory = GenFactory
def make_fabric(self, me_field, field_name=None, fake=None, kwargs=None): # noqa
""" Make a fabric for field.
:param me_field: Mongoengine field's instance
:param field_name: Field name
:param fake: Force fake data
:return function:
"""
ftype = type(me_field)
types = {
(String, VARCHAR, Unicode, NVARCHAR, NCHAR, CHAR): str,
(Text, UnicodeText, TEXT): t.Text,
(Boolean, BOOLEAN): bool,
(Date, DATE): datetime.date,
(DateTime, DATETIME): datetime.datetime,
(Time, TIME): datetime.time,
(DECIMAL, Numeric, NUMERIC): decimal.Decimal,
(Float, FLOAT): float,
(Integer, INTEGER, INT): int,
(BigInteger, BIGINT): t.BigInteger,
(SmallInteger, SMALLINT): t.SmallInteger,
}
class TypeMixer(BaseTypeMixer):
""" TypeMixer for SQLAlchemy. """
factory = GenFactory
def __init__(self, cls, **params):
""" Init TypeMixer and save the mapper. """
super(TypeMixer, self).__init__(cls, **params)
self.mapper = self.__scheme._sa_class_manager.mapper
def postprocess(self, target, postprocess_values):
""" Fill postprocess values. """
mixed = []
for name, deffered in postprocess_values:
value = deffered.value
if isinstance(value, GeneratorType):
DatetimeField: datetime.datetime,
DecimalField: decimal.Decimal,
EmailField: t.EmailString,
FloatField: float,
IntegerField: int,
StringField: str,
}
generators = {
ListField: get_list_field,
SetField: get_set_field,
ObjectIdField: get_objectid,
}
class TypeMixer(BaseTypeMixer):
""" TypeMixer for YADM. """
factory = GenFactory
def __load_fields(self):
for fname, field in self.__scheme.__fields__.items():
yield fname, t.Field(field, fname)
@staticmethod
def get_default(field):
""" Get default value from field.
:return value: A default value or NO_VALUE
"""
CharField: str,
TextField: t.Text,
DateTimeField: datetime.datetime,
DateField: datetime.date,
TimeField: datetime.time,
BooleanField: bool,
# BlobField: None,
}
generators = {
BlobField: get_blob,
ForeignKeyField: get_relation,
}
class TypeMixer(BaseTypeMixer):
""" TypeMixer for Peewee ORM. """
factory = GenFactory
def __load_fields(self):
for field in self.__scheme._meta.sorted_fields:
yield field.name, t.Field(field, field.name)
def populate_target(self, values):
""" Populate target. """
return self.__scheme(**dict(values))
def gen_field(self, field):
""" Function description. """
if isinstance(field.scheme, AutoField)\