loopback-component-explorer/test/model-helper.test.js

102 lines
3.2 KiB
JavaScript
Raw Normal View History

'use strict';
var modelHelper = require('../lib/model-helper');
var expect = require('chai').expect;
describe('model-helper', function() {
describe('properly converts LDL definitions to swagger types', function() {
it('converts constructor types', function() {
var def = getDefinition({
str: String, // 'string'
num: Number, // {type: 'number', format: 'double'}
date: Date, // {type: 'string', format: 'date'}
bool: Boolean, // 'boolean'
buf: Buffer // {type: 'string', format: 'byte'}
});
var props = def.properties;
2014-07-11 13:55:19 +00:00
expect(props.str).to.eql({ type: 'string' });
expect(props.num).to.eql({ type: 'number', format: 'double' });
expect(props.date).eql({ type: 'string', format: 'date' });
expect(props.bool).to.eql({ type: 'boolean' });
expect(props.buf).to.eql({ type: 'string', format: 'byte' });
});
it('converts string types', function() {
var def = getDefinition({
str: 'string', // 'string'
num: 'number', // {type: 'number', format: 'double'}
date: 'date', // {type: 'string', format: 'date'}
bool: 'boolean', // 'boolean'
buf: 'buffer' // {type: 'string', format: 'byte'}
});
var props = def.properties;
2014-07-11 13:55:19 +00:00
expect(props.str).to.eql({ type: 'string' });
expect(props.num).to.eql({ type: 'number', format: 'double' });
expect(props.date).eql({ type: 'string', format: 'date' });
expect(props.bool).to.eql({ type: 'boolean' });
expect(props.buf).to.eql({ type: 'string', format: 'byte' });
});
describe('array definitions', function() {
// There are three types we want to checK:
// [String]
// ["string"],
// [{type: String, ...}]
it('converts [Constructor] type', function() {
var def = getDefinition({
array: [String]
});
var props = def.properties;
2014-07-11 13:55:19 +00:00
expect(props.array).to.eql({ type: 'array', items: {
type: 'string'
}});
});
it('converts ["string"] type', function() {
var def = getDefinition({
array: ['string']
});
var props = def.properties;
2014-07-11 13:55:19 +00:00
expect(props.array).to.eql({ type: 'array', items: {
type: 'string'
}});
});
it('converts [{type: "string", length: 64}] type', function() {
var def = getDefinition({
array: [{type: 'string', length: 64}]
});
var props = def.properties;
2014-07-11 13:55:19 +00:00
expect(props.array).to.eql({ type: 'array', items: {
type: 'string',
length: 64
}});
});
it('converts [{type: "date"}] type (with `format`)', function() {
var def = getDefinition({
array: [{type: 'date'}]
});
var props = def.properties;
2014-07-11 13:55:19 +00:00
expect(props.array).to.eql({ type: 'array', items: {
type: 'string', format: 'date'
}});
});
});
});
});
2014-07-11 13:55:19 +00:00
// Simulates the format of a remoting class.
function getDefinition(model) {
Object.keys(model).forEach(function(name) {
model[name] = {type: model[name]};
});
var aClass = {
ctor: {
definition: {
name: 'testModel',
properties: model
}
}
};
return modelHelper.generateModelDefinition(aClass).testModel;
}