croach / Flask-Fixtures

A simple library for adding database fixtures for unit tests using nothing but JSON or YAML.
MIT License
63 stars 30 forks source link

Add the ability to nest fixtures #4

Open croach opened 10 years ago

croach commented 10 years ago

The idea here is that, instead of creating two separate fixture objects, when one is a child of the other, it would be nice if we could nest them. For example, assume we have the following classes:

class Author(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String(30))
    last_name = db.Column(db.String(30))

class Book(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200))
    author = db.relationship('Author', backref='books')

It would be nice to be able to do something like this in the fixtures data:

[
    {
        "table": "authors",
        "fields": {
            "first_name": "Christopher",
                "last_name": "Roach",
                "books": [{
                    "title": "Flask in Action"
                }]
        }
    }
]

Flask-Fixtures should be able to match up attribute names against relationships and add them accordingly.