Skip to content

Commit

Permalink
spanner: add a test for embedded fields
Browse files Browse the repository at this point in the history
Confirm that Row.ToStruct can handle embedded fields.

See #692.

Change-Id: Ib031549f04889d38e61c1b5b618ab32ea693c1bd
Reviewed-on: https://code-review.googlesource.com/21690
Reviewed-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Michael Darakananda <pongad@google.com>
  • Loading branch information
jba committed Jan 3, 2018
1 parent b26d29c commit a4c0747
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions spanner/row_test.go
Expand Up @@ -1684,12 +1684,39 @@ func TestToStruct(t *testing.T) {
err := row.ToStruct(&s[0])
if err != nil {
t.Errorf("row.ToStruct() returns error: %v, want nil", err)
}
if !testEqual(s[0], s[1]) {
} else if !testEqual(s[0], s[1]) {
t.Errorf("row.ToStruct() fetches struct %v, want %v", s[0], s[1])
}
}

func TestToStructEmbedded(t *testing.T) {
type (
S1 struct{ F1 string }
S2 struct {
S1
F2 string
}
)
r := Row{
[]*sppb.StructType_Field{
{"F1", stringType()},
{"F2", stringType()},
},
[]*proto3.Value{
stringProto("v1"),
stringProto("v2"),
},
}
var got S2
if err := r.ToStruct(&got); err != nil {
t.Fatal(err)
}
want := S2{S1: S1{F1: "v1"}, F2: "v2"}
if !testEqual(got, want) {
t.Errorf("got %+v, want %+v", got, want)
}
}

// Test helpers for getting column names.
func TestColumnNameAndIndex(t *testing.T) {
// Test Row.Size().
Expand Down

0 comments on commit a4c0747

Please sign in to comment.