github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/db_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 zgraph 16 17 import ( 18 "context" 19 "testing" 20 21 "github.com/stretchr/testify/require" 22 "github.com/vescale/zgraph/session" 23 ) 24 25 func TestOpen(t *testing.T) { 26 db, err := Open(t.TempDir(), nil) 27 require.NoError(t, err) 28 require.NotNil(t, db) 29 } 30 31 type TestKit struct { 32 t *testing.T 33 sess *session.Session 34 } 35 36 func NewTestKit(t *testing.T, sess *session.Session) *TestKit { 37 return &TestKit{ 38 t: t, 39 sess: sess, 40 } 41 } 42 43 func (tk *TestKit) MustExec(ctx context.Context, query string) { 44 rs, err := tk.sess.Execute(ctx, query) 45 require.NoError(tk.t, err) 46 require.NoError(tk.t, rs.Next(ctx)) 47 } 48 49 func TestDDL(t *testing.T) { 50 db, err := Open(t.TempDir(), nil) 51 require.NoError(t, err) 52 require.NotNil(t, db) 53 defer db.Close() 54 55 catalog := db.Catalog() 56 sess := db.NewSession() 57 require.NotNil(t, sess) 58 59 tk := NewTestKit(t, sess) 60 61 ctx := context.Background() 62 tk.MustExec(ctx, "CREATE GRAPH graph101") 63 require.NoError(t, err) 64 graph := catalog.Graph("graph101") 65 require.NotNil(t, graph) 66 67 sess.StmtContext().SetCurrentGraphName("graph101") 68 tk.MustExec(ctx, "CREATE LABEL label01") 69 require.NoError(t, err) 70 require.NotNil(t, graph.Label("label01")) 71 72 tk.MustExec(ctx, "CREATE LABEL IF NOT EXISTS label01") 73 require.NoError(t, err) 74 75 tk.MustExec(ctx, "DROP LABEL label01") 76 require.NoError(t, err) 77 require.Nil(t, graph.Label("label01")) 78 79 tk.MustExec(ctx, "DROP LABEL IF EXISTS label01") 80 require.NoError(t, err) 81 require.Nil(t, graph.Label("label01")) 82 83 tk.MustExec(ctx, "DROP GRAPH graph101") 84 require.NoError(t, err) 85 require.Nil(t, catalog.Graph("graph101")) 86 }