From e900dac47a26cd2f8422584d3eaa9c0a94f7073a Mon Sep 17 00:00:00 2001 From: ydkmlt84 Date: Thu, 2 Jan 2025 22:13:12 -0500 Subject: [PATCH] fix: Equals with lists now only returns true if they are identical Previously, equals would check that every element in firstValue is in the secondValue list (case insensitive). This meant that the secondValue list could contain a value not within firstValue and still return true. --- .../rules/helpers/rule.comparator.service.ts | 77 +-- ...le.comparator.service.doRuleAction.spec.ts | 547 ++++-------------- 2 files changed, 167 insertions(+), 457 deletions(-) diff --git a/server/src/modules/rules/helpers/rule.comparator.service.ts b/server/src/modules/rules/helpers/rule.comparator.service.ts index cd394141..4ef68214 100644 --- a/server/src/modules/rules/helpers/rule.comparator.service.ts +++ b/server/src/modules/rules/helpers/rule.comparator.service.ts @@ -374,68 +374,72 @@ export class RuleComparatorService { } private doRuleAction(val1: T, val2: T, action: RulePossibility): boolean { - if ( - typeof val1 === 'string' || - (Array.isArray(val1) ? typeof val1[0] === 'string' : false) - ) { - val1 = Array.isArray(val1) - ? (val1.map((el) => el?.toLowerCase()) as unknown as T) - : ((val1 as string)?.toLowerCase() as unknown as T); + if (typeof val1 === 'string') { + val1 = val1.toLowerCase() as T; } - if ( - typeof val2 === 'string' || - (Array.isArray(val2) ? typeof val2[0] === 'string' : false) - ) { - val2 = Array.isArray(val2) - ? (val2.map((el) => el?.toLowerCase()) as unknown as T) - : ((val2 as string)?.toLowerCase() as unknown as T); + + if (typeof val2 === 'string') { + val2 = val2.toLowerCase() as T; + } + + if (Array.isArray(val1)) { + val1 = val1.map((el) => + typeof el == 'string' ? el.toLowerCase() : el, + ) as T; } + + if (Array.isArray(val2)) { + val2 = val2.map((el) => + typeof el == 'string' ? el.toLowerCase() : el, + ) as T; + } + if (action === RulePossibility.BIGGER) { return val1 > val2; } + if (action === RulePossibility.SMALLER) { return val1 < val2; } + if (action === RulePossibility.EQUALS) { if (!Array.isArray(val1)) { if (val1 instanceof Date && val2 instanceof Date) { return ( - new Date(val1?.toDateString()).valueOf() === - new Date(val2?.toDateString()).valueOf() + new Date(val1.toDateString()).valueOf() === + new Date(val2.toDateString()).valueOf() ); } + if (typeof val1 === 'boolean') { return val1 == val2; } + return val1 === val2; } else { - if (val1.length > 0) { - return val1?.every((e) => { - e = - typeof e === 'string' - ? (e as unknown as string)?.toLowerCase() - : e; - if (Array.isArray(val2)) { - return (val2 as unknown as T[])?.includes(e); - } else { - return e === val2; - } - }); + const val2Array = Array.isArray(val2) ? val2 : [val2]; + + if (val1.length === val2Array.length) { + const set1 = new Set(val1); + const set2 = new Set(val2Array); + return [...set1].every((value) => set2.has(value)); } else { return false; } } } + if (action === RulePossibility.NOT_EQUALS) { return !this.doRuleAction(val1, val2, RulePossibility.EQUALS); } + if (action === RulePossibility.CONTAINS) { try { if (!Array.isArray(val2)) { return (val1 as unknown as T[])?.includes(val2); } else { if (val2.length > 0) { - return val2?.some((el) => { + return val2.some((el) => { return (val1 as unknown as T[])?.includes(el); }); } else { @@ -446,12 +450,12 @@ export class RuleComparatorService { return null; } } + if (action === RulePossibility.CONTAINS_PARTIAL) { try { if (!Array.isArray(val2)) { - // return (val1 as unknown as T[])?.includes(val2); return ( - (Array.isArray(val1) ? (val1 as unknown as T[]) : [val1])?.some( + (Array.isArray(val1) ? (val1 as unknown as T[]) : [val1]).some( (line) => { return typeof line === 'string' && val2 != undefined && @@ -465,10 +469,9 @@ export class RuleComparatorService { ); } else { if (val2.length > 0) { - return val2?.some((el) => { - // return (val1 as unknown as T[])?.includes(el); + return val2.some((el) => { return ( - (val1 as unknown as T[])?.some((line) => { + (val1 as unknown as T[]).some((line) => { return typeof line === 'string' && el != undefined && el.length > 0 @@ -487,24 +490,30 @@ export class RuleComparatorService { return null; } } + if (action === RulePossibility.NOT_CONTAINS) { return !this.doRuleAction(val1, val2, RulePossibility.CONTAINS); } + if (action === RulePossibility.NOT_CONTAINS_PARTIAL) { return !this.doRuleAction(val1, val2, RulePossibility.CONTAINS_PARTIAL); } + if (action === RulePossibility.BEFORE) { return val1 && val2 ? val1 <= val2 : false; } + if (action === RulePossibility.AFTER) { return val1 && val2 ? val1 >= val2 : false; } + if (action === RulePossibility.IN_LAST) { return ( val1 >= val2 && // time in s (val1 as unknown as Date) <= new Date() ); } + if (action === RulePossibility.IN_NEXT) { return ( val1 <= val2 && // time in s diff --git a/server/src/modules/rules/tests/rule.comparator.service.doRuleAction.spec.ts b/server/src/modules/rules/tests/rule.comparator.service.doRuleAction.spec.ts index 699bf20d..e82eea01 100644 --- a/server/src/modules/rules/tests/rule.comparator.service.doRuleAction.spec.ts +++ b/server/src/modules/rules/tests/rule.comparator.service.doRuleAction.spec.ts @@ -17,413 +17,130 @@ describe('RuleComparatorService', () => { }); describe('doRuleAction', () => { - it('should return true when comparing two strings with action EQUALS', () => { - const val1 = 'abc'; - const val2 = 'abc'; - const action = RulePossibility.EQUALS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return false when comparing strings with action EQUALS and value is an empty string', () => { - const val1 = 'abc'; - const val2 = ''; - const action = RulePossibility.EQUALS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return false when comparing two strings with action EQUALS and value is undefined', () => { - const val1 = 'abc'; - const val2 = undefined; - const action = RulePossibility.EQUALS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return false when comparing two strings with action EQUALS', () => { - const val1 = 'abc'; - const val2 = 'abd'; - const action = RulePossibility.EQUALS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return false when comparing two strings with action NOT_EQUALS', () => { - const val1 = 'abc'; - const val2 = 'abc'; - const action = RulePossibility.NOT_EQUALS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return true when comparing two strings with action NOT_EQUALS', () => { - const val1 = 'abc'; - const val2 = 'abd'; - const action = RulePossibility.NOT_EQUALS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return true when comparing an array of strings with action EQUALS', () => { - const val1 = ['abc', 'def']; - const val2 = ['abc', 'def']; - const action = RulePossibility.EQUALS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return false when comparing a faulty array of strings with action EQUALS', () => { - const val1 = ['abc', 'def']; - const val2 = ['abc', 'cde']; - const action = RulePossibility.EQUALS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return false when comparing an array of strings with action NOT_EQUALS', () => { - const val1 = ['abc', 'def']; - const val2 = ['abc', 'def']; - const action = RulePossibility.NOT_EQUALS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return true when comparing a faulty array of strings with action NOT_EQUALS', () => { - const val1 = ['abc', 'def']; - const val2 = ['abc', 'cde']; - const action = RulePossibility.NOT_EQUALS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return true when comparing two dates with action EQUALS', () => { - const val1 = new Date('2022-01-01'); - const val2 = new Date('2022-01-01'); - const action = RulePossibility.EQUALS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return false when comparing two dates with action EQUALS', () => { - const val1 = new Date('2022-01-01'); - const val2 = new Date('2022-01-02'); - const action = RulePossibility.EQUALS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return false when comparing two dates with action NOT_EQUALS', () => { - const val1 = new Date('2022-01-01'); - const val2 = new Date('2022-01-01'); - const action = RulePossibility.NOT_EQUALS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return true when comparing two dates with action NOT_EQUALS', () => { - const val1 = new Date('2022-01-01'); - const val2 = new Date('2022-01-02'); - const action = RulePossibility.NOT_EQUALS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return true when comparing two numbers with action EQUALS', () => { - const val1 = 5; - const val2 = 5; - const action = RulePossibility.EQUALS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return false when comparing two numbers with action EQUALS', () => { - const val1 = 5; - const val2 = 4; - const action = RulePossibility.EQUALS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return false when comparing two numbers with action NOT_EQUALS', () => { - const val1 = 5; - const val2 = 5; - const action = RulePossibility.NOT_EQUALS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return true when comparing two numbers with action NOT_EQUALS', () => { - const val1 = 5; - const val2 = 4; - const action = RulePossibility.NOT_EQUALS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return true when comparing a string with action CONTAINS', () => { - const val1 = 'abc'; - const val2 = 'ab'; - const action = RulePossibility.CONTAINS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return false when comparing a string with action CONTAINS', () => { - const val1 = 'abc'; - const val2 = 'de'; - const action = RulePossibility.CONTAINS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return false when comparing a string with action CONTAINS_PARTIAL', () => { - const val1 = 'abc'; - const val2 = 'de'; - const action = RulePossibility.CONTAINS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return true when comparing a string with action CONTAINS_PARTIAL', () => { - const val1 = 'abc'; - const val2 = 'ab'; - const action = RulePossibility.CONTAINS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return true when comparing an array of strings with an exact match with action CONTAINS', () => { - const val1 = ['abc', 'def']; - const val2 = ['abc']; - const action = RulePossibility.CONTAINS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return true when comparing an array of strings with an exact match with action CONTAINS_PARTIAL', () => { - const val1 = ['abc', 'def']; - const val2 = ['abc']; - const action = RulePossibility.CONTAINS_PARTIAL; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return true when comparing an array of multiple word strings with action CONTAINS_PARTIAL', () => { - const val1 = ['ImDb top 250', 'My birthday', 'jef']; - const val2 = ['imdb']; - const action = RulePossibility.CONTAINS_PARTIAL; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return false when comparing an array of multiple word strings with action CONTAINS', () => { - const val1 = ['ImDb top 250', 'My birthday', 'jef']; - const val2 = ['imdb']; - const action = RulePossibility.CONTAINS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return false when comparing an array of multiple word strings with action CONTAINS_PARTIAL', () => { - const val1 = ['ImDb top 250', 'My birthday', 'jef']; - const val2 = ['jos']; - const action = RulePossibility.CONTAINS_PARTIAL; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return false when comparing an array of strings with action CONTAINS and value is an empty string', () => { - const val1 = ['abc', 'def']; - const val2 = ['']; - const action = RulePossibility.CONTAINS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return false when comparing an array of strings with action CONTAINS_PARTIAL and value is an empty string', () => { - const val1 = ['abc', 'def']; - const val2 = ['']; - const action = RulePossibility.CONTAINS_PARTIAL; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return false when comparing an array of strings with action CONTAINS and value contains undefined', () => { - const val1 = ['abc', 'def']; - const val2 = ['ral', undefined, 'rel']; - const action = RulePossibility.CONTAINS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return false when comparing an array of strings with action CONTAINS_PARTIAL and value contains undefined', () => { - const val1 = ['abc', 'def']; - const val2 = ['ral', undefined, 'rel']; - const action = RulePossibility.CONTAINS_PARTIAL; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return true when comparing an array of strings with action CONTAINS and value contains undefined', () => { - const val1 = ['abc', 'def']; - const val2 = ['ral', undefined, 'abc']; - const action = RulePossibility.CONTAINS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return true when comparing an array of strings with action CONTAINS_PARTIAL and value contains undefined and an exact match', () => { - const val1 = ['abc', 'def']; - const val2 = ['ral', undefined, 'abc']; - const action = RulePossibility.CONTAINS_PARTIAL; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return true when comparing an array of strings with action CONTAINS_PARTIAL and value contains undefined and a partial match', () => { - const val1 = ['abc', 'def']; - const val2 = ['ral', undefined, 'ab']; - const action = RulePossibility.CONTAINS_PARTIAL; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return false when comparing an array of strings with action CONTAINS', () => { - const val1 = ['abc', 'def']; - const val2 = ['ghi']; - const action = RulePossibility.CONTAINS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return true when comparing an array of strings with action NOT_CONTAINS', () => { - const val1 = ['abc', 'def']; - const val2 = ['ghi']; - const action = RulePossibility.NOT_CONTAINS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return true when comparing an array of multiple word strings with action NOT_CONTAINS', () => { - const val1 = ['ImDb top 250', 'My birthday', 'jef']; - const val2 = ['ImDb']; - const action = RulePossibility.NOT_CONTAINS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return false when comparing an array of multiple word strings with action NOT_CONTAINS_PARTIAL', () => { - const val1 = ['ImDb top 250', 'My birthday', 'jef']; - const val2 = ['ImDb']; - const action = RulePossibility.NOT_CONTAINS_PARTIAL; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return true when comparing an array of multiple word strings with action NOT_CONTAINS', () => { - const val1 = ['ImDb top 250', 'My birthday', 'jef']; - const val2 = ['Jos']; - const action = RulePossibility.NOT_CONTAINS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return true when comparing an array of multiple word strings with action NOT_CONTAINS_PARTIAL', () => { - const val1 = ['ImDb top 250', 'My birthday', 'jef']; - const val2 = ['Jos']; - const action = RulePossibility.NOT_CONTAINS_PARTIAL; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return false when comparing an array of strings with action NOT_CONTAINS', () => { - const val1 = ['abc', 'def']; - const val2 = ['abc']; - const action = RulePossibility.NOT_CONTAINS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return false when comparing an array of strings with action NOT_CONTAINS_PARTIAL', () => { - const val1 = ['abc', 'def']; - const val2 = ['abc']; - const action = RulePossibility.NOT_CONTAINS_PARTIAL; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return false when comparing an array of numbers against 1 value with action CONTAINS', () => { - const val1 = [1, 2, 3, 4]; - const val2 = [6]; - const action = RulePossibility.CONTAINS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return false when comparing an array of numbers against 1 value with action CONTAINS_PARTIAL', () => { - const val1 = [1, 2, 3, 4]; - const val2 = [6]; - const action = RulePossibility.CONTAINS_PARTIAL; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return true when comparing an array of numbers against 1 value with action NOT_CONTAINS', () => { - const val1 = [1, 2, 3, 4]; - const val2 = [6]; - const action = RulePossibility.NOT_CONTAINS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return true when comparing an array of numbers against 1 value with action NOT_CONTAINS and value contains undefined', () => { - const val1 = [1, 2, 3, 4]; - const val2 = [5, undefined, 6]; - const action = RulePossibility.NOT_CONTAINS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return true when comparing an array of numbers against 1 value with action CONTAINS', () => { - const val1 = [1, 2, 3, 4]; - const val2 = [3]; - const action = RulePossibility.CONTAINS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return false when comparing an array of numbers against 1 value with action NOT_CONTAINS', () => { - const val1 = [1, 2, 3, 4]; - const val2 = [3]; - const action = RulePossibility.NOT_CONTAINS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return false when comparing 2 arrays of multiple numbers with action CONTAINS', () => { - const val1 = [1, 2, 3, 4]; - const val2 = [6, 5]; - const action = RulePossibility.CONTAINS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - - it('should return true when comparing an array of multiple numbers with action CONTAINS', () => { - const val1 = [1, 2, 3, 4]; - const val2 = [3, 1]; - const action = RulePossibility.CONTAINS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return false when comparing an array of multiple numbers with action NOT_CONTAINS', () => { - const val1 = [1, 2, 3, 4]; - const val2 = [3, 5]; - const action = RulePossibility.NOT_CONTAINS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); + const equalsData = [ + [true, 'abc', 'abc'], + [true, 'abc', 'ABC'], + [true, ['abc', 'def'], ['abc', 'def']], + [true, ['abc', 'def'], ['ABC', 'DEF']], + [true, ['abc'], 'abc'], + [true, ['abc'], 'ABC'], + [true, new Date('2022-01-01'), new Date('2022-01-01')], + [true, 5, 5], + [false, 'abc', ''], + [false, 'abc', undefined], + [false, 'abc', 'abd'], + [false, ['abc'], ['abc', 'def']], + [false, ['abc', 'def'], ['abc']], + [false, ['abc', 'def'], ['abc', 'cde']], + [false, new Date('2022-01-01'), new Date('2022-01-02')], + [false, 5, 4], + ] as [boolean, any, any][]; + + it.each(equalsData)( + 'should return %s when val1 is %o and val2 is %o with action EQUALS', + (expected, val1, val2) => { + const action = RulePossibility.EQUALS; + const result = ruleComparatorService['doRuleAction']( + val1, + val2, + action, + ); + expect(result).toBe(expected); + }, + ); + + it.each(equalsData)( + 'should return %s when val1 is %o and val2 is %o with action NOT_EQUALS', + (expected, val1, val2) => { + const action = RulePossibility.NOT_EQUALS; + const result = ruleComparatorService['doRuleAction']( + val1, + val2, + action, + ); + expect(result).toBe(!expected); + }, + ); + + const containsData = [ + [true, 'abc', 'ab'], + [true, ['abc', 'def'], ['ral', undefined, 'abc']], + [true, [1, 2, 3, 4], [3]], + [true, [1, 2, 3, 4], [3, 1]], + [true, ['abc', 'def'], ['abc']], + [false, 'abc', 'de'], + [false, ['abc', 'def'], ['ral', undefined, 'rel']], + [false, ['abc', 'def'], ['ghi']], + [false, [1, 2, 3, 4], [6]], + [false, [1, 2, 3, 4], [6, 5]], + [false, ['ImDb top 250', 'My birthday', 'jef'], ['imdb']], + [false, ['abc', 'def'], ['']], + ] as [boolean, any, any][]; + + it.each(containsData)( + 'should return %s when val1 is %o and val2 is %o with action CONTAINS', + (expected, val1, val2) => { + const action = RulePossibility.CONTAINS; + const result = ruleComparatorService['doRuleAction']( + val1, + val2, + action, + ); + expect(result).toBe(expected); + }, + ); + + it.each(containsData)( + 'should return %s when val1 is %o and val2 is %o with action NOT_CONTAINS', + (expected, val1, val2) => { + const action = RulePossibility.NOT_CONTAINS; + const result = ruleComparatorService['doRuleAction']( + val1, + val2, + action, + ); + expect(result).toBe(!expected); + }, + ); + + const containsPartialData = [ + [true, 'abc', 'ab'], + [true, ['abc', 'def'], ['abc']], + [true, ['ImDb top 250', 'My birthday', 'jef'], ['imdb']], + [true, ['abc', 'def'], ['ral', undefined, 'abc']], + [true, ['abc', 'def'], ['ral', undefined, 'ab']], + [false, 'abc', 'de'], + [false, ['ImDb top 250', 'My birthday', 'jef'], ['jos']], + [false, ['abc', 'def'], ['']], + [false, ['abc', 'def'], ['ral', undefined, 'rel']], + [false, [1, 2, 3, 4], [6]], + ] as [boolean, any, any][]; + + it.each(containsPartialData)( + 'should return %s when val1 is %o and val2 is %o with action CONTAINS_PARTIAL', + (expected, val1, val2) => { + const action = RulePossibility.CONTAINS_PARTIAL; + const result = ruleComparatorService['doRuleAction']( + val1, + val2, + action, + ); + expect(result).toBe(expected); + }, + ); + + it.each(containsPartialData)( + 'should return %s when val1 is %o and val2 is %o with action NOT_CONTAINS_PARTIAL', + (expected, val1, val2) => { + const action = RulePossibility.NOT_CONTAINS_PARTIAL; + const result = ruleComparatorService['doRuleAction']( + val1, + val2, + action, + ); + expect(result).toBe(!expected); + }, + ); it('should return true when comparing two numbers with action BIGGER', () => { const val1 = 5; @@ -449,22 +166,6 @@ describe('RuleComparatorService', () => { expect(result).toBe(false); }); - it('should return true when comparing two numbers with action EQUALS', () => { - const val1 = 5; - const val2 = 5; - const action = RulePossibility.EQUALS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(true); - }); - - it('should return false when comparing two numbers with action NOT_EQUALS', () => { - const val1 = 5; - const val2 = 5; - const action = RulePossibility.NOT_EQUALS; - const result = ruleComparatorService['doRuleAction'](val1, val2, action); - expect(result).toBe(false); - }); - it('should return true when comparing two dates with action BEFORE', () => { const val1 = new Date('2022-01-01'); const val2 = new Date('2022-01-02');