Skip to content

Commit

Permalink
Test all the remaining files
Browse files Browse the repository at this point in the history
  • Loading branch information
sievins committed Jul 14, 2019
1 parent d7b3b2c commit 79bea54
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 33 deletions.
25 changes: 14 additions & 11 deletions src/charts/data.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
const { readFileSync } = require('fs')
const { map } = require('ramda')
const { recordsLocation } = require('../util')
const { logger, recordsLocation } = require('../util')

const parse = (file) => JSON.parse(readFileSync(file).toString())
const records = parse(recordsLocation)
try {
const records = JSON.parse(readFileSync(recordsLocation).toString())

const mapRecord = (type) => map((record) => record[type], records)
const downloads = mapRecord('download')
const uploads = mapRecord('upload')
const pings = mapRecord('ping')
const mapRecord = (type) => map((record) => record[type], records)
const downloads = mapRecord('download')
const uploads = mapRecord('upload')
const pings = mapRecord('ping')

module.exports = {
downloads,
uploads,
pings,
module.exports = {
downloads,
uploads,
pings,
}
} catch(error) {
logger.error(`Could not parse ${recordsLocation}. Does ${recordsLocation} exists and does it contain record data?\n${error}`)
}
61 changes: 61 additions & 0 deletions src/charts/data.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const test = require('tape-catch')
const sinon = require('sinon')
const proxyquire = require('proxyquire').noCallThru()
const { includes } = require('ramda')

const errorMessage = 'Could not parse'
const records =
'[{"ping":1,"download":4,"upload":7,"day":"24/06/2019","time":"17:50"},\n' +
'{"ping":2,"download":5,"upload":8,"day":"24/06/2019","time":"18:50"},\n' +
'{"ping":3,"download":6,"upload":9,"day":"29/06/2019","time":"07:30"}]'

const sandbox = sinon.createSandbox()

const readFileSync = sandbox.stub()
const errorLogger = sandbox.spy()

const teardown = () => sandbox.restore()

const requireData = () => (
proxyquire('./data', {
'fs': { readFileSync },
'../util': { logger: { error: errorLogger }, recordsLocation: '/path/to/records.txt' }
})
)

test('retrieves record data', (t) => {
readFileSync.returns({ toString: () => records })

const data = requireData()

t.deepEqual(data.pings, [ 1, 2, 3 ])
t.deepEqual(data.downloads, [ 4, 5, 6 ])
t.deepEqual(data.uploads, [ 7, 8, 9 ])

teardown()
t.end()
})

test('logs error if the file does not exist', (t) => {
readFileSync.throws(errorMessage)

requireData()

const loggedMessage = errorLogger.args[0][0]
t.ok(includes(errorMessage, loggedMessage))

teardown()
t.end()
})

test('logs error if there are no records in the file', (t) => {
readFileSync.returns({ toString: () => '' })

requireData()

const loggedMessage = errorLogger.args[0][0]
t.ok(includes(errorMessage, loggedMessage))

teardown()
t.end()
})
8 changes: 5 additions & 3 deletions src/charts/histogram/data.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const ramda = require('ramda')
const { downloads, uploads, pings } = require ('../data')
const { downloads, uploads, pings } = require('../data')

const { filter, head, map, range, reduce, sort } = ramda
const { filter, head, map, min, range, reduce, sort, uniq } = ramda

const defaultNumberOfGroups = 5

// Speeds example: [4.321, 2.123, 4.634]
// Histogram example: [{ key: '0-3', value: 1 }, { key: '3-6', value: 2 }]
Expand All @@ -10,7 +12,7 @@ const computeHistogram = (speeds) => {
const first = head(sortedSpeeds)
const last = ramda.last(sortedSpeeds)

const numberOfGroups = 5 // TODO include this as a setting
const numberOfGroups = min(uniq(speeds).length, defaultNumberOfGroups)
const windowSize = (last - first) / numberOfGroups
const windows = map((windowMultiplier) => {
const min = first + windowSize * windowMultiplier
Expand Down
117 changes: 117 additions & 0 deletions src/charts/histogram/data.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
const test = require('tape-catch')
const proxyquire = require('proxyquire').noCallThru()
const { all, pluck, split } = require('ramda')

const createSpeedData = (speeds) => ({
downloads: speeds,
uploads: speeds,
pings: speeds,
})

const requireData = (speedData) => (
proxyquire('./data', {
'../data': speedData,
})
)

test('handles one speed', (t) => {
t.plan(1)

const data = requireData(createSpeedData([ 1 ]))

t.deepEqual(data.download, [{ key: '1.00 - 1.00', value: 1 }])
})

test('handles two speeds', (t) => {
t.plan(1)

const data = requireData(createSpeedData(([ 1, 2 ])))

t.deepEqual(data.download, [
{ key: '1.00 - 1.50', value: 1 },
{ key: '1.50 - 2.00', value: 1 },
])
})

test('keys have 2 decimal points', (t) => {
t.plan(2)

const data = requireData(createSpeedData([ 1 ]))

const key = data.download[0].key
const splitKey = split(' - ', key)
const minDecimalsLength = split('.', splitKey[0])[1].length
const maxDecimalsLength = split('.', splitKey[1])[1].length

t.equal(minDecimalsLength, 2)
t.equal(maxDecimalsLength, 2)
})

test('keys are in the format \'number.2decimals - number.2decimals\'', (t) => {
t.plan(1)

const data = requireData(createSpeedData([ 1 ]))
const key = data.download[0].key

const format = /\d+.\d{2}\s.*\s\d+.\d{2}/
t.ok(format.test(key))
})

test('creates 5 windows', (t) => {
t.plan(1)

const data = requireData(createSpeedData(([ 1, 2, 3, 4, 5, 6, 7, 8, 9 ])))

t.equal(data.download.length, 5)
})

test('windows are equally spaced', (t) => {
t.plan(1)

const data = requireData(createSpeedData(([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ])))

const keys = pluck('key', data.download)
const windowSizesEqual2 = all((key) => {
const splitKey = split(' - ', key)
return splitKey[1] - splitKey[0] === 2
}, keys)

t.ok(windowSizesEqual2)
})

test('groups unequally spread data', (t) => {
t.plan(1)

const data = requireData(createSpeedData(([ 1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ])))
const values = pluck('value', data.download)

t.deepEqual(values, [ 12, 2, 2, 2, 2 ])
})

test('handles windows which have no data', (t) => {
t.plan(1)

const data = requireData(createSpeedData(([ 1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 11 ])))
const values = pluck('value', data.download)

t.deepEqual(values, [ 10, 0, 0, 0, 1 ])
})

test('groups download, upload and ping data', (t) => {
t.plan(1)

const data = requireData(createSpeedData(([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ])))
const expectedData = [
{ key: '1.00 - 3.00', value: 3 },
{ key: '3.00 - 5.00', value: 2 },
{ key: '5.00 - 7.00', value: 2 },
{ key: '7.00 - 9.00', value: 2 },
{ key: '9.00 - 11.00', value: 2 },
]

t.deepEqual({
download: expectedData,
upload: expectedData,
ping: expectedData,
}, data)
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const { resolve } = require('path')
const { mkdirSync } = require('fs')
const d3nBar = require('d3node-barchart')
const output = require('d3node-output')

const { download, upload, ping } = require('./data')

module.exports = ({ resultsDirectory }) => {
Expand Down
67 changes: 67 additions & 0 deletions src/charts/histogram/generate-charts.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const test = require('tape-catch')
const sinon = require('sinon')
const proxyquire = require('proxyquire').noCallThru()

const sandbox = sinon.createSandbox()

const download = [{ key: '1.00 - 3.00', value: 1 }]
const upload = [{ key: '1.00 - 3.00', value: 2 }]
const ping = [{ key: '1.00 - 3.00', value: 3 }]
const resultsDirectory = '/path/to/results/directory'
const chartsDirectory = '/path/to/results/directory/charts'
const downloadPath = '/path/to/results/directory/charts/download'
const uploadPath = '/path/to/results/directory/charts/upload'
const pingPath = '/path/to/results/directory/charts/ping'
const d3nBarData = 'd3BarData'

const resolve = sandbox.stub()
resolve.withArgs(resultsDirectory).returns(chartsDirectory)
resolve.withArgs(chartsDirectory, './download').returns(downloadPath)
resolve.withArgs(chartsDirectory, './upload').returns(uploadPath)
resolve.withArgs(chartsDirectory, './ping').returns(pingPath)
const d3nBar = sandbox.stub()
d3nBar.returns(d3nBarData)
const mkdirSync = sandbox.spy()
const output = sandbox.spy()

const teardown = () => sandbox.restore()

const generateCharts = proxyquire('./generate-charts', {
'path': { resolve },
'fs': { mkdirSync },
'd3node-barchart': d3nBar,
'd3node-output': output,
'./data': { download, upload, ping },
})

test('creates directory to output charts into', (t) => {
generateCharts({ resultsDirectory })

t.ok(mkdirSync.calledWith(chartsDirectory, { recursive: true }))

teardown()
t.end()
})

test('creates charts', (t) => {
generateCharts({ resultsDirectory })

t.ok(d3nBar.firstCall.calledWithMatch({ data: download }))
t.ok(d3nBar.secondCall.calledWithMatch({ data: upload }))
t.ok(d3nBar.thirdCall.calledWithMatch({ data: ping }))

teardown()
t.end()
})

test('outputs charts', (t) => {
generateCharts({ resultsDirectory })

const options = {width: 960, height: 600}
t.ok(output.firstCall.calledWithMatch(downloadPath, d3nBarData, options))
t.ok(output.secondCall.calledWithMatch(uploadPath, d3nBarData, options))
t.ok(output.thirdCall.calledWithMatch(pingPath, d3nBarData, options))

teardown()
t.end()
})
2 changes: 1 addition & 1 deletion src/charts/histogram/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const generateCharts = require('./generateCharts')
const generateCharts = require('./generate-charts')

module.exports = {
generateCharts,
Expand Down
2 changes: 1 addition & 1 deletion src/charts/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const generateCharts = require('./histogram/generateCharts')
const { generateCharts } = require('./histogram')

module.exports = generateCharts
27 changes: 27 additions & 0 deletions src/charts/run-generate-charts.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const test = require('tape-catch')
const sinon = require('sinon')
const proxyquire = require('proxyquire').noCallThru()

const sandbox = sinon.createSandbox()
const generateCharts = sandbox.spy()
const environmentVariable = sandbox.stub()
const resultsDirectory = 'C:\\path\\to\\results\\directory'
environmentVariable.withArgs('resultsDirectory').returns(resultsDirectory)

const teardown = () => sandbox.restore()

const requireRunGenerateCharts = () => (
proxyquire('./run-generate-charts', {
'./histogram': { generateCharts },
'../util': { environmentVariable },
})
)

test('generateCharts is invoked with correct arguments', (t) => {
requireRunGenerateCharts()

t.ok(generateCharts.calledWith({ resultsDirectory }))

teardown()
t.end()
})
Loading

0 comments on commit 79bea54

Please sign in to comment.