alchemy-mock
alchemy-mock copied to clipboard
Recursive Mock
I would like to understand how these objects could be extended to allow recursive operations. Let me explain a little of what this might look like. For example, it is often the case where
def setUp(): session = UnifiedAlchemyMagicMock( ...)
def my_test(self): first_item = session.query(Blah).filter(Blah.foo == 'bar').first() all_items = first_item.my_relationship.filter(Foo.bar == 'blah blah').all()
I can successfully replicate using examples the first item; however I would like all objects that are subsequently passed back from the UnifiedAlchemyMagicMock() class instance to have have embedded the same mixin as the session that I originally setup. This would allow "first_item.my_relationship.filter(" type calls on those objects via the same single setup.
My ultimate goal is also to boostrap the UnifiedAlchemyMagicMock.data from a json file; that I have first streamed out of a real set of SQLAlchemy objects. This
SQL DB ---> json file --> UnifiedAlchemyMagicMock.data --> tests
This way I can decouple the SQL DB but re-use a complex objects graph that spans my data base directly in mocked tests. The code in this repo is very close to allowing the first step; but the subsequent calls to alternative relationships and association_proxy, hybrid_properties and methods needs to be ideally supported.
can you give an example of the model which allows first_item.my_relationship.filter(Foo.bar == 'blah blah').all()
. my understanding that is not possible in SQLAlchemy. first_item.my_relationship
should either resolve to an object instance of related model if relationship(uselist=False)
or will resolve to a list of related models.
@miki725 first_item.my_relationship
can be a dynamic as in relationship(..., lazy='dynamic',.. )
. In that case it'll be a query.
interesting. didnt know that. thanks @shosca
not sure how that can be handled since first_item
is not a mocked instance. perhaps UnifiedAlchemyMagicMock
can loop over all given data and mock all dynamic relationships. will have to give it some though.