Separate files: Factories & Tests

22 views
Skip to first unread message

Jim Ferrara

unread,
May 31, 2016, 9:35:39 AM5/31/16
to Factory Boy
This problem seems to be fairly simple on the face, but I think it's a little more complex and I haven't found a great solution yet, so I'm appealing to you for help.

My setup is that I'm using sqlalchemy (1.0.8) and factory_boy (2.7.0)

There is a great example for how to build factories here: http://factoryboy.readthedocs.io/en/latest/orms.html#factory.alchemy.SQLAlchemyOptions 

My question relates to how I should split the factories out into a separate file from the tests.  I have many tests and a good number of factories, so I want to keep them separate.  When I do this, I need to specify sqlalchemy_session in the factory Meta class, but I run into some issues here.

How can I link the session I am using in my tests to the one that I'm using in my factories?  I cannot seem to figure out how to do this - if I simply redeclare the session in both files, my tests fail because the session used to generate the factories is different from the one in the tests.  If I don't declare the session in the factory file, the file won't work - because you must supply something for the session or the default is None (https://github.com/rbarrois/factory_boy/blob/master/factory/alchemy.py#L30).

Does anyone have any idea as to how I can solve this issue?  I've done a lot of reading (to include these pages: http://docs.sqlalchemy.org/en/rel_0_9/orm/contextual.html#unitofwork-contextualhttp://docs.sqlalchemy.org/en/rel_0_9/orm/session_basics.html#session-faq-whentocreate) as well, but cannot seem to reason out a good answer.

Thanks so much in advance.

Raphaël Barrois

unread,
May 31, 2016, 2:41:57 PM5/31/16
to Jim Ferrara, Factory Boy
Hi,

That's an interesting problem :)

An option would be to declare the session in a separate file, imported from both — typically a ``scoped_session`` as
you pointed.

If both modules are using, say, ``from . import testing; session = testing.SharedSession()`` where testing.SharedSession is defined through
``scoped_session``, then you would be using the *same* session in both modules.


Another option would be to push on issue https://github.com/rbarrois/factory_boy/issues/304, where we're considering accepting a callable
for the sqlalchemy_session parameter — that getter could then lazily load the session used by your testrunner.


Cheers,

--
Raphaël

Jim Ferrara

unread,
Jun 1, 2016, 7:44:00 PM6/1/16
to Factory Boy, james....@gmail.com
Raphaël - 

Thanks for your reply.  I ultimately did figure it out - the separate file from which I import both turned out to work best.  In fact, I found a lot of handy uses for having such a file.  I think having a callable might be nice, but for now just having the session in a separate file worked pretty well.  I may wrap this up into a blog post & example github repo and record it online for posterity.  I appreciate your help.

-Jimmy
Reply all
Reply to author
Forward
0 new messages