How to test sequelize query (where) logic? - node.js

I'm trying to write unit tests which will test if the search query is right In other words if the logic written in where statement is returning expected results.
async function search(some_data){
return Event.findOne({
where: {
id: 123435,
[Op.or]: [
days: {
[Op.overlap]: some_data.days,
},
[Op.or]: [
{
startTime: {
[Op.gt]: some_data.start1,
},
endTime: {
[Op.lt]:some_data.end1,
},
},
{
startTime: {
[Op.lt]: some_data.start2,
[Op.lt]: some_data.end2,
},
endTime: {
[Op.gt]: some_data.end2,
[Op.gt]: some_data.start2,
},
},
],
],
},
})};
I need to test the result for different inputs.
I don't want to convert this test into integration test and use the original db, so I used sequelize-mock lib, but this returns only the result that I've defined and does not run the real query.

To test that your method is called with the correct parameters, you will need to use dependency injetion and a library to "spy" on your findOne method. In the example below, I am using Sinon
// app.js
// Note that "Event" must be used an argument in order to mock it out
async function search(Event, some_data) {
return Event.findOne({
where: {
id: 123435
}
})
}
If your test file:
// your test file
const app = require('./app');
const sinon = require('sinon');
const EventMock = {
findOne: sinon.spy()
};
describe('search', () => {
it('should call with the right parameters', () => {
const some_data = {};
search(EventMock, some_data);
assert(EventMock.findOne.calledWith({
where: {
id: 123435
}
}))
});
});

Related

How to get random records from Strapi v4 ? (I answered this question)

Strapi doesn't have any endpoint to get random data for this purpose you should write some custom code for your endpoint
custom route for that endpoint you want
// path: ./src/api/[your-endpiont]/routes/[custom-route].js
module.exports = {
"routes": [
{
"method": "GET",
"path": "/[your-endpiont]/random", // you can define everything you want for url endpoint
"handler": "[your-endpiont].random", // random is defined as a method
"config": {
"policies": []
}
}
]
}
now you have to run yarn develop or npm ... to display a random method in your strapi panel
Save this setting and retry to reach the random endpoint.
create a function as a service for getting random data in your endpoint API services.
// path: ./src/api/[your-endpiont]/services/[your-endpiont].js
'use strict';
/**
* news-list service.
*/
const { createCoreService } = require('#strapi/strapi').factories;
module.exports = createCoreService('api::news-list.news-list', ({ strapi }) => ({
async serviceGetRandom({ locale, id_nin }) { // these parametrs come from query
function getRandomElementsFromArray(array, numberOfRandomElementsToExtract = 1) {
const elements = [];
function getRandomElement(arr) {
if (elements.length < numberOfRandomElementsToExtract) {
const index = Math.floor(Math.random() * arr.length)
const element = arr.splice(index, 1)[0];
elements.push(element)
return getRandomElement(arr)
} else {
return elements
}
}
return getRandomElement([...array])
}
const newsListArray = await strapi
.db
.query("api::news-list.news-list")
.findMany({
where: {
locale: locale, // if you have multi-language data
$not: {
id: id_nin, // depend on where this endpoint API use
},
publishedAt: {
$notNull: true,
},
},
sort: [{ datetime: 'asc' }],
limit: 10,
populate: {
content: {
populate: {
thumbnail: true,
},
},
},
//? filter object throws an error when you used populate object, everything you want to filter properly best write into where{}
// filters: {
// publishedAt: {
// $notNull: true,
// },
// locale: locale
// }
})
if (!newsListArray.length) {
return null
}
return getRandomElementsFromArray(newsListArray, 2)
}
}));
explain code:
Strapi provides a Query Engine API to interact with the database layer at a lower level
strapi.db.query("api::news-list.news-list").findMany({})
The Query Engine allows operations on database entries,
I wrote this for my purpose probably you should change based on what you needed
{
where: {
locale: locale,
$not: {
id: id_nin
},
publishedAt: {
$notNull: true,
},
},
sort: [{ datetime: 'asc' }],
limit: 10,
populate: {
content: {
populate: {
thumbnail: true,
},
},
}
}
when you get data from your query, passed it to that function getRandomElementsFromArray(newsListArray, 2) to get some random item (how many random items do you want ? pass the second parameter)
At least if your array is null return null otherwise return data
create the controller
Controllers are JavaScript files that contain a set of methods, called actions, reached by the client according to the requested route so we going to call our services in this section
// path: ./src/api/[your-endpoint]/controllers/[your-endpoint].js
'use strict';
/**
* news-list controller
*/
const { createCoreController } = require('#strapi/strapi').factories;
module.exports = createCoreController('api::news-list.news-list', ({ strapi }) => ({
async random(ctx) { // name of this methods related to something we define in route ("handler": "[your-endpiont].random",)
const entity = await strapi.service('api::news-list.news-list').serviceGetRandom(ctx.query) // call our services, you can send all query you get from url endpoint (notice that you should write your endpoint api in strapi.service("your-endpoint"))
const sanitizedEntity = await this.sanitizeOutput(entity, ctx);
return this.transformResponse(sanitizedEntity);
// console.log(entity);
}
}));
I call this endpoint in my project nextjs & stapi cms
export const getRandomNewsItem = (id, locale) => {
return API
.get(`/news-list/random?locale=${locale}&id_nin=${id}`)
.then(res => res.data);
};
That's it, I'll hope you all get what to do
all resources you need
https://docs.strapi.io/developer-docs/latest/development/backend-customization/routes.html#creating-custom-routers
https://docs.strapi.io/developer-docs/latest/development/backend-customization/services.html#implementation
https://docs.strapi.io/developer-docs/latest/development/backend-customization/controllers.html#adding-a-new-controller
https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/query-engine-api.html
https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/query-engine/filtering.html#and
https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/entity-service/order-pagination.html#ordering
https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/entity-service/order-pagination.html#ordering
https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/query-engine/populating.html

GET request with $function in Mongodb

Summary:
I am trying to Combine the result of Mongodb aggregation with a third party API and I can't find anything relevant to it.
Explanation:
The below Express route finds all Games that comes after the provided Date and have not been cancelled. The next step is to get some data of that single game from the Third party API and attach it to the object and continue further in the pipeline
Issue:
It seems that you can't have a XHR request inside the $function (I didn't find anything in the official documentation so I'm not sure on that)
const today = moment();
today.year(2021);
today.month(5);
let response = await Game.aggregate([
{
$match: {
$expr: {
$and: [
{ $gte: ["$date", moment(today).startOf('day').toDate()] },
{ $eq: ["$canceled", false] },
]
}
}
},
{ $sort: { date: 1 } },
{
$addFields: {
boxScore: {
$function:
{
body: async function (season, week, home_team) {
const result = await axios.get(
`SINGLEGAMEURL/${season}/${week}/${home_team}`,
{
headers: {
'Subscription-Key': 'SOMEKEY',
},
}
);
return result.data;
},
args: ["$season", '$week', 'home_team'],
lang: "js"
}
}
}
}
]);
I would really appreciate any help/direction on this, Cheers!
I doubt that you can use asynchronous functions in $function, because they return a promise that resolves to result.data, rather than the data themselves. Instead, consider performing the asynchronous operation in your express middleware, after the MongoDB operation. Something like this:
app.use("/path", async function(req, res) {
const today = moment();
today.year(2021);
today.month(5);
let response = await Game.aggregate([
{$match: ...},
{$sort: {date: 1}}
]).toArray();
await Promise.all(response.map(row => axios.get(
`SINGLEGAMEURL/${row.season}/${row.week}/${row.home_team}`,
{headers: {'Subscription-Key': 'SOMEKEY'}}
).then(function(result) {
row.boxScore = result.data;
})));
res.json(response);
});
(Probably the Promise.all can be avoided, but I'm not experienced enough with async/await to know how.)

I have 2 issues with mongoose aggregation and index method

I have 2 issues
FIRST ONE:
I am trying to make review schema that a user should add 1 review per bootcamp
Code:
ReviewSchema.index({ bootcamp: 1, user: 1 }, { unique: true });
It doesnt work .. and the user still can add more than one review
SECOND ISSUE:
I am trying to calculate the averagerating of reviews but it doesn`t get added to the db when am fetching the bootcamps
Code:
// Static Method to get the avg rating of reviews and save
ReviewSchema.statics.getAverageRating = async function (bootcampId) {
const obj = await this.aggregate([
{
$match: { bootcamp: bootcampId },
},
{
$group: {
_id: '$bootcamp',
averageRating: { $avg: '$rating' },
},
},
]);
try {
await this.model('Bootcamp').findByIdAndUpdate(bootcampId, {
averageRating: obj[0].averageRating,
});
} catch (err) {
console.log(err);
}
//Call averageRating after save
ReviewSchema.post('save', async function () {
await this.constructor.getAverageRating(this.bootcamp);
});
//Call averageRating before remove
ReviewSchema.pre('remove', async function () {
await this.constructor.getAverageRating(this.bootcamp);
});
** It doesnt work and the averagerating never gets added to the database (as a bootcamp`s field)**
I Did the same as the tutorial and it didn`t work at the first but then i figured out that missing a semi-colon.

Stub Mongoose find method

I'm trying to stub the find or exec functions to test the following function:
function getOpenTickets() {
return Ticket.find({})
.populate('assignees', ['fullName', 'firstName', 'email', 'notificationSettings.dailyEmail'])
.populate('property', 'name')
.populate('type', 'title')
.populate({path: 'unit', model: 'Unit', select: 'title'})
.sort('created')
.lean()
.exec();
}
I found several posts about stubbing mongoose methods but none of them worked for me, here is what I have:
it('should test getOpenTickets', async() => {
findStub = sinon.stub(Ticket, 'find');
var result = await utils.__get__('getOpenTickets')();
findStub.restore();
});
But I get:
Cannot read property 'populate' of undefined
so I tried replacing it with a fake object:
var fakeFind = {
args: {
populate: [],
sort: null,
lean: null
},
populate: function (a) {
this.args.populate.push(a)
},
sort: function (a) {
this.args.sort = a;
},
lean: function () {
this.args.lean = true
},
exec: function () {
return Promise.resolve(this.args);
}
}
And
findStub = sinon.stub(Ticket, 'find').callsFake(fakeFind);
And the result is:
TypeError: this.fakeFn.apply is not a function
I've also tried stubbing mongoose.Model, prototype, exec, and some other stuff with no luck.
Any ideas?
Try to use sinon-mongoose https://github.com/underscopeio/sinon-mongoose
Here is an example:
require('sinon');
require('sinon-mongoose');
sinon.mock(Ticket)
.expects('find')
.chain('populate').withArgs(/* args */)
.chain('sort').withArgs('create')
.chain('lean')
.chain('exec')
.resolves('SOME_VALUE');

Merging GraphQL Resolvers for Apollo Server not working with Object.assign()

I am modularizing my schema for a GraphQL API and trying to merge the resolvers without using any 3rd party libraries.
Is there a simple way to do this without Lodash.merge() or equivalent?
The Apollo Documentation says to use a library such as Lodash to merge() modularized resolvers. (http://dev.apollodata.com/tools/graphql-tools/generate-schema.html#modularizing)
The problem seems to be that by their nature, the resolvers contain functions as properties, so they seem to be omitted when I access them via Object.assign() or even JSON.stringify().
If I console.log them, I see: {"Query":{},"Mutation":{}}
Here is what one of the resolvers looks like:
const productResolvers = {
Query: {
myProducts: (root, { userId }, context) => {
return [
{ id: 1, amount: 100, expiry: '12625383984343', created: '12625383984343' },
{ id: 2, amount: 200, expiry: '12561351347311', created: '12625383984343' },
{ id: 3, amount: 200, expiry: '11346347378333', created: '12625383984343' },
{ id: 4, amount: 350, expiry: '23456234523453', created: '12625383984343' },
];
},
},
Mutation: {
addProduct: (root, { userId }, context) => {
return { id: 350, amount: 100, expiry: '12625383984343', created: '12625383984343' };
},
}
};
Let's assume there is another one virtually identical called widgetResolvers.
Here is a fully functional block of code:
export const schema = makeExecutableSchema({
typeDefs: [queries, mutations, productSchema, widgetSchema],
resolvers
});
Here is what I'm trying to achieve:
export const schema = makeExecutableSchema({
typeDefs: [queries, mutations, productSchema, widgetSchema],
resolvers: Object.assign({}, productResolvers, widgetResolvers)
});
I haven't loaded in ability to use rest spread yet (https://babeljs.io/docs/plugins/transform-object-rest-spread/). I suspect it won't work for the same reason Object.assign() doesn't work.
Oh, and here is why I suspect this merge doesn't work: Why doesn't JSON.stringify display object properties that are functions?
If you're using Object.assign(), your Query and Mutation properties shouldn't end up empty, but you will run into an issue because, unlike lodash's merge(), it's not recursive. Object.assign() only compares the "direct" properties of the objects it's passed -- overriding properties of previous sources as it moves through the list.
Because Query and Mutation are properties of the objects being passed, each subsequent resolver override the previous object's Query and Mutation, with the resulting object only holding the Query and Mutation properties of the last object passed into Object.assign().
It's a lot less neat, but if you're bent on avoiding importing lodash, you could get the expected behavior this way:
const productResolver = {
Query: { ... ✂ ... },
Mutation: { ... ✂ ... }
}
const widgetResolver = {
Query: { ... ✂ ... },
Mutation: { ... ✂ ... }
}
const resolvers = {
Query: Object.assign({}, widgetResolver.Query, productResolver.Query),
Mutation: Object.assign({}, widgetResolver.Mutation, productResolver.Mutation)
}
Got type resolvers too? No problem:
const Widget = { ... ✂ ... }
const Product = { ... ✂ ... }
const resolvers = Object.assign(
{
Query: Object.assign({}, widgetResolver.Query, productResolver.Query),
Mutation: Object.assign({}, widgetResolver.Mutation, productResolver.Mutation)
},
Widget,
Product)

Resources