如何使用 factory boy 测试 SQLalchemy 关联对象模型?

How to use factory boy to test SQLalchemy association-object models?

我正在为三个模型使用 SQLalchemy 关联对象模式 (http://docs.sqlalchemy.org/en/rel_1_1/orm/basic_relationships.html#association-object) classes。

左边是基本关系,一个User可以属于多个Organization。我在关联对象 class 中存储额外的用户组织相关数据。然后,关联对象 class 将多对一映射到组织。

从 SQLAlchemy 的角度来看,这种关系工作正常。问题是用 factory boy 测试这个已经证明很困难并且总是导致错误 RecursionError: maximum recursion depth exceeded.

下面是关联对象关系的三种模型,其中User是parent,Child是Organization:

class MemberOrgsAssoc(Model):
        """The left side of the relationship maps a User as a one-to-many to
        Organizations. User-Organization relevant data is stored in 
        this association-object table. Then, there is a one-to-many from
        this association-object table to the Organization table. """

        __tablename__ = 'member_orgs'
 
        member_id = Column(db.Integer, db.ForeignKey("users.id"), primary_key=True)
        org_id = Column(db.Integer, db.ForeignKey("organizations.id"), primary_key=True)
        manager_id = Column(db.Integer, db.ForeignKey("users.id"))
        org_title = Column(db.Unicode(50))
        organization = relationship("Organization", back_populates="members")
        member = relationship("User", back_populates="organizations",
                              foreign_keys=[member_id])
        manager = relationship("User", back_populates="subordinates",
                               foreign_keys=[manager_id])

class User(SurrogatePK, Model):
    """A user of the app."""
    __tablename__ = 'users'

    username = Column(db.Unicode(80), unique=True, nullable=False)
    organizations = relationship("MemberOrgsAssoc", back_populates="member",
                                 primaryjoin = "member_orgs.c.member_id == User.id",
                                 lazy="dynamic")
    subordinates = relationship("MemberOrgsAssoc", back_populates="manager",
                                primaryjoin = "member_orgs.c.manager_id == User.id",
                                lazy="dynamic")

class Organization(SurrogatePK, Model):
    """An organization that Users may belong to."""
    __tablename__ = 'organizations'
    name = Column(db.Unicode(128), nullable=False)
    members = relationship("MemberOrgsAssoc", back_populates="organization")

所以上述所有 SQLAlchemy 模型 classes 和关系现在似乎都按预期工作。

以下是我正在努力工作的三个工厂男孩class。

MemberOrgs 关联对象工厂:

class MemberOrgsAssocFactory(BaseFactory):
    """Association-object table Factory"""

    class Meta:
        """Factory config"""
        model = MemberOrgsAssoc

    member_id = factory.SubFactory('tests.factories.UserFactory')
    org_id = factory.SubFactory('tests.factories.OrganizationFactory')
    manager_id = factory.SubFactory('tests.factories.UserFactory')
    org_title = Sequence(lambda n: 'CEO{0}'.format(n))
    organization = factory.SubFactory('tests.factories.OrganizationFactory')
    member = factory.SubFactory('tests.factories.UserFactory')
    manager = factory.SubFactory('tests.factories.UserFactory')

class UserFactory(BaseFactory):
    """User factory."""

    class Meta:
        """Factory configuration."""
        model = User

    username = Sequence(lambda n: 'user{0}'.format(n))
    organizations = factory.List(
        [factory.SubFactory('tests.factories.MemberOrgsAssocFactory')])
    subordinates = factory.List(
        [factory.SubFactory('tests.factories.MemberOrgsAssocFactory')])

class OrganizationFactory(BaseFactory):
    """Company factory"""

    class Meta:
        """Factory config"""
        model = Organization

    id = Sequence(lambda n: '{0}'.format(n))
    name = Sequence(lambda n: 'company{0}'.format(n))
    members = factory.List(
        [factory.SubFactory('tests.factories.MemberOrgsAssocFactory')])

最后,需要为测试创建一个用户,下面是一个用于创建用户的 pytest fixture。这是由于“递归错误:超出最大递归深度”而导致测试失败的地方。

@pytest.fixture(scope='function')
def user(db):
    """An user for the unit tests.
    setup reference: https://github.com/FactoryBoy/factory_boy/issues/101
    # how to handle self referential foreign key relation in factory boy
    # https://github.com/FactoryBoy/factory_boy/issues/173
    """
    user = UserFactory(
        organizations__0=None,
        subordinates__0=None,
    )

    a = MemberOrgsAssocFactory(
        is_org_admin=True,
        is_default_org=True,
        is_active=True,
    )

    a.organization=OrganizationFactory()
    user.organizations.append(a)

    db.session.commit()
    return user

错误信息:

E   RecursionError: maximum recursion depth exceeded
!!! Recursion detected (same locals & position)

或多或少解决了这个问题,尽管总体上有点脆弱。必须仔细遵循要求的模式 as laid out in the sqlalchemy docs:

""" EXAMPLE USE:
 # create User object, append an Organization object via association
 p = User()
 a = MemberOrgsAssoc(extra_data="some data")
 a.organization = Organization()
 p.organizations.append(a)

# iterate through Organization objects via association, including association attributes:
 for assoc in p.organizations:
     print(assoc.extra_data)
     print(assoc.child)
"""   

以下对 pytest 夹具的更改解决了 RecursionError 问题并使其正常工作:

@pytest.fixture(scope='function')
def user(db):
    """An user for the tests."""

    user = UserFactory(
        organizations='',
        subordinates=''
    )

    a = MemberOrgsAssocFactory(
        member_id=None,
        org_id=None,
        manager_id=None,
        is_org_admin=True,
        is_default_org=True,
        is_active=True,
        organization=None,
        member=None,
        manager=None
    )
    a.organization = OrganizationFactory(members=[])
    user.organizations.append(a)
    db.session.commit()

    # debugging
    # thisuser = User.get_by_id(user.id)
    # for assoc in thisuser.organizations:
    #    if assoc.is_default_org:
    #        print('The default organization of thisuser is -> {}'.format(assoc.organization.name))

    return user