feat: differnt types for FilterNode

This commit is contained in:
ysandler 2020-05-16 14:51:39 -05:00 committed by root
parent fc18a3f66c
commit 113426cbaa
3 changed files with 279 additions and 15 deletions

View File

@ -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)

View File

@ -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

View File

@ -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 },
]