Skip to content

Commit bf16bab

Browse files
committed
use real type to test and simplify test
1 parent 2d7ba0d commit bf16bab

File tree

3 files changed

+25
-93
lines changed

3 files changed

+25
-93
lines changed

go.mod

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ require (
3434
github.com/upfluence/errors v0.2.9
3535
github.com/upfluence/log v0.0.5
3636
github.com/upfluence/stats v0.1.4
37-
github.com/upfluence/thrift v2.4.4+incompatible
37+
github.com/upfluence/thrift v2.6.8+incompatible
3838
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f
3939
golang.org/x/oauth2 v0.19.0
4040
golang.org/x/sync v0.9.0
@@ -43,6 +43,8 @@ require (
4343
golang.org/x/time v0.3.0
4444
)
4545

46+
require github.com/upfluence/base v0.1.138
47+
4648
require (
4749
github.com/beorn7/perks v1.0.1 // indirect
4850
github.com/cespare/xxhash/v2 v2.2.0 // indirect

go.sum

+4-2
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@ github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGr
451451
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
452452
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
453453
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
454+
github.com/upfluence/base v0.1.138 h1:QvYPq0fhSE54AVCr5rsB+pACAP2vVdpr+Yn0r2Ud7yo=
455+
github.com/upfluence/base v0.1.138/go.mod h1:jjVPOP0HtL6weh6xrqSIo2Mzt+30LIjjr4yK8npiiTM=
454456
github.com/upfluence/cfg v0.3.5 h1:NoA7vrzPM2gOso58x0H8kEDdDd9TnBJtds6zXCaieI0=
455457
github.com/upfluence/cfg v0.3.5/go.mod h1:0TzWsboPheOr4WtyZ370j6KuFAkXAS2IqpdVnTYSBSc=
456458
github.com/upfluence/errors v0.0.0-20210413181856-d161314a8a8c/go.mod h1:WWK5iZiYpEHoLYZV+MZSFsHrnYueCUMwsaEEko7aiTU=
@@ -461,8 +463,8 @@ github.com/upfluence/log v0.0.5 h1:OR7KY6NKfrqvWfXz1nWHEGLtwP+tw1a5x/6hfPBatHU=
461463
github.com/upfluence/log v0.0.5/go.mod h1:1uMfWKPiN8wDCUtENVcYC0bRrTqMeSyAP5R3P5yWXsc=
462464
github.com/upfluence/stats v0.1.4 h1:youBJzTz7H2xgXY7okdBauu321Aa/W4SeYQC6g8HHHU=
463465
github.com/upfluence/stats v0.1.4/go.mod h1:kzquL6D8zXPCcjFgPqq6e/+6lzzglTJZzJ3V7UWf/as=
464-
github.com/upfluence/thrift v2.4.4+incompatible h1:xmhLcu9nmMcMjZOVzDl95E+Q9NmMQLDQ/gQ3V54tq4Q=
465-
github.com/upfluence/thrift v2.4.4+incompatible/go.mod h1:3uGdmLrgG2WjEd1pvDN5Rnxlui3lAE1cZHtv+7O337E=
466+
github.com/upfluence/thrift v2.6.8+incompatible h1:y8wb0oxdKgPfWW+p62lYZkbks3rIvh/IWGS73RwAQ8Y=
467+
github.com/upfluence/thrift v2.6.8+incompatible/go.mod h1:3uGdmLrgG2WjEd1pvDN5Rnxlui3lAE1cZHtv+7O337E=
466468
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
467469
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
468470
github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=

thrift/sql/serializing_test.go

+18-90
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,48 @@
11
package sql
22

33
import (
4-
"database/sql/driver"
5-
"fmt"
64
"testing"
75

86
"github.com/stretchr/testify/assert"
97
"github.com/stretchr/testify/require"
10-
"github.com/upfluence/errors"
11-
"github.com/upfluence/thrift/lib/go/thrift"
8+
"github.com/upfluence/base/provider/credential"
129
)
1310

14-
type fakeTStruct struct {
15-
value int64
16-
}
17-
18-
func (t fakeTStruct) Write(p thrift.TProtocol) error {
19-
return errors.WithStack(p.WriteI64(t.value))
20-
}
21-
22-
func (t *fakeTStruct) Read(p thrift.TProtocol) error {
23-
val, err := p.ReadI64()
24-
25-
if err != nil {
26-
return errors.WithStack(err)
27-
}
28-
29-
t.value = val
30-
31-
return nil
32-
}
33-
34-
func (t *fakeTStruct) String() string {
35-
return fmt.Sprint(t.value)
36-
}
37-
38-
func TestNullableThrift_Scan(t *testing.T) {
11+
func TestNullableThrift(t *testing.T) {
3912
for _, tt := range []struct {
4013
name string
41-
data any
42-
wantValue *fakeTStruct
43-
wantErr bool
14+
wantValue *credential.CredentialReference
4415
}{
4516
{
4617
name: "nil value",
47-
data: nil,
48-
wantErr: false,
4918
wantValue: nil,
5019
},
5120
{
52-
name: "with value",
53-
data: []byte{0, 0, 0, 0, 0, 0, 0, 42},
54-
wantErr: false,
55-
wantValue: &fakeTStruct{value: 42},
56-
},
57-
{
58-
name: "invalid value",
59-
data: 42,
60-
wantErr: true,
61-
wantValue: nil,
21+
name: "with value",
22+
wantValue: &credential.CredentialReference{
23+
Type: credential.CredentialType_StripeConnectedAccount,
24+
Id: 42,
25+
},
6226
},
6327
} {
6428
t.Run(tt.name, func(t *testing.T) {
6529
var (
66-
s NullThrift[fakeTStruct, *fakeTStruct]
67-
68-
err = s.Scan(tt.data)
69-
)
70-
71-
if tt.wantErr {
72-
require.Error(t, err)
73-
} else {
74-
require.NoError(t, err)
75-
}
76-
77-
assert.Equal(t, tt.wantValue, s.Data)
78-
})
79-
}
80-
}
81-
82-
func TestNullableThrift_Value(t *testing.T) {
83-
for _, tt := range []struct {
84-
name string
85-
haveValue *fakeTStruct
86-
wantData driver.Value
87-
wantErr bool
88-
}{
89-
{
90-
name: "nil value",
91-
haveValue: nil,
92-
wantData: nil,
93-
wantErr: false,
94-
},
95-
{
96-
name: "with value",
97-
haveValue: &fakeTStruct{value: 42},
98-
wantData: []byte{0, 0, 0, 0, 0, 0, 0, 42},
99-
wantErr: false,
100-
},
101-
} {
102-
t.Run(tt.name, func(t *testing.T) {
103-
var (
104-
s = NullThrift[fakeTStruct, *fakeTStruct]{
105-
Data: tt.haveValue,
30+
s = NullThrift[
31+
credential.CredentialReference,
32+
*credential.CredentialReference,
33+
]{
34+
Data: tt.wantValue,
10635
}
10736

10837
data, err = s.Value()
10938
)
11039

111-
if tt.wantErr {
112-
require.Error(t, err)
113-
} else {
114-
require.NoError(t, err)
115-
}
40+
require.NoError(t, err)
11641

117-
assert.Equal(t, tt.wantData, data)
42+
s.Data = nil
43+
44+
require.NoError(t, s.Scan(data))
45+
assert.Equal(t, tt.wantValue, s.Data)
11846
})
11947
}
12048
}

0 commit comments

Comments
 (0)