github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/catalog/patch_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 TestCatalog_Apply(t *testing.T) {
    25  	assert := assert.New(t)
    26  
    27  	catalog := &Catalog{
    28  		byID:   map[int64]*Graph{},
    29  		byName: map[string]*Graph{},
    30  	}
    31  
    32  	cases := []struct {
    33  		patch   *Patch
    34  		checker func()
    35  	}{
    36  		{
    37  			patch: &Patch{
    38  				Type: PatchTypeCreateGraph,
    39  				Data: &model.GraphInfo{
    40  					ID:   1,
    41  					Name: model.NewCIStr("graph1"),
    42  				},
    43  			},
    44  			checker: func() {
    45  				assert.NotNil(catalog.Graph("graph1"))
    46  			},
    47  		},
    48  		{
    49  			patch: &Patch{
    50  				Type: PatchTypeCreateLabel,
    51  				Data: &PatchLabel{
    52  					GraphID: 1,
    53  					LabelInfo: &model.LabelInfo{
    54  						ID:   2,
    55  						Name: model.NewCIStr("label1"),
    56  					},
    57  				},
    58  			},
    59  			checker: func() {
    60  				graph := catalog.Graph("graph1")
    61  				assert.NotNil(graph.Label("label1"))
    62  			},
    63  		},
    64  		{
    65  			patch: &Patch{
    66  				Type: PatchTypeCreateLabel,
    67  				Data: &PatchLabel{
    68  					GraphID: 1,
    69  					LabelInfo: &model.LabelInfo{
    70  						ID:   3,
    71  						Name: model.NewCIStr("label2"),
    72  					},
    73  				},
    74  			},
    75  			checker: func() {
    76  				graph := catalog.Graph("graph1")
    77  				assert.NotNil(graph.Label("label2"))
    78  			},
    79  		},
    80  		{
    81  			patch: &Patch{
    82  				Type: PatchTypeDropLabel,
    83  				Data: &PatchLabel{
    84  					GraphID: 1,
    85  					LabelInfo: &model.LabelInfo{
    86  						ID:   2,
    87  						Name: model.NewCIStr("label1"),
    88  					},
    89  				},
    90  			},
    91  			checker: func() {
    92  				graph := catalog.Graph("graph1")
    93  				assert.Nil(graph.Label("label1"))
    94  				assert.NotNil(graph.Label("label2"))
    95  			},
    96  		},
    97  		{
    98  			patch: &Patch{
    99  				Type: PatchTypeCreateProperties,
   100  				Data: &PatchProperties{
   101  					GraphID:   1,
   102  					MaxPropID: 2,
   103  					Properties: []*model.PropertyInfo{
   104  						{
   105  							ID:   1,
   106  							Name: model.NewCIStr("property1"),
   107  						},
   108  						{
   109  							ID:   2,
   110  							Name: model.NewCIStr("property2"),
   111  						},
   112  					},
   113  				},
   114  			},
   115  			checker: func() {
   116  				graph := catalog.Graph("graph1")
   117  				assert.NotNil(graph.Property("property1"))
   118  				assert.NotNil(graph.Property("property2"))
   119  			},
   120  		},
   121  		{
   122  			patch: &Patch{
   123  				Type: PatchTypeDropGraph,
   124  				Data: &model.GraphInfo{
   125  					ID:   1,
   126  					Name: model.NewCIStr("graph1"),
   127  				},
   128  			},
   129  			checker: func() {
   130  				assert.Nil(catalog.Graph("graph1"))
   131  			},
   132  		},
   133  	}
   134  
   135  	for _, c := range cases {
   136  		catalog.Apply(c.patch)
   137  		if c.checker != nil {
   138  			c.checker()
   139  		}
   140  	}
   141  }