Skip to content

Commit

Permalink
add: compiler tests (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
flower-of-the-bridges authored Aug 26, 2024
1 parent eeac8bd commit 6c51b06
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 8 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
},
"peerDependencies": {
"ajv": "^8.17.1",
"fast-json-stringify": "^6.0.0"
"fast-json-stringify": "^6.0.0",
"fastify": "^4.28.1"
},
"devDependencies": {
"@eslint/js": "^9.9.0",
Expand Down
77 changes: 77 additions & 0 deletions tests/compilers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2024 Mia s.r.l.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict'

const tap = require('tap')
const Fastify = require('fastify')
const { addValidatorCompiler } = require('../lib/compilers')
const books = require('./collectionDefinitions/books')
const loadModels = require('../lib/loadModels')
const { getMongoDatabaseName, getMongoURL } = require('./utils')
const { registerMongoInstances } = require('../lib/mongo/mongo-plugin')
const { SCHEMAS_ID } = require('../lib/schemaGetters')


const SCHEMA_CUSTOM_KEYWORDS = {
UNIQUE_OPERATION_ID: 'operationId',
}

tap.test('compilers', async(t) => {
const databaseName = getMongoDatabaseName()

t.test('validator compiler should extract property correctly without collisions', async t => {
const fastify = Fastify()

t.teardown(async() => fastify.close())

fastify.decorate('config', { MONGODB_URL: getMongoURL(databaseName) })
fastify.decorate('collections', [books])
fastify.decorate('modelName', 'books-endpoint')
await registerMongoInstances(fastify)


await loadModels(fastify)

//* here i add a fake model to test collision
// eslint-disable-next-line require-atomic-updates
fastify.models['books-endpoint-2'] = {}

addValidatorCompiler(fastify, fastify.models, { HELPERS_PREFIX: '/helpers' })

const schema = {
[SCHEMA_CUSTOM_KEYWORDS.UNIQUE_OPERATION_ID]: `books__MIA__${SCHEMAS_ID.POST_ITEM}__MIA__response.200`,
type: 'object',
properties: {
id: {
type: 'string',
},
},
required: ['id'],
}

const url = '/test-url'

const validationFunc = fastify.validatorCompiler({ schema, url })

t.ok(validationFunc, 'Validator function should be created')
t.equal(typeof validationFunc, 'function', 'Validator function should be a function')
//* here we test that post schema from model is recognized correctly.
//* we pass an empty object for ok validation and a wrong formatted _id for ko validation
t.ok(validationFunc({}), 'Validation should return true with invalid obj')
t.notOk(validationFunc({ _id: 'a' }), 'Validation should return false with invalid obj')
})
})

0 comments on commit 6c51b06

Please sign in to comment.