Skip to content

Factories

hypha.apply.stream_forms.testing.factories

BLOCK_FACTORY_DEFINITION 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 : 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 : 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 : 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 : 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 : f'Help text for {__name__}')

default_value class-attribute instance-attribute

default_value = Faker('time_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 {})

DateTimeFieldBlockFactory

Bases: FormFieldBlockFactory

field_label class-attribute instance-attribute

field_label = Faker('sentence')

help_text class-attribute instance-attribute

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

default_value class-attribute instance-attribute

default_value = Faker('date_time')

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()),
    }

NumberFieldBlockFactory

Bases: FormFieldBlockFactory

field_label class-attribute instance-attribute

field_label = Faker('sentence')

help_text class-attribute instance-attribute

help_text = LazyAttribute(lambda : 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 : 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 : 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 : 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 : 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 : 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"] = "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 : 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"] = "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 : 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"] = "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 : 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

generate

generate(step, params)
Source code in hypha/apply/stream_forms/testing/factories.py
def generate(self, step, params):
    params = self.build_form(params)
    blocks = super().generate(step, params)
    ret_val = []
    # Convert to JSON so we can add id before create
    for block_name, value in blocks:
        block = self.factories[block_name]._meta.model()
        value = block.get_prep_value(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
    for field in self.factories:
        if field == "text_markup" or field in exclusions:
            pass
        else:
            for _ in range(multiples.get(field, 1)):
                form_fields[f"{field_count}__{field}__"] = ""
                field_count += 1
        for attr, value in extras[field].items():
            form_fields[f"{field_count}__{field}__{attr}"] = value

    return form_fields

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 = {}
    data = {
        field.id: self.factories[field.block.name].make_form_answer(
            field_values.get(field.id, {})
        )
        for field in fields
        if hasattr(self.factories[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