Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Generate Fixtures from Database with SqlAlchemy

I'm starting to write tests with Flask-SQLAlchemy, and I'd like to add some fixtures for those. I have plenty of good data for that in my development database and a lot of tables so writing data manually would get annoying. I'd really like to just sample data from the dev database into fixtures and then use those. What's a good way to do this?

like image 897
Eli Avatar asked Jul 02 '15 08:07

Eli


1 Answers

i would use factory boy

to create a model factory you just do:

import factory
from . import models

class UserFactory(factory.Factory):
    class Meta:
        model = models.User

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

then to create instances:

UserFactory.create()

to add static data just give as kwarg to create

UserFactory.create(name='hank')

so to seed a bunch of stuff throw that in a for loop. :)

like image 71
Kyle Roux Avatar answered Sep 21 '22 06:09

Kyle Roux