2016-04-01 22:25:16 +00:00
|
|
|
// 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
|
|
|
|
|
2016-08-22 19:55:22 +00:00
|
|
|
'use strict';
|
2013-07-26 20:06:43 +00:00
|
|
|
var assert = require('assert');
|
2014-09-12 21:25:35 +00:00
|
|
|
var ModelBuilder = require('..').ModelBuilder;
|
|
|
|
var DataSource = require('../').DataSource;
|
2013-08-07 21:51:32 +00:00
|
|
|
var introspectType = require('../lib/introspection')(ModelBuilder);
|
2013-07-26 20:06:43 +00:00
|
|
|
var traverse = require('traverse');
|
|
|
|
|
2014-09-12 21:25:35 +00:00
|
|
|
var json = {
|
|
|
|
name: 'Joe',
|
|
|
|
age: 30,
|
|
|
|
birthday: new Date(),
|
|
|
|
vip: true,
|
|
|
|
address: {
|
|
|
|
street: '1 Main St',
|
|
|
|
city: 'San Jose',
|
|
|
|
state: 'CA',
|
|
|
|
zipcode: '95131',
|
2016-04-01 11:48:17 +00:00
|
|
|
country: 'US',
|
2014-09-12 21:25:35 +00:00
|
|
|
},
|
|
|
|
friends: ['John', 'Mary'],
|
|
|
|
emails: [
|
2016-08-19 17:46:59 +00:00
|
|
|
{label: 'work', id: 'x@sample.com'},
|
|
|
|
{label: 'home', id: 'x@home.com'},
|
2014-09-12 21:25:35 +00:00
|
|
|
],
|
2016-04-01 11:48:17 +00:00
|
|
|
tags: [],
|
2014-09-12 21:25:35 +00:00
|
|
|
};
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('Introspection of model definitions from JSON', function() {
|
|
|
|
it('should handle simple types', function() {
|
2014-01-24 17:09:53 +00:00
|
|
|
assert.equal(introspectType('123'), 'string');
|
|
|
|
assert.equal(introspectType(true), 'boolean');
|
|
|
|
assert.equal(introspectType(false), 'boolean');
|
|
|
|
assert.equal(introspectType(12), 'number');
|
|
|
|
assert.equal(introspectType(new Date()), 'date');
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should handle array types', function() {
|
2014-01-24 17:09:53 +00:00
|
|
|
var type = introspectType(['123']);
|
|
|
|
assert.deepEqual(type, ['string'], 'type should be ["string"]');
|
|
|
|
type = introspectType([1]);
|
|
|
|
assert.deepEqual(type, ['number'], 'type should be ["number"]');
|
|
|
|
// Stop at first known type
|
|
|
|
type = introspectType([1, '123']);
|
|
|
|
assert.deepEqual(type, ['number'], 'type should be ["number"]');
|
|
|
|
type = introspectType([null, '123']);
|
|
|
|
assert.deepEqual(type, ['string'], 'type should be ["string"]');
|
|
|
|
|
|
|
|
type = introspectType([]);
|
|
|
|
assert.equal(type, 'array');
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should return Any for null or undefined', function() {
|
2014-01-24 17:09:53 +00:00
|
|
|
assert.equal(introspectType(null), ModelBuilder.Any);
|
|
|
|
assert.equal(introspectType(undefined), ModelBuilder.Any);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should return a schema for object', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
var json = {a: 'str', b: 0, c: true};
|
2014-01-24 17:09:53 +00:00
|
|
|
var type = introspectType(json);
|
|
|
|
assert.equal(type.a, 'string');
|
|
|
|
assert.equal(type.b, 'number');
|
|
|
|
assert.equal(type.c, 'boolean');
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should handle nesting objects', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
var json = {a: 'str', b: 0, c: true, d: {x: 10, y: 5}};
|
2014-01-24 17:09:53 +00:00
|
|
|
var type = introspectType(json);
|
|
|
|
assert.equal(type.a, 'string');
|
|
|
|
assert.equal(type.b, 'number');
|
|
|
|
assert.equal(type.c, 'boolean');
|
|
|
|
assert.equal(type.d.x, 'number');
|
|
|
|
assert.equal(type.d.y, 'number');
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should handle nesting arrays', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
var json = {a: 'str', b: 0, c: true, d: [1, 2]};
|
2014-01-24 17:09:53 +00:00
|
|
|
var type = introspectType(json);
|
|
|
|
assert.equal(type.a, 'string');
|
|
|
|
assert.equal(type.b, 'number');
|
|
|
|
assert.equal(type.c, 'boolean');
|
|
|
|
assert.deepEqual(type.d, ['number']);
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should build a model from the introspected schema', function(done) {
|
2014-01-24 17:09:53 +00:00
|
|
|
var copy = traverse(json).clone();
|
|
|
|
|
|
|
|
var schema = introspectType(json);
|
|
|
|
|
|
|
|
var builder = new ModelBuilder();
|
2016-08-19 17:46:59 +00:00
|
|
|
var Model = builder.define('MyModel', schema, {idInjection: false});
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
// FIXME: [rfeng] The constructor mutates the arguments
|
|
|
|
var obj = new Model(json);
|
|
|
|
|
|
|
|
obj = obj.toObject();
|
|
|
|
|
|
|
|
assert.deepEqual(obj, copy);
|
|
|
|
done();
|
|
|
|
});
|
2014-09-12 21:25:35 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should build a model using buildModelFromInstance', function(done) {
|
2014-09-12 21:25:35 +00:00
|
|
|
var copy = traverse(json).clone();
|
|
|
|
|
|
|
|
var builder = new ModelBuilder();
|
2016-08-19 17:46:59 +00:00
|
|
|
var Model = builder.buildModelFromInstance('MyModel', copy, {idInjection: false});
|
2014-09-12 21:25:35 +00:00
|
|
|
|
|
|
|
var obj = new Model(json);
|
|
|
|
obj = obj.toObject();
|
|
|
|
assert.deepEqual(obj, copy);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should build a model using DataSource.buildModelFromInstance', function(done) {
|
2014-09-12 21:25:35 +00:00
|
|
|
var copy = traverse(json).clone();
|
|
|
|
|
|
|
|
var builder = new DataSource('memory');
|
|
|
|
var Model = builder.buildModelFromInstance('MyModel', copy,
|
2016-08-19 17:46:59 +00:00
|
|
|
{idInjection: false});
|
2014-09-12 21:25:35 +00:00
|
|
|
|
|
|
|
assert.equal(Model.dataSource, builder);
|
|
|
|
|
|
|
|
var obj = new Model(json);
|
|
|
|
obj = obj.toObject();
|
|
|
|
assert.deepEqual(obj, copy);
|
|
|
|
done();
|
|
|
|
});
|
2013-07-26 20:06:43 +00:00
|
|
|
});
|
|
|
|
|