-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptional_test.go
168 lines (142 loc) · 4.5 KB
/
optional_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package nulls
import (
"database/sql/driver"
"encoding/json"
"errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"testing"
)
func TestOptional(t *testing.T) {
m := myStruct{A: "Hello World!"}
myOptional := NewOptional(m)
assert.True(t, myOptional.Valid, "should be valid")
assert.Equal(t, m, myOptional.V, "should have set correct value")
}
// TestNewOptional tests NewOptional.
func TestNewOptional(t *testing.T) {
n := NewOptional[myStruct](myStruct{A: "Hello World!"})
assert.True(t, n.Valid, "should be valid")
assert.Equal(t, "Hello World!", n.V.A, "should have set correct value")
}
// OptionalValueMock implements OptionalValue.
type OptionalValueMock struct {
mock.Mock
}
func (n OptionalValueMock) MarshalJSON() ([]byte, error) {
args := n.Called()
var b []byte
b, _ = args.Get(0).([]byte)
return b, args.Error(1)
}
func (n *OptionalValueMock) UnmarshalJSON(data []byte) error {
return n.Called(data).Error(0)
}
func (n OptionalValueMock) Scan(src any) error {
return n.Called(src).Error(0)
}
func (n OptionalValueMock) Value() (driver.Value, error) {
args := n.Called()
var v driver.Value
v, _ = args.Get(0).(driver.Value)
return v, args.Error(1)
}
// OptionalMarshalJSONSuite tests Optional.MarshalJSON.
type OptionalMarshalJSONSuite struct {
suite.Suite
}
func (suite *OptionalMarshalJSONSuite) TestNotValid() {
n := Optional[OptionalValueMock]{V: OptionalValueMock{}}
raw, err := json.Marshal(n)
suite.Require().NoError(err, "should not fail")
suite.Equal(jsonNull, raw, "should return correct value")
}
func (suite *OptionalMarshalJSONSuite) TestMarshalFail() {
n := NewOptional(OptionalValueMock{})
n.V.On("MarshalJSON").Return(nil, errors.New("sad life"))
defer n.V.AssertExpectations(suite.T())
_, err := json.Marshal(n)
suite.Require().Error(err, "should fail")
}
func (suite *OptionalMarshalJSONSuite) TestOK() {
n := NewOptional(OptionalValueMock{})
expectRaw := marshalMust("meow")
n.V.On("MarshalJSON").Return(expectRaw, nil)
defer n.V.AssertExpectations(suite.T())
raw, err := json.Marshal(n)
suite.Require().NoError(err, "should not fail")
suite.Equal(expectRaw, raw, "should return correct value")
}
func TestOptional_MarshalJSON(t *testing.T) {
suite.Run(t, new(OptionalMarshalJSONSuite))
}
// OptionalUnmarshalJSONSuite tests Optional.UnmarshalJSON.
type OptionalUnmarshalJSONSuite struct {
suite.Suite
}
func (suite *OptionalUnmarshalJSONSuite) TestNull() {
var n Optional[OptionalValueMock]
err := json.Unmarshal(jsonNull, &n)
suite.Require().NoError(err, "should not fail")
suite.False(n.Valid, "should not be valid")
}
func (suite *OptionalUnmarshalJSONSuite) TestUnmarshalFail() {
raw := marshalMust("meow")
n := Optional[OptionalValueMock]{V: OptionalValueMock{}}
n.V.On("UnmarshalJSON", raw).Return(errors.New("sad life"))
defer n.V.AssertExpectations(suite.T())
err := json.Unmarshal(raw, &n)
suite.Require().Error(err, "should fail")
}
func (suite *OptionalUnmarshalJSONSuite) TestOK() {
raw := marshalMust("meow")
n := Optional[OptionalValueMock]{V: OptionalValueMock{}}
n.V.On("UnmarshalJSON", raw).Return(nil)
defer n.V.AssertExpectations(suite.T())
err := json.Unmarshal(raw, &n)
suite.Require().NoError(err, "should not fail")
suite.True(n.Valid, "should be valid")
}
func TestOptional_UnmarshalJSON(t *testing.T) {
suite.Run(t, new(OptionalUnmarshalJSONSuite))
}
// OptionalScanSuite tests Optional.Scan.
type OptionalScanSuite struct {
suite.Suite
}
func (suite *OptionalScanSuite) TestNull() {
n := Optional[OptionalValueMock]{V: OptionalValueMock{}}
err := n.Scan(nil)
suite.Error(err, "should fail")
}
func (suite *OptionalScanSuite) TestScan() {
src := "Hello World!"
n := Optional[OptionalValueMock]{V: OptionalValueMock{}}
n.V.On("Scan", src).Return(nil).Maybe()
defer n.V.AssertExpectations(suite.T())
err := n.Scan(src)
suite.Error(err, "should fail")
}
func TestOptional_Scan(t *testing.T) {
suite.Run(t, new(OptionalScanSuite))
}
// OptionalValueSuite tests Optional.Value.
type OptionalValueSuite struct {
suite.Suite
}
func (suite *OptionalValueSuite) TestNull() {
n := Optional[OptionalValueMock]{V: OptionalValueMock{}}
_, err := n.Value()
suite.Error(err, "should fail")
}
func (suite *OptionalValueSuite) TestValue() {
n := NewOptional(OptionalValueMock{})
n.V.On("Value").Return("Hello", nil).Maybe()
defer n.V.AssertExpectations(suite.T())
_, err := n.Value()
suite.Error(err, "should fail")
}
func TestOptional_Value(t *testing.T) {
suite.Run(t, new(OptionalValueSuite))
}