Skip to content

Commit

Permalink
entity-renderer: define basic tests using mocha
Browse files Browse the repository at this point in the history
  • Loading branch information
ludovicm67 committed Nov 14, 2023
1 parent 0ef50be commit 313a9fe
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 37 deletions.
2 changes: 2 additions & 0 deletions packages/entity-renderer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,5 @@ middlewares:
```sh
npm run example-instance
```

And go to http://localhost:3000/ to see the result.
11 changes: 0 additions & 11 deletions packages/entity-renderer/examples/config/trifid.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: /
Expand Down
90 changes: 90 additions & 0 deletions packages/entity-renderer/examples/data/people.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
@prefix schema: <http://schema.org/> .
@prefix adams: <http://localhost:3000/adams/> .
@prefix ex: <http://localhost:3000/person/> .
@prefix ex0: <http://0.0.0.0:3000/person/> .

adams:eudora foaf:name "Eudora Addams" ;
adams:hasPartner adams:gomez ;
Expand Down Expand Up @@ -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" ;
<http://localhost:3000/data/nickName> "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 .

23 changes: 23 additions & 0 deletions packages/entity-renderer/examples/instance.js
Original file line number Diff line number Diff line change
@@ -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,
},
})
}
29 changes: 3 additions & 26 deletions packages/entity-renderer/examples/run-instance.js
Original file line number Diff line number Diff line change
@@ -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()
51 changes: 51 additions & 0 deletions packages/entity-renderer/test/entity-renderer.test.js
Original file line number Diff line number Diff line change
@@ -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()
}
})
})
})
20 changes: 20 additions & 0 deletions packages/entity-renderer/test/support/utils.js
Original file line number Diff line number Diff line change
@@ -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}`
}

0 comments on commit 313a9fe

Please sign in to comment.