Blame view

node_modules/loopback-datasource-juggler/test/performance.coffee 2.08 KB
f7563de62   Palak Handa   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  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: {}
    cradle: {}
    nano:
      url: 'http://localhost:5984/nano-test'
  
  testOrm = (dataSource) ->
    User = Post = 'unknown'
    maxUsers = 100
    maxPosts = 50000
    users = []
  
    it 'should define simple', (test) ->
      User = dataSource.define 'User', {
        name: String,
        bio: Text,
        approved: Boolean,
        joinedAt: Date,
        age: Number
      }
  
      Post = dataSource.define 'Post',
        title: { type: String, length: 255, index: true }
        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: 'Post number ' + (title % 5)
        }
      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 { title: 'Post number 3' }, (err, collection) ->
          console.log('User ' + num + ':', collection.length, 'posts in',
            Date.now() - ts, 'ms')
          done()
      query num for num in [0..4]
  
    return
  
    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, ->
      dataSource = new Schema schemaName, schemas[schemaName]
      testOrm(dataSource)