Skip to content

Fix unit tests #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/services/ProcessorServiceProductCategory.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async function create (message) {
}

await helper.updateMetadadaESPromise(updateDocPromise)
logger.debug(`Product category created successfully in elasticsearch index, (productCategoryId: ${message.key})`)
logger.debug(`Product category created successfully in elasticsearch index, (productCategoryKey: ${message.key})`)
}

create.schema = {
Expand All @@ -102,7 +102,7 @@ async function update (message) {
}

await helper.updateMetadadaESPromise(updateDocPromise)
logger.debug(`Product category updated successfully in elasticsearch index, (productCategoryId: ${message.key})`)
logger.debug(`Product category updated successfully in elasticsearch index, (productCategoryKey: ${message.key})`)
}

update.schema = {
Expand All @@ -122,7 +122,7 @@ async function deleteMessage (message) {
}

await helper.updateMetadadaESPromise(updateDocPromise)
logger.debug(`Product category deleted successfully in elasticsearch index, (productCategoryId: ${message.key})`)
logger.debug(`Product category deleted successfully in elasticsearch index, (productCategoryKey: ${message.key})`)
}

deleteMessage.schema = {
Expand Down
11 changes: 8 additions & 3 deletions src/services/ProcessorServiceProductTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ function createSchema () {
brief: Joi.string().max(45),
details: Joi.string().max(255),
aliases: Joi.array(),
template: Joi.object().empty(null),
template: Joi.object().allow(null),
form: Joi.object().keys({
key: Joi.string().required(),
version: Joi.number()
}).empty(null),
}).allow(null),
disabled: Joi.boolean().optional(),
hidden: Joi.boolean().optional(),
isAddOn: Joi.boolean().optional(),
Expand All @@ -47,7 +47,12 @@ function createSchema () {
updatedBy: Joi.any(),
deletedBy: Joi.any()
})
.xor('form', 'template')
// TODO rewrite this condition so only one of these must be "present" and "not-null"
// - if we just uncomment it, then if one these is passed as null, it would be treated as passed
// - if we uncomment it and change above from '.allow(null)' to '.empty(null)'
// then this check would pass successfully, BUT the null value would be removed from the object
// so the object would end up WITHOUT null value which is expected for consistency.
// .xor('form', 'template')
.unknown(true)
.required()
}
Expand Down
22 changes: 14 additions & 8 deletions src/services/ProcessorServiceProjectTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ function createIdSchema () {
*/
function updateSchema () {
return createIdSchema().keys({
scope: Joi.object().empty(null),
phases: Joi.object().empty(null),
scope: Joi.object().allow(null),
phases: Joi.object().allow(null),
form: Joi.object().keys({
key: Joi.string().required(),
version: Joi.number()
}).empty(null),
}).allow(null),
planConfig: Joi.object().keys({
key: Joi.string().required(),
version: Joi.number()
}).empty(null),
}).allow(null),
priceConfig: Joi.object().keys({
key: Joi.string().required(),
version: Joi.number()
}).empty(null),
}).allow(null),
disabled: Joi.boolean().optional(),
hidden: Joi.boolean().optional(),
createdAt: Joi.any(),
Expand All @@ -63,9 +63,15 @@ function createSchema () {
info: Joi.string().max(255).required(),
aliases: Joi.array().required()
})
.xor('form', 'scope')
.xor('phases', 'planConfig')
.nand('priceConfig', 'scope')
// TODO rewrite these condition so only one of these must be "present" and "not-null"
// - if we just uncomment it, then if one these is passed as null, it would be treated as passed
// - if we uncomment it and change above from '.allow(null)' to '.empty(null)'
// then this check would pass successfully, BUT the null value would be removed from the object
// so the object would end up WITHOUT null value which is expected for consistency.
// .xor('form', 'scope')
// .xor('phases', 'planConfig')
// TODO rewrite this rule too accordingly
// .nand('priceConfig', 'scope')
}

/**
Expand Down
8 changes: 4 additions & 4 deletions test/common/testData.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ const orgConfigId = orgConfigCreatedMessage.payload.id
const projectTemplateId = projectTemplateCreatedMessage.payload.id
const projectTypeId = projectTypeCreatedMessage.payload.key
const projectTypeNotFoundId = `${projectTypeCreatedMessage.payload.key}_notFound`
const productCategoryId = productCategoryCreatedMessage.payload.id
const productCategoryNotFoundId = `${productCategoryCreatedMessage.payload.key}_notFound`
const productCategoryKey = productCategoryCreatedMessage.payload.key
const productCategoryNotFoundKey = `${productCategoryCreatedMessage.payload.key}_notFound`
const metadataId = config.get('esConfig.ES_METADATA_DEFAULT_ID')
let notFoundId = getRandomInt(10000000)
// we use projectId+1, timelineId+1, attachmentId+1,... to create another object if need
Expand Down Expand Up @@ -186,8 +186,8 @@ module.exports = {
projectTemplateId,
projectTypeId,
projectTypeNotFoundId,
productCategoryId,
productCategoryNotFoundId,
productCategoryKey,
productCategoryNotFoundKey,
productCategoryUpdatedMessage,
productCategoryCreatedMessage,
productCategoryDeletedMessage,
Expand Down
24 changes: 12 additions & 12 deletions test/common/testHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,21 @@ async function getMetadataESData (id) {
}

/**
* Expect given objects are equal, ignoring some fields if provided.
* @param {Object} obj1 obj1
* @param {Object} obj2 obj2
* Expect given objects are equal, or only some fields should be tested if provided.
*
* @param {Object} target target
* @param {Object} expected expected
* @param {Object} compareFields compare fields
* @returns {ExpectStatic} the elastic search data of id of configured index type in configured index
*/
function expectObj (obj1, obj2, compareFields) {
let o1 = _.pick(obj1, _.identity)
let o2 = _.pick(obj2, _.identity)
if (compareFields) {
o1 = _.pick(o1, compareFields)
o2 = _.pick(o2, compareFields)
}
function expectObj (target, expected, compareFields) {
expect(target, 'expectObj(): "target" object should not be undefined').to.not.be.an('undefined')
expect(expected, 'expectObj(): "expected" object should not be undefined').to.not.be.an('undefined')

expect(_.isEqual(o1, o2)).to.equal(true)
const targetClean = compareFields ? _.pick(target, compareFields) : target
const expectedClean = compareFields ? _.pick(expected, compareFields) : expected

// check if objects are deeply equal by values
expect(targetClean).to.eql(expectedClean)
}

module.exports = {
Expand Down
16 changes: 8 additions & 8 deletions test/e2e/processor.metadata.index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const {
projectTemplateId,
projectTypeId,
projectTypeNotFoundId,
productCategoryId,
productCategoryNotFoundId,
productCategoryKey,
productCategoryNotFoundKey,
productCategoryUpdatedMessage,
productCategoryCreatedMessage,
productCategoryDeletedMessage,
Expand Down Expand Up @@ -106,39 +106,39 @@ describe('TC Product Category Topic Tests', () => {
it('create product category message', async () => {
await ProcessorService.create(productCategoryCreatedMessage)
const data = await testHelper.getMetadataESData(metadataId)
testHelper.expectObj(_.find(data.productCategories, { key: productCategoryId }),
testHelper.expectObj(_.find(data.productCategories, { key: productCategoryKey }),
productCategoryCreatedMessage.payload,
_.keys(_.omit(productCategoryCreatedMessage.payload, ['resource'])))
})

it('create product category message - already exists', async () => {
await ProcessorService.create(productCategoryCreatedMessage)
const data = await testHelper.getMetadataESData(metadataId)
testHelper.expectObj(_.find(data.productCategories, { key: productCategoryId }),
testHelper.expectObj(_.find(data.productCategories, { key: productCategoryKey }),
productCategoryCreatedMessage.payload,
_.keys(_.omit(productCategoryCreatedMessage.payload, ['resource'])))
})

it('update product category message', async () => {
await ProcessorService.update(productCategoryUpdatedMessage)
const data = await testHelper.getMetadataESData(metadataId)
testHelper.expectObj(_.find(data.productCategories, { key: productCategoryId }),
testHelper.expectObj(_.find(data.productCategories, { key: productCategoryKey }),
productCategoryUpdatedMessage.payload,
_.keys(_.omit(productCategoryUpdatedMessage.payload, ['resource'])))
})

it('update product category message - not found', async () => {
const message = _.cloneDeep(productCategoryUpdatedMessage)
message.payload.key = productCategoryNotFoundId
message.payload.key = productCategoryNotFoundKey
await ProcessorService.update(message)
const data = await testHelper.getMetadataESData(metadataId)
expect(_.find(data.productCategories, { key: productCategoryNotFoundId })).to.be.an('undefined')
expect(_.find(data.productCategories, { key: productCategoryNotFoundKey })).to.be.an('undefined')
})

it('delete product category message', async () => {
await ProcessorService.deleteMessage(productCategoryDeletedMessage)
const data = await testHelper.getMetadataESData(metadataId)
expect(_.find(data.productCategories, { key: productCategoryId })).to.be.an('undefined')
expect(_.find(data.productCategories, { key: productCategoryKey })).to.be.an('undefined')
})
})

Expand Down