UNPKG

4.13 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'use strict';
7const assert = require('assert');
8const ModelBuilder = require('..').ModelBuilder;
9const DataSource = require('../').DataSource;
10const introspectType = require('../lib/introspection')(ModelBuilder);
11const traverse = require('traverse');
12
13const json = {
14 name: 'Joe',
15 age: 30,
16 birthday: new Date(),
17 vip: true,
18 address: {
19 street: '1 Main St',
20 city: 'San Jose',
21 state: 'CA',
22 zipcode: '95131',
23 country: 'US',
24 },
25 friends: ['John', 'Mary'],
26 emails: [
27 {label: 'work', id: 'x@sample.com'},
28 {label: 'home', id: 'x@home.com'},
29 ],
30 nestedArray: [
31 [{msg: 'Hi'}],
32 ],
33 tags: [],
34};
35
36describe('Introspection of model definitions from JSON', function() {
37 it('should handle simple types', function() {
38 assert.equal(introspectType('123'), 'string');
39 assert.equal(introspectType(true), 'boolean');
40 assert.equal(introspectType(false), 'boolean');
41 assert.equal(introspectType(12), 'number');
42 assert.equal(introspectType(new Date()), 'date');
43 });
44
45 it('should handle array types', function() {
46 let type = introspectType(['123']);
47 assert.deepEqual(type, ['string'], 'type should be ["string"]');
48 type = introspectType([1]);
49 assert.deepEqual(type, ['number'], 'type should be ["number"]');
50 // Stop at first known type
51 type = introspectType([1, '123']);
52 assert.deepEqual(type, ['number'], 'type should be ["number"]');
53 type = introspectType([null, '123']);
54 assert.deepEqual(type, ['string'], 'type should be ["string"]');
55
56 type = introspectType([]);
57 assert.equal(type, 'array');
58 });
59
60 it('should return Any for null or undefined', function() {
61 assert.equal(introspectType(null), ModelBuilder.Any);
62 assert.equal(introspectType(undefined), ModelBuilder.Any);
63 });
64
65 it('should return a schema for object', function() {
66 const json = {a: 'str', b: 0, c: true};
67 const type = introspectType(json);
68 assert.equal(type.a, 'string');
69 assert.equal(type.b, 'number');
70 assert.equal(type.c, 'boolean');
71 });
72
73 it('should handle nesting objects', function() {
74 const json = {a: 'str', b: 0, c: true, d: {x: 10, y: 5}};
75 const type = introspectType(json);
76 assert.equal(type.a, 'string');
77 assert.equal(type.b, 'number');
78 assert.equal(type.c, 'boolean');
79 assert.equal(type.d.x, 'number');
80 assert.equal(type.d.y, 'number');
81 });
82
83 it('should handle nesting arrays', function() {
84 const json = {a: 'str', b: 0, c: true, d: [1, 2]};
85 const type = introspectType(json);
86 assert.equal(type.a, 'string');
87 assert.equal(type.b, 'number');
88 assert.equal(type.c, 'boolean');
89 assert.deepEqual(type.d, ['number']);
90 });
91
92 it('should build a model from the introspected schema', function(done) {
93 const copy = traverse(json).clone();
94
95 const schema = introspectType(json);
96
97 const builder = new ModelBuilder();
98 const Model = builder.define('MyModel', schema, {idInjection: false});
99
100 // FIXME: [rfeng] The constructor mutates the arguments
101 let obj = new Model(json);
102
103 obj = obj.toObject();
104
105 assert.deepEqual(obj, copy);
106 done();
107 });
108
109 it('should build a model using buildModelFromInstance', function(done) {
110 const copy = traverse(json).clone();
111
112 const builder = new ModelBuilder();
113 const Model = builder.buildModelFromInstance('MyModel', copy, {idInjection: false});
114
115 let obj = new Model(json);
116 obj = obj.toObject();
117 assert.deepEqual(obj, copy);
118 done();
119 });
120
121 it('should build a model using DataSource.buildModelFromInstance', function(done) {
122 const copy = traverse(json).clone();
123
124 const builder = new DataSource('memory');
125 const Model = builder.buildModelFromInstance('MyModel', copy,
126 {idInjection: false});
127
128 assert.equal(Model.dataSource, builder);
129
130 let obj = new Model(json);
131 obj = obj.toObject();
132 assert.deepEqual(obj, copy);
133 done();
134 });
135});
136