better tests for /stations & /stations?query

This commit is contained in:
Jannis R 2022-11-22 16:05:11 +01:00
parent 6bca0d4f59
commit f982e152c5
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
2 changed files with 35 additions and 4 deletions

View file

@ -39,6 +39,7 @@
"axios": "^0.26.1", "axios": "^0.26.1",
"eslint": "^8.12.0", "eslint": "^8.12.0",
"get-port": "^5.1.1", "get-port": "^5.1.1",
"ndjson": "^2.0.0",
"pino-pretty": "^4.0.0", "pino-pretty": "^4.0.0",
"tap-min": "^2.0.0", "tap-min": "^2.0.0",
"tape": "^5.5.2" "tape": "^5.5.2"

View file

@ -1,8 +1,10 @@
'use strict' 'use strict'
const tape = require('tape') const tape = require('tape')
const {parse: ndjsonParser} = require('ndjson')
const {loyaltyCards} = require('../lib/loyalty-cards') const {loyaltyCards} = require('../lib/loyalty-cards')
const {fetchWithTestApi} = require('./util') const {fetchWithTestApi} = require('./util')
const pAllStations = require('../lib/db-stations')
const NO_JOURNEYS = { const NO_JOURNEYS = {
// todo? // todo?
@ -41,41 +43,69 @@ tape.test('/journeys?loyaltyCard works', async (t) => {
}) })
tape.test('/stations works', async (t) => { tape.test('/stations works', async (t) => {
const {data: allStations} = await pAllStations
const someStationId = Object.keys(allStations)[0]
{ {
const {headers} = await fetchWithTestApi({}, {}, '/stations', { const {headers, data} = await fetchWithTestApi({}, {}, '/stations', {
headers: { headers: {
'accept': 'application/json', 'accept': 'application/json',
}, },
}) })
t.equal(headers['content-type'], 'application/json') t.equal(headers['content-type'], 'application/json')
t.equal(typeof data, 'object')
t.ok(data)
t.ok(data[someStationId])
t.equal(Object.keys(data).length, Object.keys(allStations).length)
} }
{ {
const {headers} = await fetchWithTestApi({}, {}, '/stations', { const {headers, data} = await fetchWithTestApi({}, {}, '/stations', {
headers: { headers: {
'accept': 'application/x-ndjson', 'accept': 'application/x-ndjson',
}, },
}) })
t.equal(headers['content-type'], 'application/x-ndjson') t.equal(headers['content-type'], 'application/x-ndjson')
let nrOfStations = 0
const parser = ndjsonParser()
parser.end(data)
for await (const station of parser) nrOfStations++
t.equal(nrOfStations, Object.keys(allStations).length)
} }
}) })
tape.test('/stations?query=frankf works', async (t) => { tape.test('/stations?query=frankf works', async (t) => {
const FRANKFURT_MAIN_HBF = '8000105'
{ {
const {headers} = await fetchWithTestApi({}, {}, '/stations?query=frankf', { const {headers, data} = await fetchWithTestApi({}, {}, '/stations?query=frankf', {
headers: { headers: {
'accept': 'application/json', 'accept': 'application/json',
}, },
}) })
t.equal(headers['content-type'], 'application/json') t.equal(headers['content-type'], 'application/json')
t.equal(typeof data, 'object')
t.ok(data)
t.ok(data[FRANKFURT_MAIN_HBF])
t.ok(Object.keys(data).length > 0)
} }
{ {
const {headers} = await fetchWithTestApi({}, {}, '/stations?query=frankf', { const {headers, data} = await fetchWithTestApi({}, {}, '/stations?query=frankf', {
headers: { headers: {
'accept': 'application/x-ndjson', 'accept': 'application/x-ndjson',
}, },
}) })
t.equal(headers['content-type'], 'application/x-ndjson') t.equal(headers['content-type'], 'application/x-ndjson')
const stations = []
const parser = ndjsonParser()
parser.end(data)
for await (const station of parser) stations.push(station)
t.ok(stations.find(s => s.id === FRANKFURT_MAIN_HBF))
t.ok(Object.keys(stations).length > 0)
} }
}) })