Skip to content

Commit

Permalink
Add common tests and fix typo
Browse files Browse the repository at this point in the history
  • Loading branch information
289Adam289 committed Dec 9, 2024
1 parent eb896c6 commit 40e4b77
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/libs/SearchQueryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ function getQueryWithUpdatedValues(query: string, policyID?: string) {
}

/**
* A mapping object that transforms filter names from the internal codebase format to user-friendly names.
* A mapping object that maps filter names from the internal codebase format to user-friendly names.
*/
const UserFriendlyKeyMap: Record<SearchFilterKey | typeof CONST.SEARCH.SYNTAX_ROOT_KEYS.SORT_BY | typeof CONST.SEARCH.SYNTAX_ROOT_KEYS.SORT_ORDER, UserFriendlyKey> = {
type: 'type',
Expand Down
69 changes: 55 additions & 14 deletions tests/unit/SearchAutocompleteParserTest.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,84 @@
import type {SearchQueryJSON} from '@components/Search/types';
import * as autocompleteParser from '@libs/SearchParser/autocompleteParser';
import parserCommonTests from '../utils/fixtures/searchParsersCommonQueries';

const tests = [
{
query: 'date>2024-01-01 amount>100 merchant:"A B" description:A,B,C ,, reportid:123456789 word',
query: parserCommonTests.simple,
expected: {
autocomplete: null,
ranges: [],
autocomplete: {
key: 'status',
value: 'all',
start: 20,
length: 3,
},
ranges: [
{key: 'type', value: 'expense', start: 5, length: 7},
{key: 'status', value: 'all', start: 20, length: 3},
],
},
},
{
query: ',',
query: parserCommonTests.userFriendlyNames,
expected: {
autocomplete: null,
ranges: [],
ranges: [
{key: 'taxRate', value: 'rate1', start: 9, length: 5},
{key: 'expenseType', value: 'card', start: 28, length: 4},
{key: 'cardID', value: 'Big Bank', start: 38, length: 10},
],
},
},
{
query: 'tag:,,',
query: parserCommonTests.oldNames,
expected: {
autocomplete: null,
ranges: [],
ranges: [
{key: 'taxRate', value: 'rate1', start: 8, length: 5},
{key: 'expenseType', value: 'card', start: 26, length: 4},
{key: 'cardID', value: 'Big Bank', start: 38, length: 10},
],
},
},
{
query: 'type:expense status:all',
query: parserCommonTests.complex,
expected: {
autocomplete: {
key: 'status',
value: 'all',
start: 20,
length: 3,
key: 'category',
length: 22,
start: 102,
value: 'meal & entertainment',
},
ranges: [
{key: 'type', value: 'expense', start: 5, length: 7},
{key: 'status', value: 'all', start: 20, length: 3},
{key: 'expenseType', length: 4, start: 24, value: 'cash'},
{key: 'expenseType', length: 4, start: 29, value: 'card'},
{key: 'category', length: 6, start: 89, value: 'travel'},
{key: 'category', length: 5, start: 96, value: 'hotel'},
{key: 'category', length: 22, start: 102, value: 'meal & entertainment'},
],
},
},
{
query: 'date>2024-01-01 amount>100 merchant:"A B" description:A,B,C ,, reportid:123456789 word',
expected: {
autocomplete: null,
ranges: [],
},
},
{
query: ',',
expected: {
autocomplete: null,
ranges: [],
},
},
{
query: 'tag:,,',
expected: {
autocomplete: null,
ranges: [],
},
},
{
query: 'in:123456 currency:USD ',
expected: {
Expand Down
171 changes: 124 additions & 47 deletions tests/unit/SearchParserTest.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type {SearchQueryJSON} from '@components/Search/types';
import * as searchParser from '@libs/SearchParser/searchParser';
import parserCommonTests from '../utils/fixtures/searchParsersCommonQueries';

const tests = [
{
query: 'type:expense status:all',
query: parserCommonTests.simple,
expected: {
type: 'expense',
status: 'all',
Expand All @@ -12,6 +13,128 @@ const tests = [
filters: null,
},
},
{
query: parserCommonTests.userFriendlyNames,
expected: {
type: 'expense',
status: 'all',
sortBy: 'date',
sortOrder: 'desc',
filters: {
operator: 'and',
left: {
operator: 'and',
left: {
operator: 'and',
left: {
operator: 'eq',
left: 'taxRate',
right: 'rate1',
},
right: {
operator: 'eq',
left: 'expenseType',
right: 'card',
},
},
right: {
operator: 'eq',
left: 'cardID',
right: 'Big Bank',
},
},
right: {
operator: 'eq',
left: 'reportID',
right: 'report',
},
},
},
},
{
query: parserCommonTests.oldNames,
expected: {
type: 'expense',
status: 'all',
sortBy: 'date',
sortOrder: 'desc',
filters: {
operator: 'and',
left: {
operator: 'and',
left: {
operator: 'and',
left: {
operator: 'eq',
left: 'taxRate',
right: 'rate1',
},
right: {
operator: 'eq',
left: 'expenseType',
right: 'card',
},
},
right: {
operator: 'eq',
left: 'cardID',
right: 'Big Bank',
},
},
right: {
operator: 'eq',
left: 'reportID',
right: 'report',
},
},
},
},
{
query: parserCommonTests.complex,
expected: {
type: 'expense',
status: 'all',
sortBy: 'date',
sortOrder: 'desc',
filters: {
operator: 'and',
left: {
operator: 'and',
left: {
operator: 'and',
left: {
operator: 'and',
left: {
operator: 'gt',
left: 'amount',
right: '200',
},
right: {
operator: 'eq',
left: 'expenseType',
right: ['cash', 'card'],
},
},
right: {
operator: 'eq',
left: 'description',
right: 'Las Vegas party',
},
},
right: {
operator: 'eq',
left: 'date',
right: '2024-06-01',
},
},
right: {
operator: 'eq',
left: 'category',
right: ['travel', 'hotel', 'meal & entertainment'],
},
},
},
},
{
query: ',',
expected: {
Expand Down Expand Up @@ -228,52 +351,6 @@ const tests = [
},
},
},
{
query: 'amount>200 expense-type:cash,card description:"Las Vegas party" date:2024-06-01 category:travel,hotel,"meal & entertainment"',
expected: {
type: 'expense',
status: 'all',
sortBy: 'date',
sortOrder: 'desc',
filters: {
operator: 'and',
left: {
operator: 'and',
left: {
operator: 'and',
left: {
operator: 'and',
left: {
operator: 'gt',
left: 'amount',
right: '200',
},
right: {
operator: 'eq',
left: 'expenseType',
right: ['cash', 'card'],
},
},
right: {
operator: 'eq',
left: 'description',
right: 'Las Vegas party',
},
},
right: {
operator: 'eq',
left: 'date',
right: '2024-06-01',
},
},
right: {
operator: 'eq',
left: 'category',
right: ['travel', 'hotel', 'meal & entertainment'],
},
},
},
},
{
query: 'amount>200 las vegas',
expected: {
Expand Down
11 changes: 11 additions & 0 deletions tests/utils/fixtures/searchParsersCommonQueries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Tests to ensure that both parsers use the same set of base rules
*/
const parserCommonTests = {
simple: 'type:expense status:all',
userFriendlyNames: 'tax-rate:rate1 expense-type:card card:"Big Bank" reportid:report',
oldNames: 'taxRate:rate1 expenseType:card cardID:"Big Bank" reportID:report',
complex: 'amount>200 expense-type:cash,card description:"Las Vegas party" date:2024-06-01 category:travel,hotel,"meal & entertainment"',
};

export default parserCommonTests;

0 comments on commit 40e4b77

Please sign in to comment.