github.com/lyft/flytestdlib@v0.3.12-0.20210213045714-8cdd111ecda1/pbhash/pbhash_test.go (about)

     1  package pbhash
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/golang/protobuf/proto"
     9  	"github.com/golang/protobuf/ptypes"
    10  	"github.com/golang/protobuf/ptypes/duration"
    11  	"github.com/golang/protobuf/ptypes/timestamp"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  // Mock a Protobuf generated GO object
    16  type mockProtoMessage struct {
    17  	Integer     int64                `protobuf:"varint,1,opt,name=integer,proto3" json:"integer,omitempty"`
    18  	FloatValue  float64              `protobuf:"fixed64,2,opt,name=float_value,json=floatValue,proto3" json:"float_value,omitempty"`
    19  	StringValue string               `protobuf:"bytes,3,opt,name=string_value,json=stringValue,proto3" json:"string_value,omitempty"`
    20  	Boolean     bool                 `protobuf:"varint,4,opt,name=boolean,proto3" json:"boolean,omitempty"`
    21  	Datetime    *timestamp.Timestamp `protobuf:"bytes,5,opt,name=datetime,proto3" json:"datetime,omitempty"`
    22  	Duration    *duration.Duration   `protobuf:"bytes,6,opt,name=duration,proto3" json:"duration,omitempty"`
    23  	MapValue    map[string]string    `protobuf:"bytes,7,rep,name=map_value,json=mapValue,proto3" json:"map_value,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
    24  	Collections []string             `protobuf:"bytes,8,rep,name=collections,proto3" json:"collections,omitempty"`
    25  }
    26  
    27  func (mockProtoMessage) Reset() {
    28  }
    29  
    30  func (m mockProtoMessage) String() string {
    31  	return proto.CompactTextString(m)
    32  }
    33  
    34  func (mockProtoMessage) ProtoMessage() {
    35  }
    36  
    37  // Mock an older version of the above pb object that doesn't have some fields
    38  type mockOlderProto struct {
    39  	Integer     int64   `protobuf:"varint,1,opt,name=integer,proto3" json:"integer,omitempty"`
    40  	FloatValue  float64 `protobuf:"fixed64,2,opt,name=float_value,json=floatValue,proto3" json:"float_value,omitempty"`
    41  	StringValue string  `protobuf:"bytes,3,opt,name=string_value,json=stringValue,proto3" json:"string_value,omitempty"`
    42  	Boolean     bool    `protobuf:"varint,4,opt,name=boolean,proto3" json:"boolean,omitempty"`
    43  }
    44  
    45  func (mockOlderProto) Reset() {
    46  }
    47  
    48  func (m mockOlderProto) String() string {
    49  	return proto.CompactTextString(m)
    50  }
    51  
    52  func (mockOlderProto) ProtoMessage() {
    53  }
    54  
    55  var sampleTime, _ = ptypes.TimestampProto(
    56  	time.Date(2019, 03, 29, 12, 0, 0, 0, time.UTC))
    57  
    58  func TestProtoHash(t *testing.T) {
    59  	mockProto := &mockProtoMessage{
    60  		Integer:     18,
    61  		FloatValue:  1.3,
    62  		StringValue: "lets test this",
    63  		Boolean:     true,
    64  		Datetime:    sampleTime,
    65  		Duration:    ptypes.DurationProto(time.Millisecond),
    66  		MapValue: map[string]string{
    67  			"z": "last",
    68  			"a": "first",
    69  		},
    70  		Collections: []string{"1", "2", "3"},
    71  	}
    72  
    73  	expectedHashedMockProto := []byte{0x62, 0x95, 0xb2, 0x2c, 0x23, 0xf5, 0x35, 0x6d, 0x3, 0x56, 0x4d, 0xc7, 0x8f, 0xae,
    74  		0x2d, 0x2b, 0xbd, 0x7, 0xff, 0xdb, 0x7e, 0xe5, 0xf4, 0x25, 0x8f, 0xbc, 0xb2, 0xc, 0xad, 0xa5, 0x48, 0x44}
    75  	expectedHashString := "YpWyLCP1NW0DVk3Hj64tK70H/9t+5fQlj7yyDK2lSEQ="
    76  
    77  	t.Run("TestFullProtoHash", func(t *testing.T) {
    78  		hashedBytes, err := ComputeHash(context.Background(), mockProto)
    79  		assert.Nil(t, err)
    80  		assert.Equal(t, expectedHashedMockProto, hashedBytes)
    81  		assert.Len(t, hashedBytes, 32)
    82  
    83  		hashedString, err := ComputeHashString(context.Background(), mockProto)
    84  		assert.Nil(t, err)
    85  		assert.Equal(t, hashedString, expectedHashString)
    86  	})
    87  
    88  	t.Run("TestFullProtoHashReorderKeys", func(t *testing.T) {
    89  		mockProto.MapValue = map[string]string{"a": "first", "z": "last"}
    90  		hashedBytes, err := ComputeHash(context.Background(), mockProto)
    91  		assert.Nil(t, err)
    92  		assert.Equal(t, expectedHashedMockProto, hashedBytes)
    93  		assert.Len(t, hashedBytes, 32)
    94  
    95  		hashedString, err := ComputeHashString(context.Background(), mockProto)
    96  		assert.Nil(t, err)
    97  		assert.Equal(t, hashedString, expectedHashString)
    98  	})
    99  }
   100  
   101  func TestPartialFilledProtoHash(t *testing.T) {
   102  
   103  	mockProtoOmitEmpty := &mockProtoMessage{
   104  		Integer:     18,
   105  		FloatValue:  1.3,
   106  		StringValue: "lets test this",
   107  		Boolean:     true,
   108  	}
   109  
   110  	expectedHashedMockProtoOmitEmpty := []byte{0x1a, 0x13, 0xcc, 0x4c, 0xab, 0xc9, 0x7d, 0x43, 0xc7, 0x2b, 0xc5, 0x37,
   111  		0xbc, 0x49, 0xa8, 0x8b, 0xfc, 0x1d, 0x54, 0x1c, 0x7b, 0x21, 0x04, 0x8f, 0xab, 0x28, 0xc6, 0x5c, 0x06, 0x73,
   112  		0xaa, 0xe2}
   113  
   114  	expectedHashStringOmitEmpty := "GhPMTKvJfUPHK8U3vEmoi/wdVBx7IQSPqyjGXAZzquI="
   115  
   116  	t.Run("TestPartial", func(t *testing.T) {
   117  		hashedBytes, err := ComputeHash(context.Background(), mockProtoOmitEmpty)
   118  		assert.Nil(t, err)
   119  		assert.Equal(t, expectedHashedMockProtoOmitEmpty, hashedBytes)
   120  		assert.Len(t, hashedBytes, 32)
   121  
   122  		hashedString, err := ComputeHashString(context.Background(), mockProtoOmitEmpty)
   123  		assert.Nil(t, err)
   124  		assert.Equal(t, hashedString, expectedHashStringOmitEmpty)
   125  	})
   126  
   127  	mockOldProtoMessage := &mockOlderProto{
   128  		Integer:     18,
   129  		FloatValue:  1.3,
   130  		StringValue: "lets test this",
   131  		Boolean:     true,
   132  	}
   133  
   134  	t.Run("TestOlderProto", func(t *testing.T) {
   135  		hashedBytes, err := ComputeHash(context.Background(), mockOldProtoMessage)
   136  		assert.Nil(t, err)
   137  		assert.Equal(t, expectedHashedMockProtoOmitEmpty, hashedBytes)
   138  		assert.Len(t, hashedBytes, 32)
   139  
   140  		hashedString, err := ComputeHashString(context.Background(), mockProtoOmitEmpty)
   141  		assert.Nil(t, err)
   142  		assert.Equal(t, hashedString, expectedHashStringOmitEmpty)
   143  	})
   144  
   145  }