From 313a9fe160a641fefd1f0a7d62e9707815fe2795 Mon Sep 17 00:00:00 2001 From: Ludovic Muller Date: Tue, 14 Nov 2023 14:37:07 +0100 Subject: [PATCH] entity-renderer: define basic tests using mocha --- packages/entity-renderer/README.md | 2 + .../examples/config/trifid.yaml | 11 --- .../entity-renderer/examples/data/people.ttl | 90 +++++++++++++++++++ packages/entity-renderer/examples/instance.js | 23 +++++ .../entity-renderer/examples/run-instance.js | 29 +----- .../test/entity-renderer.test.js | 51 +++++++++++ .../entity-renderer/test/support/utils.js | 20 +++++ 7 files changed, 189 insertions(+), 37 deletions(-) create mode 100644 packages/entity-renderer/examples/instance.js create mode 100644 packages/entity-renderer/test/entity-renderer.test.js create mode 100644 packages/entity-renderer/test/support/utils.js diff --git a/packages/entity-renderer/README.md b/packages/entity-renderer/README.md index 19925980..00a09a93 100644 --- a/packages/entity-renderer/README.md +++ b/packages/entity-renderer/README.md @@ -56,3 +56,5 @@ middlewares: ```sh npm run example-instance ``` + +And go to http://localhost:3000/ to see the result. diff --git a/packages/entity-renderer/examples/config/trifid.yaml b/packages/entity-renderer/examples/config/trifid.yaml index 3c5d6cd2..927d4e41 100644 --- a/packages/entity-renderer/examples/config/trifid.yaml +++ b/packages/entity-renderer/examples/config/trifid.yaml @@ -2,17 +2,6 @@ server: logLevel: debug middlewares: - entity-renderer: - paths: / - module: file:../../index.js - config: - path: file:../../views/render.hbs - # labelLoader: - # endpointUrl: https://ld.zazuko.com/query - # chunkSize: 30 - # concurrency: 2 - # timeout: 1000 - entity-host-web-page: module: trifid-core/middlewares/view.js paths: / diff --git a/packages/entity-renderer/examples/data/people.ttl b/packages/entity-renderer/examples/data/people.ttl index 59e84de6..71d3e6e2 100644 --- a/packages/entity-renderer/examples/data/people.ttl +++ b/packages/entity-renderer/examples/data/people.ttl @@ -4,6 +4,7 @@ @prefix schema: . @prefix adams: . @prefix ex: . +@prefix ex0: . adams:eudora foaf:name "Eudora Addams" ; adams:hasPartner adams:gomez ; @@ -163,3 +164,92 @@ ex:stuart-bloom a schema:Person ; schema:jobTitle "comic book store owner" ; schema:knows ex:amy-farrah-fowler, ex:bernadette-rostenkowski, ex:howard-wolowitz, ex:leonard-hofstadter, ex:penny, ex:rajesh-koothrappali, ex:sheldon-cooper . +ex0:amy-farrah-fowler a schema:Person ; + schema:additionalName "Farrah" ; + "Farrah" ; + schema:familyName "Fowler" ; + schema:name "Amy" ; + schema:givenName "Amy" ; + schema:jobTitle "neurobiologist" ; + schema:knows ex0:bernadette-rostenkowski, ex0:howard-wolowitz, ex0:leonard-hofstadter, ex0:penny, ex0:rajesh-koothrappali, ex0:sheldon-cooper, ex0:stuart-bloom . + +ex0:bernadette-rostenkowski a schema:Person ; + schema:additionalName "Maryann" ; + schema:familyName "Rostenkowski-Wolowitz" ; + schema:givenName "Bernadette" ; + schema:jobTitle "microbiologist" ; + schema:knows ex0:amy-farrah-fowler, ex0:howard-wolowitz, ex0:leonard-hofstadter, ex0:penny, ex0:rajesh-koothrappali, ex0:sheldon-cooper, ex0:stuart-bloom ; + schema:spouse ex0:howard-wolowitz . + +ex0:howard-wolowitz a schema:Person ; + schema:additionalName "Joel" ; + schema:familyName "Wolowitz" ; + schema:givenName "Howard" ; + schema:jobTitle "aerospace engineer" ; + schema:knows ex0:amy-farrah-fowler, ex0:bernadette-rostenkowski, ex0:leonard-hofstadter, ex0:penny, ex0:rajesh-koothrappali, ex0:sheldon-cooper, ex0:stuart-bloom ; + schema:spouse ex0:bernadette-rostenkowski . + +ex0:leonard-hofstadter a schema:Person ; + schema:additionalName "Leakey" ; + schema:address [ + schema:addressCountry "US" ; + schema:addressLocality "Pasadena" ; + schema:addressRegion "CA" ; + schema:postalCode "91104" ; + schema:streetAddress "2311 North Los Robles Avenue, Apartment 4A" ; + ] ; + schema:familyName "Hofstadter" ; + schema:givenName "Leonard" ; + schema:jobTitle "experimental physicist" ; + schema:knows ex0:amy-farrah-fowler, ex0:bernadette-rostenkowski, ex0:howard-wolowitz, ex0:mary-cooper, ex0:penny, ex0:rajesh-koothrappali, ex0:sheldon-cooper, ex0:stuart-bloom . + +ex0:mary-cooper a schema:Person ; + schema:address [ + schema:addressCountry "US" ; + schema:addressRegion "TX" ; + ] ; + schema:children ex0:sheldon-cooper ; + schema:familyName "Cooper" ; + schema:givenName "Mary" ; + schema:knows ex0:howard-wolowitz, ex0:penny, ex0:rajesh-koothrappali, ex0:sheldon-cooper . + +ex0:penny a schema:Person ; + schema:address [ + schema:addressCountry "US" ; + schema:addressLocality "Pasadena" ; + schema:addressRegion "CA" ; + schema:postalCode "91104" ; + schema:streetAddress "2311 North Los Robles Avenue, Apartment 4B" ; + ] ; + schema:givenName "Penny" ; + schema:jobTitle "pharmaceutical sales representative" ; + schema:knows ex0:amy-farrah-fowler, ex0:bernadette-rostenkowski, ex0:howard-wolowitz, ex0:leonard-hofstadter, ex0:rajesh-koothrappali, ex0:sheldon-cooper, ex0:stuart-bloom . + +ex0:rajesh-koothrappali a schema:Person ; + schema:additionalName "Ramayan Raj" ; + schema:familyName "Koothrappali" ; + schema:givenName "Rajesh" ; + schema:jobTitle "astrophysicist" ; + schema:knows ex0:amy-farrah-fowler, ex0:bernadette-rostenkowski, ex0:howard-wolowitz, ex0:leonard-hofstadter, ex0:penny, ex0:sheldon-cooper, ex0:stuart-bloom . + +ex0:sheldon-cooper a schema:Person ; + schema:additionalName "Lee" ; + schema:address [ + schema:addressCountry "US" ; + schema:addressLocality "Pasadena" ; + schema:addressRegion "CA" ; + schema:postalCode "91104" ; + schema:streetAddress "2311 North Los Robles Avenue, Apartment 4A" ; + ] ; + schema:familyName "Cooper" ; + schema:givenName "Sheldon" ; + schema:jobTitle "theoretical physicist" ; + schema:knows ex0:amy-farrah-fowler, ex0:bernadette-rostenkowski, ex0:howard-wolowitz, ex0:leonard-hofstadter, ex0:penny, ex0:rajesh-koothrappali, ex0:stuart-bloom ; + schema:parent ex0:mary-cooper . + +ex0:stuart-bloom a schema:Person ; + schema:familyName "Bloom" ; + schema:givenName "Stuart" ; + schema:jobTitle "comic book store owner" ; + schema:knows ex0:amy-farrah-fowler, ex0:bernadette-rostenkowski, ex0:howard-wolowitz, ex0:leonard-hofstadter, ex0:penny, ex0:rajesh-koothrappali, ex0:sheldon-cooper . + diff --git a/packages/entity-renderer/examples/instance.js b/packages/entity-renderer/examples/instance.js new file mode 100644 index 00000000..d2a14b2d --- /dev/null +++ b/packages/entity-renderer/examples/instance.js @@ -0,0 +1,23 @@ +import { join } from 'path' +import trifid from 'trifid-core' +import entityRendererTrifidPlugin from '../index.js' + +const port = 3000 + +export const createTrifidInstance = async (configFilePath, logLevel = 'debug') => { + const configFile = join(process.cwd(), configFilePath) + return await trifid({ + extends: [configFile], + server: { + logLevel, + listener: { + port, + host: '0.0.0.0', + }, + }, + }, { + entityRenderer: { + module: entityRendererTrifidPlugin, + }, + }) +} diff --git a/packages/entity-renderer/examples/run-instance.js b/packages/entity-renderer/examples/run-instance.js index fc0376b4..415dca79 100644 --- a/packages/entity-renderer/examples/run-instance.js +++ b/packages/entity-renderer/examples/run-instance.js @@ -1,30 +1,7 @@ #!/usr/bin/env node -/* eslint-disable no-console */ +import { createTrifidInstance } from './instance.js' -import { join } from 'path' -import express from 'express' +const trifidInstance = await createTrifidInstance('examples/config/trifid.yaml', 'debug') -import trifid from 'trifid-core' - -async function createTrifidInstance (filePath) { - const configFile = join(process.cwd(), filePath) - const config = { - extends: [configFile], - server: { - listener: {}, - }, - } - return await trifid(config) -} - -const test = await createTrifidInstance('examples/config/trifid.yaml') - -const app = express() // The main app -const PORT = 3000 - -app.use('/', test.server) -app.listen(PORT, function (err) { - if (err) console.log(err) - console.log('Server listening on PORT', PORT) -}) +await trifidInstance.start() diff --git a/packages/entity-renderer/test/entity-renderer.test.js b/packages/entity-renderer/test/entity-renderer.test.js new file mode 100644 index 00000000..7d54eac9 --- /dev/null +++ b/packages/entity-renderer/test/entity-renderer.test.js @@ -0,0 +1,51 @@ +// @ts-check + +/* eslint-disable no-useless-catch */ + +import { strictEqual } from 'assert' +import { describe, it } from 'mocha' + +import { createTrifidInstance } from '../examples/instance.js' +import { getListenerURL } from './support/utils.js' + +const trifidConfigUrl = './examples/config/trifid.yaml' + +describe('@zazuko/trifid-plugin-ckan', () => { + describe('basic tests', () => { + it('should create a middleware with factory and default options', async () => { + const trifidInstance = await createTrifidInstance(trifidConfigUrl, 'warn') + const trifidListener = await trifidInstance.start() + trifidListener.close() + }) + + it('should be able to load a rendered entity', async () => { + const trifidInstance = await createTrifidInstance(trifidConfigUrl, 'warn') + const trifidListener = await trifidInstance.start() + + try { + const entityUrl = `${getListenerURL(trifidListener)}/person/amy-farrah-fowler` + const res = await fetch(entityUrl) + strictEqual(res.status, 200) + } catch (e) { + throw e + } finally { + trifidListener.close() + } + }) + + it('should not render non-existant entity', async () => { + const trifidInstance = await createTrifidInstance(trifidConfigUrl, 'warn') + const trifidListener = await trifidInstance.start() + + try { + const entityUrl = `${getListenerURL(trifidListener)}/person/someone-that-does-not-exist` + const res = await fetch(entityUrl) + strictEqual(res.status, 404) + } catch (e) { + throw e + } finally { + trifidListener.close() + } + }) + }) +}) diff --git a/packages/entity-renderer/test/support/utils.js b/packages/entity-renderer/test/support/utils.js new file mode 100644 index 00000000..372e1cbb --- /dev/null +++ b/packages/entity-renderer/test/support/utils.js @@ -0,0 +1,20 @@ +// @ts-check + +/** + * Get the URL of a listener. + * + * @param {import('http').Server} listener HTTP listener + * @returns {string} + */ +export const getListenerURL = (listener) => { + const address = listener.address() + if (!address) { + throw new Error('The listener is not listening') + } + if (typeof address === 'string') { + return address + } + + const { address: hostname, port } = address + return `http://${hostname}:${port}` +}