-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement advanced search for distribution version on cluster list ta…
…ble (#3902) Signed-off-by: Randy Bruno Piverger <rbrunopi@redhat.com>
- Loading branch information
Showing
12 changed files
with
371 additions
and
112 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* Copyright Contributors to the Open Cluster Management project */ | ||
import { SearchOperator } from '../ui-components/AcmSearchInput' | ||
import { handleStandardComparison, handleSemverOperatorComparison } from './search-utils' | ||
|
||
const stringData = { | ||
stringOne: 'Adam', | ||
stringTwo: 'Becky', | ||
stringThree: 'Charlie', | ||
stringFour: 'David', | ||
stringFive: 'Eve', | ||
} | ||
const versionData = { | ||
versionOne: '1.0.0', | ||
versionTwo: '1.5.0', | ||
versionThree: '2.0.0', | ||
versionFour: '2.5.0', | ||
versionIncomplete: '1.', | ||
} | ||
|
||
describe('search-utils basic string operator', () => { | ||
const { stringOne, stringTwo, stringThree, stringFour, stringFive } = stringData | ||
it('can determine equals', () => { | ||
expect(handleStandardComparison(stringOne, stringOne, SearchOperator.Equals)).toBeTruthy() | ||
}) | ||
it('can determine greater', () => { | ||
expect(handleStandardComparison(stringOne, stringTwo, SearchOperator.GreaterThan)).toBeTruthy() | ||
}) | ||
it('can determine less', () => { | ||
expect(handleStandardComparison(stringFour, stringThree, SearchOperator.LessThan)).toBeTruthy() | ||
}) | ||
it('can determine greater than or equal to', () => { | ||
expect(handleStandardComparison(stringOne, stringOne, SearchOperator.GreaterThanOrEqualTo)).toBeTruthy() | ||
expect(handleStandardComparison(stringOne, stringTwo, SearchOperator.GreaterThanOrEqualTo)).toBeTruthy() | ||
}) | ||
it('can determine less than or equal to', () => { | ||
expect(handleStandardComparison(stringOne, stringOne, SearchOperator.LessThanOrEqualTo)).toBeTruthy() | ||
expect(handleStandardComparison(stringFive, stringOne, SearchOperator.LessThanOrEqualTo)).toBeTruthy() | ||
}) | ||
it('can determine non-equals', () => { | ||
expect(handleStandardComparison(stringOne, stringFive, SearchOperator.Equals)).toBeFalsy() | ||
}) | ||
}) | ||
|
||
describe('search-utils sermver operator', () => { | ||
const { versionOne, versionTwo, versionThree, versionFour, versionIncomplete } = versionData | ||
it('can determine greater semver', () => { | ||
expect(handleSemverOperatorComparison(versionFour, versionThree, SearchOperator.GreaterThan)).toBeTruthy() | ||
}) | ||
it('can determine less semver', () => { | ||
expect(handleSemverOperatorComparison(versionOne, versionTwo, SearchOperator.LessThan)).toBeTruthy() | ||
}) | ||
it('can determine equals semver', () => { | ||
expect(handleSemverOperatorComparison(versionOne, versionOne, SearchOperator.Equals)).toBeTruthy() | ||
}) | ||
it('can determine greater than or equal to semver', () => { | ||
expect(handleSemverOperatorComparison(versionThree, versionThree, SearchOperator.GreaterThanOrEqualTo)).toBeTruthy() | ||
expect(handleSemverOperatorComparison(versionFour, versionThree, SearchOperator.GreaterThanOrEqualTo)).toBeTruthy() | ||
}) | ||
it('can determine less than or equal to semver', () => { | ||
expect(handleSemverOperatorComparison(versionTwo, versionTwo, SearchOperator.LessThanOrEqualTo)).toBeTruthy() | ||
expect(handleSemverOperatorComparison(versionOne, versionTwo, SearchOperator.LessThanOrEqualTo)).toBeTruthy() | ||
}) | ||
it('can determine non-equal semver', () => { | ||
expect(handleSemverOperatorComparison(versionOne, versionTwo, SearchOperator.NotEquals)).toBeTruthy() | ||
}) | ||
it('can coerce incomplete semver strings', () => { | ||
expect(handleSemverOperatorComparison(versionIncomplete, versionOne, SearchOperator.Equals)).toBeTruthy() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,54 @@ | ||
/* Copyright Contributors to the Open Cluster Management project */ | ||
import * as semver from 'semver' | ||
import { SearchOperator } from '../ui-components/AcmSearchInput' | ||
|
||
export const handleOperatorComparison = (value: string, selectedValue: string) => { | ||
return value.localeCompare(selectedValue) === 0 | ||
// handleStandardComparison uses localeCompare's API to evaluate string sort order. We assume values sorted earlier are "greater than" | ||
export const handleStandardComparison = (valueOne: string, valueTwo: string, operator: SearchOperator) => { | ||
switch (operator) { | ||
case SearchOperator.Equals: | ||
return valueOne.localeCompare(valueTwo) === 0 | ||
case SearchOperator.GreaterThan: | ||
return valueOne.localeCompare(valueTwo) < 0 | ||
case SearchOperator.LessThan: | ||
return valueOne.localeCompare(valueTwo) > 0 | ||
case SearchOperator.GreaterThanOrEqualTo: | ||
return valueOne.localeCompare(valueTwo) < 0 || valueOne.localeCompare(valueTwo) === 0 | ||
case SearchOperator.LessThanOrEqualTo: | ||
return valueOne.localeCompare(valueTwo) > 0 || valueOne.localeCompare(valueTwo) === 0 | ||
case SearchOperator.NotEquals: | ||
return valueOne.localeCompare(valueTwo) !== 0 | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
// for a given displayVersion string there is a distribution value a ' ' and a semver value, here we divide them and take the semver | ||
export const handleSemverOperatorComparison = (versionOne: string, versionTwo: string, operator: SearchOperator) => { | ||
// Semver coerces the version to a valid semver version if possible, otherwise it returns the original value | ||
const coercedVersionOne = semver.valid(semver.coerce(versionOne)) ?? versionOne | ||
const coercedVersionTwo = semver.valid(semver.coerce(versionTwo)) ?? versionTwo | ||
|
||
const validInputSemvers = !!semver.valid(coercedVersionOne) && !!semver.valid(coercedVersionTwo) | ||
if (!validInputSemvers) { | ||
if (operator === SearchOperator.NotEquals) { | ||
return true | ||
} | ||
return false | ||
} | ||
switch (operator) { | ||
case SearchOperator.Equals: | ||
return semver.eq(coercedVersionOne, coercedVersionTwo) | ||
case SearchOperator.GreaterThan: | ||
return semver.gt(coercedVersionOne, coercedVersionTwo) | ||
case SearchOperator.LessThan: | ||
return semver.lt(coercedVersionOne, coercedVersionTwo) | ||
case SearchOperator.GreaterThanOrEqualTo: | ||
return semver.gte(coercedVersionOne, coercedVersionTwo) | ||
case SearchOperator.LessThanOrEqualTo: | ||
return semver.lte(coercedVersionOne, coercedVersionTwo) | ||
case SearchOperator.NotEquals: | ||
return !semver.eq(coercedVersionOne, coercedVersionTwo) | ||
default: | ||
return false | ||
} | ||
} |
Oops, something went wrong.