github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/meta/property_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 TestProperty(t *testing.T) { 28 assert := assert.New(t) 29 store, err := storage.Open(t.TempDir()) 30 assert.Nil(err) 31 32 var graphID = int64(100) 33 34 err = kv.TxnContext(context.TODO(), store, func(_ context.Context, txn kv.Transaction) error { 35 meta := New(txn) 36 info := &model.GraphInfo{ 37 ID: graphID, 38 Name: model.NewCIStr("test-graph"), 39 } 40 return meta.CreateGraph(info) 41 }) 42 assert.Nil(err) 43 44 propertyNames := []string{ 45 "property1", 46 "property2", 47 "property3", 48 "property4", 49 "property5", 50 } 51 52 // Create propertys 53 for i, name := range propertyNames { 54 err := kv.TxnContext(context.TODO(), store, func(_ context.Context, txn kv.Transaction) error { 55 meta := New(txn) 56 info := &model.PropertyInfo{ 57 ID: uint16(i) + 1, 58 Name: model.NewCIStr(name), 59 } 60 return meta.CreateProperty(graphID, info) 61 }) 62 assert.Nil(err) 63 } 64 65 // List properties 66 err = kv.TxnContext(context.TODO(), store, func(_ context.Context, txn kv.Transaction) error { 67 meta := New(txn) 68 propertys, err := meta.ListProperties(graphID) 69 assert.Nil(err) 70 names := make([]string, 0, len(propertys)) 71 ids := make([]uint16, 0, len(propertys)) 72 for _, g := range propertys { 73 names = append(names, g.Name.L) 74 ids = append(ids, g.ID) 75 } 76 77 assert.Equal(propertyNames, names) 78 assert.Equal([]uint16{1, 2, 3, 4, 5}, ids) 79 80 return nil 81 }) 82 assert.Nil(err) 83 84 // Update property 85 err = kv.TxnContext(context.TODO(), store, func(_ context.Context, txn kv.Transaction) error { 86 meta := New(txn) 87 err := meta.UpdateProperty(graphID, &model.PropertyInfo{ 88 ID: 4, 89 Name: model.NewCIStr("PROPERTY4-modified"), 90 }) 91 assert.Nil(err) 92 return nil 93 }) 94 assert.Nil(err) 95 96 // Get property 97 err = kv.TxnContext(context.TODO(), store, func(_ context.Context, txn kv.Transaction) error { 98 meta := New(txn) 99 property, err := meta.GetProperty(graphID, 4) 100 assert.Nil(err) 101 assert.Equal(uint16(4), property.ID) 102 assert.Equal("property4-modified", property.Name.L) 103 return nil 104 }) 105 assert.Nil(err) 106 107 // Drop property 108 err = kv.TxnContext(context.TODO(), store, func(_ context.Context, txn kv.Transaction) error { 109 meta := New(txn) 110 err := meta.DropProperty(graphID, 3) 111 assert.Nil(err) 112 return nil 113 }) 114 assert.Nil(err) 115 116 // List propertys again 117 err = kv.TxnContext(context.TODO(), store, func(_ context.Context, txn kv.Transaction) error { 118 meta := New(txn) 119 propertys, err := meta.ListProperties(graphID) 120 assert.Nil(err) 121 names := make([]string, 0, len(propertys)) 122 ids := make([]uint16, 0, len(propertys)) 123 for _, g := range propertys { 124 names = append(names, g.Name.L) 125 ids = append(ids, g.ID) 126 } 127 128 propertyNames := []string{ 129 "property1", 130 "property2", 131 "property4-modified", 132 "property5", 133 } 134 assert.Equal(propertyNames, names) 135 assert.Equal([]uint16{1, 2, 4, 5}, ids) 136 137 return nil 138 }) 139 assert.Nil(err) 140 }