github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/planner/insert_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 "sync/atomic" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 "github.com/vescale/zgraph" 23 "github.com/vescale/zgraph/catalog" 24 "github.com/vescale/zgraph/parser" 25 "github.com/vescale/zgraph/parser/ast" 26 "github.com/vescale/zgraph/parser/model" 27 "github.com/vescale/zgraph/planner" 28 "github.com/vescale/zgraph/stmtctx" 29 ) 30 31 func TestBuilder_BuildInsert(t *testing.T) { 32 assert := assert.New(t) 33 34 db, err := zgraph.Open(t.TempDir(), nil) 35 assert.Nil(err) 36 37 // Prepare mock catalog. 38 mockGraphs := []string{"graph101", "graph102"} 39 mockLabels := []string{"A", "B", "C"} 40 mockProps := []string{"name", "age"} 41 id := atomic.Int64{} 42 for _, g := range mockGraphs { 43 graphID := id.Add(1) 44 db.Catalog().Apply(&catalog.Patch{ 45 Type: catalog.PatchTypeCreateGraph, 46 Data: &model.GraphInfo{ 47 ID: graphID, 48 Name: model.NewCIStr(g), 49 }, 50 }) 51 for _, l := range mockLabels { 52 db.Catalog().Apply(&catalog.Patch{ 53 Type: catalog.PatchTypeCreateLabel, 54 Data: &catalog.PatchLabel{ 55 GraphID: graphID, 56 LabelInfo: &model.LabelInfo{ 57 ID: id.Add(1), 58 Name: model.NewCIStr(l), 59 }, 60 }, 61 }) 62 63 } 64 var properties []*model.PropertyInfo 65 for i, p := range mockProps { 66 properties = append(properties, &model.PropertyInfo{ 67 ID: uint16(i + 1), 68 Name: model.NewCIStr(p), 69 }) 70 } 71 db.Catalog().Apply(&catalog.Patch{ 72 Type: catalog.PatchTypeCreateProperties, 73 Data: &catalog.PatchProperties{ 74 MaxPropID: uint16(len(properties)), 75 GraphID: graphID, 76 Properties: properties, 77 }, 78 }) 79 80 } 81 82 cases := []struct { 83 query string 84 check func(insert *planner.Insert) 85 }{ 86 // Catalog information refer: initCatalog 87 { 88 query: "insert vertex x labels(A, B, C)", 89 check: func(insert *planner.Insert) { 90 assert.Equal(1, len(insert.Insertions)) 91 assert.Equal(3, len(insert.Insertions[0].Labels)) 92 }, 93 }, 94 { 95 query: "insert into graph102 vertex x labels(A, B, C)", 96 check: func(insert *planner.Insert) { 97 assert.Equal("graph102", insert.Graph.Meta().Name.L) 98 }, 99 }, 100 { 101 query: "insert vertex x properties (x.name = 'test')", 102 check: func(insert *planner.Insert) { 103 assert.Equal(1, len(insert.Insertions[0].Assignments)) 104 }, 105 }, 106 { 107 query: `insert vertex x labels(A, B, C) properties (x.name = 'test'), 108 vertex y labels(A, B) properties (y.name = 'test'), 109 vertex z labels(B, C) properties (z.name = 'test')`, 110 check: func(insert *planner.Insert) { 111 assert.Equal(3, len(insert.Insertions)) 112 }, 113 }, 114 { 115 query: `insert vertex x labels(A, B, C) properties (x.name = 'test'), 116 vertex y labels(A, B) properties (y.name = 'test'), 117 edge z between x and y labels(B, C) from match (x), match (y)`, 118 check: func(insert *planner.Insert) { 119 assert.Equal(3, len(insert.Insertions)) 120 assert.Equal(ast.InsertionTypeEdge, insert.Insertions[2].Type) 121 }, 122 }, 123 } 124 125 for _, c := range cases { 126 parser := parser.New() 127 stmt, err := parser.ParseOneStmt(c.query) 128 assert.Nil(err, c.query) 129 130 sc := stmtctx.New(db.Store(), db.Catalog()) 131 sc.SetCurrentGraphName("graph101") 132 133 builder := planner.NewBuilder(sc) 134 plan, err := builder.Build(stmt) 135 assert.Nil(err) 136 insert, ok := plan.(*planner.Insert) 137 assert.True(ok) 138 c.check(insert) 139 } 140 }