Skip to content

Commit

Permalink
Promisifying tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danvk committed Apr 23, 2015
1 parent 8e4d60c commit e356c59
Show file tree
Hide file tree
Showing 18 changed files with 136 additions and 197 deletions.
3 changes: 1 addition & 2 deletions test/BamDataSource-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* @flow */
'use strict';

var chai = require('chai');
var expect = chai.expect;
var expect = require('chai').expect;

var Bam = require('../src/bam'),
createBamDataSource = require('../src/BamDataSource'),
Expand Down
44 changes: 17 additions & 27 deletions test/BigBed-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* @flow */
'use strict';

var chai = require('chai');
var expect = chai.expect;
var expect = require('chai').expect;

var Q = require('q');
var BigBed = require('../src/BigBed');
Expand All @@ -13,10 +12,10 @@ describe('BigBed', function() {
return new BigBed('/test/data/itemRgb.bb'); // See test/data/README.md
}

it('should extract features in a range', function(done) {
it('should extract features in a range', function() {
var bb = getTestBigBed();

bb.getFeaturesInRange('chrX', 151077036, 151078532)
return bb.getFeaturesInRange('chrX', 151077036, 151078532)
.then(features => {
// Here's what these two lines in the file look like:
// chrX 151077031 151078198 MID_BLUE 0 - 151077031 151078198 0,0,128
Expand All @@ -40,12 +39,10 @@ describe('BigBed', function() {
expect(rest1[0]).to.equal('VIOLET_RED1');
expect(rest1[2]).to.equal('-');
expect(rest1[5]).to.equal('255,62,150');
done();
})
.done();
});
});

it('should have inclusive ranges', function(done) {
it('should have inclusive ranges', function() {
// The matches looks like this:
// chrX 151071196 151072363 RED
// chrX 151094536 151095703 PeachPuff
Expand All @@ -56,7 +53,7 @@ describe('BigBed', function() {
expect(features).to.have.length(n);
};

Q.all([
return Q.all([
// request for precisely one row from the file.
bb.getFeaturesInRange('chrX', red[0], red[1])
.then(expectN(1)),
Expand All @@ -69,29 +66,25 @@ describe('BigBed', function() {
// but this range ends one base pair before it.
bb.getFeaturesInRange('chrX', red[0] - 1000, red[0] - 1)
.then(expectN(0))
]).then(() => {
done();
}).done();
]);
});

it('should add "chr" to contig names', function(done) {
it('should add "chr" to contig names', function() {
var bb = getTestBigBed();

bb.getFeaturesInRange('X', 151077036, 151078532)
return bb.getFeaturesInRange('X', 151077036, 151078532)
.then(features => {
// (same as 'should extract features in a range' test)
expect(features).to.have.length(2);
expect(features[0].contig).to.equal('chrX');
expect(features[1].contig).to.equal('chrX');
done();
})
.done();
});
});

it('should cache requests in a block', function(done) {
it('should cache requests in a block', function() {
var bb = getTestBigBed(),
remote = bb.remoteFile;
bb.getFeaturesInRange('X', 151077036, 151078532).then(() => {
return bb.getFeaturesInRange('X', 151077036, 151078532).then(() => {
// cache has been warmed up -- flush it to get a deterministic test.
remote.clearCache();
remote.numNetworkRequests = 0;
Expand All @@ -111,24 +104,21 @@ describe('BigBed', function() {
// But a request from another block (the 'Y' block) should.
expect(features).to.have.length(1);
expect(remote.numNetworkRequests).to.equal(2);
done();
})
.done();
});
});

it('should fetch full blocks', function(done) {
it('should fetch full blocks', function() {
var bb = getTestBigBed();

bb.getFeatureBlocksOverlapping(new ContigInterval('X', 151077036, 151078532))
var range = new ContigInterval('X', 151077036, 151078532);
return bb.getFeatureBlocksOverlapping(range)
.then(blockFeatures => {
expect(blockFeatures).to.have.length(1); // just one block fetched.
var range = blockFeatures[0].range,
rows = blockFeatures[0].rows;
expect(rows).to.have.length(21); // all the chrX features.
expect(range.toString()).to.equal('chrX:151071196-151095703');
done();
})
.done();
});
});

// Things left to test:
Expand Down
3 changes: 1 addition & 2 deletions test/BigBedDataSource-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* @flow */
'use strict';

var chai = require('chai');
var expect = chai.expect;
var expect = require('chai').expect;

var BigBed = require('../src/BigBed'),
createBigBedDataSource = require('../src/BigBedDataSource'),
Expand Down
3 changes: 1 addition & 2 deletions test/ContigInterval-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* @flow */
'use strict';

var chai = require('chai');
var expect = chai.expect;
var expect = require('chai').expect;

var ContigInterval = require('../src/ContigInterval');

Expand Down
3 changes: 1 addition & 2 deletions test/Interval-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* @flow */
'use strict';

var chai = require('chai');
var expect = chai.expect;
var expect = require('chai').expect;

var Interval = require('../src/Interval');

Expand Down
17 changes: 8 additions & 9 deletions test/MappedRemoteFile-test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* @flow */
'use strict';

var chai = require('chai'),
expect = chai.expect,
jBinary = require('jbinary'),
var expect = require('chai').expect;

var jBinary = require('jbinary'),
Q = require('q');

var MappedRemoteFile = require('./MappedRemoteFile');
Expand All @@ -13,7 +13,7 @@ describe('MappedRemoteFile', function() {
return new jBinary(buf).read('string');
}

it('should serve requests through the map', function(done) {
it('should serve requests through the map', function() {
var remoteFile = new MappedRemoteFile('/test/data/0to9.txt', [
[0, 2], // 0,1,2
[12345678, 12345680], // 3,4,5
Expand Down Expand Up @@ -46,22 +46,21 @@ describe('MappedRemoteFile', function() {
}),
];

Q.all(promises).then(() => { done(); }).done();
return Q.all(promises);
});

it('should forget file length', function(done) {
it('should forget file length', function() {
var remoteFile = new MappedRemoteFile('/test/data/0to9.txt', [
[0, 2], // 0,1,2
[12345673, 12345690] // 3456789\n
]);

remoteFile.getBytes(0, 3).then(buf => {
return remoteFile.getBytes(0, 3).then(buf => {
expect(bufferToText(buf)).to.equal('012');
// This second read would fail if the file remembered its length.
return remoteFile.getBytes(12345673, 8).then(buf => {
expect(bufferToText(buf)).to.equal('3456789\n');
done();
});
}).done();
});
});
});
78 changes: 34 additions & 44 deletions test/RemoteFile-test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* @flow */
'use strict';

var chai = require('chai'),
expect = chai.expect,
jBinary = require('jbinary');
var expect = require('chai').expect;

var jBinary = require('jbinary');

var RemoteFile = require('../src/RemoteFile');

Expand All @@ -12,78 +12,72 @@ describe('RemoteFile', () => {
return new jBinary(buf).read('string');
}

it('should fetch a subset of a file', done => {
it('should fetch a subset of a file', function() {
var f = new RemoteFile('/test/data/0to9.txt');
var promisedData = f.getBytes(4, 5);

expect(f.numNetworkRequests).to.equal(1);
promisedData.then(buf => {
return promisedData.then(buf => {
expect(buf.byteLength).to.equal(5);
expect(bufferToText(buf)).to.equal('45678');
done();
}).done();
});
});

it('should fetch subsets from cache', done => {
it('should fetch subsets from cache', function() {
var f = new RemoteFile('/test/data/0to9.txt');
f.getBytes(0, 10).then(buf => {
return f.getBytes(0, 10).then(buf => {
expect(buf.byteLength).to.equal(10);
expect(bufferToText(buf)).to.equal('0123456789');
expect(f.numNetworkRequests).to.equal(1);
f.getBytes(4, 5).then(buf => {
return f.getBytes(4, 5).then(buf => {
expect(buf.byteLength).to.equal(5);
expect(bufferToText(buf)).to.equal('45678');
expect(f.numNetworkRequests).to.equal(1); // it was cached
done();
}).done();
}).done();
});
});
});

it('should fetch entire files', done => {
it('should fetch entire files', function() {
var f = new RemoteFile('/test/data/0to9.txt');
f.getAll().then(buf => {
return f.getAll().then(buf => {
expect(buf.byteLength).to.equal(11);
expect(bufferToText(buf)).to.equal('0123456789\n');
expect(f.numNetworkRequests).to.equal(1);
done();
}).done();
});
});

it('should determine file lengths', done => {
it('should determine file lengths', function() {
var f = new RemoteFile('/test/data/0to9.txt');
f.getSize().then(size => {
return f.getSize().then(size => {
expect(size).to.equal(11);
// TODO: make sure this was a HEAD request
expect(f.numNetworkRequests).to.equal(1);
done();
}).done();
});
});

it('should get file lengths from full requests', done => {
it('should get file lengths from full requests', function() {
var f = new RemoteFile('/test/data/0to9.txt');
f.getAll().then(buf => {
return f.getAll().then(buf => {
expect(f.numNetworkRequests).to.equal(1);
return f.getSize().then(size => {
expect(size).to.equal(11);
expect(f.numNetworkRequests).to.equal(1); // no additional requests
done();
});
}).done();
});
});

it('should get file lengths from range requests', done => {
it('should get file lengths from range requests', function() {
var f = new RemoteFile('/test/data/0to9.txt');
f.getBytes(4, 5).then(buf => {
return f.getBytes(4, 5).then(buf => {
expect(f.numNetworkRequests).to.equal(1);
return f.getSize().then(size => {
expect(size).to.equal(11);
expect(f.numNetworkRequests).to.equal(1); // no additional requests
done();
});
}).done();
});
});

it('should cache requests for full files', done => {
it('should cache requests for full files', function() {
var f = new RemoteFile('/test/data/0to9.txt');
f.getAll().then(buf => {
expect(buf.byteLength).to.equal(11);
Expand All @@ -93,50 +87,46 @@ describe('RemoteFile', () => {
expect(buf.byteLength).to.equal(11);
expect(bufferToText(buf)).to.equal('0123456789\n');
expect(f.numNetworkRequests).to.equal(1); // still 1
done();
});
}).done();
});
});

it('should serve range requests from cache after getAll', done => {
it('should serve range requests from cache after getAll', function() {
var f = new RemoteFile('/test/data/0to9.txt');
f.getAll().then(buf => {
return f.getAll().then(buf => {
expect(buf.byteLength).to.equal(11);
expect(bufferToText(buf)).to.equal('0123456789\n');
expect(f.numNetworkRequests).to.equal(1);
return f.getBytes(4, 5).then(buf => {
expect(buf.byteLength).to.equal(5);
expect(bufferToText(buf)).to.equal('45678');
expect(f.numNetworkRequests).to.equal(1); // still 1
done();
});
}).done();
});
});

it('should reject requests to a non-existent file', done => {
it('should reject requests to a non-existent file', function() {
var f = new RemoteFile('/test/data/nonexistent-file.txt');
f.getAll().then(buf => {
return f.getAll().then(buf => {
throw 'Requests for non-existent files should not succeed';
}, err => {
expect(err).to.match(/404/);
done();
}).done();
});
});

it('should truncate requests past EOF', done => {
it('should truncate requests past EOF', function() {
var f = new RemoteFile('/test/data/0to9.txt');
var promisedData = f.getBytes(4, 100);

promisedData.then(buf => {
return promisedData.then(buf => {
expect(buf.byteLength).to.equal(7);
expect(bufferToText(buf)).to.equal('456789\n');
expect(f.numNetworkRequests).to.equal(1);
return f.getBytes(6, 90).then(buf => {
expect(buf.byteLength).to.equal(5);
expect(bufferToText(buf)).to.equal('6789\n');
expect(f.numNetworkRequests).to.equal(1);
done();
});
}).done();
});
});
});

0 comments on commit e356c59

Please sign in to comment.