github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/codec/decoder_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 codec
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/vescale/zgraph/datum"
    23  	"github.com/vescale/zgraph/parser/model"
    24  )
    25  
    26  func TestPropertyDecoder_Decode(t *testing.T) {
    27  	cases := []struct {
    28  		labelIDs    []uint16
    29  		propertyIDs []uint16
    30  		values      []datum.Datum
    31  	}{
    32  		{
    33  			labelIDs:    []uint16{1, 2, 3},
    34  			propertyIDs: []uint16{1, 2, 3},
    35  			values: []datum.Datum{
    36  				datum.NewString("hello"),
    37  				datum.NewInt(1),
    38  				datum.NewFloat(1.1),
    39  			},
    40  		},
    41  		{
    42  			labelIDs:    []uint16{2, 3, 1},
    43  			propertyIDs: []uint16{2, 3, 1},
    44  			values: []datum.Datum{
    45  				datum.NewInt(1),
    46  				datum.NewFloat(1.1),
    47  				datum.NewString("hello"),
    48  			},
    49  		},
    50  	}
    51  
    52  	for _, c := range cases {
    53  		encoder := &PropertyEncoder{}
    54  		bytes, err := encoder.Encode(nil, c.labelIDs, c.propertyIDs, c.values)
    55  		assert.Nil(t, err)
    56  
    57  		var labels []*model.LabelInfo
    58  		for _, id := range c.labelIDs {
    59  			labels = append(labels, &model.LabelInfo{
    60  				ID:   int64(id),
    61  				Name: model.NewCIStr(fmt.Sprintf("label%d", id)),
    62  			})
    63  		}
    64  
    65  		var properties []*model.PropertyInfo
    66  		for _, id := range c.propertyIDs {
    67  			properties = append(properties, &model.PropertyInfo{
    68  				ID:   id,
    69  				Name: model.NewCIStr(fmt.Sprintf("property%d", id)),
    70  			})
    71  		}
    72  
    73  		decoder := NewPropertyDecoder(labels, properties)
    74  		labelIDs, row, err := decoder.Decode(bytes)
    75  		assert.NoError(t, err)
    76  		assert.Equal(t, map[uint16]struct{}{
    77  			1: {},
    78  			2: {},
    79  			3: {},
    80  		}, labelIDs)
    81  		assert.Equal(t, map[uint16]datum.Datum{
    82  			1: datum.NewString("hello"),
    83  			2: datum.NewInt(1),
    84  			3: datum.NewFloat(1.1),
    85  		}, row)
    86  	}
    87  }