vitess.io/vitess@v0.16.2/web/vtadmin/src/util/vschemas.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 * as vs from './vschemas'; 18 19 describe('getVindexesForTable', () => { 20 const tests: { 21 name: string; 22 input: Parameters<typeof vs.getVindexesForTable>; 23 expected: ReturnType<typeof vs.getVindexesForTable>; 24 }[] = [ 25 { 26 name: 'should return column vindexes', 27 input: [ 28 pb.VSchema.create({ 29 v_schema: { 30 tables: { 31 customer: { 32 column_vindexes: [{ column: 'customer_id', name: 'hash' }], 33 }, 34 }, 35 vindexes: { 36 hash: { type: 'hash' }, 37 }, 38 }, 39 }), 40 'customer', 41 ], 42 expected: [ 43 { 44 column: 'customer_id', 45 name: 'hash', 46 meta: { type: 'hash' }, 47 }, 48 ], 49 }, 50 { 51 name: 'should return column vindexes + metadata', 52 input: [ 53 pb.VSchema.create({ 54 v_schema: { 55 tables: { 56 dogs: { 57 column_vindexes: [ 58 { column: 'id', name: 'hash' }, 59 { name: 'dogs_domain_vdx', columns: ['domain', 'is_good_dog'] }, 60 ], 61 }, 62 }, 63 vindexes: { 64 hash: { type: 'hash' }, 65 dogs_domain_vdx: { 66 type: 'lookup_hash', 67 owner: 'dogs', 68 params: { 69 from: 'domain,is_good_dog', 70 table: 'dogs_domain_idx', 71 to: 'id', 72 }, 73 }, 74 }, 75 }, 76 }), 77 'dogs', 78 ], 79 expected: [ 80 { 81 column: 'id', 82 name: 'hash', 83 meta: { type: 'hash' }, 84 }, 85 { 86 columns: ['domain', 'is_good_dog'], 87 name: 'dogs_domain_vdx', 88 meta: { 89 owner: 'dogs', 90 params: { 91 from: 'domain,is_good_dog', 92 table: 'dogs_domain_idx', 93 to: 'id', 94 }, 95 type: 'lookup_hash', 96 }, 97 }, 98 ], 99 }, 100 { 101 name: 'should handle vschemas where the given table is not defined', 102 input: [ 103 pb.VSchema.create({ 104 v_schema: { 105 tables: { 106 customer: { 107 column_vindexes: [{ column: 'customer_id', name: 'hash' }], 108 }, 109 }, 110 vindexes: { 111 hash: { type: 'hash' }, 112 }, 113 }, 114 }), 115 'does-not-exist', 116 ], 117 expected: [], 118 }, 119 ]; 120 121 test.each(tests.map(Object.values))( 122 '%s', 123 ( 124 name: string, 125 input: Parameters<typeof vs.getVindexesForTable>, 126 expected: ReturnType<typeof vs.getVindexesForTable> 127 ) => { 128 const result = vs.getVindexesForTable(...input); 129 expect(result).toEqual(expected); 130 } 131 ); 132 });