Skip to content
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

Loading and dumping to and from data #73

Closed
mehcode opened this issue Jul 16, 2013 · 3 comments
Closed

Loading and dumping to and from data #73

mehcode opened this issue Jul 16, 2013 · 3 comments

Comments

@mehcode
Copy link

mehcode commented Jul 16, 2013

What does everyone think about adding a load/dump API that takes/consumes lists of dictionaries? This would allow for easy consuming and producing JSON / YAML fixtures.

Something like the following.

import factory
from . import models

class UserFactory(factory.Factory):
    FACTORY_FOR = models.User

    first_name = 'John'
    last_name = 'Doe'
    admin = False

# Deserialize an iterable of dictionaries using the factory.
UserFactory.load([{...}, {...}])

# Serialize an iterable of targets to an list of dictionaries
# factoring out attributes provided by factories, etc. 
data = UserFactory.dump([models.User(first_name="Bob"), 
                         models.User()])

assert 'first_name' not in data[1]
@rbarrois
Copy link
Member

The load part sounds easy to handle (though dicts would need to be "flattened" into foo__bar keys for nested relations). The dump is much more complicated, especially once taking into account LazyAttribute fields.

The API I have in mind for load would look like:

MyFactory.load([{}, {}], defaults={'x=1'})

What would you think about that API?

@jeffwidman
Copy link
Member

@mehcode Can you explain a little more about the scenarios you'd find the load behavior useful?

I've never personally run into a situation where this was necessary... if I want to specify what appears in the DB, then I can just submit it straight to the ORM. If I need to pull from a list of specific options when creating a Factory, then I pass the list of option to the Factory.

That said, the dump behavior would be convenient for using fake data created by FactoryBoy in API testing--see #68 (comment).

@jeffwidman
Copy link
Member

Closing for now due to lack of interest. If someone stumbles across this and really wants this, feel free to leave a comment and we can re-open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants