Top

telemeta.tests.instrument_factories module

Factories for the telemeta.models.instrument

# -*- coding: utf-8 -*-
"""
Factories for the telemeta.models.instrument

"""
import factory

from telemeta.models.instrument import Instrument


class InstrumentFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Instrument

    name = factory.Sequence(lambda n: 'name{0}'.format(n))

Classes

class InstrumentFactory

class InstrumentFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = Instrument

    name = factory.Sequence(lambda n: 'name{0}'.format(n))

Ancestors (in MRO)

  • InstrumentFactory
  • factory.django.DjangoModelFactory
  • factory.base.Factory
  • factory.base.BaseFactory
  • __builtin__.object

Class variables

var AssociatedClassError

var UnknownStrategy

var UnsupportedStrategy

var name

Methods

def attributes(

cls, create=False, extra=None)

Build a dict of attribute values, respecting declaration order.

The process is: - Handle 'orderless' attributes, overriding defaults with provided kwargs when applicable - Handle ordered attributes, overriding them with provided kwargs when applicable; the current list of computed attributes is available to the currently processed object.

@classmethod
def attributes(cls, create=False, extra=None):
    """Build a dict of attribute values, respecting declaration order.
    The process is:
    - Handle 'orderless' attributes, overriding defaults with provided
        kwargs when applicable
    - Handle ordered attributes, overriding them with provided kwargs when
        applicable; the current list of computed attributes is available
        to the currently processed object.
    """
    warnings.warn(
        "Usage of Factory.attributes() is deprecated.",
        DeprecationWarning,
        stacklevel=2,
    )
    declarations = cls._meta.pre_declarations.as_dict()
    declarations.update(extra or {})
    from . import helpers
    return helpers.make_factory(dict, **declarations)

def build(

cls, **kwargs)

Build an instance of the associated class, with overriden attrs.

@classmethod
def build(cls, **kwargs):
    """Build an instance of the associated class, with overriden attrs."""
    return cls._generate(enums.BUILD_STRATEGY, kwargs)

def build_batch(

cls, size, **kwargs)

Build a batch of instances of the given class, with overriden attrs.

Args: size (int): the number of instances to build

Returns: object list: the built instances

@classmethod
def build_batch(cls, size, **kwargs):
    """Build a batch of instances of the given class, with overriden attrs.
    Args:
        size (int): the number of instances to build
    Returns:
        object list: the built instances
    """
    return [cls.build(**kwargs) for _ in range(size)]

def create(

cls, **kwargs)

Create an instance of the associated class, with overriden attrs.

@classmethod
def create(cls, **kwargs):
    """Create an instance of the associated class, with overriden attrs."""
    return cls._generate(enums.CREATE_STRATEGY, kwargs)

def create_batch(

cls, size, **kwargs)

Create a batch of instances of the given class, with overriden attrs.

Args: size (int): the number of instances to create

Returns: object list: the created instances

@classmethod
def create_batch(cls, size, **kwargs):
    """Create a batch of instances of the given class, with overriden attrs.
    Args:
        size (int): the number of instances to create
    Returns:
        object list: the created instances
    """
    return [cls.create(**kwargs) for _ in range(size)]

def declarations(

cls, extra_defs=None)

Retrieve a copy of the declared attributes.

Args: extra_defs (dict): additional definitions to insert into the retrieved DeclarationDict.

@classmethod
def declarations(cls, extra_defs=None):
    """Retrieve a copy of the declared attributes.
    Args:
        extra_defs (dict): additional definitions to insert into the
            retrieved DeclarationDict.
    """
    warnings.warn(
        "Factory.declarations is deprecated; use Factory._meta.pre_declarations instead.",
        DeprecationWarning,
        stacklevel=2,
    )
    decls = cls._meta.pre_declarations.as_dict()
    decls.update(extra_defs or {})
    return decls

def generate(

cls, strategy, **kwargs)

Generate a new instance.

The instance will be created with the given strategy (one of BUILD_STRATEGY, CREATE_STRATEGY, STUB_STRATEGY).

Args: strategy (str): the strategy to use for generating the instance.

Returns: object: the generated instance

@classmethod
def generate(cls, strategy, **kwargs):
    """Generate a new instance.
    The instance will be created with the given strategy (one of
    BUILD_STRATEGY, CREATE_STRATEGY, STUB_STRATEGY).
    Args:
        strategy (str): the strategy to use for generating the instance.
    Returns:
        object: the generated instance
    """
    assert strategy in (enums.STUB_STRATEGY, enums.BUILD_STRATEGY, enums.CREATE_STRATEGY)
    action = getattr(cls, strategy)
    return action(**kwargs)

def generate_batch(

cls, strategy, size, **kwargs)

Generate a batch of instances.

The instances will be created with the given strategy (one of BUILD_STRATEGY, CREATE_STRATEGY, STUB_STRATEGY).

Args: strategy (str): the strategy to use for generating the instance. size (int): the number of instances to generate

Returns: object list: the generated instances

@classmethod
def generate_batch(cls, strategy, size, **kwargs):
    """Generate a batch of instances.
    The instances will be created with the given strategy (one of
    BUILD_STRATEGY, CREATE_STRATEGY, STUB_STRATEGY).
    Args:
        strategy (str): the strategy to use for generating the instance.
        size (int): the number of instances to generate
    Returns:
        object list: the generated instances
    """
    assert strategy in (enums.STUB_STRATEGY, enums.BUILD_STRATEGY, enums.CREATE_STRATEGY)
    batch_action = getattr(cls, '%s_batch' % strategy)
    return batch_action(size, **kwargs)

def reset_sequence(

cls, value=None, force=False)

Reset the sequence counter.

Args: value (int or None): the new 'next' sequence value; if None, recompute the next value from _setup_next_sequence(). force (bool): whether to force-reset parent sequence counters in a factory inheritance chain.

@classmethod
def reset_sequence(cls, value=None, force=False):
    """Reset the sequence counter.
    Args:
        value (int or None): the new 'next' sequence value; if None,
            recompute the next value from _setup_next_sequence().
        force (bool): whether to force-reset parent sequence counters
            in a factory inheritance chain.
    """
    cls._meta.reset_sequence(value, force=force)

def simple_generate(

cls, create, **kwargs)

Generate a new instance.

The instance will be either 'built' or 'created'.

Args: create (bool): whether to 'build' or 'create' the instance.

Returns: object: the generated instance

@classmethod
def simple_generate(cls, create, **kwargs):
    """Generate a new instance.
    The instance will be either 'built' or 'created'.
    Args:
        create (bool): whether to 'build' or 'create' the instance.
    Returns:
        object: the generated instance
    """
    strategy = enums.CREATE_STRATEGY if create else enums.BUILD_STRATEGY
    return cls.generate(strategy, **kwargs)

def simple_generate_batch(

cls, create, size, **kwargs)

Generate a batch of instances.

These instances will be either 'built' or 'created'.

Args: size (int): the number of instances to generate create (bool): whether to 'build' or 'create' the instances.

Returns: object list: the generated instances

@classmethod
def simple_generate_batch(cls, create, size, **kwargs):
    """Generate a batch of instances.
    These instances will be either 'built' or 'created'.
    Args:
        size (int): the number of instances to generate
        create (bool): whether to 'build' or 'create' the instances.
    Returns:
        object list: the generated instances
    """
    strategy = enums.CREATE_STRATEGY if create else enums.BUILD_STRATEGY
    return cls.generate_batch(strategy, size, **kwargs)

def stub(

cls, **kwargs)

Retrieve a stub of the associated class, with overriden attrs.

This will return an object whose attributes are those defined in this factory's declarations or in the extra kwargs.

@classmethod
def stub(cls, **kwargs):
    """Retrieve a stub of the associated class, with overriden attrs.
    This will return an object whose attributes are those defined in this
    factory's declarations or in the extra kwargs.
    """
    return cls._generate(enums.STUB_STRATEGY, kwargs)

def stub_batch(

cls, size, **kwargs)

Stub a batch of instances of the given class, with overriden attrs.

Args: size (int): the number of instances to stub

Returns: object list: the stubbed instances

@classmethod
def stub_batch(cls, size, **kwargs):
    """Stub a batch of instances of the given class, with overriden attrs.
    Args:
        size (int): the number of instances to stub
    Returns:
        object list: the stubbed instances
    """
    return [cls.stub(**kwargs) for _ in range(size)]