Skip to main content

Appengine fixture loader

Project description

A simple way to load Django-like fixtures into the local development datastore, originally intended to be used by testable_appengine.

Single-level loads

Let’s say you have a model like this:

class Person(ndb.Model):
    """Our sample class"""
    first_name = ndb.StringProperty()
    last_name = ndb.StringProperty()
    born = ndb.DateTimeProperty()
    userid = ndb.IntegerProperty()
    thermostat_set_to = ndb.FloatProperty()
    snores = ndb.BooleanProperty()
    started_school = ndb.DateProperty()
    sleeptime = ndb.TimeProperty()
    favorite_movies = ndb.JsonProperty()
    processed = ndb.BooleanProperty(default=False)

If you want to load a data file like this:

[
    {
        "born": "1968-03-03T00:00:00",
        "first_name": "John",
        "last_name": "Doe",
        "favorite_movies": [
            "2001",
            "The Day The Earth Stood Still (1951)"
        ],
        "snores": false,
        "sleeptime": "23:00",
        "started_school": "1974-02-15",
        "thermostat_set_to": 18.34,
        "userid": 1
    },

...

    {
        "born": "1980-05-25T00:00:00",
        "first_name": "Bob",
        "last_name": "Schneier",
        "favorite_movies": [
            "2001",
            "Superman"
        ],
        "snores": true,
        "sleeptime": "22:00",
        "started_school": "1985-08-01",
        "thermostat_set_to": 18.34,
        "userid": -5
    }
]

All you need to do is to:

from appengine_fixture_loader.loader import load_fixture

and then:

loaded_data = load_fixture('tests/persons.json', kind = Person)

In our example, loaded_data will contain a list of already persisted Person models you can then manipulate and persist again.

Multi-kind loads

It’s convenient to be able to load multiple kinds of objects from a single file. For those cases, we provide a simple way to identify the kind of object being loaded and to provide a set of models to use when loading the objects.

Consider our original example model:

class Person(ndb.Model):
    """Our sample class"""
    first_name = ndb.StringProperty()
    last_name = ndb.StringProperty()
    born = ndb.DateTimeProperty()
    userid = ndb.IntegerProperty()
    thermostat_set_to = ndb.FloatProperty()
    snores = ndb.BooleanProperty()
    started_school = ndb.DateProperty()
    sleeptime = ndb.TimeProperty()
    favorite_movies = ndb.JsonProperty()
    processed = ndb.BooleanProperty(default=False)

and let’s add a second one:

class Dog(ndb.Model):
    """Another sample class"""
    name = ndb.StringProperty()

Now, if we wanted to make a single file load objects of the two kinds, we’d need to use the “__kind__” attribute in the JSON:

[
    {
        "__kind__": "Person",
        "born": "1968-03-03T00:00:00",
        "first_name": "John",
        "last_name": "Doe",
        "favorite_movies": [
            "2001",
            "The Day The Earth Stood Still (1951)"
        ],
        "snores": false,
        "sleeptime": "23:00",
        "started_school": "1974-02-15",
        "thermostat_set_to": 18.34,
        "userid": 1
    },
    {
        "__kind__": "Dog",
        "name": "Fido"
    }
]

And, to load the file, we’d have to:

from appengine_fixture_loader.loader import load_fixture

and:

loaded_data = load_fixture('tests/persons_and_dogs.json',
                           kinds={'Person': Person, 'Dog': Dog})

will result in a list of Persons and Dogs (in this case, one person and one dog).

History

0.1.0 (2014-10-13)

  • First release on GitHub.

0.1.1 (2014-12-4)

  • Add support for multi-kind JSON files

0.1.2 (2014-12-4)

  • Minor fixes

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Appengine-Fixture-Loader-0.1.2.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

Appengine_Fixture_Loader-0.1.2-py2.7.egg (5.4 kB view details)

Uploaded Source

File details

Details for the file Appengine-Fixture-Loader-0.1.2.tar.gz.

File metadata

File hashes

Hashes for Appengine-Fixture-Loader-0.1.2.tar.gz
Algorithm Hash digest
SHA256 99b812dd5ad92948604800cd39c39f17b717dffa2886d1eb99c1cebb34662cb4
MD5 7a74de9e79f7a8e4aa9c9e3edd7e220c
BLAKE2b-256 c0c36b2a1acbeb8bd5a8056c12090e23038bfb24530d120082b4434675cea229

See more details on using hashes here.

File details

Details for the file Appengine_Fixture_Loader-0.1.2-py2.7.egg.

File metadata

File hashes

Hashes for Appengine_Fixture_Loader-0.1.2-py2.7.egg
Algorithm Hash digest
SHA256 f5bcda6cf864a8d4fba3ba7191bfa24a5f2c4033e11befc782c63019c10bfc88
MD5 4d9002a06f15a03f8e8641c099507867
BLAKE2b-256 77e4e40c0c5a9938b45df337e3adc25ef68bfe9fecee613e0be31f8cb4ef7549

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page