loopback-datasource-juggler/test/schema.test.js

66 lines
1.7 KiB
JavaScript
Raw Normal View History

// Copyright IBM Corp. 2013,2019. All Rights Reserved.
2016-04-01 22:25:16 +00:00
// Node module: loopback-datasource-juggler
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
2013-04-06 10:57:12 +00:00
// This test written in mocha+should.js
2016-08-22 19:55:22 +00:00
'use strict';
/* global getSchema:false */
2018-12-07 14:54:29 +00:00
const should = require('./init.js');
2013-04-06 10:57:12 +00:00
2018-12-07 15:22:36 +00:00
const db = getSchema();
const slave = getSchema();
let Model, SlaveModel;
2013-04-04 22:12:34 +00:00
2016-04-01 11:48:17 +00:00
describe('dataSource', function() {
it('should define Model', function() {
2014-01-24 17:09:53 +00:00
Model = db.define('Model');
Model.dataSource.should.eql(db);
2018-12-07 14:54:29 +00:00
const m = new Model;
2014-01-24 17:09:53 +00:00
m.getDataSource().should.eql(db);
});
2016-04-01 11:48:17 +00:00
it('should clone existing model', function() {
2014-01-24 17:09:53 +00:00
SlaveModel = slave.copyModel(Model);
SlaveModel.dataSource.should.equal(slave);
slave.should.not.equal(db);
2018-12-07 14:54:29 +00:00
const sm = new SlaveModel;
2014-01-24 17:09:53 +00:00
sm.should.be.instanceOf(Model);
sm.getDataSource().should.not.equal(db);
sm.getDataSource().should.equal(slave);
2014-01-24 17:09:53 +00:00
});
2016-04-01 11:48:17 +00:00
it('should automigrate', function(done) {
2014-01-24 17:09:53 +00:00
db.automigrate(done);
});
2016-04-01 11:48:17 +00:00
it('should create transaction', function(done) {
2018-12-07 14:54:29 +00:00
const tr = db.transaction();
2014-01-24 17:09:53 +00:00
tr.connected.should.be.false;
tr.connecting.should.be.false;
2018-12-07 14:54:29 +00:00
let called = false;
2016-04-01 11:48:17 +00:00
tr.models.Model.create(Array(3), function() {
2014-01-24 17:09:53 +00:00
called = true;
2013-04-04 22:12:34 +00:00
});
2014-01-24 17:09:53 +00:00
tr.connected.should.be.false;
tr.connecting.should.be.true;
2016-04-01 11:48:17 +00:00
db.models.Model.count(function(err, c) {
2014-01-24 17:09:53 +00:00
should.not.exist(err);
should.exist(c);
c.should.equal(0);
called.should.be.false;
2016-04-01 11:48:17 +00:00
tr.exec(function() {
setTimeout(function() {
2014-01-24 17:09:53 +00:00
called.should.be.true;
2016-04-01 11:48:17 +00:00
db.models.Model.count(function(err, c) {
2014-01-24 17:09:53 +00:00
c.should.equal(3);
done();
});
}, 100);
});
2013-04-04 22:12:34 +00:00
});
2014-01-24 17:09:53 +00:00
});
2013-04-04 22:12:34 +00:00
});