From a23336387bef8f69dfad55e6081381e7248d1af4 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 16 May 2020 14:51:39 -0500 Subject: [PATCH] feat: differnt types for FilterNode --- cli/index.js | 3 +- core/entities/nodes/FilterNode.js | 26 +-- tests/core/nodes/filterNodeTests.js | 265 +++++++++++++++++++++++++++- 3 files changed, 279 insertions(+), 15 deletions(-) diff --git a/cli/index.js b/cli/index.js index d328132..c0fa798 100644 --- a/cli/index.js +++ b/cli/index.js @@ -26,7 +26,8 @@ try { filterParams: { siteName: 'Seewee Road DMS', fieldMonitorUserCertificationNumber: '9YW6ZAY' - } + }, + type: 'EQUAL' }) console.log(filterNode.tables[0].getRows().length) console.log(filterNode.export().length) diff --git a/core/entities/nodes/FilterNode.js b/core/entities/nodes/FilterNode.js index fe4ff13..4e8b83b 100644 --- a/core/entities/nodes/FilterNode.js +++ b/core/entities/nodes/FilterNode.js @@ -22,12 +22,6 @@ class FilterNode extends Node { export = () => { let rows = this.tables.map(t => t.getRows() ).flat() let filters = this._createFilterMethods() - for (let key in this.filterParams) { - const filterMethod = t => { - return t[key] === this.filterParams[key] - } - filters.push(filterMethod) - } filters.forEach(f => { rows = rows.filter(f) @@ -36,21 +30,29 @@ class FilterNode extends Node { return rows } - _assignProps = props => { this.filterParams = props.filterParams || {} if (props.type) this.setType(props.type) } _createFilterMethods = () => { + const typeValidation = this._validateType(this.type) + if (typeValidation.status !== 'OK') throw typeValidation + let filters = [] for (let key in this.filterParams) { let filterMethod = {} - if (this.type === filterTypes.EQUAL){ - filterMethod = t => { - return t[key] === this.filterParams[key] - } - } + if (this.type === filterTypes.EQUAL) + filterMethod = t => t[key] === this.filterParams[key] + else if (this.type === filterTypes.GREATER) + filterMethod = t => t[key] > this.filterParams[key] + else if (this.type === filterTypes.GREATEREQUAL) + filterMethod = t => t[key] >= this.filterParams[key] + else if (this.type === filterTypes.LESSER) + filterMethod = t => t[key] < this.filterParams[key] + else if (this.type === filterTypes.LESSEREQUAL) + filterMethod = t => t[key] <= this.filterParams[key] + filters.push(filterMethod) } return filters diff --git a/tests/core/nodes/filterNodeTests.js b/tests/core/nodes/filterNodeTests.js index 3df7989..618e582 100644 --- a/tests/core/nodes/filterNodeTests.js +++ b/tests/core/nodes/filterNodeTests.js @@ -1,7 +1,7 @@ import FilterNode from '../../../core/entities/nodes/FilterNode.js' import Table from '../../../core/entities/Table.js' -const exportRows = () => { +const equalFilter = () => { const expectedOutput = [ { id: 'abc', data: 'row', contractor: 'AshBritt' }, { id: 'XYZ', data: 'row', contractor: 'AshBritt' } @@ -101,6 +101,55 @@ const addFilter = () => { } } +const exportWithoutTypeErrorHandle = () => { + const expectedOutput = { + status: 'ERR', + error: { + label: 'Filter Type is not valid', + messages: ['Type must be one of: EQUAL,GREATER,GREATEREQUAL,LESSER,LESSEREQUAL'] + } + } + + let table = {} + try { + table = new Table({ + id: 'XYZ', + label: 'Test Table', + rows: [ + { id: 'abc', data: 'row', contractor: 'AshBritt' } + ] + }) + } catch (err) { + return false + } + + let filterNode = {} + try { + filterNode = new FilterNode({ + id: 'ABC', + label: 'Test Node', + tables: [table], + filterParams: { + data: 'row', + contractor: 'AshBritt' + } + }) + } catch (err) { + console.log(err) + return false + } + + try { + filterNode.export() + } catch (err) { + if (JSON.stringify(err) === JSON.stringify(expectedOutput)) { + return true + } else { + return false + } + } +} + const setType = () => { const expectedOutput = [ { id: 'abc', data: 'row', contractor: 'AshBritt' }, @@ -154,9 +203,221 @@ const setType = () => { } } +const greaterFilter = () => { + const expectedOutput = [ + { id: 'abc', count: 5, contractor: 'AshBritt' }, + { id: 'XYZ', count: 8, contractor: 'AshBritt' } + ] + + let table = {} + try { + table = new Table({ + id: 'XYZ', + label: 'Test Table', + rows: [ + { id: 'abc', count: 5, contractor: 'AshBritt' }, + { id: 'qwe', count: 4, contractor: 'AshBritt' }, + { id: 'XYZ', count: 8, contractor: 'AshBritt' }, + { id: 'xyz', count: 2, contractor: 'HeyDay' }, + ] + }) + } catch (err) { + return false + } + + let filterNode = {} + try { + filterNode = new FilterNode({ + id: 'ABC', + label: 'Test Node', + tables: [table], + filterParams: { + count: 4 + }, + type: 'GREATER' + }) + } catch (err) { + console.log(err) + return false + } + + let filteredRows = [] + try { + filteredRows = filterNode.export() + } catch (err) { + console.log(err) + return false + } + if (JSON.stringify(filteredRows) === JSON.stringify(expectedOutput)) { + return true + } else { + return false + } +} + +const greaterEqualFilter = () => { + const expectedOutput = [ + { id: 'abc', count: 5, contractor: 'AshBritt' }, + { id: 'qwe', count: 4, contractor: 'AshBritt' }, + { id: 'XYZ', count: 8, contractor: 'AshBritt' } + ] + + let table = {} + try { + table = new Table({ + id: 'XYZ', + label: 'Test Table', + rows: [ + { id: 'abc', count: 5, contractor: 'AshBritt' }, + { id: 'qwe', count: 4, contractor: 'AshBritt' }, + { id: 'XYZ', count: 8, contractor: 'AshBritt' }, + { id: 'xyz', count: 2, contractor: 'HeyDay' }, + ] + }) + } catch (err) { + return false + } + + let filterNode = {} + try { + filterNode = new FilterNode({ + id: 'ABC', + label: 'Test Node', + tables: [table], + filterParams: { + count: 4 + }, + type: 'GREATEREQUAL' + }) + } catch (err) { + console.log(err) + return false + } + + let filteredRows = [] + try { + filteredRows = filterNode.export() + } catch (err) { + console.log(err) + return false + } + if (JSON.stringify(filteredRows) === JSON.stringify(expectedOutput)) { + return true + } else { + return false + } +} + +const lesserFilter = () => { + const expectedOutput = [ + { id: 'xyz', count: 2, contractor: 'HeyDay' } + ] + + let table = {} + try { + table = new Table({ + id: 'XYZ', + label: 'Test Table', + rows: [ + { id: 'abc', count: 5, contractor: 'AshBritt' }, + { id: 'qwe', count: 4, contractor: 'AshBritt' }, + { id: 'XYZ', count: 8, contractor: 'AshBritt' }, + { id: 'xyz', count: 2, contractor: 'HeyDay' }, + ] + }) + } catch (err) { + return false + } + + let filterNode = {} + try { + filterNode = new FilterNode({ + id: 'ABC', + label: 'Test Node', + tables: [table], + filterParams: { + count: 4 + }, + type: 'LESSER' + }) + } catch (err) { + console.log(err) + return false + } + + let filteredRows = [] + try { + filteredRows = filterNode.export() + } catch (err) { + console.log(err) + return false + } + if (JSON.stringify(filteredRows) === JSON.stringify(expectedOutput)) { + return true + } else { + return false + } +} + +const lesserEqualFilter = () => { + const expectedOutput = [ + { id: 'qwe', count: 4, contractor: 'AshBritt' }, + { id: 'xyz', count: 2, contractor: 'HeyDay' } + ] + + let table = {} + try { + table = new Table({ + id: 'XYZ', + label: 'Test Table', + rows: [ + { id: 'abc', count: 5, contractor: 'AshBritt' }, + { id: 'qwe', count: 4, contractor: 'AshBritt' }, + { id: 'XYZ', count: 8, contractor: 'AshBritt' }, + { id: 'xyz', count: 2, contractor: 'HeyDay' }, + ] + }) + } catch (err) { + return false + } + + let filterNode = {} + try { + filterNode = new FilterNode({ + id: 'ABC', + label: 'Test Node', + tables: [table], + filterParams: { + count: 4 + }, + type: 'LESSEREQUAL' + }) + } catch (err) { + console.log(err) + return false + } + + let filteredRows = [] + try { + filteredRows = filterNode.export() + } catch (err) { + console.log(err) + return false + } + if (JSON.stringify(filteredRows) === JSON.stringify(expectedOutput)) { + return true + } else { + return false + } +} export default [ - { name: 'Entity | FilterNode Export Rows', test: exportRows }, + { name: 'Entity | FilterNode Equal Filter', test: equalFilter }, + { name: 'Entity | FilterNode GREATER Filter', test: greaterFilter }, + { name: 'Entity | FilterNode GREATEREQUAL Filter', test: greaterEqualFilter }, + { name: 'Entity | FilterNode LESSER Filter', test: lesserFilter }, + { name: 'Entity | FilterNode LESSEREQUAL Filter', test: lesserEqualFilter }, { name: 'Entity | FilterNode Add Filter', test: addFilter }, { name: 'Entity | FilterNode Set Type', test: setType }, + { name: 'Entity | FilterNode Export Without Type Error', test: exportWithoutTypeErrorHandle }, ] \ No newline at end of file