Refactor, add coverage
This commit is contained in:
144
src-ui/src/app/utils/custom-field-query-element.spec.ts
Normal file
144
src-ui/src/app/utils/custom-field-query-element.spec.ts
Normal file
@@ -0,0 +1,144 @@
|
||||
import {
|
||||
CustomFieldQueryElement,
|
||||
CustomFieldQueryAtom,
|
||||
CustomFieldQueryExpression,
|
||||
} from './custom-field-query-element'
|
||||
import {
|
||||
CustomFieldQueryElementType,
|
||||
CustomFieldQueryLogicalOperator,
|
||||
CustomFieldQueryOperator,
|
||||
} from '../data/custom-field-query'
|
||||
|
||||
describe('CustomFieldQueryElement', () => {
|
||||
it('should initialize with correct type and id', () => {
|
||||
const element = new CustomFieldQueryElement(
|
||||
CustomFieldQueryElementType.Atom
|
||||
)
|
||||
expect(element.type).toBe(CustomFieldQueryElementType.Atom)
|
||||
expect(element.id).toBeDefined()
|
||||
})
|
||||
|
||||
it('should trigger changed on operator change', () => {
|
||||
const element = new CustomFieldQueryElement(
|
||||
CustomFieldQueryElementType.Atom
|
||||
)
|
||||
element.changed.subscribe((changedElement) => {
|
||||
expect(changedElement).toBe(element)
|
||||
})
|
||||
element.operator = CustomFieldQueryOperator.Exists
|
||||
})
|
||||
|
||||
it('should trigger changed subject on value change', () => {
|
||||
const element = new CustomFieldQueryElement(
|
||||
CustomFieldQueryElementType.Atom
|
||||
)
|
||||
element.changed.subscribe((changedElement) => {
|
||||
expect(changedElement).toBe(element)
|
||||
})
|
||||
element.value = 'new value'
|
||||
})
|
||||
|
||||
it('should throw error on serialize call', () => {
|
||||
const element = new CustomFieldQueryElement(
|
||||
CustomFieldQueryElementType.Atom
|
||||
)
|
||||
expect(() => element.serialize()).toThrow('Implemented in subclass')
|
||||
})
|
||||
})
|
||||
|
||||
describe('CustomFieldQueryAtom', () => {
|
||||
it('should initialize with correct field, operator, and value', () => {
|
||||
const atom = new CustomFieldQueryAtom([1, 'operator', 'value'])
|
||||
expect(atom.field).toBe(1)
|
||||
expect(atom.operator).toBe('operator')
|
||||
expect(atom.value).toBe('value')
|
||||
})
|
||||
|
||||
it('should trigger changed subject on field change', () => {
|
||||
const atom = new CustomFieldQueryAtom()
|
||||
atom.changed.subscribe((changedAtom) => {
|
||||
expect(changedAtom).toBe(atom)
|
||||
})
|
||||
atom.field = 2
|
||||
})
|
||||
|
||||
it('should set value to null if operator is not found in CUSTOM_FIELD_QUERY_VALUE_TYPES_BY_OPERATOR', () => {
|
||||
const atom = new CustomFieldQueryAtom()
|
||||
atom.operator = 'nonexistent_operator'
|
||||
expect(atom.value).toBeNull()
|
||||
})
|
||||
|
||||
it('should set value to empty string if new type is string', () => {
|
||||
const atom = new CustomFieldQueryAtom()
|
||||
atom.operator = CustomFieldQueryOperator.IContains
|
||||
expect(atom.value).toBe('')
|
||||
})
|
||||
|
||||
it('should set value to "true" if new type is boolean', () => {
|
||||
const atom = new CustomFieldQueryAtom()
|
||||
atom.operator = CustomFieldQueryOperator.Exists
|
||||
expect(atom.value).toBe('true')
|
||||
})
|
||||
|
||||
it('should set value to empty array if new type is array', () => {
|
||||
const atom = new CustomFieldQueryAtom()
|
||||
atom.operator = CustomFieldQueryOperator.In
|
||||
expect(atom.value).toEqual([])
|
||||
})
|
||||
|
||||
it('should serialize correctly', () => {
|
||||
const atom = new CustomFieldQueryAtom([1, 'operator', 'value'])
|
||||
expect(atom.serialize()).toEqual([1, 'operator', 'value'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('CustomFieldQueryExpression', () => {
|
||||
it('should initialize with correct operator and value', () => {
|
||||
const expression = new CustomFieldQueryExpression([
|
||||
CustomFieldQueryLogicalOperator.And,
|
||||
[],
|
||||
])
|
||||
expect(expression.operator).toBe(CustomFieldQueryLogicalOperator.And)
|
||||
expect(expression.value).toEqual([])
|
||||
})
|
||||
|
||||
it('should add atom correctly', () => {
|
||||
const expression = new CustomFieldQueryExpression()
|
||||
const atom = new CustomFieldQueryAtom([
|
||||
1,
|
||||
CustomFieldQueryOperator.Exists,
|
||||
'true',
|
||||
])
|
||||
expression.addAtom(atom)
|
||||
expect(expression.value).toContain(atom)
|
||||
})
|
||||
|
||||
it('should add expression correctly', () => {
|
||||
const expression = new CustomFieldQueryExpression()
|
||||
const subExpression = new CustomFieldQueryExpression([
|
||||
CustomFieldQueryLogicalOperator.Or,
|
||||
[],
|
||||
])
|
||||
expression.addExpression(subExpression)
|
||||
expect(expression.value).toContain(subExpression)
|
||||
})
|
||||
|
||||
it('should serialize correctly', () => {
|
||||
const expression = new CustomFieldQueryExpression([
|
||||
CustomFieldQueryLogicalOperator.And,
|
||||
[[1, 'operator', 'value']],
|
||||
])
|
||||
expect(expression.serialize()).toEqual([
|
||||
CustomFieldQueryLogicalOperator.And,
|
||||
[[1, 'operator', 'value']],
|
||||
])
|
||||
})
|
||||
|
||||
it('should be negatable if it has one child which is an expression', () => {
|
||||
const expression = new CustomFieldQueryExpression([
|
||||
CustomFieldQueryLogicalOperator.Not,
|
||||
[[CustomFieldQueryLogicalOperator.Or, []]],
|
||||
])
|
||||
expect(expression.negatable).toBe(true)
|
||||
})
|
||||
})
|
||||
223
src-ui/src/app/utils/custom-field-query-element.ts
Normal file
223
src-ui/src/app/utils/custom-field-query-element.ts
Normal file
@@ -0,0 +1,223 @@
|
||||
import { Subject, debounceTime, distinctUntilChanged } from 'rxjs'
|
||||
import { v4 as uuidv4 } from 'uuid'
|
||||
import {
|
||||
CustomFieldQueryElementType,
|
||||
CUSTOM_FIELD_QUERY_VALUE_TYPES_BY_OPERATOR,
|
||||
CustomFieldQueryLogicalOperator,
|
||||
CustomFieldQueryOperator,
|
||||
} from '../data/custom-field-query'
|
||||
|
||||
export class CustomFieldQueryElement {
|
||||
public readonly type: CustomFieldQueryElementType
|
||||
public changed: Subject<CustomFieldQueryElement>
|
||||
protected valueModelChanged: Subject<
|
||||
string | string[] | number[] | CustomFieldQueryElement[]
|
||||
>
|
||||
public depth: number = 0
|
||||
public id: string = uuidv4()
|
||||
|
||||
constructor(type: CustomFieldQueryElementType) {
|
||||
this.type = type
|
||||
this.changed = new Subject<CustomFieldQueryElement>()
|
||||
this.valueModelChanged = new Subject<string | CustomFieldQueryElement[]>()
|
||||
this.connectValueModelChanged()
|
||||
}
|
||||
|
||||
protected connectValueModelChanged() {
|
||||
// Allows overriding in subclasses
|
||||
this.valueModelChanged.subscribe(() => {
|
||||
this.changed.next(this)
|
||||
})
|
||||
}
|
||||
|
||||
public serialize() {
|
||||
throw new Error('Implemented in subclass')
|
||||
}
|
||||
|
||||
protected _operator: string = null
|
||||
public set operator(value: string) {
|
||||
this._operator = value
|
||||
this.changed.next(this)
|
||||
}
|
||||
public get operator(): string {
|
||||
return this._operator
|
||||
}
|
||||
|
||||
protected _value: string | string[] | number[] | CustomFieldQueryElement[] =
|
||||
null
|
||||
public set value(
|
||||
value: string | string[] | number[] | CustomFieldQueryElement[]
|
||||
) {
|
||||
this._value = value
|
||||
this.valueModelChanged.next(value)
|
||||
}
|
||||
public get value(): string | string[] | number[] | CustomFieldQueryElement[] {
|
||||
return this._value
|
||||
}
|
||||
}
|
||||
|
||||
export class CustomFieldQueryAtom extends CustomFieldQueryElement {
|
||||
protected _field: number
|
||||
set field(field: any) {
|
||||
this._field = parseInt(field, 10)
|
||||
this.changed.next(this)
|
||||
}
|
||||
get field(): number {
|
||||
return this._field
|
||||
}
|
||||
|
||||
override set operator(operator: string) {
|
||||
const newTypes: string[] =
|
||||
CUSTOM_FIELD_QUERY_VALUE_TYPES_BY_OPERATOR[operator]?.split('|')
|
||||
if (!newTypes) {
|
||||
this.value = null
|
||||
} else {
|
||||
if (!newTypes.includes(typeof this.value)) {
|
||||
if (newTypes.length === 1) {
|
||||
switch (newTypes[0]) {
|
||||
case 'string':
|
||||
this.value = ''
|
||||
break
|
||||
case 'boolean':
|
||||
this.value = 'true'
|
||||
break
|
||||
case 'array':
|
||||
this.value = []
|
||||
break
|
||||
default:
|
||||
this.value = null
|
||||
break
|
||||
}
|
||||
} else {
|
||||
if (newTypes.includes('number')) {
|
||||
try {
|
||||
this.value = parseFloat(this.value as string).toString()
|
||||
} catch (e) {
|
||||
this.value = null
|
||||
}
|
||||
} else {
|
||||
this.value = null
|
||||
}
|
||||
}
|
||||
} else if (
|
||||
['true', 'false'].includes(this.value as string) &&
|
||||
newTypes.includes('string')
|
||||
) {
|
||||
this.value = ''
|
||||
}
|
||||
}
|
||||
super.operator = operator
|
||||
}
|
||||
|
||||
override get operator(): string {
|
||||
// why?
|
||||
return super.operator
|
||||
}
|
||||
|
||||
constructor(queryArray: [number, string, string] = [null, null, null]) {
|
||||
super(CustomFieldQueryElementType.Atom)
|
||||
;[this._field, this._operator, this._value] = queryArray
|
||||
}
|
||||
|
||||
protected override connectValueModelChanged(): void {
|
||||
this.valueModelChanged
|
||||
.pipe(debounceTime(1000), distinctUntilChanged())
|
||||
.subscribe(() => {
|
||||
this.changed.next(this)
|
||||
})
|
||||
}
|
||||
|
||||
public override serialize() {
|
||||
return [this._field, this._operator, this._value]
|
||||
}
|
||||
}
|
||||
|
||||
export class CustomFieldQueryExpression extends CustomFieldQueryElement {
|
||||
constructor(
|
||||
expressionArray: [CustomFieldQueryLogicalOperator, any[]] = [
|
||||
CustomFieldQueryLogicalOperator.Or,
|
||||
null,
|
||||
]
|
||||
) {
|
||||
super(CustomFieldQueryElementType.Expression)
|
||||
let values
|
||||
;[this._operator, values] = expressionArray
|
||||
if (!values || values.length === 0) {
|
||||
this._value = []
|
||||
} else if (values?.length > 0 && values[0] instanceof Array) {
|
||||
this._value = values.map((value) => {
|
||||
if (value.length === 3) {
|
||||
const atom = new CustomFieldQueryAtom(value)
|
||||
atom.depth = this.depth + 1
|
||||
atom.changed.subscribe(() => {
|
||||
this.changed.next(this)
|
||||
})
|
||||
return atom
|
||||
} else {
|
||||
const expression = new CustomFieldQueryExpression(value)
|
||||
expression.depth = this.depth + 1
|
||||
expression.changed.subscribe(() => {
|
||||
this.changed.next(this)
|
||||
})
|
||||
return expression
|
||||
}
|
||||
})
|
||||
} else {
|
||||
const expression = new CustomFieldQueryExpression(values as any)
|
||||
expression.depth = this.depth + 1
|
||||
expression.changed.subscribe(() => {
|
||||
this.changed.next(this)
|
||||
})
|
||||
this._value = [expression]
|
||||
}
|
||||
}
|
||||
|
||||
public override serialize() {
|
||||
let value
|
||||
if (this._value instanceof Array) {
|
||||
value = this._value.map((atom) => atom.serialize())
|
||||
// If the expression is negated it should have only one child which is an expression
|
||||
if (
|
||||
this._operator === CustomFieldQueryLogicalOperator.Not &&
|
||||
value.length === 1
|
||||
) {
|
||||
value = value[0]
|
||||
}
|
||||
} else {
|
||||
value = value.serialize()
|
||||
}
|
||||
return [this._operator, value]
|
||||
}
|
||||
|
||||
public addAtom(
|
||||
atom: CustomFieldQueryAtom = new CustomFieldQueryAtom([
|
||||
null,
|
||||
CustomFieldQueryOperator.Exists,
|
||||
'true',
|
||||
])
|
||||
) {
|
||||
atom.depth = this.depth + 1
|
||||
;(this._value as CustomFieldQueryElement[]).push(atom)
|
||||
atom.changed.subscribe(() => {
|
||||
this.changed.next(this)
|
||||
})
|
||||
}
|
||||
|
||||
public addExpression(
|
||||
expression: CustomFieldQueryExpression = new CustomFieldQueryExpression()
|
||||
) {
|
||||
expression.depth = this.depth + 1
|
||||
;(this._value as CustomFieldQueryElement[]).push(expression)
|
||||
expression.changed.subscribe(() => {
|
||||
this.changed.next(this)
|
||||
})
|
||||
}
|
||||
|
||||
public get negatable(): boolean {
|
||||
return (
|
||||
this.value.length === 1 &&
|
||||
(this.value[0] as CustomFieldQueryElement).type ===
|
||||
CustomFieldQueryElementType.Expression
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user