kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/serving/graph/columnar/columnar_test.go (about)

     1  /*
     2   * Copyright 2018 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package columnar
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"kythe.io/kythe/go/util/compare"
    24  	"kythe.io/kythe/go/util/keys"
    25  
    26  	gspb "kythe.io/kythe/proto/graph_serving_go_proto"
    27  	scpb "kythe.io/kythe/proto/schema_go_proto"
    28  	spb "kythe.io/kythe/proto/storage_go_proto"
    29  )
    30  
    31  func TestEdgesEncodingRoundtrip(t *testing.T) {
    32  	src := &spb.VName{Corpus: "corpus", Root: "root", Path: "path", Signature: "sig"}
    33  	tests := []*gspb.Edges{{
    34  		Source: src,
    35  		Entry: &gspb.Edges_Index_{&gspb.Edges_Index{
    36  			Node: &scpb.Node{Kind: &scpb.Node_KytheKind{scpb.NodeKind_RECORD}},
    37  		}},
    38  	}, {
    39  		Source: src,
    40  		Entry: &gspb.Edges_Edge_{&gspb.Edges_Edge{
    41  			Kind:    &gspb.Edges_Edge_KytheKind{scpb.EdgeKind_CHILD_OF},
    42  			Ordinal: 52,
    43  			Reverse: true,
    44  			Target:  &spb.VName{Signature: "target"},
    45  		}},
    46  	}, {
    47  		Source: src,
    48  		Entry: &gspb.Edges_Target_{&gspb.Edges_Target{
    49  			Node: &scpb.Node{
    50  				Source: &spb.VName{Signature: "target"},
    51  				Kind:   &scpb.Node_KytheKind{scpb.NodeKind_RECORD},
    52  			},
    53  		}},
    54  	}}
    55  
    56  	for _, test := range tests {
    57  		t.Run(fmt.Sprintf("%T", test.Entry), func(t *testing.T) {
    58  			kv, err := EncodeEdgesEntry(nil, test)
    59  			if err != nil {
    60  				t.Errorf("Error encoding %T: %v", test.Entry, err)
    61  				return
    62  			}
    63  			var src spb.VName
    64  			key, err := keys.Parse(string(kv.Key), &src)
    65  			if err != nil {
    66  				t.Errorf("Error decoding source for %T: %v", test.Entry, err)
    67  				return
    68  			}
    69  			found, err := DecodeEdgesEntry(&src, string(key), kv.Value)
    70  			if err != nil {
    71  				t.Errorf("Error decoding %T: %v", test.Entry, err)
    72  			} else if diff := compare.ProtoDiff(test, found); diff != "" {
    73  				t.Errorf("%T roundtrip differences: (- expected; + found)\n%s", test.Entry, diff)
    74  			}
    75  		})
    76  	}
    77  }