Skip to content

Factories

hypha.apply.stream_forms.testing.factories

NON_FILE_BLOCK_FACTORY_DEFINITION module-attribute

NON_FILE_BLOCK_FACTORY_DEFINITION = {'text_markup': SubFactory(ParagraphBlockFactory), 'char': SubFactory(CharFieldBlockFactory), 'text': SubFactory(TextFieldBlockFactory), 'number': SubFactory(NumberFieldBlockFactory), 'checkbox': SubFactory(CheckboxFieldBlockFactory), 'radios': SubFactory(RadioFieldBlockFactory), 'dropdown': SubFactory(DropdownFieldBlockFactory), 'checkboxes': SubFactory(CheckboxesFieldBlockFactory), 'date': SubFactory(DateFieldBlockFactory), 'time': SubFactory(TimeFieldBlockFactory), 'datetime': SubFactory(DateTimeFieldBlockFactory)}

BLOCK_FACTORY_DEFINITION module-attribute

BLOCK_FACTORY_DEFINITION = {None: NON_FILE_BLOCK_FACTORY_DEFINITION, 'image': SubFactory(ImageFieldBlockFactory), 'file': SubFactory(FileFieldBlockFactory), 'multi_file': SubFactory(MultiFileFieldBlockFactory)}

NonFileFormFieldsBlockFactory module-attribute

FormFieldsBlockFactory module-attribute

AnswerFactory

Bases: Factory

AddFormFieldsMetaclass

Bases: FactoryMetaClass

FormDataFactory

Bases: Factory

ParagraphBlockFactory

Bases: BlockFactory

Meta

model class-attribute instance-attribute
model = RichTextBlock

FormFieldBlockFactory

Bases: StructBlockFactory

default_value class-attribute instance-attribute

default_value = Faker('sentence')

field_label class-attribute instance-attribute

field_label = Faker('sentence')

help_text class-attribute instance-attribute

help_text = LazyAttribute(lambda o: f'Help text for {__name__}')

Meta

model class-attribute instance-attribute

make_answer classmethod

make_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_answer(cls, params=None):
    params = params or {}
    return cls.default_value.evaluate(None, None, dict(params, locale=None))

make_form_answer classmethod

make_form_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_form_answer(cls, params=None):
    if params:
        return params
    return cls.make_answer(params or {})

CharFieldBlockFactory

Bases: FormFieldBlockFactory

field_label class-attribute instance-attribute

field_label = Faker('sentence')

help_text class-attribute instance-attribute

help_text = LazyAttribute(lambda o: f'Help text for {__name__}')

default_value class-attribute instance-attribute

default_value = Faker('sentence')

Meta

model class-attribute instance-attribute

make_answer classmethod

make_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_answer(cls, params=None):
    params = params or {}
    return cls.default_value.evaluate(None, None, dict(params, locale=None))

make_form_answer classmethod

make_form_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_form_answer(cls, params=None):
    if params:
        return params
    return cls.make_answer(params or {})

TextFieldBlockFactory

Bases: FormFieldBlockFactory

field_label class-attribute instance-attribute

field_label = Faker('sentence')

help_text class-attribute instance-attribute

help_text = LazyAttribute(lambda o: f'Help text for {__name__}')

default_value class-attribute instance-attribute

default_value = Faker('sentence')

Meta

model class-attribute instance-attribute

make_answer classmethod

make_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_answer(cls, params=None):
    params = params or {}
    return cls.default_value.evaluate(None, None, dict(params, locale=None))

make_form_answer classmethod

make_form_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_form_answer(cls, params=None):
    if params:
        return params
    return cls.make_answer(params or {})

DateFieldBlockFactory

Bases: FormFieldBlockFactory

field_label class-attribute instance-attribute

field_label = Faker('sentence')

help_text class-attribute instance-attribute

help_text = LazyAttribute(lambda o: f'Help text for {__name__}')

default_value class-attribute instance-attribute

default_value = Faker('date_object')

Meta

model class-attribute instance-attribute

make_answer classmethod

make_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_answer(cls, params=None):
    params = params or {}
    return cls.default_value.evaluate(None, None, dict(params, locale=None))

make_form_answer classmethod

make_form_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_form_answer(cls, params=None):
    if params:
        return params
    return cls.make_answer(params or {})

TimeFieldBlockFactory

Bases: FormFieldBlockFactory

field_label class-attribute instance-attribute

field_label = Faker('sentence')

help_text class-attribute instance-attribute

help_text = LazyAttribute(lambda o: f'Help text for {__name__}')

default_value class-attribute instance-attribute

default_value = LazyFunction(lambda: replace(microsecond=0))

Meta

model class-attribute instance-attribute

make_answer classmethod

make_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_answer(cls, params=None):
    params = params or {}
    return cls.default_value.evaluate(None, None, dict(params, locale=None))

make_form_answer classmethod

make_form_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_form_answer(cls, params=None):
    if params:
        return params
    return cls.make_answer(params or {})

DateTimeFieldBlockFactory

Bases: FormFieldBlockFactory

field_label class-attribute instance-attribute

field_label = Faker('sentence')

help_text class-attribute instance-attribute

help_text = LazyAttribute(lambda o: f'Help text for {__name__}')

default_value class-attribute instance-attribute

default_value = LazyFunction(lambda: replace(microsecond=0))

Meta

model class-attribute instance-attribute

make_answer classmethod

make_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_answer(cls, params=None):
    params = params or {}
    return cls.default_value.evaluate(None, None, dict(params, locale=None))

make_form_answer classmethod

make_form_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_form_answer(cls, params=None):
    if params:
        date_time = params
    else:
        date_time = super().make_form_answer(params)
    return {
        "date": str(date_time.date()),
        "time": str(date_time.time().replace(microsecond=0)),
    }

NumberFieldBlockFactory

Bases: FormFieldBlockFactory

field_label class-attribute instance-attribute

field_label = Faker('sentence')

help_text class-attribute instance-attribute

help_text = LazyAttribute(lambda o: f'Help text for {__name__}')

default_value class-attribute instance-attribute

default_value = 100

Meta

model class-attribute instance-attribute

make_form_answer classmethod

make_form_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_form_answer(cls, params=None):
    if params:
        return params
    return cls.make_answer(params or {})

make_answer classmethod

make_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_answer(cls, params=None):
    return cls.default_value

CheckboxFieldBlockFactory

Bases: FormFieldBlockFactory

default_value class-attribute instance-attribute

default_value = Faker('sentence')

field_label class-attribute instance-attribute

field_label = Faker('sentence')

help_text class-attribute instance-attribute

help_text = LazyAttribute(lambda o: f'Help text for {__name__}')

choices class-attribute instance-attribute

choices = ['check_one', 'check_two']

Meta

model class-attribute instance-attribute

make_form_answer classmethod

make_form_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_form_answer(cls, params=None):
    if params:
        return params
    return cls.make_answer(params or {})

make_answer classmethod

make_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_answer(cls, params=None):
    return cls.choices[0]

CheckboxesFieldBlockFactory

Bases: FormFieldBlockFactory

default_value class-attribute instance-attribute

default_value = Faker('sentence')

field_label class-attribute instance-attribute

field_label = Faker('sentence')

help_text class-attribute instance-attribute

help_text = LazyAttribute(lambda o: f'Help text for {__name__}')

checkboxes class-attribute instance-attribute

checkboxes = ['check_multiple_one', 'check_multiple_two', 'check_multiple_three']

Meta

model class-attribute instance-attribute

make_form_answer classmethod

make_form_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_form_answer(cls, params=None):
    if params:
        return params
    return cls.make_answer(params or {})

make_answer classmethod

make_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_answer(cls, params=None):
    return cls.checkboxes[0:2]

RadioFieldBlockFactory

Bases: FormFieldBlockFactory

default_value class-attribute instance-attribute

default_value = Faker('sentence')

field_label class-attribute instance-attribute

field_label = Faker('sentence')

help_text class-attribute instance-attribute

help_text = LazyAttribute(lambda o: f'Help text for {__name__}')

choices class-attribute instance-attribute

choices = ['first', 'second']

Meta

model class-attribute instance-attribute

make_form_answer classmethod

make_form_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_form_answer(cls, params=None):
    if params:
        return params
    return cls.make_answer(params or {})

make_answer classmethod

make_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_answer(cls, params=None):
    return cls.choices[0]

DropdownFieldBlockFactory

Bases: FormFieldBlockFactory

default_value class-attribute instance-attribute

default_value = Faker('sentence')

field_label class-attribute instance-attribute

field_label = Faker('sentence')

help_text class-attribute instance-attribute

help_text = LazyAttribute(lambda o: f'Help text for {__name__}')

choices class-attribute instance-attribute

choices = ['first', 'second']

Meta

model class-attribute instance-attribute

make_form_answer classmethod

make_form_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_form_answer(cls, params=None):
    if params:
        return params
    return cls.make_answer(params or {})

make_answer classmethod

make_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_answer(cls, params=None):
    return cls.choices[0]

UploadableMediaFactory

Bases: FormFieldBlockFactory

field_label class-attribute instance-attribute

field_label = Faker('sentence')

help_text class-attribute instance-attribute

help_text = LazyAttribute(lambda o: f'Help text for {__name__}')

default_value class-attribute instance-attribute

default_value = FileField()

Meta

model class-attribute instance-attribute

make_form_answer classmethod

make_form_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_form_answer(cls, params=None):
    if params:
        return params
    return cls.make_answer(params or {})

make_answer classmethod

make_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_answer(cls, params=None):
    params = params or {}
    params.setdefault("data", b"this is some content")
    if params.get("filename") is None:
        params["filename"] = "test_example.pdf"
    file_name, file = cls.default_value._make_content(params)
    return SimpleUploadedFile(file_name, file.read())

ImageFieldBlockFactory

Bases: UploadableMediaFactory

field_label class-attribute instance-attribute

field_label = Faker('sentence')

help_text class-attribute instance-attribute

help_text = LazyAttribute(lambda o: f'Help text for {__name__}')

default_value class-attribute instance-attribute

default_value = ImageField()

Meta

model class-attribute instance-attribute

make_answer classmethod

make_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_answer(cls, params=None):
    params = params or {}
    params.setdefault("data", b"this is some content")
    if params.get("filename") is None:
        params["filename"] = "test_example.pdf"
    file_name, file = cls.default_value._make_content(params)
    return SimpleUploadedFile(file_name, file.read())

make_form_answer classmethod

make_form_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_form_answer(cls, params=None):
    if params:
        return params
    return cls.make_answer(params or {})

FileFieldBlockFactory

Bases: UploadableMediaFactory

default_value class-attribute instance-attribute

default_value = FileField()

field_label class-attribute instance-attribute

field_label = Faker('sentence')

help_text class-attribute instance-attribute

help_text = LazyAttribute(lambda o: f'Help text for {__name__}')

Meta

model class-attribute instance-attribute

make_answer classmethod

make_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_answer(cls, params=None):
    params = params or {}
    params.setdefault("data", b"this is some content")
    if params.get("filename") is None:
        params["filename"] = "test_example.pdf"
    file_name, file = cls.default_value._make_content(params)
    return SimpleUploadedFile(file_name, file.read())

make_form_answer classmethod

make_form_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_form_answer(cls, params=None):
    if params:
        return params
    return cls.make_answer(params or {})

MultiFileFieldBlockFactory

Bases: UploadableMediaFactory

default_value class-attribute instance-attribute

default_value = FileField()

field_label class-attribute instance-attribute

field_label = Faker('sentence')

help_text class-attribute instance-attribute

help_text = LazyAttribute(lambda o: f'Help text for {__name__}')

Meta

model class-attribute instance-attribute

make_form_answer classmethod

make_form_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_form_answer(cls, params=None):
    if params:
        return params
    return cls.make_answer(params or {})

make_answer classmethod

make_answer(params=None)
Source code in hypha/apply/stream_forms/testing/factories.py
@classmethod
def make_answer(cls, params=None):
    return [UploadableMediaFactory.make_answer() for _ in range(2)]

StreamFieldUUIDFactory

Bases: StreamFieldFactory

evaluate

evaluate(instance, step, extra)
Source code in hypha/apply/stream_forms/testing/factories.py
def evaluate(self, instance, step, extra):
    params = self.build_form(extra)
    blocks = super().evaluate(instance, step, params)
    ret_val = []
    factories = {
        k: v.get_factory()
        for k, v in self.stream_block_factory.__dict__.items()
        if isinstance(v, factory.SubFactory)
    }
    # Convert to JSON so we can add id before create
    for block_name, value in blocks:
        block = factories[block_name]._meta.model()

        value = block.get_prep_value(self.filtered_child_block_value(block, value))
        ret_val.append(
            {"type": block_name, "value": value, "id": str(uuid.uuid4())}
        )
    return json.dumps(ret_val, cls=DjangoJSONEncoder)

build_form

build_form(data)
Source code in hypha/apply/stream_forms/testing/factories.py
def build_form(self, data):
    extras = defaultdict(dict)
    exclusions = []
    multiples = {}
    for field, value in data.items():
        # we dont care about position
        name, attr = field.split("__")
        if name == "exclude":
            exclusions.append(attr)
        elif name == "multiple":
            multiples[attr] = value
        else:
            extras[name] = {attr: value}

    defined_both = set(exclusions) & set(multiples)
    if defined_both:
        raise ValueError(
            "Cant exclude and provide multiple at the same time: {}".format(
                ", ".join(defined_both)
            )
        )

    form_fields = {}
    field_count = 0
    stream_blocks = {
        k: v.get_factory()
        for k, v in self.stream_block_factory.__dict__.items()
        if isinstance(v, factory.SubFactory)
    }
    for field in stream_blocks:
        if field == "text_markup" or field in exclusions or not field:
            pass
        else:
            for _ in range(multiples.get(field, 1)):
                form_fields[f"{field_count}__{field}__"] = ""
                field_count += 1
        if extras[field]:
            for attr, value in extras[field].items():
                form_fields[f"{field_count}__{field}__{attr}"] = value
            field_count += 1

    return form_fields

filtered_child_block_value

filtered_child_block_value(block, value)
Source code in hypha/apply/stream_forms/testing/factories.py
def filtered_child_block_value(self, block, value):
    filtered_value = value
    if isinstance(value, StructValue):
        filtered_value = {
            key: val for key, val in value.items() if key in block.child_blocks
        }
    return filtered_value

form_response

form_response(fields, field_values=None)
Source code in hypha/apply/stream_forms/testing/factories.py
def form_response(self, fields, field_values=None):
    if not field_values:
        field_values = {}
    stream_blocks = {
        k: v.get_factory()
        for k, v in self.stream_block_factory.__dict__.items()
        if isinstance(v, factory.SubFactory)
    }
    data = {
        field.id: stream_blocks[field.block.name].make_form_answer(
            field_values.get(field.id, {})
        )
        for field in fields
        if hasattr(stream_blocks[field.block.name], "make_form_answer")
    }
    return flatten_for_form(data)

flatten_for_form

flatten_for_form(data, field_name='', number=False)
Source code in hypha/apply/stream_forms/testing/factories.py
def flatten_for_form(data, field_name="", number=False):
    result = {}
    for i, (field, value) in enumerate(data.items()):
        if number:
            field = f"{field_name}_{i}"
        if isinstance(value, dict):
            result.update(**flatten_for_form(value, field_name=field, number=True))
        else:
            result[field] = value
    return result