Blame view

node_modules/loopback-datasource-juggler/test/schema.test.js 1.67 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
  // Copyright IBM Corp. 2013,2016. All Rights Reserved.
  // Node module: loopback-datasource-juggler
  // This file is licensed under the MIT License.
  // License text available at https://opensource.org/licenses/MIT
  'use strict';
  
  // This test written in mocha+should.js
  var should = require('./init.js');
  
  var db = getSchema(), slave = getSchema(), Model, SlaveModel;
  
  describe('dataSource', function() {
    it('should define Model', function() {
      Model = db.define('Model');
      Model.dataSource.should.eql(db);
      var m = new Model;
      m.getDataSource().should.eql(db);
    });
  
    it('should clone existing model', function() {
      SlaveModel = slave.copyModel(Model);
      SlaveModel.dataSource.should.equal(slave);
      slave.should.not.equal(db);
      var sm = new SlaveModel;
      sm.should.be.instanceOf(Model);
      sm.getDataSource().should.not.equal(db);
      sm.getDataSource().should.equal(slave);
    });
  
    it('should automigrate', function(done) {
      db.automigrate(done);
    });
  
    it('should create transaction', function(done) {
      var tr = db.transaction();
      tr.connected.should.be.false;
      tr.connecting.should.be.false;
      var called = false;
      tr.models.Model.create(Array(3), function() {
        called = true;
      });
      tr.connected.should.be.false;
      tr.connecting.should.be.true;
  
      db.models.Model.count(function(err, c) {
        should.not.exist(err);
        should.exist(c);
        c.should.equal(0);
        called.should.be.false;
        tr.exec(function() {
          setTimeout(function() {
            called.should.be.true;
            db.models.Model.count(function(err, c) {
              c.should.equal(3);
              done();
            });
          }, 100);
        });
      });
    });
  });