Merge pull request #4 from exogen/add-tests-and-coverage

Switch to AVA, add new tests and coverage
This commit is contained in:
Brian Beck 2016-12-07 00:40:00 -08:00 committed by GitHub
commit 1ace6f55e5
12 changed files with 1652 additions and 184 deletions

View file

@ -1,3 +1,10 @@
{
"presets": ["es2015", "stage-2"]
"presets": ["es2015", "stage-2"],
"plugins": ["transform-runtime"],
"ignore": "test/**",
"env": {
"test": {
"plugins": ["istanbul"]
}
}
}

View file

@ -2,7 +2,6 @@ language: node_js
node_js:
- "4"
- "5"
- "6"
# Use container-based Travis infrastructure.
@ -13,4 +12,7 @@ branches:
- master
script:
- yarn run check
- yarn test
after_success:
- $(yarn bin)/nyc report --reporter=text-lcov | $(yarn bin)/coveralls

View file

@ -1,6 +1,7 @@
# graphbrainz
[![build status](https://travis-ci.org/exogen/graphbrainz.svg?branch=master)](https://travis-ci.org/exogen/graphbrainz)
[![build status](https://img.shields.io/travis/exogen/graphbrainz.svg)](https://travis-ci.org/exogen/graphbrainz)
[![coverage](https://img.shields.io/coveralls/exogen/graphbrainz.svg)](https://coveralls.io/github/exogen/graphbrainz)
[![latest release](https://img.shields.io/npm/v/graphbrainz.svg?label=latest+release)](https://www.npmjs.com/package/graphbrainz)
[![license](https://img.shields.io/npm/l/graphbrainz.svg)](https://github.com/exogen/graphbrainz/blob/master/LICENSE)

View file

@ -24,16 +24,19 @@
"build:lib": "babel --out-dir lib src",
"clean": "npm run clean:lib",
"clean:lib": "rm -rf lib",
"check": "npm run lint && npm run test",
"deploy": "./scripts/deploy.sh",
"lint": "standard --verbose | snazzy",
"prepublish": "npm run clean && npm run check && npm run build",
"prepublish": "npm run clean && npm test && npm run build",
"print-schema": "babel-node scripts/print-schema.js",
"print-schema:json": "npm run print-schema -- --json",
"print-schema:md": "printf '```graphql\\n%s\\n```' \"$(npm run -s print-schema)\"",
"start": "node lib/index.js",
"start:dev": "nodemon --exec babel-node src/index.js",
"test": "mocha --compilers js:babel-register",
"test": "npm run lint && npm run test:coverage",
"test:coverage": "cross-env NODE_ENV=test nyc npm run test:only",
"test:only": "cross-env VCR_MODE=cache ava",
"test:record": "rimraf test/fixtures && npm run test:only -- --serial",
"test:watch": "npm run test:only -- --watch",
"update-schema": "npm run -s print-schema:json > schema.json"
},
"keywords": [
@ -56,6 +59,7 @@
},
"license": "MIT",
"dependencies": {
"babel-runtime": "^6.18.0",
"chalk": "^1.1.3",
"compression": "^1.6.2",
"dashify": "^0.2.2",
@ -74,20 +78,44 @@
"retry": "^0.10.0"
},
"devDependencies": {
"ava": "^0.17.0",
"babel-cli": "^6.18.0",
"babel-eslint": "^7.1.1",
"babel-plugin-istanbul": "^3.0.0",
"babel-plugin-transform-runtime": "^6.15.0",
"babel-preset-es2015": "^6.18.0",
"babel-preset-stage-2": "^6.18.0",
"babel-register": "^6.18.0",
"chai": "^3.5.0",
"coveralls": "^2.11.15",
"cross-env": "^3.1.3",
"doctoc": "^1.2.0",
"marked": "^0.3.6",
"mocha": "^3.2.0",
"nodemon": "^1.11.0",
"nyc": "^10.0.0",
"rimraf": "^2.5.4",
"sepia": "^2.0.2",
"snazzy": "^5.0.0",
"standard": "^8.6.0"
},
"standard": {
"parser": "babel-eslint"
},
"ava": {
"require": [
"babel-register"
]
},
"nyc": {
"include": [
"src/**"
],
"reporter": [
"lcov",
"text"
],
"all": true,
"cache": true,
"sourceMap": false,
"instrument": false
}
}

View file

@ -34,6 +34,7 @@ export default class MusicBrainz {
baseURL = process.env.MUSICBRAINZ_BASE_URL || 'http://musicbrainz.org/ws/2/',
userAgent = `${pkg.name}/${pkg.version} ` +
`( ${pkg.homepage || pkg.author.url || pkg.author.email} )`,
extraHeaders = {},
timeout = 60000,
// MusicBrainz API requests are limited to an *average* of 1 req/sec.
// That means if, for example, we only need to make a few API requests to
@ -56,6 +57,7 @@ export default class MusicBrainz {
} = {}) {
this.baseURL = baseURL
this.userAgent = userAgent
this.extraHeaders = extraHeaders
this.timeout = timeout
this.limiter = new RateLimit({ limit, period, concurrency })
this.retryOptions = {
@ -90,7 +92,7 @@ export default class MusicBrainz {
qs: { ...params, fmt: 'json' },
json: true,
gzip: true,
headers: { 'User-Agent': this.userAgent },
headers: { 'User-Agent': this.userAgent, ...this.extraHeaders },
timeout: this.timeout
}
@ -132,19 +134,19 @@ export default class MusicBrainz {
}
stringifyParams (params) {
if (typeof params.inc === 'object') {
if (Array.isArray(params.inc)) {
params = {
...params,
inc: params.inc.join('+')
}
}
if (typeof params.type === 'object') {
if (Array.isArray(params.type)) {
params = {
...params,
type: params.type.join('|')
}
}
if (typeof params.status === 'object') {
if (Array.isArray(params.status)) {
params = {
...params,
status: params.status.join('|')

39
test/api.js Normal file
View file

@ -0,0 +1,39 @@
import path from 'path'
import test from 'ava'
import sepia from 'sepia'
import MusicBrainz from '../src/api'
sepia.fixtureDir(path.join(__dirname, 'fixtures'))
test.beforeEach(t => {
t.context.client = new MusicBrainz()
})
test('getLookupURL() generates a lookup URL', t => {
const { client } = t.context
t.is(client.getLookupURL('artist', 'c8da2e40-bd28-4d4e-813a-bd2f51958ba8', {
inc: ['recordings', 'release-groups']
}), 'artist/c8da2e40-bd28-4d4e-813a-bd2f51958ba8?inc=recordings%2Brelease-groups')
})
test('getBrowseURL() generates a browse URL', t => {
const { client } = t.context
t.is(client.getBrowseURL('recording', {
artist: 'c8da2e40-bd28-4d4e-813a-bd2f51958ba8',
limit: null,
offset: 0
}), 'recording?artist=c8da2e40-bd28-4d4e-813a-bd2f51958ba8&offset=0')
})
test('getSearchURL() generates a search URL', t => {
const { client } = t.context
t.is(client.getSearchURL('artist', 'Lures', { inc: null }), 'artist?query=Lures')
})
test('lookup() sends a lookup query', t => {
const { client } = t.context
return client.lookup('artist', 'c8da2e40-bd28-4d4e-813a-bd2f51958ba8').then(response => {
t.is(response.id, 'c8da2e40-bd28-4d4e-813a-bd2f51958ba8')
t.is(response.type, 'Group')
})
})

Binary file not shown.

View file

@ -0,0 +1,29 @@
{
"statusCode": 200,
"headers": {
"date": "Wed, 07 Dec 2016 07:23:58 GMT",
"content-type": "application/json; charset=utf-8",
"transfer-encoding": "chunked",
"connection": "keep-alive",
"keep-alive": "timeout=15",
"vary": "Accept-Encoding",
"x-ratelimit-limit": "700",
"x-ratelimit-remaining": "493",
"x-ratelimit-reset": "1481095439",
"server": "Plack::Handler::Starlet",
"etag": "W/\"4401734fbc36b86339b05ac714ef9b07\"",
"access-control-allow-origin": "*",
"content-encoding": "gzip"
},
"url": "http://musicbrainz.org:80/ws/2/artist/c8da2e40-bd28-4d4e-813a-bd2f51958ba8?fmt=json",
"time": 402,
"request": {
"method": "GET",
"headers": {
"User-Agent": "graphbrainz/3.0.1 ( https://github.com/exogen/graphbrainz )",
"host": "musicbrainz.org",
"accept-encoding": "gzip, deflate",
"accept": "application/json"
}
}
}

15
test/rate-limit.js Normal file
View file

@ -0,0 +1,15 @@
import test from 'ava'
import RateLimit from '../src/rate-limit'
test('defaults to 1 request per second', t => {
const limiter = new RateLimit()
t.is(limiter.limit, 1)
t.is(limiter.period, 1000)
})
test('concurrency defaults to limit', t => {
let limiter = new RateLimit()
t.is(limiter.concurrency, 1)
limiter = new RateLimit({ limit: 5 })
t.is(limiter.concurrency, 5)
})

View file

@ -1,18 +0,0 @@
/* global describe, it */
import { expect } from 'chai'
import RateLimit from '../src/rate-limit'
describe('RateLimit', () => {
it('defaults to 1 request per second', () => {
const limiter = new RateLimit()
expect(limiter.limit).to.equal(1)
expect(limiter.period).to.equal(1000)
})
it('concurrency defaults to limit', () => {
let limiter = new RateLimit()
expect(limiter.concurrency).to.equal(1)
limiter = new RateLimit({ limit: 5 })
expect(limiter.concurrency).to.equal(5)
})
})

42
test/schema.js Normal file
View file

@ -0,0 +1,42 @@
import path from 'path'
import test from 'ava'
import sepia from 'sepia'
import { graphql } from 'graphql'
import MusicBrainz from '../src/api'
import schema from '../src/schema'
import createLoaders from '../src/loaders'
sepia.fixtureDir(path.join(__dirname, 'fixtures'))
test.beforeEach(t => {
const client = new MusicBrainz()
const loaders = createLoaders(client)
t.context = { client, loaders }
})
test('schema has a lookup query', t => {
const query = `
{
lookup {
artist (mbid: "c8da2e40-bd28-4d4e-813a-bd2f51958ba8") {
mbid
name
type
}
}
}
`
return graphql(schema, query, null, t.context).then(result => {
t.deepEqual(result, {
data: {
lookup: {
artist: {
mbid: 'c8da2e40-bd28-4d4e-813a-bd2f51958ba8',
name: 'Lures',
type: 'Group'
}
}
}
})
})
})

1627
yarn.lock

File diff suppressed because it is too large Load diff