github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/executor/ddl_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 executor_test
    16  
    17  import (
    18  	"context"
    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/parser"
    25  )
    26  
    27  func TestDDLExec_Next(t *testing.T) {
    28  	assert := assert.New(t)
    29  	db, err := zgraph.Open(t.TempDir(), nil)
    30  	assert.Nil(err)
    31  
    32  	catalog := db.Catalog()
    33  
    34  	cases := []struct {
    35  		query string
    36  		graph string
    37  		check func()
    38  	}{
    39  		{
    40  			query: "create graph g1",
    41  			check: func() {
    42  				assert.NotNil(catalog.Graph("g1"))
    43  			},
    44  		},
    45  		{
    46  			graph: "g1",
    47  			query: "create label l1",
    48  			check: func() {
    49  				graph := catalog.Graph("g1")
    50  				label := graph.Label("l1")
    51  				assert.NotNil(label)
    52  				labelInfo := label.Meta()
    53  				assert.Equal("l1", labelInfo.Name.L)
    54  			},
    55  		},
    56  		{
    57  			graph: "g1",
    58  			query: "drop label l1",
    59  			check: func() {
    60  				graph := catalog.Graph("g1")
    61  				label := graph.Label("l1")
    62  				assert.Nil(label)
    63  			},
    64  		},
    65  		{
    66  			query: "drop graph g1",
    67  			check: func() {
    68  				assert.Nil(catalog.Graph("g1"))
    69  			},
    70  		},
    71  	}
    72  
    73  	ctx := context.Background()
    74  	for _, c := range cases {
    75  		parser := parser.New()
    76  		stmt, err := parser.ParseOneStmt(c.query)
    77  		assert.Nil(err)
    78  
    79  		s := db.NewSession()
    80  		sc := s.StmtContext()
    81  		if c.graph != "" {
    82  			sc.SetCurrentGraphName(c.graph)
    83  		}
    84  		exec, err := compiler.Compile(sc, stmt)
    85  		assert.Nil(err)
    86  
    87  		err = exec.Open(ctx)
    88  		assert.Nil(err)
    89  		_, err = exec.Next(ctx)
    90  		assert.Nil(err)
    91  
    92  		if c.check != nil {
    93  			c.check()
    94  		}
    95  	}
    96  }