github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/internal/rsg/rsg_test.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package rsg
    12  
    13  import (
    14  	"fmt"
    15  	"testing"
    16  )
    17  
    18  const yaccExample = `
    19  name:
    20    IDENT
    21  | unreserved_keyword
    22  | col_name_keyword
    23  
    24  unreserved_keyword:
    25    ABORT
    26  | ACTION
    27  | ADD
    28  | ADMIN
    29  
    30  col_name_keyword:
    31    ANNOTATE_TYPE
    32  | BETWEEN
    33  | BIGINT
    34  | BIT
    35  
    36  column_name:         name
    37  
    38  constraint_name:     name
    39  
    40  column_def:
    41    column_name typename col_qual_list
    42    {
    43      tableDef, err := tree.NewColumnTableDef(tree.Name($1), $2.colType(), $3.colQuals())
    44      if err != nil {
    45        sqllex.Error(err.Error())
    46        return 1
    47      }
    48      $$.val = tableDef
    49    }
    50  
    51  col_qual_list:
    52    col_qual_list col_qualification
    53    {
    54      $$.val = append($1.colQuals(), $2.colQual())
    55    }
    56  | /* EMPTY */
    57    {
    58      $$.val = []tree.NamedColumnQualification(nil)
    59    }
    60  
    61  col_qualification:
    62    CONSTRAINT constraint_name col_qualification_elem
    63    {
    64      $$.val = tree.NamedColumnQualification{Name: tree.Name($2), Qualification: $3.colQualElem()}
    65    }
    66  | col_qualification_elem
    67    {
    68      $$.val = tree.NamedColumnQualification{Qualification: $1.colQualElem()}
    69    }
    70  
    71  col_qualification_elem:
    72    NOT NULL
    73    {
    74      $$.val = tree.NotNullConstraint{}
    75    }
    76  | NULL
    77    {
    78      $$.val = tree.NullConstraint{}
    79    }
    80  | UNIQUE
    81    {
    82      $$.val = tree.UniqueConstraint{}
    83    }
    84  | PRIMARY KEY
    85    {
    86      $$.val = tree.PrimaryKeyConstraint{}
    87    }
    88  `
    89  
    90  func getRSG(t *testing.T) *RSG {
    91  	r, err := NewRSG(1, yaccExample, false)
    92  	if err != nil {
    93  		t.Fatal(err)
    94  	}
    95  	return r
    96  }
    97  
    98  func TestGenerate(t *testing.T) {
    99  	tests := []struct {
   100  		root        string
   101  		depth       int
   102  		repetitions int
   103  		expected    []string
   104  	}{
   105  		{
   106  			root:        "column_def",
   107  			depth:       20,
   108  			repetitions: 10,
   109  			expected: []string{
   110  				"BIT typename",
   111  				"ANNOTATE_TYPE typename CONSTRAINT ADD PRIMARY KEY NULL",
   112  				"ident typename PRIMARY KEY CONSTRAINT ident NULL",
   113  				"BETWEEN typename NULL",
   114  				"ADD typename",
   115  				"ABORT typename",
   116  				"ACTION typename",
   117  				"BIGINT typename",
   118  				"ident typename",
   119  				"BETWEEN typename CONSTRAINT ident UNIQUE",
   120  			},
   121  		},
   122  	}
   123  
   124  	for _, tc := range tests {
   125  		t.Run(fmt.Sprintf("%s-%d-%d", tc.root, tc.depth, tc.repetitions), func(t *testing.T) {
   126  			r := getRSG(t)
   127  
   128  			out := make([]string, tc.repetitions)
   129  			for i := range out {
   130  				out[i] = r.Generate(tc.root, tc.depth)
   131  			}
   132  
   133  			// Enable to help with writing tests.
   134  			if false {
   135  				for _, o := range out {
   136  					fmt.Printf("%q,\n", o)
   137  				}
   138  				return
   139  			}
   140  
   141  			if len(out) != len(tc.expected) {
   142  				t.Fatal("unexpected")
   143  			}
   144  			for i, o := range out {
   145  				if o != tc.expected[i] {
   146  					t.Fatalf("got %q, expected %q", o, tc.expected[i])
   147  				}
   148  			}
   149  		})
   150  	}
   151  }