UNPKG

1.76 kBJavaScriptView Raw
1// Copyright IBM Corp. 2013,2019. All Rights Reserved.
2// Node module: loopback-datasource-juggler
3// This file is licensed under the MIT License.
4// License text available at https://opensource.org/licenses/MIT
5
6// This test written in mocha+should.js
7'use strict';
8
9/* global getSchema:false */
10const should = require('./init.js');
11
12const db = getSchema();
13const slave = getSchema();
14let Model, SlaveModel;
15
16describe('dataSource', function() {
17 it('should define Model', function() {
18 Model = db.define('Model');
19 Model.dataSource.should.eql(db);
20 const m = new Model;
21 m.getDataSource().should.eql(db);
22 });
23
24 it('should clone existing model', function() {
25 SlaveModel = slave.copyModel(Model);
26 SlaveModel.dataSource.should.equal(slave);
27 slave.should.not.equal(db);
28 const sm = new SlaveModel;
29 sm.should.be.instanceOf(Model);
30 sm.getDataSource().should.not.equal(db);
31 sm.getDataSource().should.equal(slave);
32 });
33
34 it('should automigrate', function(done) {
35 db.automigrate(done);
36 });
37
38 it('should create transaction', function(done) {
39 const tr = db.transaction();
40 tr.connected.should.be.false;
41 tr.connecting.should.be.false;
42 let called = false;
43 tr.models.Model.create(Array(3), function() {
44 called = true;
45 });
46 tr.connected.should.be.false;
47 tr.connecting.should.be.true;
48
49 db.models.Model.count(function(err, c) {
50 should.not.exist(err);
51 should.exist(c);
52 c.should.equal(0);
53 called.should.be.false;
54 tr.exec(function() {
55 setTimeout(function() {
56 called.should.be.true;
57 db.models.Model.count(function(err, c) {
58 c.should.equal(3);
59 done();
60 });
61 }, 100);
62 });
63 });
64 });
65});