NDB Model & Properties for creating entities with fake data on Google App Engine's datastore.
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 outerfaker
folder with the innerfaker
module
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.
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()
...
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'
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.
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']
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
The following fake values are available for the following property types:
- name
- first_name
- last_name
- username
- 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)
- all those available for StringProperty
- lorem
Fallback: lorem (blob of lorem ipsum text)
- age
- zip
- integer
Fallback: integer (random integer between 1 and 1000000)
- latitude
- longitude
- float
Fallback: float (random float between 1 and 10000)
- chance
Fallback: chance (50/50 chance of being True or False)
- all those available for any Property
Fallback: caption (64 chars of Lorem Ipsum)
- now
Fallback: now (current date time)
- now
- today
Fallback: today (current date)
- timestamp
Fallback: timestamp (current time)
- coordinates
Fallback: coordinates (fake ndb.GeoPt)
- key
Fallback: key (fake ndb.Key)
- user
Fallback: user (fake users.User)
- profile
Fallback: profile (dict of user key/value pairs)
- same as JsonProperty
- None needed
- None needed
- None needed
- Not implemented
- Not implemented
This package is offered under the MIT License, see LICENSE
for more details.