|
| 1 | +/* eslint-disable no-unused-expressions */ |
| 2 | + |
| 3 | +const _ = require('lodash') |
| 4 | +const chai = require('chai') |
| 5 | +const chaiAsPromised = require('chai-as-promised') |
| 6 | +const sinon = require('sinon') |
| 7 | +const sequelize = require('../../src/models/index') |
| 8 | +const service = require('../../src/modules/usersSkill/service') |
| 9 | +const helper = require('../../src/common/helper') |
| 10 | +const serviceHelper = require('../../src/common/service-helper') |
| 11 | +const dbHelper = require('../../src/common/db-helper') |
| 12 | +const errors = require('../../src/common/errors') |
| 13 | + |
| 14 | +chai.use(chaiAsPromised) |
| 15 | +const expect = chai.expect |
| 16 | + |
| 17 | +describe('user skills service test', () => { |
| 18 | + beforeEach(() => { |
| 19 | + sinon.stub(sequelize, 'transaction').callsFake(f => f()) |
| 20 | + }) |
| 21 | + afterEach(() => { |
| 22 | + sinon.restore() |
| 23 | + }) |
| 24 | + |
| 25 | + describe('Create user skills', () => { |
| 26 | + it('Create user skills successfully', async () => { |
| 27 | + const entity = { userId: 'userId', skillId: 'skillId' } |
| 28 | + const newEntity = _.assign({ createdBy: 'test' }, entity) |
| 29 | + sinon.stub(dbHelper, 'get').resolves({}) |
| 30 | + sinon.stub(dbHelper, 'makeSureUnique').resolves({}) |
| 31 | + const stubDBCreate = sinon.stub(dbHelper, 'create').resolves({ toJSON: () => newEntity }) |
| 32 | + const stubEsInsert = sinon.stub(serviceHelper, 'createRecordInEs').resolves({}) |
| 33 | + const result = await service.create(entity, {}) |
| 34 | + expect(result).to.deep.eql(newEntity) |
| 35 | + expect(stubDBCreate.calledOnce).to.be.true |
| 36 | + expect(stubEsInsert.calledOnce).to.be.true |
| 37 | + expect(stubDBCreate.getCall(0).args[1]).to.deep.eq(entity) |
| 38 | + expect(stubEsInsert.getCall(0).args[1]).to.deep.eq(newEntity) |
| 39 | + }) |
| 40 | + it('Throw error when require parameters absence', async () => { |
| 41 | + await expect(service.create({ userId: 'userId' })).to.be.rejectedWith('"entity.skillId" is required') |
| 42 | + await expect(service.create({ skillId: 'skillId' })).to.be.rejectedWith('"entity.userId" is required') |
| 43 | + }) |
| 44 | + it('Throw error when user id does not exist', async () => { |
| 45 | + const error = errors.newEntityNotFoundError('cannot find user where id:userId') |
| 46 | + sinon.stub(dbHelper, 'get').rejects(error) |
| 47 | + await expect(service.create({ userId: 'userId', skillId: 'skillId' })).to.eventually.rejectedWith(error) |
| 48 | + }) |
| 49 | + it('Throw error when a record with same user id and skill id exist', async () => { |
| 50 | + const error = errors.newConflictError('usersSkill already exists with userId:userId,skillId:skillId') |
| 51 | + sinon.stub(dbHelper, 'get').resolves({}) |
| 52 | + sinon.stub(dbHelper, 'makeSureUnique').rejects(error) |
| 53 | + await expect(service.create({ userId: 'userId', skillId: 'skillId' })).to.eventually.rejectedWith(error) |
| 54 | + }) |
| 55 | + it('Throw error and does not publish error event when insert into db error', async () => { |
| 56 | + const error = new Error('db error') |
| 57 | + sinon.stub(dbHelper, 'get').resolves({}) |
| 58 | + sinon.stub(dbHelper, 'makeSureUnique').resolves({}) |
| 59 | + const stubPublishError = sinon.stub(helper, 'publishError') |
| 60 | + sinon.stub(dbHelper, 'create').rejects(error) |
| 61 | + await expect(service.create({ userId: 'userId', skillId: 'skillId' })).to.eventually.rejectedWith(error) |
| 62 | + expect(stubPublishError.called).to.be.false |
| 63 | + }) |
| 64 | + it('Throw error and publish error event when insert into es error', async () => { |
| 65 | + const error = new Error('es error') |
| 66 | + const entity = { userId: 'userId', skillId: 'skillId' } |
| 67 | + const newEntity = _.assign({ createdBy: 'test' }, entity) |
| 68 | + sinon.stub(dbHelper, 'get').resolves({}) |
| 69 | + sinon.stub(dbHelper, 'makeSureUnique').resolves({}) |
| 70 | + const stubPublishError = sinon.stub(helper, 'publishError').resolves({}) |
| 71 | + const stubDBCreate = sinon.stub(dbHelper, 'create').resolves({ toJSON: () => newEntity }) |
| 72 | + sinon.stub(serviceHelper, 'createRecordInEs').rejects(error) |
| 73 | + await expect(service.create({ userId: 'userId', skillId: 'skillId' })).to.eventually.rejectedWith(error) |
| 74 | + expect(stubPublishError.called).to.be.true |
| 75 | + expect(stubDBCreate.called).to.be.true |
| 76 | + }) |
| 77 | + }) |
| 78 | + describe('Search user skills', () => { |
| 79 | + it('Throw error when require parameters absence', async () => { |
| 80 | + await expect(service.search({ })).to.be.rejectedWith(errors.BadRequestError) |
| 81 | + }) |
| 82 | + it('Throw error when user id does not exist', async () => { |
| 83 | + const error = errors.newEntityNotFoundError('cannot find user where id:userId') |
| 84 | + sinon.stub(dbHelper, 'get').rejects(error) |
| 85 | + await expect(service.search({ userId: 'userId' })).to.eventually.rejectedWith(error) |
| 86 | + }) |
| 87 | + it('Search user skills from es successfully', async () => { |
| 88 | + const entity = { userId: 'userId', skillId: 'skillId' } |
| 89 | + const newEntity = _.assign({ createdBy: 'test' }, entity) |
| 90 | + sinon.stub(dbHelper, 'get').resolves({}) |
| 91 | + const searchEs = sinon.stub(serviceHelper, 'searchRecordInEs').resolves([newEntity]) |
| 92 | + const searchDb = sinon.stub(dbHelper, 'find') |
| 93 | + const result = await service.search({ userId: 'userId' }, {}) |
| 94 | + expect(result).to.deep.eql([newEntity]) |
| 95 | + expect(searchEs.calledOnce).to.be.true |
| 96 | + expect(searchDb.called).to.be.false |
| 97 | + }) |
| 98 | + it('Search user skills from db when es throw errors', async () => { |
| 99 | + const entity = { userId: 'userId', skillId: 'skillId' } |
| 100 | + const newEntity = _.assign({ createdBy: 'test' }, entity) |
| 101 | + sinon.stub(dbHelper, 'get').resolves({}) |
| 102 | + const searchEs = sinon.stub(serviceHelper, 'searchRecordInEs').resolves(null) |
| 103 | + const searchDb = sinon.stub(dbHelper, 'find').resolves([newEntity]) |
| 104 | + const result = await service.search({ userId: 'userId' }, {}) |
| 105 | + expect(result).to.deep.eql({ fromDb: true, result: [newEntity], total: 1 }) |
| 106 | + expect(searchEs.calledOnce).to.be.true |
| 107 | + expect(searchDb.called).to.be.true |
| 108 | + }) |
| 109 | + }) |
| 110 | +}) |
0 commit comments