From f982e152c52c2356752b8bf4e566fcc25afe43a1 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Tue, 22 Nov 2022 16:05:11 +0100 Subject: [PATCH] =?UTF-8?q?better=20tests=20for=20/stations=20&=20/station?= =?UTF-8?q?s=3Fquery=20=E2=9C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + test/index.js | 38 ++++++++++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index c434dfa..15581b2 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "axios": "^0.26.1", "eslint": "^8.12.0", "get-port": "^5.1.1", + "ndjson": "^2.0.0", "pino-pretty": "^4.0.0", "tap-min": "^2.0.0", "tape": "^5.5.2" diff --git a/test/index.js b/test/index.js index 718dea9..4ef1e52 100644 --- a/test/index.js +++ b/test/index.js @@ -1,8 +1,10 @@ 'use strict' const tape = require('tape') +const {parse: ndjsonParser} = require('ndjson') const {loyaltyCards} = require('../lib/loyalty-cards') const {fetchWithTestApi} = require('./util') +const pAllStations = require('../lib/db-stations') const NO_JOURNEYS = { // todo? @@ -41,41 +43,69 @@ tape.test('/journeys?loyaltyCard 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: { 'accept': '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: { 'accept': '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) => { + const FRANKFURT_MAIN_HBF = '8000105' + { - const {headers} = await fetchWithTestApi({}, {}, '/stations?query=frankf', { + const {headers, data} = await fetchWithTestApi({}, {}, '/stations?query=frankf', { headers: { 'accept': '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: { 'accept': '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) } })