github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/compiler/preprocess_test.go (about)

     1  // Copyright 2022 zGraph Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package compiler_test
    16  
    17  import (
    18  	"sync/atomic"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/vescale/zgraph"
    23  	"github.com/vescale/zgraph/compiler"
    24  	"github.com/vescale/zgraph/meta"
    25  	"github.com/vescale/zgraph/parser"
    26  	"github.com/vescale/zgraph/parser/model"
    27  	"github.com/vescale/zgraph/stmtctx"
    28  	"github.com/vescale/zgraph/storage"
    29  	"github.com/vescale/zgraph/storage/kv"
    30  )
    31  
    32  func initCatalog(assert *assert.Assertions, dirname string) {
    33  	store, err := storage.Open(dirname)
    34  	assert.Nil(err)
    35  	defer store.Close()
    36  
    37  	// Create
    38  	ID := atomic.Int64{}
    39  	cases := []*model.GraphInfo{
    40  		{
    41  			ID:   ID.Add(1),
    42  			Name: model.NewCIStr("graph1"),
    43  			Labels: []*model.LabelInfo{
    44  				{
    45  					ID:   ID.Add(1),
    46  					Name: model.NewCIStr("label1"),
    47  				},
    48  				{
    49  					ID:   ID.Add(1),
    50  					Name: model.NewCIStr("label2"),
    51  				},
    52  			},
    53  		},
    54  		{
    55  			ID:   ID.Add(1),
    56  			Name: model.NewCIStr("graph2"),
    57  			Labels: []*model.LabelInfo{
    58  				{
    59  					ID:   ID.Add(1),
    60  					Name: model.NewCIStr("label1"),
    61  				},
    62  				{
    63  					ID:   ID.Add(1),
    64  					Name: model.NewCIStr("label2"),
    65  				},
    66  			},
    67  			Indexes: []*model.IndexInfo{
    68  				{
    69  					ID:   ID.Add(1),
    70  					Name: model.NewCIStr("index"),
    71  				},
    72  				{
    73  					ID:   ID.Add(1),
    74  					Name: model.NewCIStr("index2"),
    75  				},
    76  			},
    77  			Properties: []*model.PropertyInfo{
    78  				{
    79  					ID:   uint16(ID.Add(1)),
    80  					Name: model.NewCIStr("property"),
    81  				},
    82  				{
    83  					ID:   uint16(ID.Add(1)),
    84  					Name: model.NewCIStr("property2"),
    85  				},
    86  			},
    87  		},
    88  	}
    89  
    90  	// Create mock data.
    91  	err = kv.Txn(store, func(txn kv.Transaction) error {
    92  		meta := meta.New(txn)
    93  		for _, g := range cases {
    94  			err := meta.CreateGraph(g)
    95  			assert.Nil(err)
    96  			for _, l := range g.Labels {
    97  				err := meta.CreateLabel(g.ID, l)
    98  				assert.Nil(err)
    99  			}
   100  		}
   101  		return nil
   102  	})
   103  	assert.Nil(err)
   104  }
   105  
   106  func TestPreprocess(t *testing.T) {
   107  	assert := assert.New(t)
   108  	tempDir := t.TempDir()
   109  	initCatalog(assert, tempDir)
   110  
   111  	cases := []struct {
   112  		graph string
   113  		query string
   114  		err   string
   115  	}{
   116  		{
   117  			query: "create graph graph1",
   118  			err:   "graph exists",
   119  		},
   120  		{
   121  			query: "drop graph graph1",
   122  		},
   123  		{
   124  			query: "drop graph graph4",
   125  			err:   "graph not exists",
   126  		},
   127  		{
   128  			query: "drop graph if exists graph4",
   129  		},
   130  		{
   131  			query: "create graph if not exists graph1",
   132  		},
   133  		{
   134  			graph: "graph0",
   135  			query: "create label label1",
   136  			err:   "graph not exists",
   137  		},
   138  		{
   139  			graph: "graph1",
   140  			query: "create label label1",
   141  			err:   "label exists",
   142  		},
   143  		{
   144  			graph: "graph1",
   145  			query: "drop label label1",
   146  		},
   147  		{
   148  			graph: "graph1",
   149  			query: "drop label label4",
   150  			err:   "label not exists",
   151  		},
   152  		{
   153  			graph: "graph1",
   154  			query: "drop label if exists label4",
   155  		},
   156  		{
   157  			graph: "graph1",
   158  			query: "create label if not exists label2",
   159  		},
   160  		{
   161  			graph: "graph2",
   162  			query: "create index if not exists idx_name (a, b)",
   163  			err:   "property a: property not exists",
   164  		},
   165  		{
   166  			graph: "graph2",
   167  			query: "create index index2 (a, b)",
   168  			err:   "index exists",
   169  		},
   170  		{
   171  			graph: "graph2",
   172  			query: "drop index index2",
   173  		},
   174  		{
   175  			graph: "graph2",
   176  			query: "drop index label4_index2",
   177  			err:   "index not exists",
   178  		},
   179  		{
   180  			graph: "graph2",
   181  			query: "drop index if exists label4_index2",
   182  		},
   183  		{
   184  			query: "use graph100",
   185  			err:   "graph not exists",
   186  		},
   187  		{
   188  			query: "use graph1",
   189  		},
   190  		{
   191  			query: "INSERT VERTEX x",
   192  			err:   "graph not exists",
   193  		},
   194  		{
   195  			query: "INSERT INTO graph1 VERTEX x",
   196  		},
   197  		{
   198  			query: "INSERT INTO graph1 VERTEX x LABELS (label1, label2) PROPERTIES ( x.name = 'test')",
   199  		},
   200  		{
   201  			query: "INSERT INTO graph1 VERTEX x LABELS (label0, label2) PROPERTIES ( x.name = 'test')",
   202  			err:   "label not exists",
   203  		},
   204  		{
   205  			query: "INSERT INTO graph1 VERTEX x LABELS (label1, label2) PROPERTIES ( y.name = 'test')",
   206  			err:   "reference not exists variable",
   207  		},
   208  		{
   209  			graph: "graph2",
   210  			query: "INSERT VERTEX x LABELS (label1, label2) PROPERTIES ( y.name = 'test')",
   211  			err:   "reference not exists variable",
   212  		},
   213  	}
   214  
   215  	db, err := zgraph.Open(tempDir, nil)
   216  	assert.Nil(err)
   217  
   218  	for _, c := range cases {
   219  		parser := parser.New()
   220  		stmt, err := parser.ParseOneStmt(c.query)
   221  		assert.Nil(err)
   222  		sc := stmtctx.New(db.Store(), db.Catalog())
   223  		sc.SetCurrentGraphName(c.graph)
   224  
   225  		prep := compiler.NewPreprocess(sc)
   226  		stmt.Accept(prep)
   227  		if c.err == "" {
   228  			assert.Nil(prep.Error(), c.query)
   229  		} else {
   230  			assert.ErrorContains(prep.Error(), c.err, c.query)
   231  		}
   232  	}
   233  }