UNPKG

node-fit-model

Version:

Checks whether an object is fits into a model.

254 lines (194 loc) 8.45 kB
"use strict"; /** * Represents a test for "reqiured" part of the modelling * * */ var expect = require('chai').expect, options = { modelDir: __dirname + '/data', postFix: 'Model.js' }, fitModel = require('./../index'), modelling = new fitModel(options), data = require('./TestObject'); /** * Modify Parameter Data Type * @param obj * @param parameter * @param type */ var modifyParameterDataType = function(obj, parameter, type) { switch(type){ case "int": obj[parameter] = 1234; break; case "float": obj[parameter] = 123.456; break; case "string": obj[parameter] = 'hello'; break; case "object": obj[parameter] = { a: 'b', c: 'd'}; break; } }; describe('Data Type', function () { it('should not throw if a value is an integer or a string of integer', function () { var copy = JSON.parse(JSON.stringify(data)); expect(modelling.fitModel('User', copy)).to.equal(true); modifyParameterDataType(copy, 'profile_id', 'int'); expect(modelling.fitModel('User', copy)).to.equal(true); }); it('should check if the parameter data type is an integer and the value is an integer or a string of integer', function () { var copy = JSON.parse(JSON.stringify(data)); // cannot contain non-numeric value copy.profile_id = '123x3'; expect(function(){ modelling.fitModel('User', copy); }).to.throw("Value of profile_id must be integer. Actual => 123x3"); // cannot be float when integer copy.profile_id = '123.4'; expect(function(){ modelling.fitModel('User', copy); }).to.throw("Value of profile_id must be integer. Actual => 123.4"); // cannot be array copy.profile_id = []; expect(function(){ modelling.fitModel('User', copy); }).to.throw("Value of profile_id must be integer. Actual => "); // cannot be object copy.profile_id = {}; expect(function(){ modelling.fitModel('User', copy); }).to.throw("Value of profile_id must be integer. Actual => [object Object]"); }); it('should check if the parameter data type is a float and the value is also a float', function(){ var copy = JSON.parse(JSON.stringify(data)); copy.latitude = 34.5252; expect(modelling.fitModel('User', copy)).to.equal(true); copy.latitude = '34.5252'; expect(modelling.fitModel('User', copy)).to.equal(true); copy.latitude = 'Taku'; expect(function(){ modelling.fitModel('User', copy); }).to.throw("Value of latitude must be float. Actual => Taku"); copy.latitude = '1234'; expect(modelling.fitModel('User', copy)).to.equal(true); copy.latitude = []; expect(function(){ modelling.fitModel('User', copy); }).to.throw("Value of latitude must be float. Actual => "); copy.latitude = {}; expect(function(){ modelling.fitModel('User', copy); }).to.throw("Value of latitude must be float. Actual => [object Object]"); }); it('should be allow integer to be passed as float', function(){ var copy = JSON.parse(JSON.stringify(data)); copy.latitude = 34; expect(modelling.fitModel('User', copy)).to.equal(true); }); it('should check if the parameter data type is a number and the value is also a number', function(){ /** * Number consists of all rational numbers, x ∈ Q * */ var copy = JSON.parse(JSON.stringify(data)); copy.weight = 185; expect(modelling.fitModel('User', copy)).to.equal(true); copy.weight = '185'; expect(modelling.fitModel('User', copy)).to.equal(true); copy.weight = '185.85'; expect(modelling.fitModel('User', copy)).to.equal(true); copy.weight = 185.85; expect(modelling.fitModel('User', copy)).to.equal(true); copy.weight = 'n/a'; expect(function(){ modelling.fitModel('User', copy); }).to.throw("Value of weight must be number. Actual => n/a"); }); it('should check if the parameter data type is a string and the value is also a string', function(){ var copy = JSON.parse(JSON.stringify(data)); copy.name = 1234; expect(function(){ modelling.fitModel('User', copy); }).to.throw("Value of name must be string. Actual => 1234"); copy.name = 'Taku'; expect(modelling.fitModel('User', copy)).to.equal(true); }); it('should check if the parameter data type is an enum and the value is within an enum options', function(){ var copy = JSON.parse(JSON.stringify(data)); // Invalid value copy.gender = 1234; expect(function(){ modelling.fitModel('User', copy); }).to.throw("Value of gender must be enum and have a value in [ Male, Female ]. Actual => 1234"); // Correct value copy.gender = 'Male'; expect(modelling.fitModel('User', copy)).to.equal(true); // Case sensitive because the docs doesn't usually say case insensitive values copy.gender = 'male'; expect(function(){ modelling.fitModel('User', copy); }).to.throw("Value of gender must be enum and have a value in [ Male, Female ]. Actual => male"); }); it('should check if the parameter data type is an timestamp and the value is also timestamp', function(){ /** * Timestamp is determined if the value can be converted into a Date object. * This includes but not limited to UNIX timestamp and stringified timestamp. */ var copy = JSON.parse(JSON.stringify(data)); // Technically, this is still a valid UNIX timestamp. // So it should be fine copy.create_time = 1234; expect(modelling.fitModel('User', copy)).to.equal(true); // Correct value copy.create_time = 'Male'; expect(function(){ modelling.fitModel('User', copy); }).to.throw("Value of create_time must be timestamp. Actual => Male"); // Stringified timestamp copy.create_time = 'Jan 1, 2015'; expect(modelling.fitModel('User', copy)).to.equal(true); // Also a stringified timestamp copy.create_time = 'Thu Oct 29 2015 08:46:30 GMT+0100'; expect(modelling.fitModel('User', copy)).to.equal(true); // Also a stringified timestamp copy.create_time = '1995-12-17T03:24:00'; expect(modelling.fitModel('User', copy)).to.equal(true); }); it('should check if the parameter data type is an object and the value is also a valid object', function(){ /** * Timestamp is determined if the value can be converted into a Date object. * This includes but not limited to UNIX timestamp and stringified timestamp. */ var copy = JSON.parse(JSON.stringify(data)); // Technically, this is still a valid UNIX timestamp. // So it should be fine copy.review.average = 123.4; expect(modelling.fitModel('User', copy)).to.equal(true); // This should still work within an object parameter // So it should be fine copy.review.average = 123; expect(modelling.fitModel('User', copy)).to.equal(true); }); it('should check if the parameter data type is an array and the value is also a valid array', function(){ /** * Timestamp is determined if the value can be converted into a Date object. * This includes but not limited to UNIX timestamp and stringified timestamp. */ var copy = JSON.parse(JSON.stringify(data)); // Technically, this is still a valid UNIX timestamp. // So it should be fine copy.activities[0].activity_id = 123; expect(modelling.fitModel('User', copy)).to.equal(true); // This should still work within an object parameter // So it should be fine copy.activities[0].activity_id = 'some-string'; expect(function() { modelling.fitModel('User', copy); }).to.throw("Value of activity_id must be int. Actual => some-string"); }); });