Skip to content

Commit

Permalink
Move histo tests and helpers to seperate files
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanZim committed Feb 25, 2017
1 parent 84029ac commit 8c7da7c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 63 deletions.
69 changes: 6 additions & 63 deletions test/cryptocompare.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
'use strict'
const test = require('tape')

// set to global
global.fetch = require('node-fetch')
if (!global.fetch) global.fetch = require('node-fetch')
const cc = require('../')

// NOT_USD_TRADABLE is a cryptocurrency which does not trade directly with USD.
// This value is used in testing tryConversion. Currently, this is set to LTD.
// If LTD trades directly with USD in the future, tests will fail.
// In that case, change this value:
const NOT_USD_TRADABLE = 'LTD'
const helpers = require('./helpers')

test('price()', t => {
t.plan(2)
Expand All @@ -29,7 +23,7 @@ test('price() allows passing a string as the second parameter', t => {
})

test("price()'s tryConversion=false works", t => {
testTryConversion(cc.price(NOT_USD_TRADABLE, 'USD', false), t)
helpers.testTryConversion(cc.price(helpers.NOT_USD_TRADABLE, 'USD', false), t)
})

test('priceMulti()', t => {
Expand All @@ -52,7 +46,7 @@ test('priceMulti() allows passing strings instead of arrays', t => {
})

test("priceMulti()'s tryConversion=false works", t => {
testTryConversion(cc.priceMulti(NOT_USD_TRADABLE, 'USD', false), t)
helpers.testTryConversion(cc.priceMulti(helpers.NOT_USD_TRADABLE, 'USD', false), t)
})

test('priceFull()', t => {
Expand Down Expand Up @@ -86,7 +80,7 @@ test('priceFull() allows passing strings instead of arrays', t => {
})

test("priceFull()'s tryConversion=false works", t => {
testTryConversion(cc.priceFull(NOT_USD_TRADABLE, 'USD', false), t)
helpers.testTryConversion(cc.priceFull(helpers.NOT_USD_TRADABLE, 'USD', false), t)
})

test('priceHistorical()', t => {
Expand All @@ -103,7 +97,7 @@ test('priceHistorical()', t => {

test("priceHistorical()'s tryConversion=false works", t => {
const timestamp = new Date('2017-01-01')
testTryConversion(cc.priceHistorical(NOT_USD_TRADABLE, 'USD', timestamp, false), t)
helpers.testTryConversion(cc.priceHistorical(helpers.NOT_USD_TRADABLE, 'USD', timestamp, false), t)
})

test('topPairs()', t => {
Expand Down Expand Up @@ -153,45 +147,6 @@ test("topExchanges()'s limit option works", t => {
}).catch(t.end)
})

test('histoDay()', t => {
t.plan(8)
cc.histoDay('BTC', 'USD').then(data => {
t.is(data.length, 31, 'returns 31 items by default')
const item = data[0]
t.is(typeof item.time, 'number', 'item.time is a number')
t.is(typeof item.close, 'number', 'item.close is a number')
t.is(typeof item.high, 'number', 'item.high is a number')
t.is(typeof item.low, 'number', 'item.low is a number')
t.is(typeof item.open, 'number', 'item.open is a number')
t.is(typeof item.volumefrom, 'number', 'item.volumefrom is a number')
t.is(typeof item.volumeto, 'number', 'item.volumeto is a number')
t.end()
}).catch(t.end)
})

test("histoDay()'s limit option works", t => {
t.plan(1)
cc.histoDay('BTC', 'USD', { limit: 2 }).then(data => {
t.is(data.length, 3, 'returns limit plus timestamped data')
t.end()
}).catch(t.end)
})

test("histoDay()'s timestamp option works", t => {
t.plan(1)
let data = []
data.push(cc.histoDay('BTC', 'USD', { timestamp: new Date('2017-01-01') }))
data.push(cc.histoDay('BTC', 'USD', { timestamp: new Date('2017-01-02') }))
Promise.all(data).then(data => {
t.notDeepEqual(data[0], data[1], 'data from different days should not be equivalent')
t.end()
}).catch(t.end)
})

test("histoDay()'s tryConversion=false works", t => {
testTryConversion(cc.histoDay(NOT_USD_TRADABLE, 'USD', { tryConversion: false }), t)
})

test('error handling', t => {
cc.price('BTC').then(prices => {
t.end('Promise should not resolve')
Expand All @@ -200,15 +155,3 @@ test('error handling', t => {
t.end()
}).catch(t.end)
})

// Helper Functions:

function testTryConversion (promise, t) {
t.plan(1)
promise.then(prices => {
t.end('Promise should not resolve')
}).catch(e => {
t.ok(e.match(/market does not exist/i), `Converting ${NOT_USD_TRADABLE} to USD fails`)
t.end()
}).catch(t.end)
}
17 changes: 17 additions & 0 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

// NOT_USD_TRADABLE is a cryptocurrency which does not trade directly with USD.
// This value is used in testing tryConversion. Currently, this is set to LTD.
// If LTD trades directly with USD in the future, tests will fail.
// In that case, change this value:
exports.NOT_USD_TRADABLE = 'LTD'

exports.testTryConversion = function (promise, t) {
t.plan(1)
promise.then(prices => {
t.end('Promise should not resolve')
}).catch(e => {
t.ok(e.match(/market does not exist/i), `Converting ${exports.NOT_USD_TRADABLE} to USD fails`)
t.end()
}).catch(t.end)
}
45 changes: 45 additions & 0 deletions test/histo.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict'
const test = require('tape')

if (!global.fetch) global.fetch = require('node-fetch')
const cc = require('../')
const helpers = require('./helpers')

test('histoDay()', t => {
t.plan(8)
cc.histoDay('BTC', 'USD').then(data => {
t.is(data.length, 31, 'returns 31 items by default')
const item = data[0]
t.is(typeof item.time, 'number', 'item.time is a number')
t.is(typeof item.close, 'number', 'item.close is a number')
t.is(typeof item.high, 'number', 'item.high is a number')
t.is(typeof item.low, 'number', 'item.low is a number')
t.is(typeof item.open, 'number', 'item.open is a number')
t.is(typeof item.volumefrom, 'number', 'item.volumefrom is a number')
t.is(typeof item.volumeto, 'number', 'item.volumeto is a number')
t.end()
}).catch(t.end)
})

test("histoDay()'s limit option works", t => {
t.plan(1)
cc.histoDay('BTC', 'USD', { limit: 2 }).then(data => {
t.is(data.length, 3, 'returns limit plus timestamped data')
t.end()
}).catch(t.end)
})

test("histoDay()'s timestamp option works", t => {
t.plan(1)
let data = []
data.push(cc.histoDay('BTC', 'USD', { timestamp: new Date('2017-01-01') }))
data.push(cc.histoDay('BTC', 'USD', { timestamp: new Date('2017-01-02') }))
Promise.all(data).then(data => {
t.notDeepEqual(data[0], data[1], 'data from different days should not be equivalent')
t.end()
}).catch(t.end)
})

test("histoDay()'s tryConversion=false works", t => {
helpers.testTryConversion(cc.histoDay(helpers.NOT_USD_TRADABLE, 'USD', { tryConversion: false }), t)
})

0 comments on commit 8c7da7c

Please sign in to comment.