如果不添加MyFlushingModelFactory
作为所有Factory类的基类,如何设置Meta类值sqlalchemy_session_persistence
我正在运行pytest,当机器负载很重时,我的一些测试失败并出现完整性错误。通过简单地将冲洗添加到工厂模型创建周围的代码中,可以清除大多数这些问题。但是我不想把它添加到我的所有工厂类中。有没有办法设置Meta类值一次,如果没有额外的工作就会影响我的所有工厂?
# encoding: utf-8
import factory
class MyFlushingModelFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
sqlalchemy_session_persistence = factory.alchemy.SESSION_PERSISTENCE_FLUSH
我希望设置SQLAlchemyModelFactory
的默认行为,以便在.create
调用中将创建的对象刷新到磁盘。
答案 0 :(得分:0)
对于我们来说,具有重写选项的基类似乎有效:
from factory.alchemy import SQLAlchemyModelFactory
class BaseSQLAlchemyModelFactory(SQLAlchemyModelFactory):
"""Always inherit from here"""
class Meta:
abstract = True # Cannot initialize
sqlalchemy_session = db.session # The session object. In our case, this is the flask-sqlalchemy `db` object
sqlalchemy_session_persistence = 'flush'
class WorkflowFactory(BaseSQLAlchemyModelFactory):
"""Subclass that actually is the factory"""
class Meta:
model = MyModel