github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/catalog/catalog_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 catalog 16 17 import ( 18 "sync/atomic" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 "github.com/vescale/zgraph/meta" 23 "github.com/vescale/zgraph/parser/model" 24 "github.com/vescale/zgraph/storage" 25 "github.com/vescale/zgraph/storage/kv" 26 ) 27 28 func Test_Load(t *testing.T) { 29 assert := assert.New(t) 30 store, err := storage.Open(t.TempDir()) 31 assert.Nil(err) 32 defer store.Close() 33 34 ID := atomic.Int64{} 35 cases := []*model.GraphInfo{ 36 { 37 ID: ID.Add(1), 38 Name: model.NewCIStr("graph1"), 39 Labels: []*model.LabelInfo{ 40 { 41 ID: ID.Add(1), 42 Name: model.NewCIStr("label1"), 43 }, 44 { 45 ID: ID.Add(1), 46 Name: model.NewCIStr("label2"), 47 }, 48 }, 49 }, 50 { 51 ID: ID.Add(1), 52 Name: model.NewCIStr("graph2"), 53 Labels: []*model.LabelInfo{ 54 { 55 ID: ID.Add(1), 56 Name: model.NewCIStr("label1"), 57 }, 58 { 59 ID: ID.Add(1), 60 Name: model.NewCIStr("label2"), 61 }, 62 }, 63 Properties: []*model.PropertyInfo{ 64 { 65 ID: uint16(ID.Add(1)), 66 Name: model.NewCIStr("property1"), 67 }, 68 { 69 ID: uint16(ID.Add(1)), 70 Name: model.NewCIStr("property2"), 71 }, 72 }, 73 }, 74 } 75 76 // Create mock data. 77 err = kv.Txn(store, func(txn kv.Transaction) error { 78 meta := meta.New(txn) 79 for _, g := range cases { 80 err := meta.CreateGraph(g) 81 assert.Nil(err) 82 for _, l := range g.Labels { 83 err := meta.CreateLabel(g.ID, l) 84 assert.Nil(err) 85 } 86 for _, p := range g.Properties { 87 err := meta.CreateProperty(g.ID, p) 88 assert.Nil(err) 89 } 90 } 91 return nil 92 }) 93 assert.Nil(err) 94 95 snapshot, err := store.Snapshot(store.CurrentVersion()) 96 assert.Nil(err) 97 98 catalog, err := Load(snapshot) 99 assert.Nil(err) 100 assert.Equal(len(cases), len(catalog.byName)) 101 assert.Equal(len(catalog.byID), len(catalog.byName)) 102 103 for _, g := range cases { 104 graph := catalog.Graph(g.Name.L) 105 assert.Equal(g.ID, graph.Meta().ID) 106 assert.Equal(graph, catalog.GraphByID(g.ID)) 107 108 // Labels 109 for _, l := range g.Labels { 110 label := graph.Label(l.Name.L) 111 assert.Equal(l, label.Meta()) 112 assert.Equal(label, graph.LabelByID(l.ID)) 113 } 114 115 // Properties 116 for _, p := range g.Properties { 117 property := graph.Property(p.Name.L) 118 assert.Equal(p, property) 119 assert.Equal(property, graph.PropertyByID(p.ID)) 120 } 121 } 122 }