github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/catalog/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 catalog 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/assert" 21 "github.com/vescale/zgraph/parser/model" 22 ) 23 24 func TestNewGraph(t *testing.T) { 25 meta := &model.GraphInfo{ 26 ID: 1, 27 Name: model.NewCIStr("test-graph"), 28 } 29 30 graph := NewGraph(meta) 31 assert := assert.New(t) 32 assert.Equal(graph.Meta(), meta) 33 } 34 35 func TestGraph_Label(t *testing.T) { 36 meta := &model.GraphInfo{ 37 ID: 1, 38 Name: model.NewCIStr("test-graph"), 39 Labels: []*model.LabelInfo{ 40 { 41 ID: 2, 42 Name: model.NewCIStr("label1"), 43 }, 44 }, 45 } 46 47 graph := NewGraph(meta) 48 assert := assert.New(t) 49 assert.Equal(graph.Label("label1").Meta(), meta.Labels[0]) 50 } 51 52 func TestGraph_LabelByID(t *testing.T) { 53 meta := &model.GraphInfo{ 54 ID: 1, 55 Name: model.NewCIStr("test-graph"), 56 Labels: []*model.LabelInfo{ 57 { 58 ID: 2, 59 Name: model.NewCIStr("label1"), 60 }, 61 }, 62 } 63 64 graph := NewGraph(meta) 65 assert := assert.New(t) 66 assert.Equal(graph.LabelByID(2).Meta(), meta.Labels[0]) 67 } 68 69 func TestGraph_CreateLabel(t *testing.T) { 70 meta := &model.GraphInfo{ 71 ID: 1, 72 Name: model.NewCIStr("test-graph"), 73 } 74 75 graph := NewGraph(meta) 76 assert := assert.New(t) 77 label := &model.LabelInfo{ 78 ID: 2, 79 Name: model.NewCIStr("label1"), 80 } 81 graph.CreateLabel(label) 82 assert.Equal(graph.LabelByID(2).Meta(), label) 83 assert.Equal(graph.Label("label1").Meta(), label) 84 } 85 86 func TestGraph_DropLabel(t *testing.T) { 87 meta := &model.GraphInfo{ 88 ID: 1, 89 Name: model.NewCIStr("test-graph"), 90 Labels: []*model.LabelInfo{ 91 { 92 ID: 2, 93 Name: model.NewCIStr("label1"), 94 }, 95 }, 96 } 97 98 graph := NewGraph(meta) 99 assert := assert.New(t) 100 graph.DropLabel(meta.Labels[0]) 101 assert.Nil(graph.LabelByID(2)) 102 } 103 104 func TestGraph_Property(t *testing.T) { 105 meta := &model.GraphInfo{ 106 ID: 1, 107 Name: model.NewCIStr("test-graph"), 108 Properties: []*model.PropertyInfo{ 109 { 110 ID: 2, 111 Name: model.NewCIStr("property1"), 112 }, 113 }, 114 } 115 116 graph := NewGraph(meta) 117 assert := assert.New(t) 118 assert.Equal(graph.Property("property1"), meta.Properties[0]) 119 } 120 121 func TestGraph_PropertyByID(t *testing.T) { 122 meta := &model.GraphInfo{ 123 ID: 1, 124 Name: model.NewCIStr("test-graph"), 125 Properties: []*model.PropertyInfo{ 126 { 127 ID: 2, 128 Name: model.NewCIStr("property1"), 129 }, 130 }, 131 } 132 133 graph := NewGraph(meta) 134 assert := assert.New(t) 135 assert.Equal(graph.PropertyByID(2), meta.Properties[0]) 136 } 137 138 func TestGraph_CreateProperty(t *testing.T) { 139 meta := &model.GraphInfo{ 140 ID: 1, 141 Name: model.NewCIStr("test-graph"), 142 } 143 144 graph := NewGraph(meta) 145 assert := assert.New(t) 146 property := &model.PropertyInfo{ 147 ID: 2, 148 Name: model.NewCIStr("property1"), 149 } 150 graph.CreateProperty(property) 151 assert.Equal(graph.PropertyByID(2), property) 152 assert.Equal(graph.Property("property1"), property) 153 } 154 155 func TestGraph_DropProperty(t *testing.T) { 156 meta := &model.GraphInfo{ 157 ID: 1, 158 Name: model.NewCIStr("test-graph"), 159 Properties: []*model.PropertyInfo{ 160 { 161 ID: 2, 162 Name: model.NewCIStr("property1"), 163 }, 164 }, 165 } 166 167 graph := NewGraph(meta) 168 assert := assert.New(t) 169 graph.DropProperty(meta.Properties[0]) 170 assert.Nil(graph.PropertyByID(2)) 171 assert.Nil(graph.Property("property1")) 172 }