github.com/grailbio/base@v0.0.11/recordio/deprecated/generic_test.go (about)

     1  // Copyright 2017 GRAIL, Inc. All rights reserved.
     2  // Use of this source code is governed by the Apache-2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  package deprecated_test
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"testing"
    11  
    12  	"github.com/golang/protobuf/proto"
    13  	"github.com/grailbio/base/recordio/deprecated"
    14  	"github.com/grailbio/testutil/assert"
    15  	"github.com/grailbio/testutil/expect"
    16  )
    17  
    18  type TestPB struct {
    19  	Message string `protobuf:"bytes,1,opt,name=message" json:"message,omitempty"`
    20  }
    21  
    22  func (m *TestPB) Reset()         { *m = TestPB{} }
    23  func (m *TestPB) String() string { return proto.CompactTextString(m) }
    24  func (*TestPB) ProtoMessage()    {}
    25  
    26  func (m *TestPB) GetName() string {
    27  	return m.Message
    28  }
    29  
    30  func getTestMessages() []proto.Message {
    31  	return []proto.Message{
    32  		&TestPB{"hello"},
    33  		&TestPB{"goodbye"},
    34  	}
    35  }
    36  
    37  func TestProto(t *testing.T) {
    38  	buf := bytes.NewBuffer(nil)
    39  	wropts := deprecated.LegacyWriterOpts{Marshal: recordioMarshal}
    40  
    41  	writer := deprecated.NewLegacyWriter(buf, wropts)
    42  
    43  	testMessages := getTestMessages()
    44  	for _, pb := range testMessages {
    45  		n, err := writer.Marshal(pb)
    46  		assert.NoError(t, err)
    47  		tmp := proto.NewBuffer(nil)
    48  		assert.NoError(t, tmp.Marshal(pb))
    49  		if got, want := n, len(tmp.Bytes()); got != want {
    50  			t.Fatalf("%v: got %v, want %v", pb.String(), got, want)
    51  		}
    52  	}
    53  
    54  	scopts := deprecated.LegacyScannerOpts{Unmarshal: recordioUnmarshal}
    55  	scanner := deprecated.NewLegacyScanner(buf, scopts)
    56  
    57  	nMessages := 0
    58  	for scanner.Scan() {
    59  		pb := &TestPB{}
    60  		if err := scanner.Unmarshal(pb); err != nil {
    61  			t.Errorf("unmarshal: %v", err)
    62  		}
    63  		if got, want := pb.String(), testMessages[nMessages].String(); got != want {
    64  			t.Fatalf("%v: got %v, want %v", nMessages, got, want)
    65  		}
    66  
    67  		nMessages++
    68  	}
    69  	if err := scanner.Err(); err != nil {
    70  		t.Errorf("unexpected scanner error: %v", err)
    71  	}
    72  
    73  	if got, want := nMessages, len(testMessages); got != want {
    74  		t.Fatalf("got %v, want %v", got, want)
    75  	}
    76  }
    77  
    78  func TestMarshalErrors(t *testing.T) {
    79  	buf := bytes.NewBuffer(nil)
    80  	writer := deprecated.NewLegacyWriter(buf, deprecated.LegacyWriterOpts{})
    81  	_, err := writer.Marshal(&TestPB{"error"})
    82  	expect.HasSubstr(t, err, "Marshal function not configured")
    83  
    84  	wropts := deprecated.LegacyWriterOpts{Marshal: func(scratch []byte, v interface{}) ([]byte, error) {
    85  		return nil, fmt.Errorf("oh a marshaling error")
    86  	}}
    87  
    88  	writer = deprecated.NewLegacyWriter(buf, wropts)
    89  	_, err = writer.Marshal(&TestPB{"error"})
    90  	expect.HasSubstr(t, err, "oh a marshaling error")
    91  
    92  	wropts.Marshal = recordioMarshal
    93  	writer = deprecated.NewLegacyWriter(buf, wropts)
    94  	_, err = writer.Marshal(&TestPB{"error"})
    95  	if err != nil {
    96  		t.Fatal(err)
    97  	}
    98  	scanner := deprecated.NewLegacyScanner(buf, deprecated.LegacyScannerOpts{})
    99  	scanner.Scan()
   100  	pb := &TestPB{}
   101  	err = scanner.Unmarshal(pb)
   102  	expect.HasSubstr(t, err, "Unmarshal function not configured")
   103  
   104  	scopts := deprecated.LegacyScannerOpts{Unmarshal: func(b []byte, v interface{}) error {
   105  		return fmt.Errorf("oh an unmarshaling error")
   106  	}}
   107  	scanner = deprecated.NewLegacyScanner(buf, scopts)
   108  	scanner.Scan()
   109  	err = scanner.Unmarshal(pb)
   110  	expect.HasSubstr(t, err, "oh an unmarshaling error")
   111  }
   112  
   113  func TestPackedMarshalErrors(t *testing.T) {
   114  	buf := bytes.NewBuffer(nil)
   115  	wropts := deprecated.LegacyPackedWriterOpts{}
   116  	writer := deprecated.NewLegacyPackedWriter(buf, wropts)
   117  	_, err := writer.Marshal(&TestPB{"error"})
   118  	expect.HasSubstr(t, err, "Marshal function not configured")
   119  
   120  	wropts.Marshal = func(scratch []byte, v interface{}) ([]byte, error) {
   121  		return nil, fmt.Errorf("oh a marshaling error")
   122  	}
   123  	writer = deprecated.NewLegacyPackedWriter(buf, wropts)
   124  	_, err = writer.Marshal(&TestPB{"error"})
   125  	expect.HasSubstr(t, err, "oh a marshaling error")
   126  
   127  	wropts.Marshal = recordioMarshal
   128  	writer = deprecated.NewLegacyPackedWriter(buf, wropts)
   129  	_, err = writer.Marshal(&TestPB{"error"})
   130  	if err != nil {
   131  		t.Fatal(err)
   132  	}
   133  	scopts := deprecated.LegacyPackedScannerOpts{}
   134  	scanner := deprecated.NewLegacyPackedScanner(buf, scopts)
   135  	scanner.Scan()
   136  	pb := &TestPB{}
   137  	err = scanner.Unmarshal(pb)
   138  	expect.HasSubstr(t, err, "Unmarshal function not configured")
   139  	scopts.Unmarshal = func(b []byte, v interface{}) error {
   140  		return fmt.Errorf("oh an unmarshaling error")
   141  	}
   142  	scanner = deprecated.NewLegacyPackedScanner(buf, scopts)
   143  	scanner.Scan()
   144  	err = scanner.Unmarshal(pb)
   145  	expect.HasSubstr(t, err, "oh an unmarshaling error")
   146  }