alchemy-mock icon indicating copy to clipboard operation
alchemy-mock copied to clipboard

Recursive Mock

Open bakera1 opened this issue 6 years ago • 3 comments

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.

bakera1 avatar Jul 11 '18 10:07 bakera1

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 avatar Jul 11 '18 12:07 miki725

@miki725 first_item.my_relationship can be a dynamic as in relationship(..., lazy='dynamic',.. ). In that case it'll be a query.

shosca avatar Jul 11 '18 13:07 shosca

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.

miki725 avatar Jul 11 '18 13:07 miki725