80 lines
2.2 KiB
CoffeeScript
80 lines
2.2 KiB
CoffeeScript
|
Schema = require('../index').Schema
|
||
|
Text = Schema.Text
|
||
|
|
||
|
require('./spec_helper').init exports
|
||
|
|
||
|
schemas =
|
||
|
neo4j:
|
||
|
url: 'http://localhost:7474/'
|
||
|
mongoose:
|
||
|
url: 'mongodb://localhost/test'
|
||
|
redis: {}
|
||
|
memory: {}
|
||
|
|
||
|
testOrm = (schema) ->
|
||
|
|
||
|
User = Post = 'unknown'
|
||
|
maxUsers = 50
|
||
|
maxPosts = 10000
|
||
|
users = []
|
||
|
|
||
|
it 'should define simple', (test) ->
|
||
|
|
||
|
User = schema.define 'User', {
|
||
|
name: String,
|
||
|
bio: Text,
|
||
|
approved: Boolean,
|
||
|
joinedAt: Date,
|
||
|
age: Number
|
||
|
}
|
||
|
|
||
|
Post = schema.define('Post', {
|
||
|
title: { type: String, length: 255 }
|
||
|
content: { type: Text }
|
||
|
date: { type: Date, detault: Date.now }
|
||
|
published: { type: Boolean, default: false }
|
||
|
})
|
||
|
|
||
|
User.hasMany(Post, {as: 'posts', foreignKey: 'userId'})
|
||
|
Post.belongsTo(User, {as: 'author', foreignKey: 'userId'})
|
||
|
|
||
|
test.done()
|
||
|
|
||
|
it 'should create users', (test) ->
|
||
|
wait = maxUsers
|
||
|
done = (e, u) ->
|
||
|
users.push(u)
|
||
|
test.done() if --wait == 0
|
||
|
User.create(done) for i in [1..maxUsers]
|
||
|
|
||
|
it 'should create bunch of data', (test) ->
|
||
|
wait = maxPosts
|
||
|
done = -> test.done() if --wait == 0
|
||
|
rnd = (title) ->
|
||
|
{
|
||
|
userId: users[Math.floor(Math.random() * maxUsers)].id,
|
||
|
title: title
|
||
|
}
|
||
|
Post.create(rnd(num), done) for num in [1..maxPosts]
|
||
|
|
||
|
it 'do some queries using foreign keys', (test) ->
|
||
|
wait = 4
|
||
|
done = -> test.done() if --wait == 0
|
||
|
ts = Date.now()
|
||
|
query = (num) ->
|
||
|
users[num].posts (err, collection) ->
|
||
|
console.log('User ' + num + ':', collection.length, 'posts in', Date.now() - ts,'ms')
|
||
|
done()
|
||
|
query num for num in [1..4]
|
||
|
|
||
|
it 'should destroy all data', (test) ->
|
||
|
Post.destroyAll ->
|
||
|
User.destroyAll(test.done)
|
||
|
|
||
|
Object.keys(schemas).forEach (schemaName) ->
|
||
|
return if process.env.ONLY && process.env.ONLY != schemaName
|
||
|
context schemaName, ->
|
||
|
schema = new Schema schemaName, schemas[schemaName]
|
||
|
testOrm(schema)
|
||
|
|