kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/serving/pipeline/beamio/entries_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 beamio
    18  
    19  import (
    20  	"context"
    21  	"io/ioutil"
    22  	"os"
    23  	"testing"
    24  
    25  	"kythe.io/kythe/go/platform/delimited"
    26  	"kythe.io/kythe/go/util/compare"
    27  
    28  	"github.com/apache/beam/sdks/go/pkg/beam"
    29  	"github.com/apache/beam/sdks/go/pkg/beam/testing/ptest"
    30  
    31  	spb "kythe.io/kythe/proto/storage_go_proto"
    32  
    33  	_ "github.com/apache/beam/sdks/go/pkg/beam/io/filesystem/local"
    34  )
    35  
    36  func TestReadEntries(t *testing.T) {
    37  	entries := []*spb.Entry{{
    38  		Source:   &spb.VName{Signature: "sig1"},
    39  		FactName: "/kythe/fact/name",
    40  	}, {
    41  		Source:    &spb.VName{Signature: "sig1"},
    42  		FactName:  "/kythe/fact/name2",
    43  		FactValue: []byte("value"),
    44  	}, {
    45  		Source: &spb.VName{Signature: "sig2"},
    46  		Target: &spb.VName{Signature: "sig1"},
    47  	}, {
    48  		EdgeKind: "/kythe/edge/kind",
    49  	}}
    50  
    51  	f, err := ioutil.TempFile("", "entries")
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	defer os.Remove(f.Name())
    56  	wr := delimited.NewWriter(f)
    57  
    58  	for _, e := range entries {
    59  		if err := wr.PutProto(e); err != nil {
    60  			t.Fatal(err)
    61  		}
    62  	}
    63  	if err := f.Close(); err != nil {
    64  		t.Fatal(err)
    65  	}
    66  
    67  	p, s := beam.NewPipelineWithRoot()
    68  
    69  	coll, err := ReadEntries(context.Background(), s, f.Name())
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  
    74  	var found []*spb.Entry
    75  	beam.ParDo(s, func(e *spb.Entry, emit func(*spb.Entry)) { found = append(found, e) }, coll)
    76  
    77  	if err := ptest.Run(p); err != nil {
    78  		t.Fatal(err)
    79  	}
    80  
    81  	if diff := compare.ProtoDiff(entries, found); diff != "" {
    82  		t.Fatalf("Diff found (-expected; +found):\n%s", diff)
    83  	}
    84  }