I have a Django Model that I would like to test using Factoryboy.
The issue here is that the fields depend on each other.
class SearchPreferences(models.Model):
min_age = models.PositiveSmallIntegerField(null=True)
max_age = models.PositiveSmallIntegerField(null=True)
In this case, max_age
cannot be smaller then min_age
.
class SearchPreferencesFactory(DjangoModelFactory):
min_age = FuzzyInteger(30, 90)
max_age = FuzzyInteger(SelfAttribute('min_age'), 100)
This is what I have tried to do, which should give me a value for max_age
between min_age
and 100, but what happens is a TypeError:
TypeError: unsupported operand type(s) for +: 'SelfAttribute' and 'int'
This makes sense to me, but I can't really figure out how to get this to work?
Could someone explain what would be the best approach here?
You could use a LazyAttribute on your max_age, ie:
class SearchPreferencesFactory(DjangoModelFactory):
min_age = FuzzyInteger(30, 90)
max_age = LazyAttribute(lambda x: FuzzyInteger(x.min_age, 100).fuzz())