Skip to content

NDB Model & Properties for creating entities with fake data on GAE's datastore.

License

Notifications You must be signed in to change notification settings

ozburo/ndb-faker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NDB Faker

NDB Model & Properties for creating entities with fake data on Google App Engine's datastore.

Installing

Include the ndb_faker folder in your app libs folder, or wherever you keep your third-party libraries, along with the faker module that it's dependent on.

Since the Faker repo is included as a git submodule you will need to extract and replace the outer faker folder with the inner faker module

Dependencies

Note that we are using the Faker module from deepthawtz and not the one from joke2k because of its handy memoization features.

Since NDB Faker simply calls the methods of the Faker class to create its fake data, it is possible to swap out and use whichever Faker module you prefer.

Usage

Instead of:

from google.appengine.ext import ndb

class MyModel(ndb.Model):
    name = ndb.StringProperty()
    ...

You use:

import ndb_faker

class MyModel(ndb_faker.Model):
    name = ndb_faker.StringProperty()
    ...

Or if you prefer:

from ndb_faker import model

class MyModel(model.Model):
    name = model.StringProperty()
    ...

Or even:

from ndb_faker import fake

class MyModel(fake.Model):
    name = fake.StringProperty()
    ...

Model

Using ndb_faker.Model exposes a handy generate method for creating multiple entities with fake data:

from ndb_faker import model

class MyModel(model.Model):
    name = model.StringProperty()
    email = model.StringProperty()

entities = MyModel.generate(6) # generates 6 entities

print entities # '[MyModel(key=Key('MyModel', 3), email=u'mgibson@gmail.com', name=u'Marcia Gibson'), MyModel(key=Key('MyModel', 4), email=u'ganderson@gmail.com', name=u'Gonzalo Anderson'), MyModel(key=Key('MyModel', 5), email=u'crobel@gmail.com', name=u'Clint Robel'), MyModel(key=Key('MyModel', 6), email=u'vspinka@gmail.com', name=u'Victoria Spinka'), MyModel(key=Key('MyModel', 7), email=u'jfeest@yahoo.com', name=u'Juliana Feest'), MyModel(key=Key('MyModel', 8), email=u'eparker@gmail.com', name=u'Emilie Parker')]'

The generate method in turn calls a create method that can also be called directly to create a single entity:

entity = MyModel.create()

print entity.name # 'Makayla Blanda'
print entity.email # 'mblanda@yahoo.com'

Create can also be given named arguments to override auto-generated fake data if need be:

entity = MyModel.create(name='John Smith', email='john@example.com')

print entity.name # 'John Smith'
print entity.email # 'john@example.com'

Properties

All properties offered by NDB Faker operate much in the same way as regular NDB Properties, e.g. validation, datastore_errors etc., but with the added benefit of always being assigned fake data when an entity is created in the datastore.

Default values and those set after model instantiation will be honored

A property's fake value will be generated by making a method call lookup on the Faker class based on either the property's name or a given fake option:

first_name = model.StringProperty() # 'first_name' is called on the Faker class

my_property = model.StringProperty(fake='first_name') # 'first_name' is called on the Faker class

first_name = model.StringProperty(fake='email') # 'email' is called and used instead of 'first_name'

Using the fake option takes precedent over the property's name and raises a ValueError if the corresponding Faker class does not have a method with that name

If no fake option is used and the property's name isn't found as a method on the Faker class, then it's fallback value is used instead.

Repeated Properties

Properties assigned with the repeated option are also honored generating a list of fake values.

By default, repeated values will have a length of 1, but can be changed by using the custom option length to set the number of list elements to generate:

first_name = model.StringProperty(repeated=True) # ['Emelia']

first_name = model.StringProperty(repeated=True, length=3) # ['Bernadine', 'Alexanne', 'Anita']

Memoization

Because of the handy memoization features of the Faker class, creating entities with multiple properties based on the same underlying fake data will have corresponding values:

from ndb_faker import model

class User(model.Model):
    first_name = model.StringProperty()
    last_name = model.StringProperty()
    username = model.StringProperty()
    email = model.StringProperty()
    name = model.StringProperty()

user = User.create()

print user.first_name # Ellis
print user.last_name # Renner
print user.username # erenner
print user.email # erenner@hotmail.com
print user.name # Ellis Renner

Fake Values

The following fake values are available for the following property types:

StringProperty

  • name
  • first_name
  • last_name
  • username
  • email
  • phonenumber (or phone_number)
  • full_address
  • street_address (or address)
  • city
  • state
  • zip_code
  • company
  • gender
  • ssn
  • website
  • guid
  • md5
  • sha1
  • caption

Fallback: caption (64 chars of Lorem Ipsum)

TextProperty

  • all those available for StringProperty
  • lorem

Fallback: lorem (blob of lorem ipsum text)

IntegerProperty

  • age
  • zip
  • integer

Fallback: integer (random integer between 1 and 1000000)

FloatProperty

  • latitude
  • longitude
  • float

Fallback: float (random float between 1 and 10000)

BooleanProperty

  • chance

Fallback: chance (50/50 chance of being True or False)

GenericProperty

  • all those available for any Property

Fallback: caption (64 chars of Lorem Ipsum)

DateTimeProperty

  • now

Fallback: now (current date time)

DateProperty

  • now
  • today

Fallback: today (current date)

TimeProperty

  • timestamp

Fallback: timestamp (current time)

GeoPtProperty

  • coordinates

Fallback: coordinates (fake ndb.GeoPt)

KeyProperty

  • key

Fallback: key (fake ndb.Key)

UserProperty

  • user

Fallback: user (fake users.User)

JsonProperty

  • profile

Fallback: profile (dict of user key/value pairs)

PickleProperty

  • same as JsonProperty

ComputedProperty

  • None needed

StructuredProperty

  • None needed

LocalStructuredProperty

  • None needed

BlobProperty

  • Not implemented

BlobKeyProperty

  • Not implemented

License

This package is offered under the MIT License, see LICENSE for more details.

About

NDB Model & Properties for creating entities with fake data on GAE's datastore.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages