test시 원활한 테스트를 위해 데이터 셋을 만들때 사용하기 편한 package : pytest-factoryboy
import factory
from datetime import datetime
from faker import Factory as FakerFactory
from src.model.models import User, Todos
faker = FakerFactory.create()
class UserFactory(factory.alchemy.SQLAlchemyModelFactory):
""" Define user factory
"""
class Meta:
model = User
email = factory.Faker("safe_email")
name = factory.LazyAttribute(lambda x: faker.name())
# todos = factory.RelatedFactoryList(".tests.factories.TodosFactory", size=3)
class TodosFactory(factory.alchemy.SQLAlchemyModelFactory):
"""Define todo factory
"""
class Meta:
model = Todos
title = factory.LazyAttribute(lambda x: faker.name())
created_at = factory.LazyAttribute(lambda x: datetime.now())
updated_at = factory.LazyAttribute(lambda x: datetime.now())
user = factory.SubFactory(UserFactory)
UserFactory(factory.alchemy.SQLAlchemyModelFactory)
로 사용하였다.
from src.model.models import User
Class UserFactory(...):
class Meta:
model = User
class TodosFactory(...):
...
user = factory.SubFactory(UserFactory)