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_factory():
""" Test base generator's factory. """
from mixer.main import GenFactory
g = GenFactory()
test = g.get_fabric(int)
assert -2147483647 <= test() < 2147483647
test = g.get_fabric(bool)
assert test() in (True, False)
mixer.register(
Test,
name='Mike',
one=mixer.faker.pyfloat,
body=mixer.faker.date_time,
)
test = mixer.blend(Test)
assert test.name == 'Mike Done'
assert isinstance(test.one, float)
assert isinstance(test.body, datetime.datetime)
from mixer.main import GenFactory
class MyFactory(GenFactory):
generators = {str: lambda: "Always same"}
mixer = Mixer(factory=MyFactory, fake=False)
test = mixer.blend(Test)
assert test.name == "Always same"
if scheme is ContentType:
choices = [m for m in apps.get_models() if m is not ContentType]
return ContentType.objects.get_for_model(faker.random_element(choices))
return TypeMixer(scheme, mixer=_typemixer._TypeMixer__mixer,
factory=_typemixer._TypeMixer__factory,
fake=_typemixer._TypeMixer__fake,).blend(**params)
def get_datetime(**params):
""" Support Django TZ support. """
return faker.date_time(tzinfo=UTC if settings.USE_TZ else None)
class GenFactory(BaseFactory):
""" Map a django classes to simple types. """
types = {
(models.AutoField, models.PositiveIntegerField): t.PositiveInteger,
models.BigIntegerField: t.BigInteger,
models.BooleanField: bool,
(models.CharField, models.SlugField): str,
models.DateField: dt.date,
models.DecimalField: decimal.Decimal,
models.EmailField: t.EmailString,
models.FloatField: float,
models.GenericIPAddressField: t.IPString,
models.IPAddressField: t.IP4String,
models.IntegerField: int,
models.PositiveSmallIntegerField: t.PositiveSmallInteger,
scheme = _scheme.rel_model
return TypeMixer(
scheme,
mixer=_typemixer._TypeMixer__mixer,
factory=_typemixer._TypeMixer__factory,
fake=_typemixer._TypeMixer__fake,
).blend(**params)
def get_blob(**kwargs):
""" Generate value for BlobField. """
raise NotImplementedError
class GenFactory(BaseFactory):
""" Map a peewee classes to simple types. """
types = {
AutoField: t.PositiveInteger,
IntegerField: int,
BigIntegerField: t.BigInteger,
(FloatField, DoubleField): float,
DecimalField: decimal.Decimal,
CharField: str,
TextField: t.Text,
DateTimeField: datetime.datetime,
DateField: datetime.date,
TimeField: datetime.time,
BooleanField: bool,
# BlobField: None,
def get_nested(_scheme=None, _typemixer=None, _many=False, **kwargs):
"""Create nested objects."""
obj = TypeMixer(
_scheme,
mixer=_typemixer._TypeMixer__mixer,
factory=_typemixer._TypeMixer__factory,
fake=_typemixer._TypeMixer__fake,
).blend(**kwargs)
if _many:
return [obj]
return obj
class GenFactory(BaseFactory):
"""Support for Marshmallow fields."""
types = {
(fields.Str, fields.String): str,
fields.UUID: t.UUID,
(fields.Number, fields.Integer, fields.Int): t.BigInteger,
fields.Decimal: decimal.Decimal,
(fields.Bool, fields.Boolean): bool,
fields.Float: float,
(fields.DateTime, fields.LocalDateTime): dt.datetime,
fields.Time: dt.time,
fields.Date: dt.date,
(fields.URL, fields.Url): t.URL,
fields.Email: t.EmailString,
# fields.FormattedString
def get_generic_reference(_typemixer=None, **params):
""" Choose a GenericRelation. """
meta = type(_typemixer)
scheme = faker.random_element([
m for (_, m, _, _) in meta.mixers.keys()
if issubclass(m, Document) and m is not _typemixer._TypeMixer__scheme # noqa
])
return TypeMixer(scheme, mixer=_typemixer._TypeMixer__mixer,
factory=_typemixer._TypeMixer__factory,
fake=_typemixer._TypeMixer__fake).blend(**params)
class GenFactory(BaseFactory):
""" Map a mongoengine classes to simple types. """
types = {
BooleanField: bool,
DateTimeField: datetime.datetime,
DecimalField: decimal.Decimal,
EmailField: t.EmailString,
FloatField: float,
IntField: int,
StringField: str,
URLField: t.URL,
UUIDField: t.UUID,
}
generators = {
from sqlalchemy.orm.relationships import RelationshipProperty
except ImportError:
from sqlalchemy.orm.properties import RelationshipProperty
from sqlalchemy.types import (
BIGINT, BOOLEAN, BigInteger, Boolean, CHAR, DATE, DATETIME, DECIMAL, Date,
DateTime, FLOAT, Float, INT, INTEGER, Integer, NCHAR, NVARCHAR, NUMERIC,
Numeric, SMALLINT, SmallInteger, String, TEXT, TIME, Text, Time, Unicode,
UnicodeText, VARCHAR, Enum)
from .. import mix_types as t
from ..main import (
SKIP_VALUE, LOGGER, TypeMixer as BaseTypeMixer, GenFactory as BaseFactory,
Mixer as BaseMixer, partial, faker)
class GenFactory(BaseFactory):
""" Map a sqlalchemy classes to simple types. """
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,
}
def get_set_field(**kwargs):
return set(get_list_field(**kwargs))
def get_objectid(*args, **kwargs):
""" Create a new ObjectId instance.
:return ObjectId:
"""
return ObjectId()
class GenFactory(BaseFactory):
""" Map a mongoengine classes to simple types. """
types = {
BooleanField: bool,
DatetimeField: datetime.datetime,
DecimalField: decimal.Decimal,
EmailField: t.EmailString,
FloatField: float,
IntegerField: int,
StringField: str,
}
generators = {
ListField: get_list_field,
SetField: get_set_field,