factory_boy icon indicating copy to clipboard operation
factory_boy copied to clipboard

Document how to use Faker inside LazyAttribute

Open mcosta74 opened this issue 2 years ago • 3 comments

The problem

The class factory.Faker is a wrapper around the "real" Faker. It works fine but sometimes you need to use it inside a LazyAttribute and you need to generate a value.

At the moment I use a trick like

class MyFactory(factory.Factory):
    class Params:
        user = None

    @factory.lazy_attribute
    def current_ip_address(obj):
        if obj.user:
            return obj.user.ip_address
        else:
            return factory.Faker('ipv4').evaluate(None, None, {'locale': factory.Faker._DEFAULT_LOCALE})

Proposed solution

In some cases I can use Maybe but not always. Would be nice to have a section in the documentation that explain how to use Faker in LazyAttribute

mcosta74 avatar Mar 15 '22 10:03 mcosta74

It's currently not supported, indeed. The code you're using is a private API.

Instead, you could use:

class MyFactory(factory.Factory):
    class Params:
        fallback_ip_address = factory.Faker("ipv4")

    current_ip_address = factory.LazyAttribute(
        lambda o: o.user.ip_address if o.user else o.fallback_ip_address,
    )

rbarrois avatar Mar 16 '22 13:03 rbarrois

thanks for the reply, maybe a note in documentation might help

mcosta74 avatar Mar 16 '22 15:03 mcosta74

I am in the midst of implementing a testing suite built on SQLAlchemy and Factory Boy. There are a number of small updates I would like to add to the documentation, specifically for users trying to use Factory Boy with SQLAlchemy directly. I do not mind including this update to the documentation with my planned PR. That is, unless someone else wants to take it.

nmaynes avatar Apr 22 '22 20:04 nmaynes