-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse_test.go
183 lines (156 loc) · 3.58 KB
/
parse_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
package parse_test
import (
"fmt"
"log"
"os"
"strings"
"testing"
"time"
"github.com/tmc/parse"
)
type GameScore struct {
parse.ParseObject
CheatMode bool `json:"cheatMode,omitempty"`
PlayerName string `json:"playerName,omitempty"`
Score float64 `json:"score,omitempty"`
}
func mkclient(t *testing.T) *parse.Client {
appID := os.Getenv("APPLICATION_ID")
apiKey := os.Getenv("REST_API_KEY")
if appID == "" || apiKey == "" {
t.Skip("skipped due to missing APPLICATION_ID and/or REST_API_KEY env vars")
}
client, err := parse.NewClient(appID, apiKey)
if testing.Verbose() {
client.TraceOn(log.New(os.Stderr, "[parse test] ", log.LstdFlags))
}
if err != nil {
t.Fatal(err)
}
return client
}
func TestClassesEndpoints(t *testing.T) {
client := mkclient(t)
// Create
objID, err := client.Create(&GameScore{
CheatMode: true,
PlayerName: "Sean Plott",
Score: 1337,
})
if err != nil {
t.Fatal(err)
}
// Update
upd := &GameScore{
Score: 31337,
}
upd.ID = objID
if _, err := client.Update(upd); err != nil {
t.Fatal(err)
}
// Get
obj := new(GameScore)
if err := client.Get(objID, obj); err != nil {
t.Fatal(err)
}
if obj.Score != 31337 {
t.Errorf("Update failed, Score is not 31337\nObj:%+v", obj)
}
// Query
objs := []GameScore{}
if err := client.Query(nil, &objs); err != nil {
t.Fatal(err)
}
// Delete
if err := client.Delete(obj); err != nil {
t.Fatal(err)
}
// Query again and ensure count is reduced
prevLen := len(objs)
objs = []GameScore{}
if err := client.Query(nil, &objs); err != nil {
t.Fatal(err)
}
if len(objs) >= prevLen {
t.Fatal("Number of objects didn't decrease")
}
}
func TestFileOperations(t *testing.T) {
if os.Getenv("MASTER_KEY") == "" {
t.Skip("skipped due to missing APPLICATION_ID and/or REST_API_KEY env vars")
}
client := mkclient(t)
client = client.WithMasterKey(os.Getenv("MASTER_KEY"))
f, err := client.UploadFile("answer.txt", strings.NewReader("42"), "text/plain")
if err != nil {
t.Fatal(err)
}
err = client.DeleteFile(f.Name)
if err != nil {
t.Fatal(err)
}
}
func TestUserOperations(t *testing.T) {
client := mkclient(t)
type MyParseUser struct {
parse.ParseUser
Extra string `json:"extra"`
}
user := MyParseUser{}
user.Username = "joe" + fmt.Sprint(time.Now().Unix())
user.Password = "kinginyell0"
user.Extra = "extra content"
// Create user
loadedUser, err := client.CreateUser(user)
if err != nil {
t.Fatal(err)
}
// Login user
loggedInUser := MyParseUser{}
err = client.LoginUser(user.Username, user.Password, &loggedInUser)
if err != nil {
t.Fatal(err)
}
if loggedInUser.Extra != "extra content" {
t.Fatalf(".Extra content unexpected: %v", loggedInUser.Extra)
}
client = client.WithSessionToken(loadedUser.SessionToken)
// CurrentUser()
currentUser := MyParseUser{}
if err := client.CurrentUser(¤tUser); err != nil {
t.Fatal(err)
}
// Delete user
err = client.DeleteUser(loggedInUser)
if err != nil {
t.Fatal(err)
}
}
func TestQuery(t *testing.T) {
client := mkclient(t)
type Widget struct {
parse.ParseObject
Name string `json:"name"`
Quantity int `json:"qty"`
}
widgets := []*Widget{
{Name: "widget a", Quantity: 42},
{Name: "widget b", Quantity: 41},
{Name: "widget c", Quantity: 40},
}
for _, w := range widgets {
if _, err := client.Create(w); err != nil {
t.Error(err)
}
}
dest := []*Widget{}
if err := client.Query(nil, &dest); err != nil {
t.Fatal(err)
}
if len(dest) != len(widgets) {
t.Errorf("expected %v widgets, got %v", len(widgets), len(dest))
}
for _, w := range dest {
client.Delete(w)
}
}