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

710 lines
19 KiB
JavaScript
Raw Normal View History

// Copyright IBM Corp. 2015,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
2016-08-22 19:55:22 +00:00
'use strict';
2018-12-07 14:54:29 +00:00
const jdb = require('../');
const DataSource = jdb.DataSource;
const should = require('./init.js');
describe('Memory connector with mocked discovery', function() {
2018-12-07 14:54:29 +00:00
let ds;
before(function() {
2016-08-19 17:46:59 +00:00
ds = new DataSource({connector: 'memory'});
2018-12-07 14:54:29 +00:00
const models = [{type: 'table', name: 'CUSTOMER', owner: 'STRONGLOOP'},
2016-08-19 17:46:59 +00:00
{type: 'table', name: 'INVENTORY', owner: 'STRONGLOOP'},
{type: 'table', name: 'LOCATION', owner: 'STRONGLOOP'}];
ds.discoverModelDefinitions = function(options, cb) {
process.nextTick(function() {
cb(null, models);
});
};
2018-12-07 14:54:29 +00:00
const modelProperties = [{
owner: 'STRONGLOOP',
tableName: 'INVENTORY',
columnName: 'PRODUCT_ID',
dataType: 'varchar',
dataLength: 20,
dataPrecision: null,
dataScale: null,
2016-04-01 11:48:17 +00:00
nullable: 0,
generated: true,
},
{
owner: 'STRONGLOOP',
tableName: 'INVENTORY',
columnName: 'LOCATION_ID',
dataType: 'varchar',
dataLength: 20,
dataPrecision: null,
dataScale: null,
nullable: 0,
generated: false,
},
{
owner: 'STRONGLOOP',
tableName: 'INVENTORY',
columnName: 'AVAILABLE',
dataType: 'int',
dataLength: null,
dataPrecision: 10,
dataScale: 0,
nullable: 1,
generated: false,
},
{
owner: 'STRONGLOOP',
tableName: 'INVENTORY',
columnName: 'TOTAL',
dataType: 'int',
dataLength: null,
dataPrecision: 10,
dataScale: 0,
nullable: 1,
generated: false,
}];
ds.discoverModelProperties = function(modelName, options, cb) {
process.nextTick(function() {
cb(null, modelProperties);
});
};
});
it('should convert table names to pascal cases and column names to camel case', function(done) {
ds.discoverSchemas('INVENTORY', {}, function(err, schemas) {
if (err) return done(err);
schemas.should.have.property('STRONGLOOP.INVENTORY');
2018-12-07 14:54:29 +00:00
const s = schemas['STRONGLOOP.INVENTORY'];
s.name.should.be.eql('Inventory');
Object.keys(s.properties).should.be.eql(
['productId', 'locationId', 'available', 'total'],
);
done();
});
});
it('should have jsonSchema: {nullable: true} in property for `available`', function(done) {
ds.discoverSchemas('INVENTORY', {}, function(err, schemas) {
if (err) return done(err);
schemas.should.have.property('STRONGLOOP.INVENTORY');
const s = schemas['STRONGLOOP.INVENTORY'];
s.name.should.be.eql('Inventory');
s.properties.available.should.have.property('jsonSchema');
s.properties.available.jsonSchema.should.have.property('nullable');
s.properties.available.jsonSchema.nullable.should.be.eql(true);
done();
});
});
it('should keep the column names the same as database', function(done) {
ds.discoverSchemas('INVENTORY', {disableCamelCase: true}, function(err, schemas) {
if (err) return done(err);
schemas.should.have.property('STRONGLOOP.INVENTORY');
const s = schemas['STRONGLOOP.INVENTORY'];
s.name.should.be.eql('Inventory');
Object.keys(s.properties).should.be.eql(
['PRODUCT_ID', 'LOCATION_ID', 'AVAILABLE', 'TOTAL'],
);
done();
});
});
it('should convert table/column names with custom mapper', function(done) {
ds.discoverSchemas('INVENTORY', {
nameMapper: function(type, name) {
// Convert all names to lower case
return name.toLowerCase();
2016-04-01 11:48:17 +00:00
},
}, function(err, schemas) {
if (err) return done(err);
schemas.should.have.property('STRONGLOOP.INVENTORY');
2018-12-07 14:54:29 +00:00
const s = schemas['STRONGLOOP.INVENTORY'];
s.name.should.be.eql('inventory');
Object.keys(s.properties).should.be.eql(
['product_id', 'location_id', 'available', 'total'],
);
done();
});
});
it('should not convert table/column names with null custom mapper',
function(done) {
2016-08-19 17:46:59 +00:00
ds.discoverSchemas('INVENTORY', {nameMapper: null}, function(err, schemas) {
if (err) return done(err);
schemas.should.have.property('STRONGLOOP.INVENTORY');
2018-12-07 14:54:29 +00:00
const s = schemas['STRONGLOOP.INVENTORY'];
s.name.should.be.eql('INVENTORY');
Object.keys(s.properties).should.be.eql(
['PRODUCT_ID', 'LOCATION_ID', 'AVAILABLE', 'TOTAL'],
);
done();
});
});
it('should honor connector\'s discoverSchemas implementation',
function(done) {
2018-12-07 14:54:29 +00:00
const models = {
inventory: {
2016-08-19 17:46:59 +00:00
product: {type: 'string'},
location: {type: 'string'},
2016-04-01 11:48:17 +00:00
},
};
ds.connector.discoverSchemas = function(modelName, options, cb) {
process.nextTick(function() {
cb(null, models);
});
};
2016-08-19 17:46:59 +00:00
ds.discoverSchemas('INVENTORY', {nameMapper: null}, function(err, schemas) {
if (err) return done(err);
schemas.should.be.eql(models);
done();
});
});
2015-06-25 08:38:15 +00:00
it('should callback function, passed as options parameter',
function(done) {
2018-12-07 14:54:29 +00:00
const models = {
2015-06-25 08:38:15 +00:00
inventory: {
2016-08-19 17:46:59 +00:00
product: {type: 'string'},
location: {type: 'string'},
2016-04-01 11:48:17 +00:00
},
2015-06-25 08:38:15 +00:00
};
ds.connector.discoverSchemas = function(modelName, options, cb) {
process.nextTick(function() {
cb(null, models);
});
};
2018-12-07 14:54:29 +00:00
const options = function(err, schemas) {
2015-06-25 08:38:15 +00:00
if (err) return done(err);
schemas.should.be.eql(models);
done();
};
ds.discoverSchemas('INVENTORY', options);
});
it('should discover schemas using `discoverSchemas` - promise variant',
function(done) {
ds.connector.discoverSchemas = null;
ds.discoverSchemas('INVENTORY', {})
2018-06-12 07:13:32 +00:00
.then(function(schemas) {
schemas.should.have.property('STRONGLOOP.INVENTORY');
2015-06-25 08:38:15 +00:00
2018-12-07 14:54:29 +00:00
const s = schemas['STRONGLOOP.INVENTORY'];
2018-06-12 07:13:32 +00:00
s.name.should.be.eql('Inventory');
2015-06-25 08:38:15 +00:00
2018-06-12 07:13:32 +00:00
Object.keys(s.properties).should.be.eql(
['productId', 'locationId', 'available', 'total'],
2018-06-12 07:13:32 +00:00
);
done();
})
.catch(function(err) {
done(err);
});
2016-04-01 11:48:17 +00:00
});
2015-06-25 08:38:15 +00:00
2016-04-01 11:48:17 +00:00
describe('discoverSchema', function() {
2018-12-07 14:54:29 +00:00
let models;
let schema;
2015-06-25 08:38:15 +00:00
before(function() {
schema = {
name: 'Inventory',
options: {
idInjection: false,
2016-08-19 17:46:59 +00:00
memory: {schema: 'STRONGLOOP', table: 'INVENTORY'},
2015-06-25 08:38:15 +00:00
},
properties: {
available: {
length: null,
jsonSchema: {
nullable: true,
},
2015-06-25 08:38:15 +00:00
memory: {
columnName: 'AVAILABLE',
dataLength: null,
dataPrecision: 10,
dataScale: 0,
dataType: 'int',
2016-04-01 11:48:17 +00:00
nullable: 1,
generated: false,
2015-06-25 08:38:15 +00:00
},
precision: 10,
required: false,
scale: 0,
2016-04-01 11:48:17 +00:00
type: undefined,
generated: false,
2015-06-25 08:38:15 +00:00
},
locationId: {
length: 20,
jsonSchema: {
nullable: false,
},
2015-06-25 08:38:15 +00:00
memory: {
columnName: 'LOCATION_ID',
dataLength: 20,
dataPrecision: null,
dataScale: null,
dataType: 'varchar',
2016-04-01 11:48:17 +00:00
nullable: 0,
generated: false,
2015-06-25 08:38:15 +00:00
},
precision: null,
required: true,
scale: null,
2016-04-01 11:48:17 +00:00
type: undefined,
generated: false,
2015-06-25 08:38:15 +00:00
},
productId: {
length: 20,
jsonSchema: {
nullable: false,
},
2015-06-25 08:38:15 +00:00
memory: {
columnName: 'PRODUCT_ID',
dataLength: 20,
dataPrecision: null,
dataScale: null,
dataType: 'varchar',
2016-04-01 11:48:17 +00:00
nullable: 0,
generated: true,
2015-06-25 08:38:15 +00:00
},
precision: null,
required: false,
2015-06-25 08:38:15 +00:00
scale: null,
2016-04-01 11:48:17 +00:00
type: undefined,
generated: true,
2015-06-25 08:38:15 +00:00
},
total: {
length: null,
jsonSchema: {
nullable: true,
},
2015-06-25 08:38:15 +00:00
memory: {
columnName: 'TOTAL',
dataLength: null,
dataPrecision: 10,
dataScale: 0,
dataType: 'int',
2016-04-01 11:48:17 +00:00
nullable: 1,
generated: false,
2015-06-25 08:38:15 +00:00
},
precision: 10,
required: false,
scale: 0,
2016-04-01 11:48:17 +00:00
type: undefined,
generated: false,
2016-04-01 11:48:17 +00:00
},
},
};
2015-06-25 08:38:15 +00:00
});
it('should discover schema using `discoverSchema`', function(done) {
ds.discoverSchema('INVENTORY', {}, function(err, schemas) {
if (err) return done(err);
schemas.should.be.eql(schema);
done();
});
});
it('should callback function, passed as options parameter', function(done) {
2018-12-07 14:54:29 +00:00
const options = function(err, schemas) {
2015-06-25 08:38:15 +00:00
if (err) return done(err);
schemas.should.be.eql(schema);
done();
};
ds.discoverSchema('INVENTORY', options);
});
it('should discover schema using `discoverSchema` - promise variant', function(done) {
ds.discoverSchema('INVENTORY', {})
.then(function(schemas) {
schemas.should.be.eql(schema);
done();
})
2016-04-01 11:48:17 +00:00
.catch(function(err) {
2015-06-25 08:38:15 +00:00
done(err);
});
});
});
});
2016-04-01 11:48:17 +00:00
describe('discoverModelDefinitions', function() {
2018-12-07 14:54:29 +00:00
let ds;
2016-04-01 11:48:17 +00:00
before(function() {
2016-08-19 17:46:59 +00:00
ds = new DataSource({connector: 'memory'});
2015-06-25 08:38:15 +00:00
2018-12-07 14:54:29 +00:00
const models = [{type: 'table', name: 'CUSTOMER', owner: 'STRONGLOOP'},
2016-08-19 17:46:59 +00:00
{type: 'table', name: 'INVENTORY', owner: 'STRONGLOOP'},
{type: 'table', name: 'LOCATION', owner: 'STRONGLOOP'}];
2015-06-25 08:38:15 +00:00
ds.connector.discoverModelDefinitions = function(options, cb) {
process.nextTick(function() {
cb(null, models);
});
};
});
it('should discover model using `discoverModelDefinitions`', function(done) {
ds.discoverModelDefinitions({}, function(err, schemas) {
if (err) return done(err);
2018-12-07 14:54:29 +00:00
const tableNames = schemas.map(function(s) {
2015-06-25 08:38:15 +00:00
return s.name;
});
tableNames.should.be.eql(
['CUSTOMER', 'INVENTORY', 'LOCATION'],
2015-06-25 08:38:15 +00:00
);
done();
});
});
it('should callback function, passed as options parameter', function(done) {
2018-12-07 14:54:29 +00:00
const options = function(err, schemas) {
2015-06-25 08:38:15 +00:00
if (err) return done(err);
2018-12-07 14:54:29 +00:00
const tableNames = schemas.map(function(s) {
2015-06-25 08:38:15 +00:00
return s.name;
});
tableNames.should.be.eql(
['CUSTOMER', 'INVENTORY', 'LOCATION'],
2015-06-25 08:38:15 +00:00
);
done();
};
ds.discoverModelDefinitions(options);
});
it('should discover model using `discoverModelDefinitions` - promise variant', function(done) {
ds.discoverModelDefinitions({})
.then(function(schemas) {
2018-12-07 14:54:29 +00:00
const tableNames = schemas.map(function(s) {
2015-06-25 08:38:15 +00:00
return s.name;
});
tableNames.should.be.eql(
['CUSTOMER', 'INVENTORY', 'LOCATION'],
2015-06-25 08:38:15 +00:00
);
done();
})
2018-06-12 07:13:32 +00:00
.catch(function(err) {
done(err);
});
2015-06-25 08:38:15 +00:00
});
});
2016-04-01 11:48:17 +00:00
describe('discoverModelProperties', function() {
2018-12-07 14:54:29 +00:00
let ds;
let modelProperties;
2016-04-01 11:48:17 +00:00
before(function() {
2016-08-19 17:46:59 +00:00
ds = new DataSource({connector: 'memory'});
2015-06-25 08:38:15 +00:00
modelProperties = [{
owner: 'STRONGLOOP',
tableName: 'INVENTORY',
columnName: 'PRODUCT_ID',
dataType: 'varchar',
dataLength: 20,
dataPrecision: null,
dataScale: null,
2016-04-01 11:48:17 +00:00
nullable: 0,
generated: false,
2015-06-25 08:38:15 +00:00
},
2016-04-01 11:48:17 +00:00
{
owner: 'STRONGLOOP',
tableName: 'INVENTORY',
columnName: 'LOCATION_ID',
dataType: 'varchar',
dataLength: 20,
dataPrecision: null,
dataScale: null,
nullable: 0,
generated: false,
2016-04-01 11:48:17 +00:00
},
{
owner: 'STRONGLOOP',
tableName: 'INVENTORY',
columnName: 'AVAILABLE',
dataType: 'int',
dataLength: null,
dataPrecision: 10,
dataScale: 0,
nullable: 1,
},
{
owner: 'STRONGLOOP',
tableName: 'INVENTORY',
columnName: 'TOTAL',
dataType: 'int',
dataLength: null,
dataPrecision: 10,
dataScale: 0,
nullable: 1,
}];
2015-06-25 08:38:15 +00:00
ds.connector.discoverModelProperties = function(modelName, options, cb) {
process.nextTick(function() {
cb(null, modelProperties);
});
};
});
it('should callback function, passed as options parameter', function(done) {
2018-12-07 14:54:29 +00:00
const options = function(err, schemas) {
2016-04-01 11:48:17 +00:00
if (err) return done(err);
2015-06-25 08:38:15 +00:00
2016-04-01 11:48:17 +00:00
schemas.should.be.eql(modelProperties);
done();
};
2015-06-25 08:38:15 +00:00
ds.discoverModelProperties('INVENTORY', options);
});
it('should discover model metadata using `discoverModelProperties`', function(done) {
ds.discoverModelProperties('INVENTORY', {}, function(err, schemas) {
if (err) return done(err);
schemas.should.be.eql(modelProperties);
done();
});
});
it('should discover model metadata using `discoverModelProperties` - promise variant', function(done) {
ds.discoverModelProperties('INVENTORY', {})
.then(function(schemas) {
schemas.should.be.eql(modelProperties);
done();
})
2018-06-12 07:13:32 +00:00
.catch(function(err) {
done(err);
});
2015-06-25 08:38:15 +00:00
});
});
2016-04-01 11:48:17 +00:00
describe('discoverPrimaryKeys', function() {
2018-12-07 14:54:29 +00:00
let ds;
let modelProperties, primaryKeys;
2016-04-01 11:48:17 +00:00
before(function() {
2016-08-19 17:46:59 +00:00
ds = new DataSource({connector: 'memory'});
2015-06-25 08:38:15 +00:00
primaryKeys = [
2016-04-01 11:48:17 +00:00
{
2015-06-25 08:38:15 +00:00
owner: 'STRONGLOOP',
tableName: 'INVENTORY',
columnName: 'PRODUCT_ID',
keySeq: 1,
2016-04-01 13:23:42 +00:00
pkName: 'ID_PK',
2016-04-01 11:48:17 +00:00
},
{
2015-06-25 08:38:15 +00:00
owner: 'STRONGLOOP',
tableName: 'INVENTORY',
columnName: 'LOCATION_ID',
keySeq: 2,
2016-04-01 13:23:42 +00:00
pkName: 'ID_PK',
2015-06-25 08:38:15 +00:00
}];
ds.connector.discoverPrimaryKeys = function(modelName, options, cb) {
process.nextTick(function() {
cb(null, primaryKeys);
});
};
});
it('should discover primary key definitions using `discoverPrimaryKeys`', function(done) {
ds.discoverPrimaryKeys('INVENTORY', {}, function(err, modelPrimaryKeys) {
if (err) return done(err);
modelPrimaryKeys.should.be.eql(primaryKeys);
done();
});
});
it('should callback function, passed as options parameter', function(done) {
2018-12-07 14:54:29 +00:00
const options = function(err, modelPrimaryKeys) {
2015-06-25 08:38:15 +00:00
if (err) return done(err);
modelPrimaryKeys.should.be.eql(primaryKeys);
done();
};
ds.discoverPrimaryKeys('INVENTORY', options);
});
it('should discover primary key definitions using `discoverPrimaryKeys` - promise variant', function(done) {
ds.discoverPrimaryKeys('INVENTORY', {})
.then(function(modelPrimaryKeys) {
modelPrimaryKeys.should.be.eql(primaryKeys);
done();
})
2016-04-01 11:48:17 +00:00
.catch(function(err) {
2015-06-25 08:38:15 +00:00
done(err);
});
});
});
2016-04-01 11:48:17 +00:00
describe('discoverForeignKeys', function() {
2018-12-07 14:54:29 +00:00
let ds;
let modelProperties, foreignKeys;
2016-04-01 11:48:17 +00:00
before(function() {
2016-08-19 17:46:59 +00:00
ds = new DataSource({connector: 'memory'});
2015-06-25 08:38:15 +00:00
2016-04-01 11:48:17 +00:00
foreignKeys = [{
2015-06-25 08:38:15 +00:00
fkOwner: 'STRONGLOOP',
fkName: 'PRODUCT_FK',
fkTableName: 'INVENTORY',
fkColumnName: 'PRODUCT_ID',
keySeq: 1,
pkOwner: 'STRONGLOOP',
pkName: 'PRODUCT_PK',
pkTableName: 'PRODUCT',
2016-04-01 11:48:17 +00:00
pkColumnName: 'ID',
2015-06-25 08:38:15 +00:00
}];
ds.connector.discoverForeignKeys = function(modelName, options, cb) {
process.nextTick(function() {
cb(null, foreignKeys);
});
};
});
it('should discover foreign key definitions using `discoverForeignKeys`', function(done) {
ds.discoverForeignKeys('INVENTORY', {}, function(err, modelForeignKeys) {
if (err) return done(err);
modelForeignKeys.should.be.eql(foreignKeys);
done();
});
});
it('should callback function, passed as options parameter', function(done) {
2018-12-07 14:54:29 +00:00
const options = function(err, modelForeignKeys) {
2015-06-25 08:38:15 +00:00
if (err) return done(err);
modelForeignKeys.should.be.eql(foreignKeys);
done();
};
ds.discoverForeignKeys('INVENTORY', options);
});
it('should discover foreign key definitions using `discoverForeignKeys` - promise variant', function(done) {
ds.discoverForeignKeys('INVENTORY', {})
.then(function(modelForeignKeys) {
modelForeignKeys.should.be.eql(foreignKeys);
done();
})
2016-04-01 11:48:17 +00:00
.catch(function(err) {
2015-06-25 08:38:15 +00:00
done(err);
});
});
});
2016-04-01 11:48:17 +00:00
describe('discoverExportedForeignKeys', function() {
2018-12-07 14:54:29 +00:00
let ds;
let modelProperties, exportedForeignKeys;
2016-04-01 11:48:17 +00:00
before(function() {
2016-08-19 17:46:59 +00:00
ds = new DataSource({connector: 'memory'});
2015-06-25 08:38:15 +00:00
exportedForeignKeys = [{
fkName: 'PRODUCT_FK',
fkOwner: 'STRONGLOOP',
fkTableName: 'INVENTORY',
fkColumnName: 'PRODUCT_ID',
keySeq: 1,
pkName: 'PRODUCT_PK',
pkOwner: 'STRONGLOOP',
pkTableName: 'PRODUCT',
2016-04-01 11:48:17 +00:00
pkColumnName: 'ID',
2015-06-25 08:38:15 +00:00
}];
ds.connector.discoverExportedForeignKeys = function(modelName, options, cb) {
process.nextTick(function() {
cb(null, exportedForeignKeys);
});
};
});
it('should discover foreign key definitions using `discoverExportedForeignKeys`', function(done) {
ds.discoverExportedForeignKeys('INVENTORY', {}, function(err, modelForeignKeys) {
if (err) return done(err);
modelForeignKeys.should.be.eql(exportedForeignKeys);
done();
});
});
it('should callback function, passed as options parameter', function(done) {
2018-12-07 14:54:29 +00:00
const options = function(err, modelForeignKeys) {
2015-06-25 08:38:15 +00:00
if (err) return done(err);
modelForeignKeys.should.be.eql(exportedForeignKeys);
done();
};
ds.discoverExportedForeignKeys('INVENTORY', options);
});
2016-04-01 13:23:42 +00:00
it('should discover foreign key definitions using `discoverExportedForeignKeys` - promise variant',
2018-06-12 07:13:32 +00:00
function(done) {
ds.discoverExportedForeignKeys('INVENTORY', {})
.then(function(modelForeignKeys) {
modelForeignKeys.should.be.eql(exportedForeignKeys);
done();
})
.catch(function(err) {
done(err);
});
});
});
describe('Mock connector', function() {
2018-12-07 14:54:29 +00:00
const mockConnectors = require('./mock-connectors');
describe('customFieldSettings', function() {
2018-12-07 14:54:29 +00:00
const ds = new DataSource(mockConnectors.customFieldSettings);
it('should be present in discoverSchemas', function(done) {
ds.discoverSchemas('person', function(err, schemas) {
should.not.exist(err);
schemas.should.be.an.Object;
schemas['public.person'].properties.name
.custom.storage.should.equal('quantum');
done();
});
});
});
});
describe('Default memory connector', function() {
2018-12-07 15:22:36 +00:00
const nonExistantError = 'Table \'NONEXISTENT\' does not exist.';
let ds;
before(function() {
2016-08-19 17:46:59 +00:00
ds = new DataSource({connector: 'memory'});
});
it('discoverSchema should return an error when table does not exist', function(done) {
ds.discoverSchema('NONEXISTENT', {}, function(err, schemas) {
should.exist(err);
err.message.should.eql(nonExistantError);
done();
});
});
it('discoverSchemas should return an error when table does not exist', function(done) {
ds.discoverSchemas('NONEXISTENT', {}, function(err, schemas) {
should.exist(err);
err.message.should.eql(nonExistantError);
done();
});
});
});