Updated Branches: refs/heads/trunk 861f3f39c -> b7e9dd9fb
AMBARI-4486. Few utils unit tests. (onechiporenko) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/b7e9dd9f Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/b7e9dd9f Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/b7e9dd9f Branch: refs/heads/trunk Commit: b7e9dd9fb18cf7176c00b4d31244b189ea157f03 Parents: 861f3f3 Author: Oleg Nechiporenko <[email protected]> Authored: Fri Jan 31 18:00:32 2014 +0200 Committer: Oleg Nechiporenko <[email protected]> Committed: Fri Jan 31 18:15:47 2014 +0200 ---------------------------------------------------------------------- ambari-web/app/assets/test/tests.js | 1 + ambari-web/app/utils/date.js | 18 ++- ambari-web/app/utils/misc.js | 2 +- ambari-web/test/utils/date_test.js | 24 +++- ambari-web/test/utils/misc_test.js | 36 ++++- ambari-web/test/utils/number_utils_test.js | 172 ++++++++++++++++++++++++ 6 files changed, 232 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/b7e9dd9f/ambari-web/app/assets/test/tests.js ---------------------------------------------------------------------- diff --git a/ambari-web/app/assets/test/tests.js b/ambari-web/app/assets/test/tests.js index 85181fc..1550d0b 100644 --- a/ambari-web/app/assets/test/tests.js +++ b/ambari-web/app/assets/test/tests.js @@ -71,6 +71,7 @@ require('test/utils/config_test'); require('test/utils/date_test'); require('test/utils/form_field_test'); require('test/utils/misc_test'); +require('test/utils/number_utils_test'); require('test/utils/validator_test'); require('test/utils/config_test'); require('test/utils/string_utils_test'); http://git-wip-us.apache.org/repos/asf/ambari/blob/b7e9dd9f/ambari-web/app/utils/date.js ---------------------------------------------------------------------- diff --git a/ambari-web/app/utils/date.js b/ambari-web/app/utils/date.js index 8612e19..8d32a47 100644 --- a/ambari-web/app/utils/date.js +++ b/ambari-web/app/utils/date.js @@ -32,7 +32,7 @@ module.exports = { */ dateFormat:function (timestamp) { if (!validator.isValidInt(timestamp)) return timestamp; - var date = new Date(timestamp * 1); + var date = new Date(timestamp); var months = this.dateMonths; var days = this.dateDays; return days[date.getDay()] + ', ' + months[date.getMonth()] + ' ' + this.dateFormatZeroFirst(date.getDate()) + ', ' + date.getFullYear() + ' ' + this.dateFormatZeroFirst(date.getHours()) + ':' + this.dateFormatZeroFirst(date.getMinutes()); @@ -45,7 +45,7 @@ module.exports = { dateFormatShort: function(timestamp) { if (!validator.isValidInt(timestamp)) return timestamp; - var date = new Date(timestamp*1); + var date = new Date(timestamp); var today = new Date(); if (date.toDateString() === today.toDateString()) { return 'Today ' + date.toLocaleTimeString(); @@ -59,7 +59,7 @@ module.exports = { */ startTime: function (startTimestamp) { if (!validator.isValidInt(startTimestamp)) return ''; - var startDate = new Date(startTimestamp * 1); + var startDate = new Date(startTimestamp); var months = this.dateMonths; var days = this.dateDays; // generate start time @@ -67,7 +67,7 @@ module.exports = { return 'Not started'; } var startTimeSummary = ''; - if (new Date(startTimestamp * 1).setHours(0, 0, 0, 0) == new Date().setHours(0, 0, 0, 0) ) { //today + if (new Date(startTimestamp).setHours(0, 0, 0, 0) == new Date().setHours(0, 0, 0, 0) ) { //today startTimeSummary = 'Today ' + this.dateFormatZeroFirst(startDate.getHours()) + ':' + this.dateFormatZeroFirst(startDate.getMinutes()); } else { startTimeSummary = days[startDate.getDay()] + ' ' + months[startDate.getMonth()] + ' ' + this.dateFormatZeroFirst(startDate.getDate()) + ' ' + startDate.getFullYear() + ' ' @@ -86,8 +86,8 @@ module.exports = { durationSummary: function (startTimestamp, endTimestamp) { // generate duration var durationSummary = ''; - var startDate = new Date(startTimestamp * 1); - var endDate = new Date(endTimestamp * 1); + var startDate = new Date(startTimestamp); + var endDate = new Date(endTimestamp); if (startDate.getFullYear() == 1969 || startTimestamp < 1) { return ''; } @@ -146,10 +146,8 @@ module.exports = { * is not given, duration will be 0. If end time is not given, duration will * be till now. * - * @param {Number} - * startTime Start time from epoch - * @param {Number} - * endTime End time from epoch + * @param {Number} startTime Start time from epoch + * @param {Number} endTime End time from epoch * @return {Number} duration */ duration : function(startTime, endTime) { http://git-wip-us.apache.org/repos/asf/ambari/blob/b7e9dd9f/ambari-web/app/utils/misc.js ---------------------------------------------------------------------- diff --git a/ambari-web/app/utils/misc.js b/ambari-web/app/utils/misc.js index 994397d..2ed7504 100644 --- a/ambari-web/app/utils/misc.js +++ b/ambari-web/app/utils/misc.js @@ -58,7 +58,7 @@ module.exports = { var sorted = []; for (var i = 0; i < sortOrder.length; i++) for (var j = 0; j < array.length; j++) { - if (sortOrder[i] == ('get' in array[j] ? array[j].get('id') : array[j].id)) { + if (sortOrder[i] == Em.get(array[j], 'id')) { sorted.push(array[j]); } } http://git-wip-us.apache.org/repos/asf/ambari/blob/b7e9dd9f/ambari-web/test/utils/date_test.js ---------------------------------------------------------------------- diff --git a/ambari-web/test/utils/date_test.js b/ambari-web/test/utils/date_test.js index c015021..d9b7d61 100644 --- a/ambari-web/test/utils/date_test.js +++ b/ambari-web/test/utils/date_test.js @@ -24,13 +24,13 @@ var date = require('utils/date'); describe('date', function () { - var correct_tests = [ + var correct_tests = Em.A([ {t: 1349752195000, e: 'Tue, Oct 09, 2012 06:09', e2: 'Tue Oct 09 2012'}, {t: 1367752195000, e: 'Sun, May 05, 2013 14:09', e2: 'Sun May 05 2013'}, {t: 1369952195000, e: 'Fri, May 31, 2013 01:16', e2: 'Fri May 31 2013'} - ]; + ]); - var incorrect_tests = [ + var incorrect_tests = Em.A([ {t: null}, {t: ''}, {t: false}, @@ -38,7 +38,7 @@ describe('date', function () { {t: {}}, {t: undefined}, {t: function(){}} - ]; + ]); describe('#dateFormat', function() { it('Correct timestamps', function(){ @@ -72,7 +72,7 @@ describe('date', function () { }); describe('#timingFormat', function() { - var tests = [ + var tests = Em.A([ {i: '30', e:'30 ms'}, {i: '300', e:'300 ms'}, {i: '999', e:'999 ms'}, @@ -87,7 +87,7 @@ describe('date', function () { {i: '350000000', e:'4.05 days'}, {i: '3500000000', e:'40.51 days'}, {i: '35000000000', e:'405.09 days'} - ]; + ]); it('Correct data', function(){ tests.forEach(function(test) { @@ -103,4 +103,16 @@ describe('date', function () { }); + describe('#duration', function() { + var tests = Em.A([ + {startTime: 1, endTime: 2, e: 1}, + {startTime: 0, endTime: 2000, e: 0} + ]); + tests.forEach(function(test) { + it(test.startTime + ' ' + test.endTime, function() { + expect(date.duration(test.startTime, test.endTime)).to.equal(test.e); + }); + }); + }); + }); \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/b7e9dd9f/ambari-web/test/utils/misc_test.js ---------------------------------------------------------------------- diff --git a/ambari-web/test/utils/misc_test.js b/ambari-web/test/utils/misc_test.js index d2a2faf..fd5696d 100644 --- a/ambari-web/test/utils/misc_test.js +++ b/ambari-web/test/utils/misc_test.js @@ -21,7 +21,7 @@ var misc = require('utils/misc'); describe('misc', function () { describe('#formatBandwidth', function () { - var tests = [ + var tests = Em.A([ {m:'undefined to undefined',i:undefined,e:undefined}, {m:'0 to <1KB',i:'0',e:'<1KB'}, {m:'1000 to <1KB',i:'1000',e:'<1KB'}, @@ -30,7 +30,7 @@ describe('misc', function () { {m:'1048576 to 1.0MB',i:'1048576',e:'1.0MB'}, {m:'1782579 to 1.7MB',i:'1782579',e:'1.7MB'}, {m:'1546188226 to 1.44GB',i:'1546188226',e:'1.44GB'} - ]; + ]); tests.forEach(function(test) { it(test.m + ' ', function () { expect(misc.formatBandwidth(test.i)).to.equal(test.e); @@ -42,13 +42,13 @@ describe('misc', function () { }); describe('#ipToInt', function () { - var tests = [ + var tests = Em.A([ {m:'0.0.0.0 to 0',i:'0.0.0.0',e:0}, {m:'255.255.255.255 to 4294967295',i:'255.255.255.255',e:4294967295}, {m:'"" to false',i:'',e:false}, {m:'255.255.255.256 to false',i:'255.255.255.256',e:false}, {m:'255.255.255 to false',i:'255.255.255',e:false} - ]; + ]); tests.forEach(function(test) { it(test.m + ' ', function () { expect(misc.ipToInt(test.i)).to.equal(test.e); @@ -56,4 +56,32 @@ describe('misc', function () { }); }); + describe('#sortByOrder', function() { + var tests = Em.A([ + { + sortOrder: ['b', 'c', 'a'], + array: [{id:'a'}, {id:'b'}, Em.Object.create({id:'c'})], + e: [{id:'b'}, Em.Object.create({id:'c'}), {id:'a'}], + m: 'Array with Ember and native objects' + }, + { + sortOrder: ['b', 'c', 'a'], + array: [{id:'a'}, {id:'b'}, {id:'c'}], + e: [{id:'b'}, {id:'c'}, {id:'a'}], + m: 'Array with native objects' + }, + { + sortOrder: ['b', 'c', 'a'], + array: [Em.Object.create({id:'a'}), Em.Object.create({id:'b'}), Em.Object.create({id:'c'})], + e: [Em.Object.create({id:'b'}), Em.Object.create({id:'c'}), Em.Object.create({id:'a'})], + m: 'Array with Ember objects' + } + ]); + tests.forEach(function(test) { + it(test.m, function() { + expect(misc.sortByOrder(test.sortOrder, test.array)).to.eql(test.e); + }); + }); + }); + }); http://git-wip-us.apache.org/repos/asf/ambari/blob/b7e9dd9f/ambari-web/test/utils/number_utils_test.js ---------------------------------------------------------------------- diff --git a/ambari-web/test/utils/number_utils_test.js b/ambari-web/test/utils/number_utils_test.js new file mode 100644 index 0000000..42fcb1f --- /dev/null +++ b/ambari-web/test/utils/number_utils_test.js @@ -0,0 +1,172 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +var numberUtils = require('utils/number_utils'); + +describe('', function() { + + describe('#bytesToSize', function() { + + describe('check bytes', function() { + var tests = Em.A([ + { + bytes: null, + precision: null, + parseType: null, + multiplyBy: null, + e: 'n/a', + m: '"n/a" if bytes is null' + }, + { + bytes: undefined, + precision: null, + parseType: null, + multiplyBy: null, + e: 'n/a', + m: '"n/a" if bytes is undefined' + } + ]); + + tests.forEach(function(test) { + it(test.m, function() { + expect(numberUtils.bytesToSize(test.bytes, test.precision, test.parseType, test.multiplyBy)).to.equal(test.e); + }); + }); + }); + + describe('check sizes', function() { + var tests = Em.A([ + { + bytes: 12, + precision: null, + parseType: 'parseInt', + multiplyBy: 1, + e: 'Bytes', + m: 'Bytes' + }, + { + bytes: 1024 + 12, + precision: null, + parseType: 'parseInt', + multiplyBy: 1, + e: 'KB', + m: 'KB' + }, + { + bytes: 1024 * 1024 + 12, + precision: null, + parseType: 'parseInt', + multiplyBy: 1, + e: 'MB', + m: 'MB' + }, + { + bytes: 1024 * 1024 * 1024 + 12, + precision: null, + parseType: 'parseInt', + multiplyBy: 1, + e: 'GB', + m: 'GB' + }, + { + bytes: 1024 * 1024 * 1024 * 1024 + 12, + precision: null, + parseType: 'parseInt', + multiplyBy: 1, + e: 'TB', + m: 'TB' + }, + { + bytes: 1024 * 1024 * 1024 * 1024 * 1024 + 12, + precision: null, + parseType: 'parseInt', + multiplyBy: 1, + e: 'PB', + m: 'PB' + } + ]); + + tests.forEach(function(test) { + it(test.m, function() { + expect(numberUtils.bytesToSize(test.bytes, test.precision, test.parseType, test.multiplyBy).endsWith(test.e)).to.equal(true); + }); + }); + }); + + describe('check calculated result', function() { + var tests = Em.A([ + { + bytes: 42, + precision: null, + parseType: 'parseInt', + multiplyBy: 1, + e: '42', + m: 'Bytes' + }, + { + bytes: 1024 * 12, + precision: null, + parseType: 'parseInt', + multiplyBy: 1, + e: '12', + m: 'KB' + }, + { + bytes: 1024 * 1024 * 23, + precision: null, + parseType: 'parseInt', + multiplyBy: 1, + e: '23', + m: 'MB' + }, + { + bytes: 1024 * 1024 * 1024 * 34, + precision: null, + parseType: 'parseInt', + multiplyBy: 1, + e: '34', + m: 'GB' + }, + { + bytes: 1024 * 1024 * 1024 * 1024 * 45, + precision: null, + parseType: 'parseInt', + multiplyBy: 1, + e: '45', + m: 'TB' + }, + { + bytes: 1024 * 1024 * 1024 * 1024 * 1024 * 56, + precision: null, + parseType: 'parseInt', + multiplyBy: 1, + e: '56', + m: 'PB' + } + ]); + + tests.forEach(function(test) { + it(test.m, function() { + expect(numberUtils.bytesToSize(test.bytes, test.precision, test.parseType, test.multiplyBy).startsWith(test.e)).to.equal(true); + }); + }); + }); + + }); + +}); \ No newline at end of file
