loopback-datasource-juggler/test/perf_test.coffee

81 lines
2.3 KiB
CoffeeScript
Raw Normal View History

2011-10-04 20:18:25 +00:00
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'
2011-10-05 14:47:26 +00:00
maxUsers = 100
maxPosts = 50000
2011-10-04 20:18:25 +00:00
users = []
it 'should define simple', (test) ->
User = schema.define 'User', {
name: String,
bio: Text,
approved: Boolean,
joinedAt: Date,
age: Number
}
2011-10-05 14:47:26 +00:00
Post = schema.define 'Post',
title: { type: String, length: 255, index: true }
2011-10-04 20:18:25 +00:00
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) ->
{
2011-10-05 14:47:26 +00:00
userId: users[Math.floor(Math.random() * maxUsers)].id
title: 'Post number ' + (title % 5)
2011-10-04 20:18:25 +00:00
}
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) ->
2011-10-05 14:47:26 +00:00
users[num].posts { title: 'Post number 3' }, (err, collection) ->
2011-10-04 20:18:25 +00:00
console.log('User ' + num + ':', collection.length, 'posts in', Date.now() - ts,'ms')
done()
2011-10-05 14:47:26 +00:00
query num for num in [0..4]
return
2011-10-04 20:18:25 +00:00
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)