|
1 | 1 | package sql
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "database/sql/driver" |
5 |
| - "fmt" |
6 | 4 | "testing"
|
7 | 5 |
|
8 | 6 | "github.com/stretchr/testify/assert"
|
9 | 7 | "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" |
12 | 9 | )
|
13 | 10 |
|
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) { |
39 | 12 | for _, tt := range []struct {
|
40 | 13 | name string
|
41 |
| - data any |
42 |
| - wantValue *fakeTStruct |
43 |
| - wantErr bool |
| 14 | + wantValue *credential.CredentialReference |
44 | 15 | }{
|
45 | 16 | {
|
46 | 17 | name: "nil value",
|
47 |
| - data: nil, |
48 |
| - wantErr: false, |
49 | 18 | wantValue: nil,
|
50 | 19 | },
|
51 | 20 | {
|
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 | + }, |
62 | 26 | },
|
63 | 27 | } {
|
64 | 28 | t.Run(tt.name, func(t *testing.T) {
|
65 | 29 | 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, |
106 | 35 | }
|
107 | 36 |
|
108 | 37 | data, err = s.Value()
|
109 | 38 | )
|
110 | 39 |
|
111 |
| - if tt.wantErr { |
112 |
| - require.Error(t, err) |
113 |
| - } else { |
114 |
| - require.NoError(t, err) |
115 |
| - } |
| 40 | + require.NoError(t, err) |
116 | 41 |
|
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) |
118 | 46 | })
|
119 | 47 | }
|
120 | 48 | }
|
0 commit comments