import factory.fuzzy
import faker
from core.strings import underscore_15
from factory import DjangoModelFactory, SubFactory
faker = faker.Factory.create()
class SubProjectFactory(DjangoModelFactory):
class Meta:
model = SubProject
django_get_or_create = ("internal_name",)
internal_name = factory.Faker('pystr')
internal_short_name = factory.Faker('pystr')
underscore_15 = factory.fuzzy.FuzzyText(length=15)
main_project = SubFactory(MainProjectFactory)
I need the field underscore_15 to be a lower-case string of specifically just 15 characters long and no spaces. If there's any space it should be underscore. I tried to put a function around factory.fuzzy.FuzzyText(length=15)
. Then I realized that I was assuming FuzzyText returns a string but it was a FuzzyText object.
What should I do?
Well, you can get the FuzzyText
objects value by calling like this:
>> f = factory.fuzzy.FuzzyText(length=15)
>> f.fuzz()
But, in this text, the characters can be both uppercase or lower case. So if you want exclusive lower case characters, then you can override the FuzzyText
like this:
from factory.fuzzy import FuzzyText
class CustomFuzzyText(FuzzyText):
def fuzz(self, *args, **kwargs):
return super(CustomFuzzyText, self).fuzz(*args, **kwargs).lower()
and use it in the factory like this:
underscore_15 = CustomFuzzyText(prefix="aa_", length=12) # or CustomFuzzyText(length=15)