UNPKG

2.13 kBtext/coffeescriptView Raw
1Schema = require('../index').Schema
2Text = Schema.Text
3
4require('./spec_helper').init exports
5
6schemas =
7 neo4j:
8 url: 'http://localhost:7474/'
9 mongoose:
10 url: 'mongodb://localhost/test'
11 redis: {}
12 memory: {}
13 cradle: {}
14 nano:
15 url: 'http://localhost:5984/nano-test'
16
17testOrm = (dataSource) ->
18 User = Post = 'unknown'
19 maxUsers = 100
20 maxPosts = 50000
21 users = []
22
23 it 'should define simple', (test) ->
24 User = dataSource.define 'User', {
25 name: String,
26 bio: Text,
27 approved: Boolean,
28 joinedAt: Date,
29 age: Number
30 }
31
32 Post = dataSource.define 'Post',
33 title: { type: String, length: 255, index: true }
34 content: { type: Text }
35 date: { type: Date, detault: Date.now }
36 published: { type: Boolean, default: false }
37
38 User.hasMany(Post, {as: 'posts', foreignKey: 'userId'})
39 Post.belongsTo(User, {as: 'author', foreignKey: 'userId'})
40
41 test.done()
42
43 it 'should create users', (test) ->
44 wait = maxUsers
45 done = (e, u) ->
46 users.push(u)
47 test.done() if --wait == 0
48 User.create(done) for i in [1..maxUsers]
49
50 it 'should create bunch of data', (test) ->
51 wait = maxPosts
52 done = ->
53 test.done() if --wait == 0
54 rnd = (title) ->
55 {
56 userId: users[Math.floor(Math.random() * maxUsers)].id
57 title: 'Post number ' + (title % 5)
58 }
59 Post.create(rnd(num), done) for num in [1..maxPosts]
60
61 it 'do some queries using foreign keys', (test) ->
62 wait = 4
63 done = ->
64 test.done() if --wait == 0
65 ts = Date.now()
66 query = (num) ->
67 users[num].posts { title: 'Post number 3' }, (err, collection) ->
68 console.log('User ' + num + ':', collection.length, 'posts in',
69 Date.now() - ts, 'ms')
70 done()
71 query num for num in [0..4]
72
73 return
74
75 it 'should destroy all data', (test) ->
76 Post.destroyAll ->
77 User.destroyAll(test.done)
78
79Object.keys(schemas).forEach (schemaName) ->
80 return if process.env.ONLY && process.env.ONLY != schemaName
81 context schemaName, ->
82 dataSource = new Schema schemaName, schemas[schemaName]
83 testOrm(dataSource)
84