vitess.io/vitess@v0.16.2/go/vt/schema/parser_test.go (about)

     1  /*
     2  Copyright 2019 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  
    17  package schema
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestParseAlterTableOptions(t *testing.T) {
    26  	type expect struct {
    27  		schema, table, options string
    28  	}
    29  	tests := map[string]expect{
    30  		"add column i int, drop column d":                               {schema: "", table: "", options: "add column i int, drop column d"},
    31  		"  add column i int, drop column d  ":                           {schema: "", table: "", options: "add column i int, drop column d"},
    32  		"alter table t add column i int, drop column d":                 {schema: "", table: "t", options: "add column i int, drop column d"},
    33  		"alter    table   t      add column i int, drop column d":       {schema: "", table: "t", options: "add column i int, drop column d"},
    34  		"alter table `t` add column i int, drop column d":               {schema: "", table: "t", options: "add column i int, drop column d"},
    35  		"alter table `scm`.`t` add column i int, drop column d":         {schema: "scm", table: "t", options: "add column i int, drop column d"},
    36  		"alter table `scm`.t add column i int, drop column d":           {schema: "scm", table: "t", options: "add column i int, drop column d"},
    37  		"alter table scm.`t` add column i int, drop column d":           {schema: "scm", table: "t", options: "add column i int, drop column d"},
    38  		"alter table scm.t add column i int, drop column d":             {schema: "scm", table: "t", options: "add column i int, drop column d"},
    39  		"  alter       table   scm.`t` add column i int, drop column d": {schema: "scm", table: "t", options: "add column i int, drop column d"},
    40  		"ALTER  table scm.t ADD COLUMN i int, DROP COLUMN d":            {schema: "scm", table: "t", options: "ADD COLUMN i int, DROP COLUMN d"},
    41  		"ALTER TABLE scm.t ADD COLUMN i int, DROP COLUMN d":             {schema: "scm", table: "t", options: "ADD COLUMN i int, DROP COLUMN d"},
    42  	}
    43  	for query, expect := range tests {
    44  		schema, table, options := ParseAlterTableOptions(query)
    45  		assert.Equal(t, expect.schema, schema)
    46  		assert.Equal(t, expect.table, table)
    47  		assert.Equal(t, expect.options, options)
    48  	}
    49  }
    50  
    51  func TestLegacyParseRevertUUID(t *testing.T) {
    52  
    53  	{
    54  		uuid, err := legacyParseRevertUUID("revert 4e5dcf80_354b_11eb_82cd_f875a4d24e90")
    55  		assert.NoError(t, err)
    56  		assert.Equal(t, "4e5dcf80_354b_11eb_82cd_f875a4d24e90", uuid)
    57  	}
    58  	{
    59  		_, err := legacyParseRevertUUID("revert 4e5dcf80_354b_11eb_82cd_f875a4")
    60  		assert.Error(t, err)
    61  	}
    62  	{
    63  		_, err := legacyParseRevertUUID("revert vitess_migration '4e5dcf80_354b_11eb_82cd_f875a4d24e90'")
    64  		assert.Error(t, err)
    65  	}
    66  }
    67  
    68  func TestParseEnumValues(t *testing.T) {
    69  	{
    70  		inputs := []string{
    71  			`enum('x-small','small','medium','large','x-large')`,
    72  			`ENUM('x-small','small','medium','large','x-large')`,
    73  			`'x-small','small','medium','large','x-large'`,
    74  		}
    75  		for _, input := range inputs {
    76  			enumValues := ParseEnumValues(input)
    77  			assert.Equal(t, `'x-small','small','medium','large','x-large'`, enumValues)
    78  		}
    79  	}
    80  	{
    81  		inputs := []string{
    82  			``,
    83  			`abc`,
    84  			`func('x-small','small','medium','large','x-large')`,
    85  			`set('x-small','small','medium','large','x-large')`,
    86  		}
    87  		for _, input := range inputs {
    88  			enumValues := ParseEnumValues(input)
    89  			assert.Equal(t, input, enumValues)
    90  		}
    91  	}
    92  }
    93  
    94  func TestParseSetValues(t *testing.T) {
    95  	{
    96  		inputs := []string{
    97  			`set('x-small','small','medium','large','x-large')`,
    98  			`SET('x-small','small','medium','large','x-large')`,
    99  			`'x-small','small','medium','large','x-large'`,
   100  		}
   101  		for _, input := range inputs {
   102  			setValues := ParseSetValues(input)
   103  			assert.Equal(t, `'x-small','small','medium','large','x-large'`, setValues)
   104  		}
   105  	}
   106  	{
   107  		inputs := []string{
   108  			``,
   109  			`abc`,
   110  			`func('x-small','small','medium','large','x-large')`,
   111  			`enum('x-small','small','medium','large','x-large')`,
   112  			`ENUM('x-small','small','medium','large','x-large')`,
   113  		}
   114  		for _, input := range inputs {
   115  			setValues := ParseSetValues(input)
   116  			assert.Equal(t, input, setValues)
   117  		}
   118  	}
   119  }
   120  
   121  func TestParseEnumTokens(t *testing.T) {
   122  	{
   123  		input := `'x-small','small','medium','large','x-large'`
   124  		enumTokens := parseEnumOrSetTokens(input)
   125  		expect := []string{"x-small", "small", "medium", "large", "x-large"}
   126  		assert.Equal(t, expect, enumTokens)
   127  	}
   128  	{
   129  		input := `enum('x-small','small','medium','large','x-large')`
   130  		enumTokens := parseEnumOrSetTokens(input)
   131  		assert.Nil(t, enumTokens)
   132  	}
   133  	{
   134  		input := `set('x-small','small','medium','large','x-large')`
   135  		enumTokens := parseEnumOrSetTokens(input)
   136  		assert.Nil(t, enumTokens)
   137  	}
   138  }
   139  
   140  func TestParseEnumTokensMap(t *testing.T) {
   141  	{
   142  		input := `'x-small','small','medium','large','x-large'`
   143  
   144  		enumTokensMap := ParseEnumOrSetTokensMap(input)
   145  		expect := map[string]string{
   146  			"1": "x-small",
   147  			"2": "small",
   148  			"3": "medium",
   149  			"4": "large",
   150  			"5": "x-large",
   151  		}
   152  		assert.Equal(t, expect, enumTokensMap)
   153  	}
   154  	{
   155  		inputs := []string{
   156  			`enum('x-small','small','medium','large','x-large')`,
   157  			`set('x-small','small','medium','large','x-large')`,
   158  		}
   159  		for _, input := range inputs {
   160  			enumTokensMap := ParseEnumOrSetTokensMap(input)
   161  			expect := map[string]string{}
   162  			assert.Equal(t, expect, enumTokensMap)
   163  		}
   164  	}
   165  }