-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
153 lines (144 loc) · 4.95 KB
/
utils_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 (
"errors"
"fmt"
"reflect"
"testing"
)
// Base struct embedding Document
type Base struct {
Document // Embedded Document struct
Name string
}
// TestCheckParameter tests the checkParameter function
func TestCheckParameter(t *testing.T) {
tests := []struct {
name string
param interface{}
expected error
}{
{
name: "Test map[string]interface{} with _id and _rev",
param: map[string]interface{}{"_id": "123", "_rev": "456"},
expected: nil,
},
{
name: "Test map[string]interface{} without _id",
param: map[string]interface{}{"_rev": "456"},
expected: ErrMissingID,
},
{
name: "Test map[string]interface{} without _rev",
param: map[string]interface{}{"_id": "123"},
expected: ErrMissingRev,
},
{
name: "Test struct with embedded doc",
param: Base{Document: Document{ID: "123", Rev: "456"}},
expected: nil,
},
{
name: "Test struct with embedded doc",
param: &Base{Document: Document{ID: "123", Rev: "456"}},
expected: nil,
},
{
name: "Test unsupported type",
param: 123,
expected: errors.New("unsupported type"),
},
{
name: "Test nil parameter",
param: nil,
expected: errors.New("unsupported type"), // Adjust this expected error message if needed
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := checkParameter(tt.param)
if !reflect.DeepEqual(got, tt.expected) {
t.Errorf("checkParameter() got = %v, want %v", got, tt.expected)
}
})
}
}
func TestIsValidDBName(t *testing.T) {
testCases := []struct {
name string
expected bool
}{
{"my_database_123", true}, // Valid database name
{"Database_123", false}, // Database name doesn't start with a lowercase letter
{"my-database", true}, // Valid database name with hyphen
{"my(database)", true}, // Valid database name with parentheses
{"my+database", true}, // Valid database name with plus sign
{"my_database$", true}, // Valid database name with dollar sign
{"my_database&", false}, // Invalid character (&)
{"MyDatabase", false}, // Database name doesn't start with a lowercase letter
{"123database", false}, // Database name starts with a digit
{"_database", false}, // Database name doesn't start with a lowercase letter
{"my database", false}, // Database name contains whitespace
{"my/database", true}, // Valid database name with slash
{"my+database-123_$()", true}, // Valid database name with various special characters
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := isValidDBName(tc.name)
if result != tc.expected {
t.Errorf("Expected isValidDBName(%s) to be %v, got %v", tc.name, tc.expected, result)
}
})
}
}
func TestIsValidParam(t *testing.T) {
tests := []struct {
name string
param interface{}
expected bool
}{
{name: "nil value", param: nil, expected: false},
{name: "non-pointer value", param: "string", expected: false},
{name: "pointer to empty struct", param: struct{}{}, expected: false},
{name: "pointer to struct", param: &struct{ Name string }{}, expected: true},
{name: "pointer to map[string]interface{}", param: &map[string]interface{}{}, expected: true},
{name: "pointer to map[string]string", param: &map[string]string{}, expected: false},
{name: "pointer to map[int]interface{}", param: &map[int]interface{}{}, expected: false},
{name: "pointer to slice", param: &[]string{"a", "b", "c"}, expected: false},
{name: "pointer to slice of map[string]interface{}", param: &[]map[string]interface{}{{}}, expected: false},
{name: "pointer to slice of struct", param: &[]struct{ Name string }{{}}, expected: false},
{name: "pointer to slice of interface{}", param: &[]interface{}{"a", 1, true}, expected: false},
{name: "pointer to map[string]int", param: &map[string]int{"a": 1}, expected: false},
{name: "pointer to map[int]struct{}", param: &map[int]struct{}{}, expected: false},
{name: "pointer to map[int]string", param: &map[int]string{}, expected: false},
{name: "pointer to map[string]struct{}", param: &map[string]struct{ Name string }{"a": {Name: "b"}}, expected: false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := isValidParam(test.param)
if result != test.expected {
t.Errorf("Expected %t, got %t", test.expected, result)
}
})
}
}
func TestAddSlashIfNeeded(t *testing.T) {
testCases := []struct {
input string
expected string
}{
{"https://example.com/api/", "https://example.com/api/"},
{"https://example.com/api", "https://example.com/api/"},
{"", "/"},
{"/", "/"},
{"test/", "test/"},
{"test", "test/"},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("Input: %s", tc.input), func(t *testing.T) {
output := addSlashIfNeeded(tc.input)
if output != tc.expected {
t.Errorf("Expected: %s, Got: %s", tc.expected, output)
}
})
}
}