you want to mock the entire relational database at the engine level?  that 
sounds kind of tough?    you should mock at the highest level possible in order 
to test the actual code you're testing.    unles you're testing that "select * 
from table" returns what you expect, I'm not sure you'd be mocking at that low 
a level.

As for the session, set it up when you need it, then use it.   depends on how 
your application has structured it in the first place, can't say much without 
specifics.

the pattern I use for testing models and use cases is usually against a real 
database and I do what you see here: 
http://docs.sqlalchemy.org/en/rel_0_8/orm/session.html#joining-a-session-into-an-external-transaction


On Sep 10, 2013, at 4:46 AM, Michel Albert <[email protected]> wrote:

> I am trying to wrap my head around how to do Dependency Injection with 
> SQLAlchemy and I am walking in circles.
> 
> I want to be able to mock out SA for most of my tests. I trust SA and don't 
> want to test serialisation into the DB. I just want to test my own code. So I 
> was thinking to do dependency injection, and mock out SA during testing.
> 
> But I don't know what to mock out, how and when to set up the session 
> properly, without doing it at the module level (which causes unwanted 
> side-effects only by importing the module).
> 
> The only solution which comes to mind is to have one "singleton" which deals 
> with that. But that feels very unpythonic to me and I am wondering if there's 
> a better solution.
> 
> I also saw that "create_engine" has an optional "module" kwarg, which I could 
> mock out. But then SA begins complaining that the return types are not 
> correct. And I don't want to specify return values for every possible 
> db-module call. That's way out of scope of my tests. I am not calling 
> anything on the db-module. That's SA's job, and, as said, I already trust SA.
> 
> Whenever I work on this I always run into the session_maker initialisation as 
> well. The examples to this on the module level, which I really make me feel 
> uneasy.
> 
> Any tips? Just prodding myself in the right direction might help me out 
> enough.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/groups/opt_out.

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to