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

     1  /**
     2   * Copyright 2021 The Vitess Authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  import { vtadmin as pb } from '../proto/vtadmin';
    17  import { getTableDefinitions, TableDefinition } from './tableDefinitions';
    18  
    19  describe('getTableDefinitions', () => {
    20      const tests: {
    21          name: string;
    22          input: pb.Schema[] | null | undefined;
    23          expected: TableDefinition[];
    24      }[] = [
    25          {
    26              name: 'handles empty arrays',
    27              input: [],
    28              expected: [],
    29          },
    30          {
    31              name: 'handles undefined input',
    32              input: undefined,
    33              expected: [],
    34          },
    35          {
    36              name: 'handles null input',
    37              input: null,
    38              expected: [],
    39          },
    40          {
    41              name: 'extracts table definitions and sizes',
    42              input: [
    43                  pb.Schema.create({
    44                      cluster: { id: 'c1', name: 'cluster1' },
    45                      keyspace: 'fauna',
    46                      table_definitions: [{ name: 'cats' }, { name: 'dogs' }],
    47                      table_sizes: {
    48                          cats: { row_count: 1234, data_length: 4321 },
    49                          dogs: { row_count: 5678, data_length: 8765 },
    50                      },
    51                  }),
    52                  pb.Schema.create({
    53                      cluster: { id: 'c2', name: 'cluster2' },
    54                      keyspace: 'flora',
    55                      table_definitions: [{ name: 'trees' }, { name: 'flowers' }],
    56                      table_sizes: {
    57                          flowers: { row_count: 1234, data_length: 4321 },
    58                          trees: { row_count: 5678, data_length: 8765 },
    59                      },
    60                  }),
    61              ],
    62              expected: [
    63                  {
    64                      cluster: { id: 'c1', name: 'cluster1' },
    65                      keyspace: 'fauna',
    66                      tableDefinition: { name: 'cats' },
    67                      tableSize: { row_count: 1234, data_length: 4321 },
    68                  },
    69                  {
    70                      cluster: { id: 'c1', name: 'cluster1' },
    71                      keyspace: 'fauna',
    72                      tableDefinition: { name: 'dogs' },
    73                      tableSize: { row_count: 5678, data_length: 8765 },
    74                  },
    75                  {
    76                      cluster: { id: 'c2', name: 'cluster2' },
    77                      keyspace: 'flora',
    78                      tableDefinition: { name: 'trees' },
    79                      tableSize: { row_count: 5678, data_length: 8765 },
    80                  },
    81                  {
    82                      cluster: { id: 'c2', name: 'cluster2' },
    83                      keyspace: 'flora',
    84                      tableDefinition: { name: 'flowers' },
    85                      tableSize: { row_count: 1234, data_length: 4321 },
    86                  },
    87              ],
    88          },
    89          {
    90              name: 'handles when a table has a definition but no defined size',
    91              input: [
    92                  pb.Schema.create({
    93                      cluster: { id: 'c1', name: 'cluster1' },
    94                      keyspace: 'fauna',
    95                      table_definitions: [{ name: 'cats' }],
    96                  }),
    97              ],
    98              expected: [
    99                  {
   100                      cluster: { id: 'c1', name: 'cluster1' },
   101                      keyspace: 'fauna',
   102                      tableDefinition: { name: 'cats' },
   103                  },
   104              ],
   105          },
   106          {
   107              name: 'handles when a table defines sizes but not a definition',
   108              input: [
   109                  pb.Schema.create({
   110                      cluster: { id: 'c1', name: 'cluster1' },
   111                      keyspace: 'fauna',
   112                      table_sizes: {
   113                          cats: { row_count: 1234, data_length: 4321 },
   114                      },
   115                  }),
   116              ],
   117              expected: [
   118                  {
   119                      cluster: { id: 'c1', name: 'cluster1' },
   120                      keyspace: 'fauna',
   121                      tableDefinition: { name: 'cats' },
   122                      tableSize: { row_count: 1234, data_length: 4321 },
   123                  },
   124              ],
   125          },
   126      ];
   127  
   128      test.each(tests.map(Object.values))('%s', (name: string, input: pb.Schema[], expected: TableDefinition[]) => {
   129          const result = getTableDefinitions(input);
   130          expect(result).toEqual(expected);
   131      });
   132  });