From 62ea73f540607e825d0f8ef476c3a5ad55d619d8 Mon Sep 17 00:00:00 2001 From: Guilherme Berghauser Date: Mon, 26 Aug 2019 15:16:22 +0100 Subject: [PATCH] Adjust tests and add error to logical operators --- README.md | 4 +- index.js | 2 +- lib/compare.js | 5 +- lib/comparison.js | 1 + package.json | 2 +- test/index.test.js | 12 +++++ test/lib/compare.test.js | 114 ++++++++++++++++++++------------------- 7 files changed, 78 insertions(+), 62 deletions(-) create mode 100644 test/index.test.js diff --git a/README.md b/README.md index 646cfc0..2372591 100644 --- a/README.md +++ b/README.md @@ -173,8 +173,8 @@ console.log('$or -', 'Have to be false:', easy.compare('13', { })); //$not -console.log('$not -', 'Have to be true:', compare.compare([35, 40, 50], { $not: { $in: 45 } })); -console.log('$not -', 'Have to be false:', compare.compare([35, 40, 50], { +console.log('$not -', 'Have to be true:', easy.compare([35, 40, 50], { $not: { $in: 45 } })); +console.log('$not -', 'Have to be false:', easy.compare([35, 40, 50], { $not: { $in: 35, $and: { diff --git a/index.js b/index.js index b19f126..6456ed3 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ 'use strict' -const { compare } = require('./lib/compare') +const compare = require('./lib/compare') module.exports = { compare diff --git a/lib/compare.js b/lib/compare.js index 0e52885..c22693c 100644 --- a/lib/compare.js +++ b/lib/compare.js @@ -52,6 +52,7 @@ function interatorValues (value, operator, item) { function executeLogicalOperator (funcLogical) { return (value, operator, key) => { const keys = Object.keys(operator) + if (!keys.length) throw new Error(`The operator ${key} need to be an object valid.`) return funcLogical(keys, interatorValues, value, operator) } } @@ -62,6 +63,4 @@ function compare (value, operator) { return interatorValues(value, operator, keys[0]) } -module.exports = { - compare -} +module.exports = compare diff --git a/lib/comparison.js b/lib/comparison.js index ab485ec..b6579c0 100644 --- a/lib/comparison.js +++ b/lib/comparison.js @@ -43,6 +43,7 @@ function isNotSameSize (value, comparation) { function isRegexMatch (value, comparation) { return new RegExp(comparation).test(value) } + function isNotRegexMatch (value, comparation) { return !new RegExp(comparation).test(value) } diff --git a/package.json b/package.json index 3822d1a..a8c36e1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "easy-compare", - "version": "0.0.9", + "version": "0.0.10", "description": "Compare values using opertaros like mongodb", "main": "index.js", "scripts": { diff --git a/test/index.test.js b/test/index.test.js new file mode 100644 index 0000000..058a971 --- /dev/null +++ b/test/index.test.js @@ -0,0 +1,12 @@ +'user strict' + +const expect = require('chai').expect + +const easy = require('..') + +describe('Index test case', () => { + it('Should call the compare function', () => { + expect(easy.compare(10, { $eq: 10 })).to.be.true + expect(easy.compare(10, { $eq: 11 })).to.be.false + }) +}) diff --git a/test/lib/compare.test.js b/test/lib/compare.test.js index 9a6224a..cfb9729 100644 --- a/test/lib/compare.test.js +++ b/test/lib/compare.test.js @@ -8,125 +8,129 @@ describe('Test case to Compare Fields', () => { describe('Compare METHOD', () => { describe('Check correct options', () => { it('No compare option', () => { - expect(() => compare.compare(20)).to.throw(/You need to insert an operator/) + expect(() => compare(20)).to.throw(/You need to insert an operator/) }) it('Compare with options not correct', () => { - expect(() => compare.compare(20, { $eu: 10 })).to.throw(/The operator \$eu is incorrect. Valid operator: \$eq, \$ne, \$gt, \$gte, \$lt, \$lte, \$in, \$nin, \$size, \$nsize, \$regex, \$nregex, \$and, \$or, \$all/) + expect(() => compare(20, { $eu: 10 })).to.throw(/The operator \$eu is incorrect. Valid operator: \$eq, \$ne, \$gt, \$gte, \$lt, \$lte, \$in, \$nin, \$size, \$nsize, \$regex, \$nregex, \$and, \$or, \$all/) }) it('Compare with some options not correct and other correct', () => { - expect(() => compare.compare(20, { $and: { $eq: 20, other: 10 } })).to.throw(/The operator other is incorrect. Valid operator: \$eq, \$ne, \$gt, \$gte, \$lt, \$lte, \$in, \$nin, \$size, \$nsize, \$regex, \$nregex, \$and, \$or, \$all/) + expect(() => compare(20, { $and: { $eq: 20, other: 10 } })).to.throw(/The operator other is incorrect. Valid operator: \$eq, \$ne, \$gt, \$gte, \$lt, \$lte, \$in, \$nin, \$size, \$nsize, \$regex, \$nregex, \$and, \$or, \$all/) + }) + it('Compare with some logical operator without a object', () => { + expect(() => compare(20, { $and: 10 })).to.throw('The operator $and need to be an object valid.') + expect(() => compare(20, { $and: {} })).to.throw('The operator $and need to be an object valid.') }) }) describe('Should compare without $and or $or fields', () => { describe('$eq field', () => { it('correct', () => { - expect(compare.compare(20, { $eq: 20 })).to.be.true + expect(compare(20, { $eq: 20 })).to.be.true }) it('wrong', () => { - expect(compare.compare(21, { $eq: 20 })).to.be.false + expect(compare(21, { $eq: 20 })).to.be.false }) }) describe('$ne field', () => { it('correct', () => { - expect(compare.compare(21, { $ne: 20 })).to.be.true + expect(compare(21, { $ne: 20 })).to.be.true }) it('wrong', () => { - expect(compare.compare(20, { $ne: 20 })).to.be.false + expect(compare(20, { $ne: 20 })).to.be.false }) }) describe('$gt field', () => { it('correct', () => { - expect(compare.compare(21, { $gt: 20 })).to.be.true + expect(compare(21, { $gt: 20 })).to.be.true }) it('wrong', () => { - expect(compare.compare(10, { $gt: 20 })).to.be.false + expect(compare(10, { $gt: 20 })).to.be.false }) }) describe('$gte field', () => { it('correct', () => { - expect(compare.compare(21, { $gte: 20 })).to.be.true + expect(compare(21, { $gte: 20 })).to.be.true }) it('correct equal', () => { - expect(compare.compare(20, { $gte: 20 })).to.be.true + expect(compare(20, { $gte: 20 })).to.be.true }) it('wrong', () => { - expect(compare.compare(10, { $gte: 20 })).to.be.false + expect(compare(10, { $gte: 20 })).to.be.false }) }) describe('$lt field', () => { it('correct', () => { - expect(compare.compare(20, { $lt: 21 })).to.be.true + expect(compare(20, { $lt: 21 })).to.be.true }) it('wrong', () => { - expect(compare.compare(20, { $lt: 10 })).to.be.false + expect(compare(20, { $lt: 10 })).to.be.false }) }) describe('$lte field', () => { it('correct', () => { - expect(compare.compare(20, { $lte: 21 })).to.be.true + expect(compare(20, { $lte: 21 })).to.be.true }) it('correct equal', () => { - expect(compare.compare(20, { $lte: 20 })).to.be.true + expect(compare(20, { $lte: 20 })).to.be.true }) it('wrong', () => { - expect(compare.compare(20, { $lte: 10 })).to.be.false + expect(compare(20, { $lte: 10 })).to.be.false }) }) describe('$in field', () => { it('correct', () => { - expect(compare.compare([1, 2, 3], { $in: 2 })).to.be.true + expect(compare([1, 2, 3], { $in: 2 })).to.be.true }) it('wrong', () => { - expect(compare.compare([1, 2, 3], { $in: 4 })).to.be.false + expect(compare([1, 2, 3], { $in: 4 })).to.be.false }) }) describe('$in field', () => { it('correct', () => { - expect(compare.compare([1, 2, 3], { $in: 2 })).to.be.true + expect(compare([1, 2, 3], { $in: 2 })).to.be.true }) it('wrong', () => { - expect(compare.compare([1, 2, 3], { $in: 4 })).to.be.false + expect(compare([1, 2, 3], { $in: 4 })).to.be.false }) }) describe('$nin field', () => { it('correct', () => { - expect(compare.compare([1, 2, 3], { $nin: 4 })).to.be.true + expect(compare([1, 2, 3], { $nin: 4 })).to.be.true }) it('wrong', () => { - expect(compare.compare([1, 2, 3], { $nin: 2 })).to.be.false + expect(compare([1, 2, 3], { $nin: 2 })).to.be.false }) }) describe('$size field', () => { it('correct', () => { - expect(compare.compare([1, 2, 3], { $size: 3 })).to.be.true + expect(compare([1, 2, 3], { $size: 3 })).to.be.true }) it('wrong', () => { - expect(compare.compare([1, 2, 3], { $size: 2 })).to.be.false + expect(compare([1, 2, 3], { $size: 2 })).to.be.false }) }) describe('$nsize field', () => { it('correct', () => { - expect(compare.compare([1, 2, 3], { $nsize: 2 })).to.be.true + expect(compare([1, 2, 3], { $nsize: 2 })).to.be.true }) it('wrong', () => { - expect(compare.compare([1, 2, 3], { $nsize: 3 })).to.be.false + expect(compare([1, 2, 3], { $nsize: 3 })).to.be.false }) }) describe('$regex field', () => { it('correct', () => { - expect(compare.compare('123', { $regex: '^[0-9]+' })).to.be.true + expect(compare('123', { $regex: '^[0-9]+' })).to.be.true }) it('wrong', () => { - expect(compare.compare('asd', { $regex: '^[0-9]+' })).to.be.false + expect(compare('asd', { $regex: '^[0-9]+' })).to.be.false }) }) describe('$nregex field', () => { it('correct', () => { - expect(compare.compare('asd', { $nregex: '^[0-9]+' })).to.be.true + expect(compare('asd', { $nregex: '^[0-9]+' })).to.be.true }) it('wrong', () => { - expect(compare.compare('123', { $nregex: '^[0-9]+' })).to.be.false + expect(compare('123', { $nregex: '^[0-9]+' })).to.be.false }) }) }) @@ -134,38 +138,38 @@ describe('Test case to Compare Fields', () => { describe('Should compare with logical operators fields', () => { describe('$all field', () => { it('correct', () => { - expect(compare.compare(35, { $all: { $lte: 50, $gt: 20 } })).to.be.true + expect(compare(35, { $all: { $lte: 50, $gt: 20 } })).to.be.true }) it('wrong greater than', () => { - expect(compare.compare(100, { $all: { $lte: 50, $gt: 20 } })).to.be.false + expect(compare(100, { $all: { $lte: 50, $gt: 20 } })).to.be.false }) it('wrong less than', () => { - expect(compare.compare(10, { $all: { $lte: 50, $gt: 20 } })).to.be.false + expect(compare(10, { $all: { $lte: 50, $gt: 20 } })).to.be.false }) }) describe('$and field', () => { it('correct', () => { - expect(compare.compare(35, { $and: { $lte: 50, $gt: 20 } })).to.be.true + expect(compare(35, { $and: { $lte: 50, $gt: 20 } })).to.be.true }) it('wrong greater than', () => { - expect(compare.compare(100, { $and: { $lte: 50, $gt: 20 } })).to.be.false + expect(compare(100, { $and: { $lte: 50, $gt: 20 } })).to.be.false }) it('wrong less than', () => { - expect(compare.compare(10, { $and: { $lte: 50, $gt: 20 } })).to.be.false + expect(compare(10, { $and: { $lte: 50, $gt: 20 } })).to.be.false }) }) describe('$or field', () => { it('correct', () => { - expect(compare.compare(35, { $or: { $lte: 10, $gt: 20 } })).to.be.true + expect(compare(35, { $or: { $lte: 10, $gt: 20 } })).to.be.true }) it('wrong', () => { - expect(compare.compare(15, { $or: { $lte: 10, $gt: 20 } })).to.be.false + expect(compare(15, { $or: { $lte: 10, $gt: 20 } })).to.be.false }) }) describe('$and with $or field inside', () => { it('correct', () => { - expect(compare.compare(35, { + expect(compare(35, { $and: { $lte: 50, $or: { @@ -176,7 +180,7 @@ describe('Test case to Compare Fields', () => { })).to.be.true }) it('correct - position $or', () => { - expect(compare.compare(35, { + expect(compare(35, { $and: { $or: { $lt: 10, @@ -187,7 +191,7 @@ describe('Test case to Compare Fields', () => { })).to.be.true }) it('wrong first value in $and', () => { - expect(compare.compare(60, { + expect(compare(60, { $and: { $lte: 50, $or: { @@ -198,7 +202,7 @@ describe('Test case to Compare Fields', () => { })).to.be.false }) it('wrong second value in $and - position $or', () => { - expect(compare.compare(60, { + expect(compare(60, { $and: { $or: { $lt: 10, @@ -209,7 +213,7 @@ describe('Test case to Compare Fields', () => { })).to.be.false }) it('wrong second value in $and ($or)', () => { - expect(compare.compare(11, { + expect(compare(11, { $and: { $lte: 50, $or: { @@ -220,7 +224,7 @@ describe('Test case to Compare Fields', () => { })).to.be.false }) it('wrong first value in $and ($or) - change position of $or', () => { - expect(compare.compare(11, { + expect(compare(11, { $and: { $or: { $lt: 10, @@ -234,7 +238,7 @@ describe('Test case to Compare Fields', () => { describe('$or with $and field inside', () => { it('correct', () => { - expect(compare.compare(35, { + expect(compare(35, { $or: { $lte: 50, $and: { @@ -245,7 +249,7 @@ describe('Test case to Compare Fields', () => { })).to.be.true }) it('correct - position $and', () => { - expect(compare.compare(35, { + expect(compare(35, { $or: { $and: { $lt: 40, @@ -256,7 +260,7 @@ describe('Test case to Compare Fields', () => { })).to.be.true }) it('correct but $and is wrong', () => { - expect(compare.compare(35, { + expect(compare(35, { $or: { $lte: 50, $and: { @@ -267,7 +271,7 @@ describe('Test case to Compare Fields', () => { })).to.be.true }) it('correct but $and is wrong - position $and', () => { - expect(compare.compare(35, { + expect(compare(35, { $or: { $and: { $lt: 20, @@ -278,7 +282,7 @@ describe('Test case to Compare Fields', () => { })).to.be.true }) it('correct but the first value is wrong', () => { - expect(compare.compare(35, { + expect(compare(35, { $or: { $lte: 20, $and: { @@ -289,7 +293,7 @@ describe('Test case to Compare Fields', () => { })).to.be.true }) it('correct but the second value is wrong - postion $and', () => { - expect(compare.compare(35, { + expect(compare(35, { $or: { $and: { $lt: 50, @@ -300,7 +304,7 @@ describe('Test case to Compare Fields', () => { })).to.be.true }) it('wrong all values', () => { - expect(compare.compare(35, { + expect(compare(35, { $or: { $lte: 20, $and: { @@ -311,7 +315,7 @@ describe('Test case to Compare Fields', () => { })).to.be.false }) it('wrong all values - position $and', () => { - expect(compare.compare(35, { + expect(compare(35, { $or: { $and: { $lt: 20, @@ -325,10 +329,10 @@ describe('Test case to Compare Fields', () => { describe('$not field', () => { it('correct', () => { - expect(compare.compare([35, 40, 50], { $not: { $in: 45 } })).to.be.true + expect(compare([35, 40, 50], { $not: { $in: 45 } })).to.be.true }) it('wrong', () => { - expect(compare.compare([35, 40, 50], { + expect(compare([35, 40, 50], { $not: { $in: 35, $and: {