Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests (#18) and mocking NCBI API #21

Merged
merged 8 commits into from Apr 9, 2017
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions .gitattributes
@@ -0,0 +1,11 @@
test/fixtures/Download[[:space:]]list[[:space:]]for[[:space:]]assembly_.json filter=lfs diff=lfs merge=lfs -text
test/fixtures/Download[[:space:]]list[[:space:]]for[[:space:]]sra_.json filter=lfs diff=lfs merge=lfs -text
test/fixtures/Download[[:space:]]unless[[:space:]]file[[:space:]]exists_.json filter=lfs diff=lfs merge=lfs -text
test/fixtures/Download_.json filter=lfs diff=lfs merge=lfs -text
test/fixtures/Fetch_.json filter=lfs diff=lfs merge=lfs -text
test/fixtures/Link[[:space:]]bioproject[[:space:]]to[[:space:]]assembly_.json filter=lfs diff=lfs merge=lfs -text
test/fixtures/Link[[:space:]]sra[[:space:]]to[[:space:]]bioproject_.json filter=lfs diff=lfs merge=lfs -text
test/fixtures/Search[[:space:]]assembly_.json filter=lfs diff=lfs merge=lfs -text
test/fixtures/Search[[:space:]]sra[[:space:]]with[[:space:]]limit[[:space:]]to[[:space:]]one_.json filter=lfs diff=lfs merge=lfs -text
test/fixtures/Search[[:space:]]sra_.json filter=lfs diff=lfs merge=lfs -text
503988/GCF_000315625.1_Guith1_genomic.fna.gz filter=lfs diff=lfs merge=lfs -text
2 changes: 2 additions & 0 deletions .npmignore
Expand Up @@ -2,3 +2,5 @@ node_modules
.DS_Store
coverage
tmp
503988
test/fixtures
13 changes: 11 additions & 2 deletions .travis.yml
@@ -1,6 +1,15 @@
language: node_js
node_js:
- "0.12"
- "7"
before_install:
- mkdir -p $HOME/bin
- wget https://github.com/github/git-lfs/releases/download/v1.1.2/git-lfs-linux-amd64-1.1.2.tar.gz
- tar xvfz git-lfs-linux-amd64-1.1.2.tar.gz
- mv git-lfs-1.1.2/git-lfs $HOME/bin/git-lfs
- export PATH=$PATH:$HOME/bin/
before_script:
- git lfs pull
- git lfs pull
after_script:
- npm run coveralls
notifications:
Expand All @@ -15,4 +24,4 @@ notifications:
on_success: change
on_failure: always
on_start: false
env: DEBUG="*"
env: DEBUG=""
3 changes: 3 additions & 0 deletions 503988/GCF_000315625.1_Guith1_genomic.fna.gz
Git LFS file not shown
18 changes: 9 additions & 9 deletions cli.js
Expand Up @@ -15,15 +15,15 @@ var minimistOptions = {
}
}

var jsonPattern = /\{(.+?)\}/,
args = process.argv.slice(2).join(' '),
options = {},
match = args.match(jsonPattern)
var jsonPattern = /\{(.+?)\}/
var args = process.argv.slice(2).join(' ')
var options = {}
var match = args.match(jsonPattern)

if (match) {
var jsonLine = match[0].replace(/\'/g, '\"'),
options = JSON.parse(jsonLine),
args = args.replace(match[0], 'obj')
var jsonLine = match[0].replace(/'/g, '"')
options = JSON.parse(jsonLine)
args = args.replace(match[0], 'obj')
}

var argv = minimist(args.split(' '), minimistOptions)
Expand All @@ -46,8 +46,8 @@ if (command === 'link') {
var arg2 = argv._[2]
var arg3 = argv._[3]
} else {
var arg2 = argv._.slice(2).join(' ')
var arg3 = null
arg2 = argv._.slice(2).join(' ')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These variables aren't declared if command !== 'link'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are declared because JavaScript doesn't have block scope, only function scope, so they get hoisted. However, this might be confusing so I'll fix it. Thanks for noticing it!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

arg3 = null
}

if (Object.keys(argv).length > 1) {
Expand Down
10 changes: 5 additions & 5 deletions lib/anonymous-tracking.js
@@ -1,14 +1,14 @@
// Anonymous usage metrics for debug and funding, if user agrees
const Insight = require('insight');
const pkg = require('../package.json');
const Insight = require('insight')
const pkg = require('../package.json')

const insight = new Insight({
// Google Analytics tracking code
trackingCode: 'UA-54802258-3',
pkg
});
})
if (insight.optOut === undefined) {
insight.askPermission("Bionode is open and free. Can we anonymously report usage statistics for improvement and funding purposes?");
insight.askPermission('Bionode is open and free. Can we anonymously report usage statistics for improvement and funding purposes?')
}

module.exports = insight
module.exports = insight
32 changes: 17 additions & 15 deletions lib/bionode-ncbi.js
Expand Up @@ -50,7 +50,7 @@ var RETURNMAX = 50
var XMLPROPERTIES = {
'sra': ['expxml', 'runs'],
'biosample': ['sampledata'],
'assembly': ['meta' ]
'assembly': ['meta']
}
var LASTSTREAM = {
'sra': function () {
Expand Down Expand Up @@ -150,9 +150,9 @@ function createAPIPaginateURL (opts) {

function transform (obj, enc, next) {
var esearchRes = obj.body.esearchresult
if (esearchRes === undefined
|| esearchRes.webenv === undefined
|| esearchRes.count === undefined) {
if (esearchRes === undefined ||
esearchRes.webenv === undefined ||
esearchRes.count === undefined) {
var msg = 'NCBI returned invalid results, this could be a temporary' +
' issue with NCBI servers.\nRequest URL: ' + obj.url
this.emit('error', new Error(msg))
Expand Down Expand Up @@ -393,7 +393,7 @@ function download (db) {
function gotDir (err) {
if (err) { self.emit('error', err) }
debug('downloading', url)
var options = { dir: folder, resume: true }
var options = { dir: folder, resume: true, quiet: true }
var dld = nugget(PROXY + url, options, function (err) {
if (err) return self.destroy(err)
fs.stat(path, gotStat)
Expand Down Expand Up @@ -479,8 +479,10 @@ function createFTPURL (db) {
if (obj.meta.FtpSites) {
var ftpPath = obj.meta.FtpSites.FtpPath
var ftpArray = Array.isArray(ftpPath) ? ftpPath : [ ftpPath ]
var httpRoot = ftpArray[0]._.replace('ftp://', 'http://') // NCBI seems to return GenBank and RefSeq accessions for the same thing. We only need one.
var httpRoot = httpRoot.split('/').slice(0, -1).join('/')
// NCBI seems to return GenBank and RefSeq accessions for the same thing. We only need one.
var httpRoot = ftpArray[0]._
.replace('ftp://', 'http://')
.split('/').slice(0, -1).join('/')
request({ uri: PROXY + httpRoot, withCredentials: false }, gotFTPDir)
} else { return next() }
function gotFTPDir (err, res, body) {
Expand All @@ -496,7 +498,7 @@ function createFTPURL (db) {
var href = a.attribs.href
var base = path.basename(href)
var basename = path.basename(httpRoot)
var fileNameProperties = base.replace(new RegExp('.*'+basename+'_'), '')
var fileNameProperties = base.replace(new RegExp('.*' + basename + '_'), '')
var fileNameExtensions = fileNameProperties.split('.')
var fileType = fileNameExtensions[0]
var fileFormat = fileNameExtensions[1] || 'dir'
Expand Down Expand Up @@ -524,13 +526,13 @@ function requestStream (returnURL) {
if (self.tries > 20) { console.warn('try ' + self.tries + obj) }
request({ uri: obj, json: true, timeout: timeout, withCredentials: false }, gotData)
function gotData (err, res, body) {
if (err
|| !res
|| res.statusCode !== 200
|| !body
|| (body.esearchresult && body.esearchresult.ERROR)
|| (body.esummaryresult && body.esummaryresult[0] === 'Unable to obtain query #1')
|| body.error
if (err ||
!res ||
res.statusCode !== 200 ||
!body ||
(body.esearchresult && body.esearchresult.ERROR) ||
(body.esummaryresult && body.esummaryresult[0] === 'Unable to obtain query #1') ||
body.error
) {
self.tries++
return setTimeout(get, interval)
Expand Down
48 changes: 24 additions & 24 deletions package.json
Expand Up @@ -12,36 +12,36 @@
"email": "mail@bmpvieira.com"
},
"dependencies": {
"JSONStream": "^0.10.0",
"async": "^0.9.0",
"bionode-fasta": "^0.5.3",
"cheerio": "^0.19.0",
"concat-stream": "~1.4.8",
"debug": "^2.1.3",
"JSONStream": "^1.3.1",
"async": "^2.3.0",
"bionode-fasta": "^0.5.5",
"cheerio": "^0.22.0",
"concat-stream": "~1.6.0",
"debug": "^2.6.3",
"insight": "^0.8.4",
"minimist": "^1.1.1",
"mkdirp": "^0.5.0",
"nugget": "^1.4.1",
"pumpify": "^1.3.3",
"request": "^2.55.0",
"split2": "^2.0.1",
"through2": "^0.6.3",
"minimist": "^1.2.0",
"mkdirp": "^0.5.1",
"nugget": "^2.0.1",
"pumpify": "^1.3.5",
"request": "^2.81.0",
"split2": "^2.1.1",
"through2": "^2.0.3",
"tool-stream": "0.2.1",
"xml2js": "^0.4.6"
"xml2js": "^0.4.17"
},
"devDependencies": {
"standard": "^3.3.2",
"dependency-check": "^2.3.1",
"browserify": "^9.0.7",
"browserify": "^14.3.0",
"contributor": "~0.1.25",
"coveralls": "~2.11.2",
"coveralls": "~2.13.0",
"dependency-check": "^2.8.0",
"docco": "~0.7.0",
"istanbul": "~0.3.13",
"tap-spec": "^3.0.0",
"tape": "^4.0.0",
"istanbul": "~0.4.5",
"standard": "^10.0.1",
"tap-spec": "^4.1.1",
"tape": "^4.6.3",
"tape-nock": "^1.5.2",
"testling": "^1.7.1",
"uglify-js": "^2.4.19",
"nock": "2.11.0"
"uglify-js": "^2.8.21"
},
"keywords": [
"bio",
Expand All @@ -60,7 +60,7 @@
"bionode-ncbi": "cli.js"
},
"scripts": {
"test": "standard && dependency-check . && node test/*.js | tap-spec && rm -rf ./503988",
"test": "standard && dependency-check . && node test/*.js | tap-spec",
"test-browser": "browserify test/*.js -d | testling -x 'open -a \"Google Chrome\"' | tap-spec",
"coverage": "standard && dependency-check . && istanbul cover test/bionode-ncbi.js --report lcovonly -- | tap-spec && rm -rf ./coverage",
"coveralls": "istanbul cover test/bionode-ncbi.js --report lcovonly -- | tap-spec && cat ./coverage/lcov.info | ./node_modules/.bin/coveralls && rm -rf ./coverage",
Expand Down
35 changes: 18 additions & 17 deletions test/bionode-ncbi.js
@@ -1,15 +1,17 @@
var fs = require('fs')
var crypto = require('crypto')
var test = require('tape')
var nock = require('nock')
var tape = require('tape')
var tapeNock = require('tape-nock')
var test = tapeNock(tape)
var nock = test.nock

var ncbi = require('../')

var testData = require('./data')
var guillardiaThetaSRAData = require('./guillardia-theta.sra')
var efetchTestData = require('./p53-nucest')

test('Download list', function (t) {
test('Download list for assembly', function (t) {
var msg = 'should take a database name (assembly) and search term (Guillardia theta), and list datasets URLs'
var db = 'assembly'
var expResult = [testData.assembly['guillardia-theta'].urls]
Expand All @@ -22,7 +24,7 @@ test('Download list', function (t) {
})
})

test('Download list', function (t) {
test('Download list for sra', function (t) {
var msg = 'should take a database name (sra) and search term (Guillardia theta), and list datasets URLs'
var db = 'sra'
var expResult = testData.sra['guillardia-theta'].urls
Expand All @@ -46,14 +48,14 @@ test('Download', function (t) {
file.on('data', function (d) { shasum.update(d) })
file.on('end', function () {
var sha1 = shasum.digest('hex')
var hash = 'a2dc7b3b0ae6f40d5205c4394c2fe8bc65d52bc2'
var hash = testData['sra-sha1']
t.equal(sha1, hash, msg)
setTimeout(t.end, 2000)
})
})
})

test('Download', function (t) {
test('Download unless file exists', function (t) {
var msg = 'repeat same download to cover already downloaded branch'
var path = ''
var stream = ncbi.download('assembly', 'Guillardia theta')
Expand All @@ -64,14 +66,14 @@ test('Download', function (t) {
file.on('data', function (d) { shasum.update(d) })
file.on('end', function () {
var sha1 = shasum.digest('hex')
var hash = 'a2dc7b3b0ae6f40d5205c4394c2fe8bc65d52bc2'
var hash = testData['sra-sha1']
t.equal(sha1, hash, msg)
setTimeout(t.end, 2000)
})
})
})

test('Search', function (t) {
test('Search assembly', function (t) {
var results1 = []
var stream = ncbi.search('assembly', 'Guillardia theta')
stream.on('data', function (data) { results1.push(data) })
Expand All @@ -82,7 +84,7 @@ test('Search', function (t) {
})
})

test('Search', function (t) {
test('Search sra', function (t) {
var results2 = []
var stream = ncbi.search('sra', 'Guillardia theta')
stream.on('data', function (data) { results2.push(data) })
Expand All @@ -93,7 +95,7 @@ test('Search', function (t) {
})
})

test('Search', function (t) {
test('Search sra with limit to one', function (t) {
var results3 = []
var stream = ncbi.search({ db: 'sra', term: 'Guillardia theta', limit: 1 })
stream.on('data', function (data) {
Expand All @@ -111,7 +113,7 @@ test('Search', function (t) {
})
})

test('Link', function (t) {
test('Link sra to bioproject', function (t) {
var results = []
var stream = ncbi.link('sra', 'bioproject', '35533')
stream.on('data', function (data) { results.push(data) })
Expand All @@ -122,7 +124,7 @@ test('Link', function (t) {
})
})

test('Link', function (t) {
test('Link bioproject to assembly', function (t) {
var results = []
var stream = ncbi.link('bioproject', 'assembly', '53577')
stream.on('data', function (data) { results.push(data) })
Expand All @@ -145,10 +147,10 @@ test('Fetch', function (t) {
})

test('Error Handling', function (t) {
var base = 'http://eutils.ncbi.nlm.nih.gov',
path = '/entrez/eutils/esearch.fcgi?&retmode=json&version=2.0&db=assembly&term=Guillardia_theta&usehistory=y',
results = [],
msg = 'Should detect invalid return object and throw an error stating so, showing request URL'
var base = 'http://eutils.ncbi.nlm.nih.gov'
var path = '/entrez/eutils/esearch.fcgi?&retmode=json&version=2.0&db=assembly&term=Guillardia_theta&usehistory=y'
var results = []
var msg = 'Should detect invalid return object and throw an error stating so, showing request URL'

nock(base)
.get(path)
Expand All @@ -164,4 +166,3 @@ test('Error Handling', function (t) {
})
setTimeout(t.end, 2000)
})