samedi 6 juin 2015

Mock out database layer in SQLAlchemy

I am working on writing some unit tests around an application which uses SQLAlchemy. I would like to abstract/mock away the database layer and just test the object.

For example classes:

class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    children = relationship("Child")

class Child(Base):
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey('parent.id'))
    parent = relationship("Parent", backref=backref('children', order_by=id))

What I would like to achieve while unit testing is something like follows:

parent = Mock()
child = Child(parent)
assertTrue(someMethod(child))

Any ideas on how to accomplish something along those lines?

Aucun commentaire:

Enregistrer un commentaire