forked from simpleforce/simpleforce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sobject_test.go
327 lines (283 loc) · 7.48 KB
/
sobject_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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package simpleforce
import (
"log"
"testing"
"time"
"github.com/google/uuid"
)
func TestSObject_AttributesField(t *testing.T) {
obj := &SObject{}
if obj.AttributesField() != nil {
t.Fail()
}
obj.setType("Case")
if obj.AttributesField().Type != "Case" {
t.Fail()
}
obj.setType("")
if obj.AttributesField().Type != "" {
t.Fail()
}
}
func TestSObject_Type(t *testing.T) {
obj := &SObject{
sobjectAttributesKey: SObjectAttributes{Type: "Case"},
}
if obj.Type() != "Case" {
t.Fail()
}
obj.setType("CaseComment")
if obj.Type() != "CaseComment" {
t.Fail()
}
}
func TestSObject_InterfaceField(t *testing.T) {
obj := &SObject{}
if obj.InterfaceField("test_key") != nil {
t.Fail()
}
(*obj)["test_key"] = "hello"
if obj.InterfaceField("test_key") == nil {
t.Fail()
}
}
func TestSObject_SObjectField(t *testing.T) {
obj := &SObject{
sobjectAttributesKey: SObjectAttributes{Type: "CaseComment"},
"ParentId": "__PARENT_ID__",
}
// Positive checks
caseObj := obj.SObjectField("Case", "ParentId")
if caseObj.Type() != "Case" {
log.Println("Type mismatch")
t.Fail()
}
if caseObj.StringField("Id") != "__PARENT_ID__" {
log.Println("ID mismatch")
t.Fail()
}
// Negative checks
userObj := obj.SObjectField("User", "OwnerId")
if userObj != nil {
log.Println("Nil mismatch")
t.Fail()
}
}
func TestSObject_Describe(t *testing.T) {
client := requireClient(t, true)
meta := client.SObject("Case").Describe()
if meta == nil {
t.FailNow()
} else {
if (*meta)["name"].(string) != "Case" {
t.Fail()
}
}
}
func TestSObject_Get(t *testing.T) {
client := requireClient(t, true)
// Search for a valid Case ID first.
queryResult, err := client.Query("SELECT Id,OwnerId,Subject FROM CASE")
if err != nil || queryResult == nil {
log.Println(logPrefix, "query failed,", err)
t.FailNow()
}
if queryResult.TotalSize < 1 {
t.FailNow()
}
oid := queryResult.Records[0].ID()
ownerID := queryResult.Records[0].StringField("OwnerId")
// Positive
obj := client.SObject("Case")
obj.Get(oid)
if obj.ID() != oid || obj.StringField("OwnerId") != ownerID {
t.Fail()
}
// Positive 2
obj = client.SObject("Case")
if obj.StringField("OwnerId") != "" {
t.Fail()
}
obj.setID(oid)
obj.Get()
if obj.ID() != oid || obj.StringField("OwnerId") != ownerID {
t.Fail()
}
// Negative 1
obj = client.SObject("Case")
obj.Get("non-exist-id")
if obj != nil {
t.Fail()
}
// Negative 2
obj = &SObject{}
if obj.Get() != nil {
t.Fail()
}
}
func TestSObject_Create(t *testing.T) {
client := requireClient(t, true)
// Positive
case1 := client.SObject("Case")
case1Result := case1.Set("Subject", "Case created by simpleforce on "+time.Now().Format("2006/01/02 03:04:05"))
case1.Set("Comments", "This case is created by simpleforce")
case1.Create()
if case1Result == nil || case1Result.ID() == "" || case1Result.Type() != case1.Type() {
t.Fail()
} else {
case1Result.Get()
log.Println(logPrefix, "Case created,", case1Result.StringField("CaseNumber"))
}
// Positive 2
caseComment1 := client.SObject("CaseComment")
caseComment1Result := caseComment1.Set("ParentId", case1Result.ID())
caseComment1.Set("CommentBody", "This comment is created by simpleforce & used for testing")
caseComment1.Set("IsPublished", true)
caseComment1.Create()
caseComment1Result.Get()
caseComment1Result.SObjectField("Case", "ParentId")
if caseComment1Result.ID() != case1Result.ID() {
t.Fail()
} else {
log.Println(logPrefix, "CaseComment created,", caseComment1Result.ID())
}
// Negative: object without type.
obj := client.SObject()
if obj.Create() != nil {
t.Fail()
}
// Negative: object without client.
obj = &SObject{}
if obj.Create() != nil {
t.Fail()
}
// Negative: Invalid type
obj = client.SObject("__SOME_INVALID_TYPE__")
if obj.Create() != nil {
t.Fail()
}
// Negative: Invalid field
obj = client.SObject("Case").Set("__SOME_INVALID_FIELD__", "")
if obj.Create() != nil {
t.Fail()
}
}
func TestSObject_Update(t *testing.T) {
client := requireClient(t, true)
// Positive
c := client.SObject("Case")
c.Set("Subject", "Case created by simpleforce on "+time.Now().Format("2006/01/02 03:04:05"))
c.Create()
c.Set("Subject", "Case subject updated by simpleforce")
c.Update()
c.Get()
if c.StringField("Subject") != "Case subject updated by simpleforce" {
t.Fail()
}
}
func TestSObject_Upsert(t *testing.T) {
client := requireClient(t, true)
// Positive create new object through upsert
case1 := client.SObject("Case")
case1Result := case1.Set("Subject", "Case created by simpleforce on "+time.Now().Format("2006/01/02 03:04:05"))
case1Result.Set("Comments", "This case is created by simpleforce")
case1Result.Set("customExtIdField__c", uuid.NewString())
case1Result.Set("ExternalIDField", "customExtIdField__c")
case1Result.Upsert()
if case1Result == nil || case1Result.ID() == "" || case1Result.Type() != case1.Type() {
t.Fail()
} else {
case1Result.Get()
log.Println(logPrefix, "Case created,", case1Result.StringField("CaseNumber"))
}
// Positive update existing object through upsert
case2 := client.SObject("Case").
Set("Subject", "Case created by simpleforce on "+time.Now().Format("2006/01/02 03:04:05")).
Set("customExtIdField__c", uuid.NewString())
case2.
Set("Subject", "Case subject updated by simpleforce").
Set("ExternalIDField", "customExtIdField__c").
Upsert()
case2.Get()
if case2.StringField("Subject") != "Case subject updated by simpleforce" {
t.Fail()
} else {
case2.Get()
log.Println(logPrefix, "Case updated,", case2.StringField("CaseNumber"))
}
// Negative: object without type.
obj := client.SObject()
if obj.Upsert() != nil {
t.Fail()
}
// Negative: object without client.
obj = &SObject{}
if obj.Upsert() != nil {
t.Fail()
}
// Negative: Invalid type
obj = client.SObject("__SOME_INVALID_TYPE__").
Set("ExternalIDField", "customExtIdField__c").
Set("customExtIdField__c", uuid.NewString())
if obj.Upsert() != nil {
t.Fail()
}
// Negative: Invalid field
obj = client.SObject("Case").
Set("ExternalIDField", "customExtIdField__c").
Set("customExtIdField__c", uuid.NewString()).
Set("__SOME_INVALID_FIELD__", "")
if obj.Upsert() != nil {
t.Fail()
}
// Negative: Missing ext ID
obj = client.SObject("Case").
Set("ExternalIDField", "customExtIdField__c")
if obj.Upsert() != nil {
t.Fail()
}
}
func TestSObject_Delete(t *testing.T) {
client := requireClient(t, true)
// Positive: create a case first then delete it and verify if it is gone.
case1 := client.SObject("Case").
Set("Subject", "Case created by simpleforce on "+time.Now().Format("2006/01/02 03:04:05"))
case1.Create()
case1.Get()
if case1 == nil || case1.ID() == "" {
t.Fatal()
}
caseID := case1.ID()
if case1.Delete() != nil {
t.Fail()
}
case1 = client.SObject("Case")
case1.Get(caseID)
if case1 != nil {
t.Fail()
}
}
// TestSObject_GetUpdate validates updating of existing records.
func TestSObject_GetUpdate(t *testing.T) {
client := requireClient(t, true)
// Create a new case first.
case1 := client.SObject("Case")
case1.Set("Subject", "Original").
Create()
case1.Get()
// Query the case by ID, then update the Subject.
case2 := client.SObject("Case")
case2.Get(case1.ID())
case2.Set("Subject", "Updated").
Update()
case2.Get()
// Query the case by ID again and check if the Subject has been updated.
case3 := client.SObject("Case")
case3.Get(case2.ID())
if case3.StringField("Subject") != "Updated" {
t.Fail()
}
user1 := client.SObject("User")
user1.Create()
log.Println(user1.ID())
}