Skip to content

Commit

Permalink
Using common code in BackwardWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilianoBruni committed Feb 12, 2024
1 parent 15cf11b commit 149a895
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 480 deletions.
76 changes: 14 additions & 62 deletions test/nested_paths.test.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,18 @@
'use strict';

const fastifyMongooseAPI = require('../fastify-mongoose-api.js');

const t = require('tap');
const { test } = t;

const Fastify = require('fastify');
const mongoose = require('mongoose');

const MONGODB_URL =
process.env.DATABASE_URI || 'mongodb://127.0.0.1/fastifymongooseapitest';

const BackwardWrapper = require('./BackwardWrapper.js');

let mongooseConnection = null;
let fastify = null;
const bw = new BackwardWrapper(t);

test('mongoose db initialization', async t => {
t.plan(2);

mongooseConnection = await BackwardWrapper.createConnection(MONGODB_URL);
t.ok(mongooseConnection);
t.equal(mongooseConnection.readyState, 1, 'Ready state is connected(==1)'); /// connected
test('mongoose db initialization', async () => {
await bw.createConnection();
});

test('schema initialization', async t => {
// t.plan(2);

// const biographyEpochSchema = mongoose.Schema({ title: String, year: Number });

const authorSchema = mongoose.Schema({
firstName: String,
lastName: String,
Expand All @@ -39,45 +23,38 @@ test('schema initialization', async t => {
}
});

mongooseConnection.model('Author', authorSchema);
bw.conn.model('Author', authorSchema);

t.ok(mongooseConnection.models.Author);
t.ok(bw.conn.models.Author);
});

test('clean up test collections', async () => {
await mongooseConnection.models.Author.deleteMany({}).exec();
await bw.conn.models.Author.deleteMany({}).exec();
});

test('initialization of API server', async t => {
///// setting up the server
fastify = Fastify();

fastify.register(fastifyMongooseAPI, {
models: mongooseConnection.models,
await bw.createServer({
models: bw.conn.models,
prefix: '/api/',
setDefaults: true,
methods: ['list', 'get', 'post', 'patch', 'put', 'delete', 'options']
});

await fastify.ready();

t.ok(fastify.mongooseAPI, 'mongooseAPI decorator is available');

t.equal(
fastify.mongooseAPI.apiRouters.Author.collectionName,
bw.fastify.mongooseAPI.apiRouters.Author.collectionName,
'authors',
'Collection name used in API path'
);
t.equal(
fastify.mongooseAPI.apiRouters.Author.path,
bw.fastify.mongooseAPI.apiRouters.Author.path,
'/api/authors',
'API path is composed with prefix + collectionName'
);
});

test('POST item test', async t => {
let response = null;
response = await fastify.inject({
response = await bw.inject(t, {
method: 'POST',
url: '/api/authors',
payload: {
Expand All @@ -88,13 +65,6 @@ test('POST item test', async t => {
}
});

t.equal(response.statusCode, 200, 'POST api ok');
t.equal(
response.headers['content-type'],
'application/json; charset=utf-8',
'Content-Type is correct'
);

t.match(
JSON.parse(response.payload),
{
Expand All @@ -115,18 +85,11 @@ test('POST item test', async t => {
'POST api ok'
);

response = await fastify.inject({
response = await bw.inject(t, {
method: 'GET',
url: '/api/authors'
});

t.equal(response.statusCode, 200, 'GET api ok');
t.equal(
response.headers['content-type'],
'application/json; charset=utf-8',
'Content-Type is correct'
);

t.match(
response.json().items[0],
{
Expand All @@ -140,23 +103,17 @@ test('POST item test', async t => {
});

test('PUT item test', async t => {
let authorFromDb = await mongooseConnection.models.Author.findOne({
let authorFromDb = await bw.conn.models.Author.findOne({
firstName: 'Hutin'
});
// await BackwardWrapper.populateDoc(bookFromDb.populate('author'));

const response = await fastify.inject({
const response = await bw.inject(t, {
method: 'PUT',
url: '/api/authors/' + authorFromDb.id,
payload: { lastName: 'Chuvachello', 'biography.born': 1961 }
});

t.equal(response.statusCode, 200);
t.equal(
response.headers['content-type'],
'application/json; charset=utf-8'
);

t.match(
response.json(),
{
Expand All @@ -167,8 +124,3 @@ test('PUT item test', async t => {
'PUT api ok'
);
});

test('teardown', async () => {
await fastify.close();
await mongooseConnection.close();
});
84 changes: 23 additions & 61 deletions test/prefix.test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
'use strict';

const fastifyMongooseAPI = require('../fastify-mongoose-api.js');

const t = require('tap');
const { test } = t;

const Fastify = require('fastify');
const mongoose = require('mongoose');

const MONGODB_URL =
process.env.DATABASE_URI || 'mongodb://127.0.0.1/fastifymongooseapitest';

const BackwardWrapper = require('./BackwardWrapper.js');

let mongooseConnection = null;
let fastify = null;

test('mongoose db initialization', async t => {
t.plan(2);
const bw = new BackwardWrapper(t);

mongooseConnection = await BackwardWrapper.createConnection(MONGODB_URL);
t.ok(mongooseConnection);
t.equal(mongooseConnection.readyState, 1, 'Ready state is connected(==1)'); /// connected
test('mongoose db initialization', async () => {
await bw.createConnection();
});

test('schema initialization', async t => {
Expand All @@ -42,7 +30,7 @@ test('schema initialization', async t => {
biography: 'text'
}); /// you can use wildcard here too: https://stackoverflow.com/a/28775709/1119169

mongooseConnection.model('Author', authorSchema);
bw.conn.model('Author', authorSchema);

const bookSchema = mongoose.Schema({
title: String,
Expand All @@ -57,123 +45,97 @@ test('schema initialization', async t => {
}
});

mongooseConnection.model('Book', bookSchema);
bw.conn.model('Book', bookSchema);

t.ok(mongooseConnection.models.Author);
t.ok(mongooseConnection.models.Book);
t.ok(bw.conn.models.Author);
t.ok(bw.conn.models.Book);
});

test('clean up test collections', async () => {
await mongooseConnection.models.Author.deleteMany({}).exec();
await mongooseConnection.models.Book.deleteMany({}).exec();
await bw.conn.models.Author.deleteMany({}).exec();
await bw.conn.models.Book.deleteMany({}).exec();
});

test('schema ok', async t => {
let author = new mongooseConnection.models.Author();
let author = new bw.conn.models.Author();
author.firstName = 'Jay';
author.lastName = 'Kay';
author.biography = 'Lived. Died.';

await author.save();

let book = new mongooseConnection.models.Book();
let book = new bw.conn.models.Book();
book.title = 'The best book';
book.isbn = 'The best isbn';
book.author = author;

await book.save();

let authorFromDb = await mongooseConnection.models.Author.findOne({
let authorFromDb = await bw.conn.models.Author.findOne({
firstName: 'Jay'
});
let bookFromDb = await mongooseConnection.models.Book.findOne({
let bookFromDb = await bw.conn.models.Book.findOne({
title: 'The best book'
});

t.ok(authorFromDb);
t.ok(bookFromDb);

await BackwardWrapper.populateDoc(bookFromDb.populate('author'));
await bw.populateDoc(bookFromDb.populate('author'));
// await bookFromDb.populate('author').execPopulate();

t.equal('' + bookFromDb.author._id, '' + authorFromDb._id);
});

test('initialization of API server', async t => {
///// setting up the server
fastify = Fastify();

fastify.register(fastifyMongooseAPI, {
models: mongooseConnection.models,
await bw.createServer({
models: bw.conn.models,
prefix: '/someroute/',
setDefaults: true,
methods: ['list', 'get', 'post', 'patch', 'put', 'delete', 'options']
});

await fastify.ready();

t.ok(fastify.mongooseAPI, 'mongooseAPI decorator is available');
t.equal(
Object.keys(fastify.mongooseAPI.apiRouters).length,
Object.keys(bw.fastify.mongooseAPI.apiRouters).length,
2,
'There are 2 APIRoutes, one for each model'
);

t.equal(
fastify.mongooseAPI.apiRouters.Author.collectionName,
bw.fastify.mongooseAPI.apiRouters.Author.collectionName,
'authors',
'Collection name used in API path'
);
t.equal(
fastify.mongooseAPI.apiRouters.Book.collectionName,
bw.fastify.mongooseAPI.apiRouters.Book.collectionName,
'books',
'Collection name used in API path'
);

t.equal(
fastify.mongooseAPI.apiRouters.Author.path,
bw.fastify.mongooseAPI.apiRouters.Author.path,
'/someroute/authors',
'API path is composed with prefix + collectionName'
);
t.equal(
fastify.mongooseAPI.apiRouters.Book.path,
bw.fastify.mongooseAPI.apiRouters.Book.path,
'/someroute/books',
'API path is composed with prefix + collectionName'
);
});

test('GET collection endpoints', async t => {
let response = null;
response = await fastify.inject({
response = await bw.inject(t, {
method: 'GET',
url: '/someroute/books'
});

t.equal(response.statusCode, 200, 'API returns 200 status code');
t.equal(
response.headers['content-type'],
'application/json; charset=utf-8',
'API returns correct content type'
);

t.equal(response.json().total, 1, 'API returns 1 book');

response = await fastify.inject({
response = await bw.inject(t, {
method: 'GET',
url: '/someroute/authors'
});

t.equal(response.statusCode, 200, 'API returns 200 status code');
t.equal(
response.headers['content-type'],
'application/json; charset=utf-8',
'API returns correct content type'
);

t.equal(response.json().total, 1, 'API returns 1 author');
});

test('teardown', async () => {
await fastify.close();
await mongooseConnection.close();
});
Loading

0 comments on commit 149a895

Please sign in to comment.