BlinkUX/sequelize-mock

Mock a SQL function

biruks opened this issue · 2 comments

How can I be able to mock a SQL function without creating a model ?

What functionality exactly are you trying to mock? Do you have example code you're trying to test?

If you're using the sequelize.query() function, you can queue up results on the mock sequelize object to be returned from that function as well. Like with models, you simply need to use the .$queueResult() method.

@ktsashes i am having an issue writing test for

try { const merchant_id = req.params.merchant_id; const coupons = await models.sequelize.query(SELECT * FROM coupons LEFT JOIN coupon_activities ON coupons.coupon=coupon_activities.coupon WHERE coupons.domain='${merchant_id}' ORDER BY coupon_activities.best_discount DESC, coupon_activities.worked DESC, coupons.expire_date DESC LIMIT 50, { type: sequelize.QueryTypes.SELECT }); if (coupons && coupons.length > 0) { const keys = [...new Set(coupons[0].map(el => el.coupon))] const filteredCoupons = coupons[0].filter(el => { if (keys.indexOf(el.coupon) !== -1) { keys.splice(keys.indexOf(el.coupon), 1) return true; } else { return false; } }); res.status(200).send({ coupons: filteredCoupons }); } else { res.status(204).send({ response: "No Coupons" }); } } catch (err) { console.log(err); res.status(500).send({ response: "Error in server" }); }

Returning a coupons object with 200 status.

This is the test
describe('Get Coupons By Merchand Id', () => { it('It got all coupons of a merchant', (done) => { const merchant_id = 1; chai.request(server) .get('/api/coupons/' + merchant_id) .end((err, res) => { res.should.have.status(200); res.body.should.be.a('object'); res.body.should.not.have.property('errors'); res.body.coupons.length.should.not.equal(0); res.body.coupons[0].merchant_id.should.be.equal(merchant_id); done(); }); }); });
I have tried to add a mock like
`
let SequelizeMock = dbMock.define("sequelize", {
merchant_id: 1,
domain: 'amazon.com',
coupon: 'TESTCOUPON',
description: 'This is a test',
expiry_date: '12-12-9999',
}, {
indexes: [{ fields: ['merchant_id'] }],
createdAt: false,
updatedAt: false,
id: false,
tableName: 'coupons',
});

SequelizeMock.$queueResult(SequelizeMock.build({
domain: 'amazon.com',
coupon: 'TESTCOUPON',
description: 'This is a test',
}));

SequelizeMock.$queryInterface.$useHandler(function(query, queryOptions, done) {
console.log('TCL: query', query)
console.log('TCL: queryOptions', queryOptions)
});

module.exports = SequelizeMock;`

Can you please guide me hot to do it properly

Getting this error

image