Kin — Generate Hierarchial Families of Fixtures
Authors
- Tim Oxley https://github.com/secoif
- Geoffrey Donaldson https://github.com/geoffreyd
Sponsor
Examples
We recommend the use of Faker for generating test data.
Fixed Values
Create a User blueprint with fixed values for properties
kin
Generate a User object. The returned user will have the properties supplied in the template.
kin
As expected, if we generate another User, we'll get the same values every time.
kin
Overriding Values at Generation Time
You can optionally pass a second parameter to generate
. These values will
override any values provided in the blueprint.
kin
Overriding properties can also contain keys that aren't specified in the original blueprint.
kin
Instantiating Custom Models
Kin can optionally generate custom models for you, so long as your model's constructor takes a set of properties.
For instance, you might use this as your model's constructor:
/* User Constructor */var { properties = properties || {} thisusername = propertiesusername thisemail = propertiesemail || thisusername + '@example.com'}
Specify the constructor you'd like to use with the special _model
property.
Note: you won't find the _model property on the generated object.
kin
Kin will use the supplied constructor when generating this model, passing-in any generated values as the first (and only) argument.
kin
Dynamic Values
Generating fixed values isn't much fun. We can also define asyncronous functions as dynamic generators for properties.
kin kin
Generating another user should always run the matching generation function, generating different data each time
kin
Remember When using a generation function, you must always use a callback to
return the value, in the standard err, value
format: callback(err, value1[, value2, value3... valueN])
Multiple Values
Sometimes we need to generate multiple values for a parent object. For
example, we might want to generate many tags for a Document
We can setup our
tags
property to create a single 'tag'.
kin
Then, you can specify the number you'd like to create when calling generate
.
If the blueprint property is a function, and the overridden property is a
Number it will call the property generator function N times, and will return
the results as an array.
kin /* Example output for `kin.generate('Document', {tags: 5})` */var GeneratedDocumentExample = content: 'perferendis reiciendis sequi qui eum labore' tags: 'magnam' 'inventore' 'facere' 'ut' 'rerum' kin
Nesting Models
A common use case for generating multiple values is creating multi-level model hierarchies.
kin
We can easily generate a User
with some number of Document
s by simply
passing a number as the value of the documents property.
kin /* Example output for kin.generate('User', {documents: 5}) */var GeneratedUserWithDocumentsExample = username: 'Lydia' email: 'Jaron@angie.biz' documents: content: 'velit at earum aut molestiae odio' tags: 'voluptatem' 'sunt' 'modi' content: 'eum facilis corrupti possimus qui quia' tags: 'illo' 'qui' 'dignissimos' content: 'debitis et nisi aut vero illo rem' tags: 'magnam' 'sunt' 'quia' content: 'non iusto ratione sed' tags: 'earum' 'provident' 'voluptatem' content: 'aut maxime aspernatur expedita aut voluptates' tags: 'voluptas' 'quo' 'et'
In a real environment, you might persist your generated models to the database
and instead of embedding entire objects, you only store a reference to them
via their _id
.
Note: Kin has built-in support for Mongoose models, you can specify Model
types by the name you provided when you generated the mongoose.model. So long
as Kin can find the collection via mongoose.models, you can use a String
instead of a Model reference. This is implemented to save you having to
manually require
every model in your system.
/* Saving items might look something like this if you're using an ORM like Mongoose. */ var mongoose = mongoose
If you want to use mongoose with Kin, you need to pass your reference to mongoose to Kin
kinmongoose = mongoose /* Some fixtures we prepared earlier */var User = User // Same as our user before, except as a Mongoose modelvar Stream = Stream // Imagine a stream is like an RSS feed containing 'Activities'var Activity = Activity // each Activity references a Stream ID kin kin kin
Referencing other properties
If you want to reference another property from within a generator, use
Kin.get(this, 'someProperty')
.
Because we 'simultaneously' execute each generator function, there's no
guarantee that the generator will have run by the time we're referencing it
within another property generator. Kin.get
ensures the property's generator
has returned a value.
Hidden or Meta properties
All properties beginning with an _ are not saved on the object, and instead
are returned in the generate
callback as properties of a third parameter,
without the _ prefix.
var ObjectId = mongooseTypesObjectId kin kin
Applying post processing to a generator
Sometimes you always want to apply a certain action to every model generated by your kin instance. e.g. saving a model. Kin allows you to apply 'post' functions for exactly this situation. Remember you must call the callback with the 'meta' property if you want to keep your meta data.
var ObjectId = mongooseTypesObjectId kin kin kin
Example: save
all generated Users and Streams
kin kin kin
Generator functions
Generator functions can be created so you can apply specific changes to a
generator, in a certain situation. To create a generator function simply call
generate
with no callback. You can use generator functions just like normal,
or use their additional properties to make modifications.
/* Simple example */var generateUserSimple = kin
Pass override properties when creating the generator function or when generating objects.
/* * All Users generated with this function will by default have username: bill, * overriding the value `joe` provided in the blueprint. */var generateUserWithOverrides = kin
Applying post processing to a generator function
The main use of generator functions is to apply custom post generation processing such as saving a model. This means you can have the master blueprint only contain basic, generic data, and apply persistance or transformations on a case-by-case basis.
var generateSavedUser = kin // mongoose enhanced userkin generateSavedUser