-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuuid_test.go
123 lines (104 loc) · 2.87 KB
/
uuid_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
package nulls
import (
"encoding/json"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"testing"
)
func newUUIDV4() uuid.UUID {
id, err := uuid.NewV4()
if err != nil {
panic(err)
}
return id
}
// TestNewUUID tests NewUUID.
func TestNewUUID(t *testing.T) {
id := newUUIDV4()
s := NewUUID(id)
assert.True(t, s.Valid, "should be valid")
assert.Equal(t, id, s.UUID, "should contain correct value")
}
// UUIDMarshalJSONSuite tests UUID.MarshalJSON.
type UUIDMarshalJSONSuite struct {
suite.Suite
}
func (suite *UUIDMarshalJSONSuite) TestNotValid() {
s := uuid.NullUUID{UUID: newUUIDV4()}
raw, err := json.Marshal(s)
suite.Require().NoError(err, "should not fail")
suite.Equal(jsonNull, raw, "should return correct value")
}
func (suite *UUIDMarshalJSONSuite) TestOK() {
id := newUUIDV4()
s := NewUUID(id)
raw, err := json.Marshal(s)
suite.Require().NoError(err, "should not fail")
suite.Equal(marshalMust(id), raw, "should return correct value")
}
func TestUUID_MarshalJSON(t *testing.T) {
suite.Run(t, new(UUIDMarshalJSONSuite))
}
// UUIDUnmarshalJSONSuite tests UUID.UnmarshalJSON.
type UUIDUnmarshalJSONSuite struct {
suite.Suite
}
func (suite *UUIDUnmarshalJSONSuite) TestNull() {
var s uuid.NullUUID
err := json.Unmarshal(jsonNull, &s)
suite.Require().NoError(err, "should not fail")
suite.False(s.Valid, "should not be valid")
}
func (suite *UUIDUnmarshalJSONSuite) TestOK() {
id := newUUIDV4()
var s uuid.NullUUID
err := json.Unmarshal(marshalMust(id), &s)
suite.Require().NoError(err, "should not fail")
suite.True(s.Valid, "should be valid")
suite.Equal(id, s.UUID, "should unmarshal correct value")
}
func TestUUID_UnmarshalJSON(t *testing.T) {
suite.Run(t, new(UUIDUnmarshalJSONSuite))
}
// UUIDScanSuite tests UUID.Scan.
type UUIDScanSuite struct {
suite.Suite
}
func (suite *UUIDScanSuite) TestNull() {
var s uuid.NullUUID
err := s.Scan(nil)
suite.Require().NoError(err, "should not fail")
suite.False(s.Valid, "should not be valid")
}
func (suite *UUIDScanSuite) TestOK() {
id := newUUIDV4()
var s uuid.NullUUID
err := s.Scan(id)
suite.Require().NoError(err, "should not fail")
suite.True(s.Valid, "should be valid")
suite.Equal(id, s.UUID, "should scan correct value")
}
func TestUUID_Scan(t *testing.T) {
suite.Run(t, new(UUIDScanSuite))
}
// UUIDValueSuite tests UUID.Value.
type UUIDValueSuite struct {
suite.Suite
}
func (suite *UUIDValueSuite) TestNull() {
s := uuid.NullUUID{UUID: newUUIDV4()}
raw, err := s.Value()
suite.Require().NoError(err, "should not fail")
suite.Nil(raw, "should return correct value")
}
func (suite *UUIDValueSuite) TestOK() {
id := newUUIDV4()
s := NewUUID(id)
raw, err := s.Value()
suite.Require().NoError(err, "should not fail")
suite.EqualValues(id.String(), raw, "should return correct value")
}
func TestUUID_Value(t *testing.T) {
suite.Run(t, new(UUIDValueSuite))
}