-
Notifications
You must be signed in to change notification settings - Fork 0
/
views_test.go
153 lines (138 loc) · 3.02 KB
/
views_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
package couchdb
import (
"testing"
)
// Define a test struct for holding test cases
type testCase struct {
Name string
Input interface{}
ShouldErr bool
}
func TestCheckStructForJSONFields(t *testing.T) {
testCases := []testCase{
{
Name: "Valid struct with required fields and JSON tags",
Input: &validStruct{},
ShouldErr: false,
},
{
Name: "Struct missing 'Rows' field",
Input: &missingRowsStruct{},
ShouldErr: true,
},
{
Name: "Struct with 'Rows' field of wrong type",
Input: &wrongTypeRowsStruct{},
ShouldErr: true,
},
{
Name: "Struct with 'Rows' field missing JSON tag",
Input: &missingRowsTagStruct{},
ShouldErr: true,
},
{
Name: "Struct missing 'ID' field",
Input: &missingIDStruct{},
ShouldErr: true,
},
{
Name: "Struct missing 'Key' field",
Input: &missingKeyStruct{},
ShouldErr: true,
},
{
Name: "Struct with 'ID' field missing JSON tag",
Input: &missingIDTagStruct{},
ShouldErr: true,
},
{
Name: "Struct with 'Key' field missing JSON tag",
Input: &missingKeyTagStruct{},
ShouldErr: true,
},
{
Name: "Valid struct with 'Doc' field and JSON tag",
Input: &validDocStruct{},
ShouldErr: false,
},
{
Name: "Struct with 'Doc' field missing JSON tag",
Input: &missingDocTagStruct{},
ShouldErr: true,
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
err := checkStructForJSONFields(tc.Input)
if (err != nil) != tc.ShouldErr {
t.Errorf("Expected error: %v, Got error: %v", tc.ShouldErr, err)
}
})
}
}
// Define sample structs for testing
type validStruct struct {
Rows []struct {
ID string `json:"id"`
Key string `json:"key"`
Doc struct{} `json:"doc"`
} `json:"rows"`
}
type missingRowsStruct struct {
ID string `json:"id"`
Key string `json:"key"`
Doc struct{} `json:"doc"`
}
type wrongTypeRowsStruct struct {
Rows string `json:"rows"`
ID string `json:"id"`
Key string `json:"key"`
Doc struct{} `json:"doc"`
}
type missingRowsTagStruct struct {
Rows []struct {
ID string `json:"id"`
Key string `json:"key"`
Doc struct{} `json:"doc"`
}
}
type missingIDStruct struct {
Rows []struct {
Key string `json:"key"`
Doc struct{} `json:"doc"`
} `json:"rows"`
}
type missingKeyStruct struct {
Rows []struct {
ID string `json:"id"`
Doc struct{} `json:"doc"`
} `json:"rows"`
}
type missingIDTagStruct struct {
Rows []struct {
ID string
Key string `json:"key"`
Doc struct{} `json:"doc"`
} `json:"rows"`
}
type missingKeyTagStruct struct {
Rows []struct {
ID string `json:"id"`
Key string
Doc struct{} `json:"doc"`
} `json:"rows"`
}
type validDocStruct struct {
Rows []struct {
ID string `json:"id"`
Key string `json:"key"`
Doc struct{} `json:"doc"`
} `json:"rows"`
}
type missingDocTagStruct struct {
Rows []struct {
ID string `json:"id"`
Key string `json:"key"`
Doc struct{} `json:"dock"`
} `json:"rows"`
}