github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/planner/plan_builder_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 planner_test 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/assert" 21 "github.com/vescale/zgraph" 22 "github.com/vescale/zgraph/parser" 23 "github.com/vescale/zgraph/planner" 24 "github.com/vescale/zgraph/stmtctx" 25 ) 26 27 func TestBuilder_BuildDDL(t *testing.T) { 28 assert := assert.New(t) 29 30 cases := []string{ 31 "create graph if not exists graph5", 32 "create graph graph5", 33 "create label label1", 34 "create label if not exists label1", 35 "create index index1 (a, b)", 36 "create index if not exists index1 (a, b)", 37 "drop graph graph5", 38 "drop label label1", 39 "drop index index1", 40 } 41 42 db, err := zgraph.Open(t.TempDir(), nil) 43 assert.Nil(err) 44 45 for _, c := range cases { 46 parser := parser.New() 47 stmt, err := parser.ParseOneStmt(c) 48 assert.Nil(err) 49 50 builder := planner.NewBuilder(stmtctx.New(db.Store(), db.Catalog())) 51 plan, err := builder.Build(stmt) 52 assert.Nil(err) 53 54 ddl, ok := plan.(*planner.DDL) 55 assert.True(ok) 56 assert.Equal(stmt, ddl.Statement) 57 } 58 } 59 60 func TestBuilder_BuildSimple(t *testing.T) { 61 assert := assert.New(t) 62 63 cases := []struct { 64 query string 65 }{ 66 // Catalog information refer: initCatalog 67 { 68 query: "use graph100", 69 }, 70 { 71 query: "use graph1", 72 }, 73 } 74 75 db, err := zgraph.Open(t.TempDir(), nil) 76 assert.Nil(err) 77 78 for _, c := range cases { 79 parser := parser.New() 80 stmt, err := parser.ParseOneStmt(c.query) 81 assert.Nil(err) 82 83 builder := planner.NewBuilder(stmtctx.New(db.Store(), db.Catalog())) 84 plan, err := builder.Build(stmt) 85 assert.Nil(err) 86 ddl, ok := plan.(*planner.Simple) 87 assert.True(ok) 88 assert.Equal(stmt, ddl.Statement) 89 } 90 }