Skip to content

Commit 56fab7e

Browse files
committed
TestReflectStructColumnPointers
1 parent 4a88cad commit 56fab7e

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

impl/reflectstruct_test.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package impl
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"github.com/domonda/go-sqldb"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestReflectStructColumnPointers(t *testing.T) {
12+
type DeepEmbeddedStruct struct {
13+
DeeperEmbInt int `db:"deep_emb_int"`
14+
}
15+
type embeddedStruct struct {
16+
DeepEmbeddedStruct
17+
EmbInt int `db:"emb_int"`
18+
}
19+
type Struct struct {
20+
ID string `db:"id,pk"`
21+
Int int `db:"int"`
22+
Ignore int `db:"-"`
23+
embeddedStruct
24+
UntaggedField int
25+
Struct struct {
26+
InlineStructInt int `db:"inline_struct_int"`
27+
} `db:"-"` // TODO enable access to named embedded fields?
28+
NilPtr *byte `db:"nil_ptr"`
29+
}
30+
var (
31+
structPtr = new(Struct)
32+
structFieldPtrs = []any{
33+
&structPtr.ID,
34+
&structPtr.Int,
35+
&structPtr.DeeperEmbInt,
36+
&structPtr.EmbInt,
37+
// &structPtr.Struct.InlineStructInt,
38+
&structPtr.NilPtr,
39+
}
40+
structCols = []string{"id", "int", "deep_emb_int", "emb_int" /*"inline_struct_int",*/, "nil_ptr"}
41+
)
42+
43+
type args struct {
44+
structVal reflect.Value
45+
mapper sqldb.StructFieldMapper
46+
columns []string
47+
}
48+
tests := []struct {
49+
name string
50+
args args
51+
wantPointers []any
52+
wantErr bool
53+
}{
54+
{
55+
name: "ok",
56+
args: args{
57+
structVal: reflect.ValueOf(structPtr).Elem(),
58+
mapper: sqldb.NewTaggedStructFieldMapping(),
59+
columns: structCols,
60+
},
61+
wantPointers: structFieldPtrs,
62+
},
63+
64+
// Errors:
65+
{
66+
name: "no columns",
67+
args: args{
68+
structVal: reflect.ValueOf(structPtr).Elem(),
69+
mapper: sqldb.NewTaggedStructFieldMapping(),
70+
columns: []string{},
71+
},
72+
wantErr: true,
73+
},
74+
{
75+
name: "not a struct",
76+
args: args{
77+
structVal: reflect.ValueOf(structPtr),
78+
mapper: sqldb.NewTaggedStructFieldMapping(),
79+
columns: structCols,
80+
},
81+
wantErr: true,
82+
},
83+
{
84+
name: "extra columns",
85+
args: args{
86+
structVal: reflect.ValueOf(structPtr).Elem(),
87+
mapper: sqldb.NewTaggedStructFieldMapping(),
88+
columns: append(structCols, "some_column_not_found_at_struct"),
89+
},
90+
wantErr: true,
91+
},
92+
{
93+
name: "not enough columns",
94+
args: args{
95+
structVal: reflect.ValueOf(structPtr).Elem(),
96+
mapper: sqldb.NewTaggedStructFieldMapping(),
97+
columns: structCols[1:],
98+
},
99+
wantErr: true,
100+
},
101+
}
102+
for _, tt := range tests {
103+
t.Run(tt.name, func(t *testing.T) {
104+
gotPointers, err := ReflectStructColumnPointers(tt.args.structVal, tt.args.mapper, tt.args.columns)
105+
if tt.wantErr {
106+
require.Error(t, err)
107+
} else {
108+
require.NoError(t, err)
109+
}
110+
require.Equal(t, tt.wantPointers, gotPointers)
111+
})
112+
}
113+
}

0 commit comments

Comments
 (0)