diff options
author | Richard Wall <richard@largo> | 2011-08-07 23:04:09 +0100 |
---|---|---|
committer | Richard Wall <richard@largo> | 2011-08-07 23:04:09 +0100 |
commit | 2943b57c109cb233e93100dfe35c693fb286854b (patch) | |
tree | 5b395bb7b428f7068744ba5e9e3e01aec66efda7 /jarmon/jarmon.test.js | |
parent | ebfaaf444348e08467242a4713b2181596f76ff8 (diff) | |
parent | 478965f7208a328ee81c76cc2482e278b263c68f (diff) |
Merge lp:~richardw/jarmon/jquery-deferred
* Replace use of MochiKit.Async.Deferred with jQuery.Deferred
* Turned cache prevention back on (jQuery.Ajax.cache: false)
* Upgraded to jQuery 1.6.2
* Fix various jshint warnings
* Fix various whitespace and >80 col formatting
Diffstat (limited to 'jarmon/jarmon.test.js')
-rw-r--r-- | jarmon/jarmon.test.js | 203 |
1 files changed, 111 insertions, 92 deletions
diff --git a/jarmon/jarmon.test.js b/jarmon/jarmon.test.js index 7b009d7..6370740 100644 --- a/jarmon/jarmon.test.js +++ b/jarmon/jarmon.test.js @@ -13,14 +13,15 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * When url cannot be found, the deferred should errback with status * 404. **/ + var self = this; var d = new jarmon.downloadBinary('non-existent-file.html'); - d.addBoth( - function(self, ret) { + d.always( + function(ret) { self.resume(function() { Y.Assert.isInstanceOf(Error, ret); - Y.Assert.areEqual(404, ret.message); + Y.Assert.areEqual('error:404', ret.message); }); - }, this); + }); this.wait(); }, @@ -30,14 +31,16 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * When url is found, the deferred should callback with an instance * of javascriptrrd.BinaryFile **/ + var self = this; var d = new jarmon.downloadBinary('testfile.bin'); - d.addBoth( - function(self, ret) { + d.always( + function(ret) { self.resume(function() { Y.Assert.isInstanceOf(jarmon.BinaryFile, ret); - Y.Assert.areEqual(String.fromCharCode(0), ret.getRawData()); + Y.Assert.areEqual( + String.fromCharCode(0), ret.getCharAt(0)); }); - }, this); + }); this.wait(); } @@ -56,12 +59,11 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { name: "jarmon.RRDFile", setUp: function() { - this.d = new jarmon.downloadBinary('build/test.rrd') - .addCallback( - function(self, binary) { + var self = this; + this.d = new jarmon.downloadBinary('build/test.rrd').pipe( + function(binary) { return new RRDFile(binary); - }, this) - .addErrback( + }, function(ret) { console.log(ret); }); @@ -72,13 +74,14 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * The generated rrd file should have a lastupdate date of * 1980-01-01 00:50:01 **/ - this.d.addCallback( - function(self, rrd) { + var self = this; + this.d.done( + function(rrd) { self.resume(function() { Y.Assert.areEqual( RRD_ENDTIME/1000+1, rrd.getLastUpdate()); }); - }, this); + }); this.wait(); }, @@ -88,8 +91,9 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * 'speed'. A RangeError is thrown if the requested index or dsName * doesnt exist. **/ - this.d.addCallback( - function(self, rrd) { + var self = this; + this.d.done( + function(rrd) { self.resume(function() { Y.Assert.areEqual(RRD_DSNAME, rrd.getDS(0).getName()); Y.Assert.areEqual( @@ -102,7 +106,7 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { } Y.assert(error instanceof RangeError); }); - }, this); + }); this.wait(); }, @@ -110,12 +114,13 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { /** * The generated rrd file should have a single RRA **/ - this.d.addCallback( - function(self, rrd) { + var self = this; + this.d.done( + function(rrd) { self.resume(function() { Y.Assert.areEqual(RRD_RRACOUNT, rrd.getNrRRAs()); }); - }, this); + }); this.wait(); }, @@ -126,8 +131,9 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * rra.getEl throws a RangeError if asked for row which doesn't * exist. **/ - this.d.addCallback( - function(self, rrd) { + var self = this; + this.d.done( + function(rrd) { self.resume(function() { var rra = rrd.getRRA(0); Y.Assert.areEqual('AVERAGE', rra.getCFName()); @@ -136,7 +142,7 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { for(var i=0; i<RRD_RRAROWS; i++) { Y.Assert.areEqual(i, rra.getEl(i, RRD_DSINDEX)); } - var error = null + var error = null; try { rra.getEl(RRD_RRAROWS+1, 0); } catch(e) { @@ -144,7 +150,7 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { } Y.assert(error instanceof RangeError); }); - }, this); + }); this.wait(); } })); @@ -153,12 +159,10 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { name: "jarmon.RrdQuery", setUp: function() { - this.d = new jarmon.downloadBinary('build/test.rrd') - .addCallback( - function(self, binary) { + this.d = new jarmon.downloadBinary('build/test.rrd').pipe( + function(binary) { return new RRDFile(binary); - }, this) - .addErrback( + }, function(ret) { console.log(ret); }); @@ -168,19 +172,21 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { /** * The starttime must be less than the endtime **/ - this.d.addCallback( - function(self, rrd) { - self.resume(function() { - var rq = new jarmon.RrdQuery(rrd, ''); - var error = null; - try { - rq.getData(1, 0); - } catch(e) { - error = e; - } - Y.Assert.isInstanceOf(RangeError, error); - }); - }, this); + var self = this; + this.d.done( + function(rrd) { + self.resume( + function() { + var rq = new jarmon.RrdQuery(rrd, ''); + var error; + try { + rq.getData(1, 0); + } catch(e) { + error = e; + } + Y.Assert.isInstanceOf(RangeError, error); + }); + }); this.wait(); }, @@ -190,8 +196,9 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * Error is raised if the rrd file doesn't contain an RRA with the * requested consolidation function (CF) **/ - this.d.addCallback( - function(self, rrd) { + var self = this; + this.d.done( + function(rrd) { self.resume(function() { var rq = new jarmon.RrdQuery(rrd, ''); var error = null; @@ -202,7 +209,7 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { } Y.Assert.isInstanceOf(TypeError, error); }); - }, this); + }); this.wait(); }, @@ -214,16 +221,18 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * Result should include a data points with times > starttime and * <= endTime **/ - this.d.addCallback( - function(self, rrd) { + var self = this; + this.d.done( + function(rrd) { self.resume(function() { var rq = new jarmon.RrdQuery(rrd, ''); - // We request data starting 1 STEP +1s after the RRD file - // first val and ending 1 STEP -1s before the RRD last val - // ie one step within the RRD file, but 1s away from the - // step boundary to test the quantisation of the - // requested time range. + /* We request data starting 1 STEP +1s after + the RRD file first val and ending 1 STEP -1s + before the RRD last val ie one step within the + RRD file, but 1s away from the step boundary + to test the quantisation of the requested time + range.*/ var data = rq.getData( RRD_STARTTIME + (RRD_STEP+1) * 1000, RRD_ENDTIME - (RRD_STEP-1) * 1000); @@ -253,7 +262,7 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { RRD_ENDTIME-RRD_STEP*1000*2, data.data[data.data.length-1][0]); }); - }, this); + }); this.wait(); }, @@ -262,14 +271,15 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * If the requested time range is outside the range of the RRD file * we should not get any values back **/ - this.d.addCallback( - function(self, rrd) { + var self = this; + this.d.done( + function(rrd) { self.resume(function() { var rq = new jarmon.RrdQuery(rrd, ''); var data = rq.getData(RRD_ENDTIME, RRD_ENDTIME+1000); Y.Assert.areEqual(0, data.data.length); }); - }, this); + }); this.wait(); } @@ -287,12 +297,13 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { /** * The starttime must be less than the endtime **/ - this.rq.getData(1, 0).addBoth( - function(self, res) { + var self = this; + this.rq.getData(1, 0).fail( + function(res) { self.resume(function() { Y.Assert.isInstanceOf(RangeError, res); }); - }, this); + }); this.wait(); }, @@ -302,12 +313,13 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * Error is raised if the rrd file doesn't contain an RRA with the * requested consolidation function (CF) **/ - this.rq.getData(RRD_STARTTIME, RRD_ENDTIME, 0, 'FOO').addBoth( - function(self, res) { + var self = this; + this.rq.getData(RRD_STARTTIME, RRD_ENDTIME, 0, 'FOO').always( + function(res) { self.resume(function() { Y.Assert.isInstanceOf(TypeError, res); }); - }, this); + }); this.wait(); }, @@ -319,18 +331,19 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * Result should include a data points with times > starttime and * <= endTime **/ + var self = this; this.rq.getData(RRD_STARTTIME + (RRD_STEP+1) * 1000, - RRD_ENDTIME - (RRD_STEP-1) * 1000).addBoth( - function(self, data) { + RRD_ENDTIME - (RRD_STEP-1) * 1000).always( + function(data) { self.resume(function() { - // We request data starting 1 STEP +1s after the RRD file - // first val and ending 1 STEP -1s before the RRD last val - // ie one step within the RRD file, but 1s away from the - // step boundary to test the quantisation of the - // requested time range. - - // so we expect two less rows than the total rows in the - // file. + /* We request data starting 1 STEP +1s after + the RRD file first val and ending 1 STEP -1s + before the RRD last val ie one step within + the RRD file, but 1s away from the step + boundary to test the quantisation of the + requested time range. + so we expect two less rows than the total + rows in the file. */ Y.Assert.areEqual(RRD_RRAROWS-2, data.data.length); // The value of the first returned row should be the @@ -354,7 +367,7 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { RRD_ENDTIME-RRD_STEP*1000*2, data.data[data.data.length-1][0]); }); - }, this); + }); this.wait(); }, @@ -363,15 +376,15 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * If the requested time range is outside the range of the RRD file * we should not get any values back **/ - this.rq.getData(RRD_ENDTIME, RRD_ENDTIME+1000).addBoth( - function(self, data) { + var self = this; + this.rq.getData(RRD_ENDTIME, RRD_ENDTIME+1000).always( + function(data) { self.resume(function() { Y.Assert.areEqual(0, data.data.length); }); - }, this); + }); this.wait(); } - })); @@ -383,20 +396,25 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * Test that a rendered chart has the correct dimensions, legend, * axis, labels etc **/ - var $tpl = $('<div><div class="chart"></div></div>').appendTo($('body')); + var self = this; + var $tpl = $( + '<div><div class="chart"></div></div>').appendTo($('body')); var c = new jarmon.Chart($tpl, jarmon.Chart.BASE_OPTIONS); // c.options.xaxis.tzoffset = 0; - c.addData('speed', new jarmon.RrdQueryRemote('build/test.rrd', 'm/s'), true); + c.addData( + 'speed', + new jarmon.RrdQueryRemote('build/test.rrd', 'm/s'), + true); var d = c.setTimeRange(RRD_STARTTIME, RRD_ENDTIME); - d.addCallback( - function(self) { + d.done( + function() { self.resume(function() { // TODO: write useful tests }); - }, this); + }); this.wait(); - }, + } })); @@ -404,7 +422,7 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { name: "jarmon.RrdChooser", setUp: function() { - this.$tpl = $('<div/>').appendTo($('body')) + this.$tpl = $('<div/>').appendTo($('body')); var c = new jarmon.RrdChooser(this.$tpl); c.drawRrdUrlForm(); }, @@ -427,8 +445,7 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { this.wait( function() { Y.Assert.areEqual(self.$tpl.find('.error').size(), 1); - }, 1000 - ); + }, 1000); }, test_drawUrlListDatasources: function () { @@ -437,13 +454,15 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * list of further DS label fields **/ var self = this; - this.$tpl.find('form input[name=rrd_url]').val('build/test.rrd').submit(); + this.$tpl.find( + 'form input[name=rrd_url]').val('build/test.rrd').submit(); this.wait( function() { - Y.Assert.areEqual(self.$tpl.find('input[name=rrd_ds_label]').size(), 1); + Y.Assert.areEqual( + self.$tpl.find('input[name=rrd_ds_label]').size(), 1); }, 1000 ); - }, + } })); @@ -451,7 +470,7 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { name: "jarmon.ChartEditor", setUp: function() { - this.$tpl = $('<div/>').appendTo($('body')) + this.$tpl = $('<div/>').appendTo($('body')); var c = new jarmon.ChartEditor( this.$tpl, { |