-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient_test.go
More file actions
216 lines (186 loc) · 4.78 KB
/
client_test.go
File metadata and controls
216 lines (186 loc) · 4.78 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package godoo
import (
"testing"
)
func TestClientProtocolMethods(t *testing.T) {
pool := NewPool(5, 2, 30)
// Test JSON-RPC (default) - don't authenticate to avoid network calls
cfgJSONRPC := &ClientConfig{
Database: "test_db",
Admin: "admin",
Password: "password",
URL: "https://test.odoo.com",
Pool: pool,
// Protocol: ProtocolJSONRPC // This is now the default
}
clientJSONRPC := &Client{
cfg: cfgJSONRPC,
jsonrpc: NewJSONRPCClient(cfgJSONRPC.URL, cfgJSONRPC.Pool),
auth: false,
protocol: ProtocolJSONRPC,
}
if clientJSONRPC.GetProtocol() != ProtocolJSONRPC {
t.Errorf("Expected protocol %s, got %s", ProtocolJSONRPC, clientJSONRPC.GetProtocol())
}
if !clientJSONRPC.IsJSONRPC() {
t.Error("Expected IsJSONRPC() to return true")
}
if clientJSONRPC.IsXMLRPC() {
t.Error("Expected IsXMLRPC() to return false")
}
// Test XML-RPC (explicit) - don't authenticate to avoid network calls
cfgXMLRPC := &ClientConfig{
Database: "test_db",
Admin: "admin",
Password: "password",
URL: "https://test.odoo.com",
Pool: pool,
Protocol: ProtocolXMLRPC,
}
clientXMLRPC := &Client{
cfg: cfgXMLRPC,
auth: false,
protocol: ProtocolXMLRPC,
}
if clientXMLRPC.GetProtocol() != ProtocolXMLRPC {
t.Errorf("Expected protocol %s, got %s", ProtocolXMLRPC, clientXMLRPC.GetProtocol())
}
if !clientXMLRPC.IsXMLRPC() {
t.Error("Expected IsXMLRPC() to return true")
}
if clientXMLRPC.IsJSONRPC() {
t.Error("Expected IsJSONRPC() to return false")
}
clientXMLRPC.Close()
clientJSONRPC.Close()
}
func TestClientConfigValidation(t *testing.T) {
tests := []struct {
name string
config *ClientConfig
valid bool
}{
{
name: "Valid config",
config: &ClientConfig{
Database: "test_db",
Admin: "admin",
Password: "password",
URL: "https://test.odoo.com",
},
valid: true,
},
{
name: "Missing database",
config: &ClientConfig{
Admin: "admin",
Password: "password",
URL: "https://test.odoo.com",
},
valid: false,
},
{
name: "Missing admin",
config: &ClientConfig{
Database: "test_db",
Password: "password",
URL: "https://test.odoo.com",
},
valid: false,
},
{
name: "Missing password",
config: &ClientConfig{
Database: "test_db",
Admin: "admin",
URL: "https://test.odoo.com",
},
valid: false,
},
{
name: "Missing URL",
config: &ClientConfig{
Database: "test_db",
Admin: "admin",
Password: "password",
},
valid: false,
},
{
name: "Valid with JSON-RPC protocol",
config: &ClientConfig{
Database: "test_db",
Admin: "admin",
Password: "password",
URL: "https://test.odoo.com",
Protocol: ProtocolJSONRPC,
},
valid: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.config.valid()
if result != tt.valid {
t.Errorf("Expected valid=%v, got valid=%v", tt.valid, result)
}
})
}
}
func TestOptionsConversion(t *testing.T) {
options := NewOptions()
options.Limit(10).Offset(5).FetchFields("name", "email")
// Test that Options can be converted to map[string]interface{}
var kwargs map[string]interface{}
if options != nil {
kwargs = *options
}
if kwargs["limit"] != 10 {
t.Errorf("Expected limit 10, got %v", kwargs["limit"])
}
if kwargs["offset"] != 5 {
t.Errorf("Expected offset 5, got %v", kwargs["offset"])
}
fields, ok := kwargs["fields"].([]string)
if !ok {
t.Error("Expected fields to be []string")
}
if len(fields) != 2 || fields[0] != "name" || fields[1] != "email" {
t.Errorf("Expected fields [name, email], got %v", fields)
}
}
func TestCriteriaCreation(t *testing.T) {
criteria := NewCriteria()
criteria.Add("active", "=", true)
criteria.Add("name", "ilike", "test")
if len(*criteria) != 2 {
t.Errorf("Expected 2 criteria, got %d", len(*criteria))
}
// Test first criterion
firstCriterion := (*criteria)[0]
if len(*firstCriterion) != 3 {
t.Errorf("Expected criterion with 3 elements, got %d", len(*firstCriterion))
}
if (*firstCriterion)[0] != "active" {
t.Errorf("Expected field 'active', got %v", (*firstCriterion)[0])
}
if (*firstCriterion)[1] != "=" {
t.Errorf("Expected operator '=', got %v", (*firstCriterion)[1])
}
if (*firstCriterion)[2] != true {
t.Errorf("Expected value true, got %v", (*firstCriterion)[2])
}
// Test argsFromCriteria function
args := argsFromCriteria(criteria)
if len(args) != 1 {
t.Errorf("Expected 1 argument, got %d", len(args))
}
// argsFromCriteria returns the dereferenced criteria, not a pointer
criteriaArray, ok := args[0].(Criteria)
if !ok {
t.Error("Expected criteria array")
}
if len(criteriaArray) != 2 {
t.Errorf("Expected 2 criteria, got %d", len(criteriaArray))
}
}