I wrote a blog post on this very topic recently: http://alextechrants.blogspot.fi/2013/08/unit-testing-sqlalchemy-apps.html
tiistai, 10. syyskuuta 2013 19.43.35 UTC+3 Toph Burns kirjoitti: > > Could you use an in-memory, sqlite db for your testing? For our > applications, we have an initialization function that loads the database > connection strings from a config (.ini) file, passing those on to > create_engine. In production it's a postgresql connection string, for > test, it's a "sqlite:///:memory:' > > > Toph Burns | Software Engineer > 5885 Hollis St. Suite 100 > Emeryville, CA 94608 > 510-597-4797 > bu...@amyris.com <javascript:> > ------------------------------ > *From:* sqlal...@googlegroups.com <javascript:> > [sqlal...@googlegroups.com<javascript:>] > on behalf of Michel Albert [exh...@gmail.com <javascript:>] > *Sent:* Tuesday, September 10, 2013 1:46 AM > *To:* sqlal...@googlegroups.com <javascript:> > *Subject:* [sqlalchemy] Unit testing, mocking and dependency injection > with SA Declarative. > > 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 sqlalchemy+...@googlegroups.com <javascript:>. > To post to this group, send email to sqlal...@googlegroups.com<javascript:> > . > Visit this group at http://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/groups/opt_out. > -- 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 sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.