github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/meta/graph_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 meta 16 17 import ( 18 "context" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 "github.com/vescale/zgraph/parser/model" 23 "github.com/vescale/zgraph/storage" 24 "github.com/vescale/zgraph/storage/kv" 25 ) 26 27 func TestGraph(t *testing.T) { 28 assert := assert.New(t) 29 store, err := storage.Open(t.TempDir()) 30 assert.Nil(err) 31 32 graphNames := []string{ 33 "graph1", 34 "graph2", 35 "graph3", 36 "graph4", 37 "graph5", 38 } 39 40 // Create graphs 41 for i, name := range graphNames { 42 err := kv.TxnContext(context.TODO(), store, func(_ context.Context, txn kv.Transaction) error { 43 meta := New(txn) 44 info := &model.GraphInfo{ 45 ID: int64(i) + 1, 46 Name: model.NewCIStr(name), 47 } 48 return meta.CreateGraph(info) 49 }) 50 assert.Nil(err) 51 } 52 53 // List graphs 54 err = kv.TxnContext(context.TODO(), store, func(_ context.Context, txn kv.Transaction) error { 55 meta := New(txn) 56 graphs, err := meta.ListGraphs() 57 assert.Nil(err) 58 names := make([]string, 0, len(graphs)) 59 ids := make([]int64, 0, len(graphs)) 60 for _, g := range graphs { 61 names = append(names, g.Name.L) 62 ids = append(ids, g.ID) 63 } 64 65 assert.Equal(graphNames, names) 66 assert.Equal([]int64{1, 2, 3, 4, 5}, ids) 67 68 return nil 69 }) 70 assert.Nil(err) 71 72 // Update graph 73 err = kv.TxnContext(context.TODO(), store, func(_ context.Context, txn kv.Transaction) error { 74 meta := New(txn) 75 err := meta.UpdateGraph(&model.GraphInfo{ 76 ID: 4, 77 Name: model.NewCIStr("GRAPH4-modified"), 78 }) 79 assert.Nil(err) 80 return nil 81 }) 82 assert.Nil(err) 83 84 // Get graph 85 err = kv.TxnContext(context.TODO(), store, func(_ context.Context, txn kv.Transaction) error { 86 meta := New(txn) 87 graph, err := meta.GetGraph(4) 88 assert.Nil(err) 89 assert.Equal(int64(4), graph.ID) 90 assert.Equal("graph4-modified", graph.Name.L) 91 return nil 92 }) 93 assert.Nil(err) 94 95 // Drop graph 96 err = kv.TxnContext(context.TODO(), store, func(_ context.Context, txn kv.Transaction) error { 97 meta := New(txn) 98 err := meta.DropGraph(3) 99 assert.Nil(err) 100 return nil 101 }) 102 assert.Nil(err) 103 104 // List graphs again 105 err = kv.TxnContext(context.TODO(), store, func(_ context.Context, txn kv.Transaction) error { 106 meta := New(txn) 107 graphs, err := meta.ListGraphs() 108 assert.Nil(err) 109 names := make([]string, 0, len(graphs)) 110 ids := make([]int64, 0, len(graphs)) 111 for _, g := range graphs { 112 names = append(names, g.Name.L) 113 ids = append(ids, g.ID) 114 } 115 116 graphNames := []string{ 117 "graph1", 118 "graph2", 119 "graph4-modified", 120 "graph5", 121 } 122 assert.Equal(graphNames, names) 123 assert.Equal([]int64{1, 2, 4, 5}, ids) 124 125 return nil 126 }) 127 assert.Nil(err) 128 }