How to use the mixer.main.faker.random_element function in mixer

To help you get started, we’ve selected a few mixer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github klen / mixer / mixer / backend / django.py View on Github external
def get_relation(_scheme=None, _typemixer=None, **params):
    """ Function description. """
    scheme = _scheme.related_model

    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)
github klen / mixer / mixer / backend / peewee.py View on Github external
def make_fabric(self, field, field_name=None, fake=False, kwargs=None): # noqa
        """ Make values fabric for column.

        :param column: SqlAlchemy column
        :param field_name: Field name
        :param fake: Force fake data

        :return function:

        """
        kwargs = {} if kwargs is None else kwargs

        if field.choices:
            try:
                choices, _ = list(zip(*field.choices))
                return partial(faker.random_element, choices)
            except ValueError:
                pass

        if isinstance(field, ForeignKeyField):
            kwargs.update({'_typemixer': self, '_scheme': field})

        return super(TypeMixer, self).make_fabric(
            type(field), field_name=field_name, fake=fake, kwargs=kwargs)
github klen / mixer / mixer / backend / mongoengine.py View on Github external
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)
github klen / mixer / mixer / backend / django.py View on Github external
:return function:

        """
        kwargs = {} if kwargs is None else kwargs

        fcls = type(field)
        stype = self.__factory.cls_to_simple(fcls)

        if fcls is models.CommaSeparatedIntegerField:
            return partial(
                faker.random_sample, range(0, field.max_length), length=field.max_length)

        if field and field.choices:
            try:
                choices, _ = list(zip(*field.choices))
                return partial(faker.random_element, choices)
            except ValueError:
                pass

        if stype in (str, t.Text):
            fab = super(TypeMixer, self).make_fabric(
                fcls, field_name=fname, fake=fake, kwargs=kwargs)
            return lambda: fab()[:field.max_length]

        if stype is decimal.Decimal:
            kwargs['left_digits'] = field.max_digits - field.decimal_places
            kwargs['right_digits'] = field.decimal_places

        elif stype is t.IPString:

            # Hack for support Django 1.4/1.5
            protocol = getattr(field, 'protocol', None)
github klen / mixer / mixer / backend / sqlalchemy.py View on Github external
ftype = type(column.type)

        # augmented types created with TypeDecorator
        # don't directly inherit from the base types
        if TypeDecorator in ftype.__bases__:
            ftype = ftype.impl

        stype = self.__factory.cls_to_simple(ftype)

        if stype is str:
            fab = super(TypeMixer, self).make_fabric(
                stype, field_name=field_name, fake=fake, kwargs=kwargs)
            return lambda: fab()[:column.type.length]

        if ftype is Enum:
            return partial(faker.random_element, column.type.enums)

        return super(TypeMixer, self).make_fabric(
            stype, field_name=field_name, fake=fake, kwargs=kwargs)
github klen / mixer / mixer / backend / marshmallow.py View on Github external
def make_fabric(self, field, field_name=None, fake=False, kwargs=None): # noqa
        kwargs = {} if kwargs is None else kwargs

        if isinstance(field, fields.Nested):
            kwargs.update({'_typemixer': self, '_scheme': type(field.schema), '_many': field.many})

        if isinstance(field, fields.List):
            fab = self.make_fabric(
                field.container, field_name=field_name, fake=fake, kwargs=kwargs)
            return lambda: [fab() for _ in range(faker.small_positive_integer(4))]

        for validator in field.validators:
            if isinstance(validator, validate.OneOf):
                return partial(faker.random_element, validator.choices)

        return super(TypeMixer, self).make_fabric(
            type(field), field_name=field_name, fake=fake, kwargs=kwargs)
github klen / mixer / mixer / backend / mongoengine.py View on Github external
:param field_name: Field name
        :param fake: Force fake data

        :return function:

        """
        ftype = type(me_field)
        kwargs = {} if kwargs is None else kwargs

        if me_field.choices:
            if isinstance(me_field.choices[0], tuple):
                choices, _ = list(zip(*me_field.choices))
            else:
                choices = list(me_field.choices)

            return partial(faker.random_element, choices)

        if ftype is StringField:
            fab = super(TypeMixer, self).make_fabric(
                ftype, field_name=field_name, fake=fake, kwargs=kwargs)
            return lambda: fab()[:me_field.max_length]

        if ftype is ListField:
            fab = self.make_fabric(me_field.field, kwargs=kwargs)
            return lambda: [fab() for _ in range(3)]

        if isinstance(me_field, (EmbeddedDocumentField, ReferenceField)):
            ftype = me_field.document_type

        elif ftype is GenericReferenceField:
            kwargs.update({'_typemixer': self})