-
Notifications
You must be signed in to change notification settings - Fork 4
/
Classes_test.go
193 lines (171 loc) · 4.84 KB
/
Classes_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
package lean
import (
"github.com/johnzeng/leancloud-go-sdk/query"
"github.com/johnzeng/leancloud-go-sdk/update"
"os"
"testing"
"time"
)
type Test struct {
LeanClassesBase
Hello string `json:"hi,omitempty"`
TestBytes *LeanByte `json:"bytess,omitempty"`
TestDate *LeanTime `json:"tester,omitempty"`
TestRelation *LeanRelation `json:"user,omitempty"`
//we have some problem on LeanFile API
// TestFile *LeanFile `json:"filePtr,omitempty"`
TestPointer *LeanPointer `json:"userPtr,omitempty"`
notUpload string `json:"notUpload,omitempty"`
Ignore string `json:"-"`
Array []string `json:"ss,omitempty"`
}
var id string
//remember to uncomment this if you run this test first time otherwise you will get error on scan and query
//func TestCreateObjectFirstRun(t *testing.T) {
// client := NewClient(os.Getenv("LEAN_APPID"),
// os.Getenv("LEAN_APPKEY"),
// os.Getenv("LEAN_MASTERKEY"))
// now := NewLeanTime(time.Now())
// agent := client.Collection("test").Create(
// Test{
// Hello: "this is first message",
// notUpload: "nono",
// Ignore: "ignore",
// TestDate: &now,
// })
//
// if err := agent.Do(); nil != err {
// t.Error(err.Error())
// }
//}
func TestCreateObject(t *testing.T) {
client := NewClient(os.Getenv("LEAN_APPID"),
os.Getenv("LEAN_APPKEY"),
os.Getenv("LEAN_MASTERKEY"))
now := NewLeanTime(time.Now())
bytes := NewLeanByte([]byte("hello"))
agent := client.Collection("test").Create(
Test{
Hello: "this is first message",
TestBytes: &bytes,
notUpload: "nono",
Ignore: "ignore",
TestDate: &now,
})
if err := agent.Do(); nil != err {
t.Error(err.Error())
}
ret := Test{}
if err := agent.ScanResponse(&ret); err != nil {
t.Error(err.Error())
} else {
t.Logf("%x", ret)
id = ret.ObjectId
}
t.Log("%v", agent.superAgent.Data)
}
func TestClassQuery(t *testing.T) {
client := NewClient(os.Getenv("LEAN_APPID"),
os.Getenv("LEAN_APPKEY"),
os.Getenv("LEAN_MASTERKEY"))
agent := client.Collection("test").Query()
q := query.Eq("hi", "this is first message")
agent.WithQuery(q).Limit(1)
agent.Do()
ret := TestResp{}
agent.ScanResponse(&ret)
if len(ret.Results) == 0 {
t.Errorf("message is wrong:%v", ret)
t.Log(agent.body)
return
}
if ret.Results[0].Hello != "this is first message" {
t.Errorf("message is wrong:%v", ret)
t.Log(agent.body)
}
}
func TestClassUpdate(t *testing.T) {
client := NewClient(os.Getenv("LEAN_APPID"),
os.Getenv("LEAN_APPKEY"),
os.Getenv("LEAN_MASTERKEY"))
TestUploadPlainText(t)
TestUserLogin(t)
now := NewLeanTime(time.Now())
pointer := LeanPointer{class: "_user", objectId: userId}
// filePtr := LeanFile{Id: fileId}
test := Test{
Array: make([]string, 1),
TestDate: &now,
TestPointer: &pointer,
// TestFile: &filePtr,
}
test.Array[0] = "hello"
agent := client.Collection("test").UpdateObjectById(id, test)
if err := agent.Do(); nil != err {
t.Log("%v", agent.superAgent.Data)
t.Error(err.Error())
return
}
t.Log("%v", agent.superAgent.Data)
}
func TestClassUpdateByPart(t *testing.T) {
client := NewClient(os.Getenv("LEAN_APPID"),
os.Getenv("LEAN_APPKEY"),
os.Getenv("LEAN_MASTERKEY"))
addObj := update.AddToArray("ss", "123", "456")
updateObj := update.AddRelation("user", LeanPointer{class: "_user", objectId: userId}).And(addObj)
agent := client.Collection("test").UpdateObjectById(id, updateObj)
//if you don't wanna update by master key, you need to specify the id in update object body
agent.UseMasterKey()
if err := agent.Do(); nil != err {
t.Error(err.Error())
t.Log(agent.superAgent.Data)
}
}
func TestClassScan(t *testing.T) {
client := NewClient(os.Getenv("LEAN_APPID"),
os.Getenv("LEAN_APPKEY"),
os.Getenv("LEAN_MASTERKEY"))
agent := client.Collection("test").Scan("", "")
q := query.Eq("hi", "this is first message")
agent.WithQuery(q).Limit(1)
agent.Do()
ret := TestResp{}
agent.ScanResponse(&ret)
if len(ret.Results) == 0 {
t.Errorf("message is wrong:%v", ret)
t.Log(agent.body)
return
}
if ret.Results[0].Hello != "this is first message" {
t.Errorf("message is wrong:%v", ret)
t.Log(agent.body)
}
}
func TestGetObjectById(t *testing.T) {
client := NewClient(os.Getenv("LEAN_APPID"),
os.Getenv("LEAN_APPKEY"),
os.Getenv("LEAN_MASTERKEY"))
agent := client.Collection("test").GetObjectById(id)
if err := agent.Do(); nil != err {
t.Error(err.Error())
}
ret := Test{}
if err := agent.ScanResponse(&ret); nil != err {
t.Error(err.Error())
} else {
if ret.Hello != "this is first message" {
t.Error("message is wrong")
}
}
}
func TestClassDelete(t *testing.T) {
client := NewClient(os.Getenv("LEAN_APPID"),
os.Getenv("LEAN_APPKEY"),
os.Getenv("LEAN_MASTERKEY"))
agent := client.Collection("test").DeleteObjectById(id)
agent.UseMasterKey()
if err := agent.Do(); nil != err {
t.Error(err.Error())
}
}