New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLAlchemy Support #64

Closed
wants to merge 13 commits into
base: master
from

Conversation

Projects
None yet
3 participants
@rcommande

rcommande commented Jun 2, 2013

Hi,
Thank you for your job!
I added a class for working with my favorite ORM: SQLAlchemy.

class UserFactory(SQLAlchemyModelFactory):
    FACTORY_FOR = User
    SESSION = DBSession

    email = 'commande.romain@gmail.com'
    username = 'rcommande'

The "SESSION" attribute is the SQLAlchemy session object with which you want to work.
http://docs.sqlalchemy.org/en/rel_0_8/orm/session.html

@teserak

This comment has been minimized.

Show comment
Hide comment
@teserak

teserak commented Jun 6, 2013

+1

@rbarrois

This comment has been minimized.

Show comment
Hide comment
@rbarrois

rbarrois Jun 9, 2013

Member

Great idea!

I see you're using some __import magic; I've just moved all ORM-specific code in dedicated modules to simplify import sorting.
You can now move your code to a new factory.sqlalchemy module, with proper sqlalchemy imports :)

For the SESSION attribute:

  • If you name it FACTORY_SESSION, it will be automatically hidden
  • I'm not an expert on SQLAlchemy, what is the expected behavior of this Session class:
    • Project specific (bound to a project's way to connect to a database)
    • Totally abstract (another project can import your factory and reuse Session in its own way)
    • Would it need to be overridden on some calls?

Anyway, thanks for helping bring factory_boy to SQLAlchemy!

Member

rbarrois commented Jun 9, 2013

Great idea!

I see you're using some __import magic; I've just moved all ORM-specific code in dedicated modules to simplify import sorting.
You can now move your code to a new factory.sqlalchemy module, with proper sqlalchemy imports :)

For the SESSION attribute:

  • If you name it FACTORY_SESSION, it will be automatically hidden
  • I'm not an expert on SQLAlchemy, what is the expected behavior of this Session class:
    • Project specific (bound to a project's way to connect to a database)
    • Totally abstract (another project can import your factory and reuse Session in its own way)
    • Would it need to be overridden on some calls?

Anyway, thanks for helping bring factory_boy to SQLAlchemy!

@rcommande

This comment has been minimized.

Show comment
Hide comment
@rcommande

rcommande Jun 12, 2013

I have moved the SQLAlchemyModelFactory to factory.alchemy module and add some tests (the same as django module)

I renamed "SESSION" attribute to "FACTORY_SESSION"

For the session object, it's the equivalent to django manager objects but it's an external object.

Django:
Foo.objects.all()

SQLAlchemy:
session.query(Foo).all()

Also, the session object contains the connection configuration.

The SQLAlchemyModelFactory class is totally abstract and can work with any project as long as you provide a session object.

rcommande commented Jun 12, 2013

I have moved the SQLAlchemyModelFactory to factory.alchemy module and add some tests (the same as django module)

I renamed "SESSION" attribute to "FACTORY_SESSION"

For the session object, it's the equivalent to django manager objects but it's an external object.

Django:
Foo.objects.all()

SQLAlchemy:
session.query(Foo).all()

Also, the session object contains the connection configuration.

The SQLAlchemyModelFactory class is totally abstract and can work with any project as long as you provide a session object.

@rbarrois

This comment has been minimized.

Show comment
Hide comment
@rbarrois

rbarrois Jun 13, 2013

These imports where for backwards compatibility; I'd rather keep each orm-dedicated lib in its own module, thus avoiding try..except ImportError blocks.

rbarrois commented on factory/__init__.py in c71870b Jun 13, 2013

These imports where for backwards compatibility; I'd rather keep each orm-dedicated lib in its own module, thus avoiding try..except ImportError blocks.

@rbarrois

This comment has been minimized.

Show comment
Hide comment
@rbarrois

rbarrois Jun 13, 2013

Wow, isn't there a way to get that in an easier way?

rbarrois commented on factory/alchemy.py in c71870b Jun 13, 2013

Wow, isn't there a way to get that in an easier way?

@rbarrois

This comment has been minimized.

Show comment
Hide comment
@rbarrois

rbarrois Jun 13, 2013

I'd rather keep imports at the module toplevel

rbarrois commented on factory/alchemy.py in baad877 Jun 13, 2013

I'd rather keep imports at the module toplevel

@rbarrois

This comment has been minimized.

Show comment
Hide comment
@rbarrois

rbarrois Jun 13, 2013

Member

Hi,

This looks quite great! I've just made a couple of remarks on the code (mostly regarding style).
I'll try to merge this soon — it would be great if you could add a couple of lines to the docs (for SQLAlchemy-specific aspects, e.g. DBSession).

Thanks!

Member

rbarrois commented Jun 13, 2013

Hi,

This looks quite great! I've just made a couple of remarks on the code (mostly regarding style).
I'll try to merge this soon — it would be great if you could add a couple of lines to the docs (for SQLAlchemy-specific aspects, e.g. DBSession).

Thanks!

@rcommande

This comment has been minimized.

Show comment
Hide comment
@rcommande

rcommande Jun 19, 2013

Hi,

I did some modifications based on your feedback.
The documentation has changed as well. Some corrections will be welcome.

rcommande commented Jun 19, 2013

Hi,

I did some modifications based on your feedback.
The documentation has changed as well. Some corrections will be welcome.

@rbarrois

This comment has been minimized.

Show comment
Hide comment
@rbarrois

rbarrois Jun 19, 2013

Great!

I'll merge that right away.

rbarrois commented on 79882d2 Jun 19, 2013

Great!

I'll merge that right away.

@rbarrois

This comment has been minimized.

Show comment
Hide comment
@rbarrois

rbarrois Jun 19, 2013

Member

Merged at 4f786ac.

Member

rbarrois commented Jun 19, 2013

Merged at 4f786ac.

@rbarrois rbarrois closed this Jun 19, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment