vitess.io/vitess@v0.16.2/web/vtadmin/src/util/filterNouns.test.ts (about)

     1  import { filterNouns } from './filterNouns';
     2  
     3  describe('filterNouns', () => {
     4      const tests: {
     5          name: string;
     6          needle: string;
     7          haystack: any[];
     8          expected: any[];
     9      }[] = [
    10          {
    11              name: 'it returns the haystack for an empty needle',
    12              needle: '',
    13              haystack: [{ goodnight: 'moon' }, { goodnight: 'stars' }],
    14              expected: [{ goodnight: 'moon' }, { goodnight: 'stars' }],
    15          },
    16          {
    17              name: 'it combines phrases and non-phrases',
    18              needle: '"astronomy" moon',
    19              haystack: [
    20                  { keyspace: 'astronomy', name: 'moon' },
    21                  { keyspace: 'astronomy', name: 'moonbeam' },
    22                  { keyspace: 'gastronomy', name: 'mooncake' },
    23              ],
    24              expected: [
    25                  { keyspace: 'astronomy', name: 'moon' },
    26                  { keyspace: 'astronomy', name: 'moonbeam' },
    27              ],
    28          },
    29          {
    30              name: 'it matches key/values with the same key additively',
    31              needle: 'hello:world hello:sun',
    32              haystack: [{ hello: 'world' }, { hello: 'moon' }, { hello: 'sun' }],
    33              expected: [{ hello: 'world' }, { hello: 'sun' }],
    34          },
    35          {
    36              name: 'it matches key/values with different keys subtractively',
    37              needle: 'hello:sun goodbye:moon',
    38              haystack: [
    39                  { hello: 'sun', goodbye: 'stars' },
    40                  { hello: 'sun', goodbye: 'moon' },
    41                  { hello: 'stars', goodbye: 'moon' },
    42              ],
    43              expected: [{ hello: 'sun', goodbye: 'moon' }],
    44          },
    45          {
    46              name: 'it omits nouns with empty values for a specified key',
    47              needle: 'goodbye:moon',
    48              haystack: [{ hello: 'sun', goodbye: 'moon' }, { hello: 'stars' }],
    49              expected: [{ hello: 'sun', goodbye: 'moon' }],
    50          },
    51          {
    52              name: 'it matches queries with multiple key/value tokens and also a fuzzy string for good measure',
    53              needle: 'cluster:cluster-1 keyspace:customers keyspace:commerce re',
    54              haystack: [
    55                  { cluster: 'cluster-2', keyspace: 'customers', type: 'replica' },
    56                  { cluster: 'cluster-1', keyspace: 'customers', type: 'readonly' },
    57                  { cluster: 'cluster-1', keyspace: 'customers', type: 'primary' },
    58                  { cluster: 'cluster-1', keyspace: 'loadtest', type: 'replica' },
    59                  { cluster: 'cluster-1', keyspace: 'commerce', type: 'replica' },
    60                  { cluster: 'cluster-1', keyspace: 'commerce', type: 'primary' },
    61              ],
    62              expected: [
    63                  { cluster: 'cluster-1', keyspace: 'customers', type: 'readonly' },
    64                  { cluster: 'cluster-1', keyspace: 'commerce', type: 'replica' },
    65              ],
    66          },
    67      ];
    68  
    69      test.each(tests.map(Object.values))('%s', (name: string, needle: string, haystack: any[], expected: any[]) => {
    70          const result = filterNouns(needle, haystack);
    71          expect(result).toEqual(expected);
    72      });
    73  });