I'm using factory_boy to create test fixtures. I've got two simple factories, backed by SQLAlchemy models (simplified below).
I'd like to be able to call AddressFactory.create()
multiple times, and have it create a Country
if it doesn't already exist, otherwise I want it to re-use the existing record.
class CountryFactory(factory.Factory):
FACTORY_FOR = Country
cc = "US"
name = "United States"
class AddressFactory(factory.Factory):
FACTORY_FOR = Address
name = "Joe User"
city = "Seven Mile Beach"
country = factory.SubFactory(CountryFactory, cc="KY", name="Cayman Islands")
My question is: how can I set up these factories so that factory_boy doesn't try to create a new Country every time it creates an Address?