forked from BuxOrg/bux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_ids_test.go
135 lines (113 loc) · 2.91 KB
/
model_ids_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
package bux
import (
"bytes"
"testing"
"github.com/99designs/gqlgen/graphql"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestIDs_GormDataType will test the method GormDataType()
func TestIDs_GormDataType(t *testing.T) {
t.Parallel()
i := new(IDs)
assert.Equal(t, gormTypeText, i.GormDataType())
}
// TestIDs_Scan will test the method Scan()
func TestIDs_Scan(t *testing.T) {
t.Parallel()
t.Run("nil value", func(t *testing.T) {
i := IDs{}
err := i.Scan(nil)
require.NoError(t, err)
assert.Equal(t, 0, len(i))
})
t.Run("empty string", func(t *testing.T) {
i := IDs{}
err := i.Scan("\"\"")
assert.Error(t, err)
assert.Equal(t, 0, len(i))
})
t.Run("valid slice of ids", func(t *testing.T) {
i := IDs{}
err := i.Scan("[\"test1\",\"test2\"]")
require.NoError(t, err)
assert.Equal(t, 2, len(i))
assert.Equal(t, "test1", i[0])
assert.Equal(t, "test2", i[1])
})
t.Run("empty id slice", func(t *testing.T) {
i := IDs{}
err := i.Scan("[\"\"]")
require.NoError(t, err)
assert.Equal(t, 1, len(i))
assert.Equal(t, "", i[0])
})
t.Run("invalid JSON", func(t *testing.T) {
i := IDs{}
err := i.Scan("[test1]")
assert.Error(t, err)
})
}
// TestIDs_Value will test the method Value()
func TestIDs_Value(t *testing.T) {
t.Parallel()
t.Run("empty object", func(t *testing.T) {
i := IDs{}
value, err := i.Value()
require.NoError(t, err)
assert.Equal(t, "[]", value)
})
t.Run("ids present", func(t *testing.T) {
i := IDs{"test1"}
value, err := i.Value()
require.NoError(t, err)
assert.Equal(t, "[\"test1\"]", value)
})
}
// TestMarshalIDs will test the method MarshalIDs()
func TestMarshalIDs(t *testing.T) {
t.Parallel()
t.Run("nil", func(t *testing.T) {
writer := MarshalIDs(nil)
require.NotNil(t, writer)
assert.IsType(t, graphql.Null, writer)
})
t.Run("empty object", func(t *testing.T) {
writer := MarshalIDs(IDs{})
require.NotNil(t, writer)
b := bytes.NewBufferString("")
writer.MarshalGQL(b)
assert.Equal(t, "[]\n", b.String())
})
t.Run("map present", func(t *testing.T) {
writer := MarshalIDs(IDs{"test1"})
require.NotNil(t, writer)
b := bytes.NewBufferString("")
writer.MarshalGQL(b)
assert.Equal(t, "[\"test1\"]\n", b.String())
})
}
// TestUnmarshalIDs will test the method UnmarshalIDs()
func TestUnmarshalIDs(t *testing.T) {
t.Parallel()
t.Run("nil value", func(t *testing.T) {
i, err := UnmarshalIDs(nil)
require.NoError(t, err)
assert.Equal(t, 0, len(i))
assert.IsType(t, IDs{}, i)
})
t.Run("empty string", func(t *testing.T) {
i, err := UnmarshalIDs("\"\"")
assert.Error(t, err)
assert.Equal(t, 0, len(i))
assert.IsType(t, IDs{}, i)
})
t.Run("valid set of ids", func(t *testing.T) {
i, err := UnmarshalIDs(IDs{"test1", "test2"})
require.NoError(t, err)
assert.Equal(t, 2, len(i))
assert.IsType(t, IDs{}, i)
assert.Equal(t, "test1", i[0])
assert.Equal(t, "test2", i[1])
})
}