Skip to content

Commit

Permalink
Add TestDecodeColumnProto and TestDecodeColumnProtoArray
Browse files Browse the repository at this point in the history
  • Loading branch information
apstndb committed Aug 20, 2024
1 parent b58f480 commit 3e1f12a
Showing 1 changed file with 64 additions and 19 deletions.
83 changes: 64 additions & 19 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
package main

import (
"encoding/base64"
"github.com/google/go-cmp/cmp"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/testing/protocmp"
"math/big"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -319,30 +323,13 @@ func TestDecodeColumn(t *testing.T) {
},

// PROTO
{
desc: "proto",
value: &protos.SingerInfo{
SingerId: proto.Int64(1),
BirthDate: proto.String("1970-01-01"),
Nationality: proto.String("Japanese"),
Genre: protos.Genre_JAZZ.Enum(),
},
want: "CAESCjE5NzAtMDEtMDEaCEphcGFuZXNlIAE=",
},
// This table tests only have null proto cases because of non-stability
// See also TestDecodeColumnProtoArray and TestDecodeColumnProto
{
desc: "null proto",
value: (*protos.SingerInfo)(nil),
want: "NULL",
},
{
desc: "array proto",
value: []*protos.SingerInfo{
{SingerId: proto.Int64(0)},
{SingerId: proto.Int64(1)},
{SingerId: proto.Int64(2)},
},
want: "[CAA=, CAE=, CAI=]",
},
{
desc: "null array proto",
value: []*protos.SingerInfo(nil),
Expand Down Expand Up @@ -385,6 +372,64 @@ func TestDecodeColumn(t *testing.T) {
}
}

func TestDecodeColumnProto(t *testing.T) {
singerInfo := &protos.SingerInfo{
SingerId: proto.Int64(1),
BirthDate: proto.String("1970-01-01"),
Nationality: proto.String("Japanese"),
Genre: protos.Genre_JAZZ.Enum(),
}

decoded, err := DecodeColumn(createColumnValue(t, singerInfo))
if err != nil {
t.Error(err)
}

b, err := base64.StdEncoding.DecodeString(decoded)
if err != nil {
t.Error(err)
}

gotSingerInfo := &protos.SingerInfo{}
if err := proto.Unmarshal(b, gotSingerInfo); err != nil {
t.Error(err)
}

if !cmp.Equal(gotSingerInfo, singerInfo, protocmp.Transform()) {
t.Errorf("differ: %v", cmp.Diff(gotSingerInfo, singerInfo, protocmp.Transform()))
}
}

func TestDecodeColumnProtoArray(t *testing.T) {
singerInfos := []*protos.SingerInfo{
{SingerId: proto.Int64(1)},
{SingerId: proto.Int64(2)},
{SingerId: proto.Int64(3)},
}

decoded, err := DecodeColumn(createColumnValue(t, singerInfos))
if err != nil {
t.Fatal(err)
}
var gotSingerInfos []*protos.SingerInfo
for _, s := range strings.Split(strings.Trim(decoded, "[]"), ",") {
b, err := base64.StdEncoding.DecodeString(strings.TrimSpace(s))
if err != nil {
t.Fatal(err)
}

gotSingerInfo := &protos.SingerInfo{}
if err := proto.Unmarshal(b, gotSingerInfo); err != nil {
t.Fatal(err)
}
gotSingerInfos = append(gotSingerInfos, gotSingerInfo)
}

if !cmp.Equal(gotSingerInfos, singerInfos, protocmp.Transform()) {
t.Fatalf("differ: %v", cmp.Diff(gotSingerInfos, singerInfos, protocmp.Transform()))
}
}

func TestDecodeRow(t *testing.T) {
tests := []struct {
desc string
Expand Down

0 comments on commit 3e1f12a

Please sign in to comment.