forked from simpleforce/simpleforce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sobject_test.go
248 lines (213 loc) · 5.29 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
package simpleforce
import (
"log"
"testing"
"time"
)
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").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").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")).
Set("Comments", "This case is created by simpleforce").
Create()
if case1Result == nil || case1Result.ID() == "" || case1Result.Type() != case1.Type() {
t.Fail()
} else {
log.Println(logPrefix, "Case created,", case1Result.Get().StringField("CaseNumber"))
}
// Positive 2
caseComment1 := client.SObject("CaseComment")
caseComment1Result := caseComment1.Set("ParentId", case1Result.ID()).
Set("CommentBody", "This comment is created by simpleforce").
Set("IsPublished", true).
Create()
if caseComment1Result.Get().SObjectField("Case", "ParentId").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
if client.SObject("Case").
Set("Subject", "Case created by simpleforce on "+time.Now().Format("2006/01/02 03:04:05")).
Create().
Set("Subject", "Case subject updated by simpleforce").
Update().
Get().
StringField("Subject") != "Case subject updated by simpleforce" {
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")).
Create().
Get()
if case1 == nil || case1.ID() == "" {
t.Fatal()
}
caseID := case1.ID()
if case1.Delete() != nil {
t.Fail()
}
case1 = client.SObject("Case").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").
Set("Subject", "Original").
Create().
Get()
// Query the case by ID, then update the Subject.
case2 := client.SObject("Case").
Get(case1.ID()).
Set("Subject", "Updated").
Update().
Get()
// Query the case by ID again and check if the Subject has been updated.
case3 := client.SObject("Case").
Get(case2.ID())
if case3.StringField("Subject") != "Updated" {
t.Fail()
}
user1 := client.SObject("User").Create()
log.Println(user1.ID())
}