2019-04-18 21:32:38 +00:00
|
|
|
// Copyright IBM Corp. 2014,2019. All Rights Reserved.
|
2016-05-06 04:50:59 +00:00
|
|
|
// Node module: loopback-connector
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2017-03-06 23:40:47 +00:00
|
|
|
'use strict';
|
2019-08-02 13:50:51 +00:00
|
|
|
const expect = require('chai').expect;
|
|
|
|
const testConnector = require('./connectors/test-sql-connector');
|
2014-10-13 16:47:11 +00:00
|
|
|
|
2019-08-02 13:50:51 +00:00
|
|
|
const juggler = require('loopback-datasource-juggler');
|
|
|
|
const ds = new juggler.DataSource({
|
2014-10-13 16:47:11 +00:00
|
|
|
connector: testConnector,
|
2016-04-09 18:35:52 +00:00
|
|
|
debug: true,
|
2014-10-13 16:47:11 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('sql connector', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
ds.connector._tables = {};
|
|
|
|
ds.connector._models = {};
|
|
|
|
ds.createModel('m1', {});
|
2020-02-22 14:05:17 +00:00
|
|
|
ds.createModel('m2', {});
|
2014-10-13 16:47:11 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('automigrate all models', function(done) {
|
|
|
|
ds.automigrate(function(err) {
|
|
|
|
expect(ds.connector._tables).have.property('m1');
|
2020-02-22 14:05:17 +00:00
|
|
|
expect(ds.connector._tables).have.property('m2');
|
2014-10-13 16:47:11 +00:00
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('automigrate one model', function(done) {
|
|
|
|
ds.automigrate('m1', function(err) {
|
|
|
|
expect(ds.connector._tables).have.property('m1');
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('automigrate one or more models in an array', function(done) {
|
|
|
|
ds.automigrate(['m1'], function(err) {
|
|
|
|
expect(ds.connector._tables).have.property('m1');
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('automigrate reports errors for models not attached', function(done) {
|
2020-02-22 14:05:17 +00:00
|
|
|
ds.automigrate(['m1', 'm3'], function(err) {
|
2014-10-13 16:47:11 +00:00
|
|
|
expect(err).to.be.an.instanceOf(Error);
|
|
|
|
expect(ds.connector._tables).to.not.have.property('m1');
|
2020-02-22 14:05:17 +00:00
|
|
|
expect(ds.connector._tables).to.not.have.property('m3');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('automigrate tables in series', function(done) {
|
|
|
|
ds.automigrate(['m1', 'm2'], function(err) {
|
|
|
|
expect(Object.keys(ds.connector._tables)).to.deep.equal(['m1', 'm2']);
|
2014-10-13 16:47:11 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|