github.com/mithrandie/csvq@v1.18.1/lib/syntax/store_test.go (about)

     1  package syntax
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  var testSyntax = []Expression{
     9  	{
    10  		Label: "SELECT Statement",
    11  		Grammar: []Definition{
    12  			{
    13  				Name: "select_statement",
    14  				Group: []Grammar{
    15  					{Option{Link("with_clause")}, Link("select_query")},
    16  				},
    17  			},
    18  			{
    19  				Name: "select_query",
    20  				Group: []Grammar{
    21  					{Link("select_entity"), Option{Link("order_by_clause")}, Option{Link("limit_clause")}, Option{Link("offset_clause")}},
    22  				},
    23  			},
    24  		},
    25  		Children: []Expression{
    26  			{
    27  				Label: "WITH Clause",
    28  				Grammar: []Definition{
    29  					{
    30  						Name: "with_clause",
    31  						Group: []Grammar{
    32  							{Keyword("WITH"), ContinuousOption{Link("common_table_expression")}},
    33  						},
    34  					},
    35  					{
    36  						Name: "common_table_expression",
    37  						Group: []Grammar{
    38  							{Option{Keyword("RECURSIVE")}, Identifier("table_name"), Option{Parentheses{ContinuousOption{Identifier("column_name")}}}, Keyword("AS"), Parentheses{Link("select_query")}},
    39  						},
    40  					},
    41  				},
    42  			},
    43  			{
    44  				Label: "SELECT Clause",
    45  				Grammar: []Definition{
    46  					{
    47  						Name: "select_clause",
    48  						Group: []Grammar{
    49  							{Keyword("SELECT"), Option{Keyword("DISTINCT")}, ContinuousOption{Link("field")}},
    50  						},
    51  					},
    52  					{
    53  						Name: "field",
    54  						Group: []Grammar{
    55  							{Link("value")},
    56  							{Link("value"), Keyword("AS"), Identifier("alias")},
    57  						},
    58  					},
    59  				},
    60  			},
    61  		},
    62  	},
    63  	{
    64  		Label: "INSERT Statement",
    65  		Grammar: []Definition{
    66  			{
    67  				Name: "insert_statement",
    68  				Group: []Grammar{
    69  					{Option{Link("with_clause")}, Link("insert_query")},
    70  				},
    71  			},
    72  			{
    73  				Name: "insert_query",
    74  				Group: []Grammar{
    75  					{Keyword("INSERT"), Keyword("INTO"), Identifier("table_name"), Option{Parentheses{ContinuousOption{Identifier("column_name")}}}, Keyword("VALUES"), ContinuousOption{Link("row_value")}},
    76  					{Keyword("INSERT"), Keyword("INTO"), Identifier("table_name"), Option{Parentheses{ContinuousOption{Identifier("column_name")}}}, Link("select_query")},
    77  				},
    78  			},
    79  		},
    80  	},
    81  	{
    82  		Label: "Operators",
    83  		Children: []Expression{
    84  			{
    85  				Label: "Operator Precedence",
    86  				Description: Description{
    87  					Template: "The following table list operators from highest precedence to lowest.",
    88  				},
    89  			},
    90  			{
    91  				Label: "String Operators",
    92  				Grammar: []Definition{
    93  					{
    94  						Name: "concatenation",
    95  						Group: []Grammar{
    96  							{Link("value"), Keyword("||"), Link("value")},
    97  						},
    98  					},
    99  				},
   100  			},
   101  		},
   102  	},
   103  }
   104  
   105  var storeSearchTests = []struct {
   106  	Keys   []string
   107  	Expect []Expression
   108  }{
   109  	{
   110  		Keys: nil,
   111  		Expect: []Expression{
   112  			{
   113  				Label: "SELECT Statement",
   114  				Grammar: []Definition{
   115  					{
   116  						Name: "select_statement",
   117  						Group: []Grammar{
   118  							{Option{Link("with_clause")}, Link("select_query")},
   119  						},
   120  					},
   121  					{
   122  						Name: "select_query",
   123  						Group: []Grammar{
   124  							{Link("select_entity"), Option{Link("order_by_clause")}, Option{Link("limit_clause")}, Option{Link("offset_clause")}},
   125  						},
   126  					},
   127  				},
   128  				Children: []Expression{
   129  					{
   130  						Label: "WITH Clause",
   131  						Grammar: []Definition{
   132  							{
   133  								Name: "with_clause",
   134  								Group: []Grammar{
   135  									{Keyword("WITH"), ContinuousOption{Link("common_table_expression")}},
   136  								},
   137  							},
   138  							{
   139  								Name: "common_table_expression",
   140  								Group: []Grammar{
   141  									{Option{Keyword("RECURSIVE")}, Identifier("table_name"), Option{Parentheses{ContinuousOption{Identifier("column_name")}}}, Keyword("AS"), Parentheses{Link("select_query")}},
   142  								},
   143  							},
   144  						},
   145  					},
   146  					{
   147  						Label: "SELECT Clause",
   148  						Grammar: []Definition{
   149  							{
   150  								Name: "select_clause",
   151  								Group: []Grammar{
   152  									{Keyword("SELECT"), Option{Keyword("DISTINCT")}, ContinuousOption{Link("field")}},
   153  								},
   154  							},
   155  							{
   156  								Name: "field",
   157  								Group: []Grammar{
   158  									{Link("value")},
   159  									{Link("value"), Keyword("AS"), Identifier("alias")},
   160  								},
   161  							},
   162  						},
   163  					},
   164  				},
   165  			},
   166  			{
   167  				Label: "INSERT Statement",
   168  				Grammar: []Definition{
   169  					{
   170  						Name: "insert_statement",
   171  						Group: []Grammar{
   172  							{Option{Link("with_clause")}, Link("insert_query")},
   173  						},
   174  					},
   175  					{
   176  						Name: "insert_query",
   177  						Group: []Grammar{
   178  							{Keyword("INSERT"), Keyword("INTO"), Identifier("table_name"), Option{Parentheses{ContinuousOption{Identifier("column_name")}}}, Keyword("VALUES"), ContinuousOption{Link("row_value")}},
   179  							{Keyword("INSERT"), Keyword("INTO"), Identifier("table_name"), Option{Parentheses{ContinuousOption{Identifier("column_name")}}}, Link("select_query")},
   180  						},
   181  					},
   182  				},
   183  			},
   184  			{
   185  				Label: "Operators",
   186  				Children: []Expression{
   187  					{
   188  						Label: "Operator Precedence",
   189  						Description: Description{
   190  							Template: "The following table list operators from highest precedence to lowest.",
   191  						},
   192  					},
   193  					{
   194  						Label: "String Operators",
   195  						Grammar: []Definition{
   196  							{
   197  								Name: "concatenation",
   198  								Group: []Grammar{
   199  									{Link("value"), Keyword("||"), Link("value")},
   200  								},
   201  							},
   202  						},
   203  					},
   204  				},
   205  			},
   206  		},
   207  	},
   208  	{
   209  		Keys: []string{"select"},
   210  		Expect: []Expression{
   211  			{
   212  				Label: "SELECT Statement",
   213  				Grammar: []Definition{
   214  					{
   215  						Name: "select_statement",
   216  						Group: []Grammar{
   217  							{Option{Link("with_clause")}, Link("select_query")},
   218  						},
   219  					},
   220  					{
   221  						Name: "select_query",
   222  						Group: []Grammar{
   223  							{Link("select_entity"), Option{Link("order_by_clause")}, Option{Link("limit_clause")}, Option{Link("offset_clause")}},
   224  						},
   225  					},
   226  				},
   227  				Children: []Expression{
   228  					{
   229  						Label: "WITH Clause",
   230  						Grammar: []Definition{
   231  							{
   232  								Name: "with_clause",
   233  								Group: []Grammar{
   234  									{Keyword("WITH"), ContinuousOption{Link("common_table_expression")}},
   235  								},
   236  							},
   237  							{
   238  								Name: "common_table_expression",
   239  								Group: []Grammar{
   240  									{Option{Keyword("RECURSIVE")}, Identifier("table_name"), Option{Parentheses{ContinuousOption{Identifier("column_name")}}}, Keyword("AS"), Parentheses{Link("select_query")}},
   241  								},
   242  							},
   243  						},
   244  					},
   245  					{
   246  						Label: "SELECT Clause",
   247  						Grammar: []Definition{
   248  							{
   249  								Name: "select_clause",
   250  								Group: []Grammar{
   251  									{Keyword("SELECT"), Option{Keyword("DISTINCT")}, ContinuousOption{Link("field")}},
   252  								},
   253  							},
   254  							{
   255  								Name: "field",
   256  								Group: []Grammar{
   257  									{Link("value")},
   258  									{Link("value"), Keyword("AS"), Identifier("alias")},
   259  								},
   260  							},
   261  						},
   262  					},
   263  				},
   264  			},
   265  		},
   266  	},
   267  	{
   268  		Keys: []string{"clause"},
   269  		Expect: []Expression{
   270  			{
   271  				Label: "WITH Clause",
   272  				Grammar: []Definition{
   273  					{
   274  						Name: "with_clause",
   275  						Group: []Grammar{
   276  							{Keyword("WITH"), ContinuousOption{Link("common_table_expression")}},
   277  						},
   278  					},
   279  					{
   280  						Name: "common_table_expression",
   281  						Group: []Grammar{
   282  							{Option{Keyword("RECURSIVE")}, Identifier("table_name"), Option{Parentheses{ContinuousOption{Identifier("column_name")}}}, Keyword("AS"), Parentheses{Link("select_query")}},
   283  						},
   284  					},
   285  				},
   286  			},
   287  			{
   288  				Label: "SELECT Clause",
   289  				Grammar: []Definition{
   290  					{
   291  						Name: "select_clause",
   292  						Group: []Grammar{
   293  							{Keyword("SELECT"), Option{Keyword("DISTINCT")}, ContinuousOption{Link("field")}},
   294  						},
   295  					},
   296  					{
   297  						Name: "field",
   298  						Group: []Grammar{
   299  							{Link("value")},
   300  							{Link("value"), Keyword("AS"), Identifier("alias")},
   301  						},
   302  					},
   303  				},
   304  			},
   305  		},
   306  	},
   307  	{
   308  		Keys: []string{"field"},
   309  		Expect: []Expression{
   310  			{
   311  				Label: "SELECT Clause",
   312  				Grammar: []Definition{
   313  					{
   314  						Name: "field",
   315  						Group: []Grammar{
   316  							{Link("value")},
   317  							{Link("value"), Keyword("AS"), Identifier("alias")},
   318  						},
   319  					},
   320  				},
   321  			},
   322  		},
   323  	},
   324  	{
   325  		Keys: []string{"operator prec"},
   326  		Expect: []Expression{
   327  			{
   328  				Label: "Operator Precedence",
   329  				Description: Description{
   330  					Template: "The following table list operators from highest precedence to lowest.",
   331  				},
   332  			},
   333  		},
   334  	},
   335  }
   336  
   337  func TestStore_Search(t *testing.T) {
   338  	store := NewStore()
   339  	store.Syntax = testSyntax
   340  
   341  	for _, v := range storeSearchTests {
   342  		result := store.Search(v.Keys)
   343  		if !reflect.DeepEqual(result, v.Expect) {
   344  			t.Errorf("result = %#v, want %#v for %v", result, v.Expect, v.Keys)
   345  		}
   346  	}
   347  }