forked from leancloud/javascript-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.js
More file actions
391 lines (340 loc) · 9.64 KB
/
Copy pathobject.js
File metadata and controls
391 lines (340 loc) · 9.64 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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
var GameScore = AV.Object.extend("GameScore");
var Post=AV.Object.extend("Post");
var GameScoreCollection = AV.Collection.extend({
model: GameScore
});
describe('Objects', function(){
var objId;
var gameScore = GameScore.new();
describe('#Saving Objects', function(){
it('should crate a Object', function(done){
//gameScore.set("newcol","sss")
var myPost = new Post();
myPost.set("title", "post1");
myPost.set("content", "Where should we go for lunch?");
var point = new AV.GeoPoint({latitude: 80.01, longitude: -30.01});
myPost.set("geo",point);
myPost.save(null, {
success: function(result) {
done();
},
error: function(gameScore, error) {
throw error;
}
});
});
it('should create another Object', function(done) {
gameScore.set("score", 1111);
gameScore.set("playerName", "dd");
gameScore.set("cheatMode", false);
gameScore.set("arr", ["arr1","arr2"]);
gameScore.save(null, {
success: function(result) {
expect(result.id).to.be.ok();
objId=result.id;
done();
},
error: function(gameScore, error) {
throw error;
}
});
});
it('should create a User',function(done){
var User = AV.Object.extend("User");
var u = new User();
var r=parseInt(Math.random()*1000);
u.set("username","u"+r);
u.set("password","11111111");
u.set("email","u"+r+"@test.com");
u.save(null,{
success:function(){
done();
}
});
});
it('should validate failed.', function(done){
var TestObject = AV.Object.extend('TestObject', {
validate: function (attrs, options){
return new AV.Error(1, "test");
}
});
var testObject =new TestObject();
testObject.set('a',1, {
success: function(){
throw "should not be here.";
},
error: function(obj, err){
debug(err);
expect(obj.get('a')).to.be(undefined);
expect(err.message).to.be('test');
done();
}
});
});
});
describe("Retrieving Objects",function(){
it("should be the just save Object",function(done){
var GameScore = AV.Object.extend("GameScore");
var query = new AV.Query(GameScore);
debug(objId);
query.get(objId, {
success: function(gameScore) {
expect(gameScore.id).to.be.ok();
done();
// The object was retrieved successfully.
},
error: function(object, error) {
throw error;
}
});
});
});
describe("Updating Objects",function(){
it("should update prop",function(done){
gameScore.set("score", 10000);
gameScore.save(null, {
success: function(result) {
expect(result.id).to.be.ok();
done();
},
error: function(gameScore, error) {
throw error;
}
});
});
it('should not update prop when query not match',function(done){
gameScore.set('score', 10000);
gameScore.save(null, {
query: new AV.Query(GameScore).equalTo('score', -1)
}).then(function(result) {
done(new Error('should not success'));
}, function(error) {
expect(error.code).to.be.eql(305);
done();
});
});
it('should update prop when query match',function(done){
gameScore.set('score', 10000);
gameScore.save(null, {
query: new AV.Query(GameScore).notEqualTo('score', -1),
fetchWhenSave: true
}).then(function(result) {
done();
}, function(error) {
done(error);
});
});
});
describe("Deleting Objects",function(){
it("should delete cheatMode",function(done){
gameScore.unset("cheatMode");
gameScore.save(null,{
success:function(result){
done();
},
error:function(err){
throw err;
}
});
});
});
describe("Relational Data",function(){
var commentId,myComment,myPost,relation;
it("should set relation ",function(done){
// Declare the types.
var Post = AV.Object.extend("Post");
var Comment = AV.Object.extend("Comment");
// Create the post
myPost = Post.new();
myPost.set("title", "post1");
myPost.set("author", "author1");
myPost.set("content", "Where should we go for lunch?");
var point = new AV.GeoPoint({latitude: 80.01, longitude: -30.01});
myPost.set("location",point);
// Create the comment
myComment = new Comment();
myComment.set("content", "comment1");
// Add a relation between the Post and Comment
myComment.set("parent", myPost);
// This will save both myPost and myComment
myComment.save(null,{
success:function(myComment){
var query = new AV.Query(Comment);
query.include("parent");
query.get(myComment.id).then(function(obj){
expect(obj.get("parent").get("title")).to.be("post1");
done();
},function(err){
throw err;
});
},
error: function(err){
throw err;
}
});
});
var Person=AV.Object.extend("Person");
var p;
var posts=[];
it("create Many Post",function(done) {
var Post = AV.Object.extend("Post");
var createPost = function(num) {
if (num <= 0) {
return done();
}
var myPost = new Post();
myPost.set("title", "post" + num);
myPost.set("author", "author" + num);
myPost.set("content", "Post for relation" + num);
var point = new AV.GeoPoint({latitude: 80.01, longitude: -30.01});
myPost.set("location",point);
myPost.save(null, {
success: function(result) {
if (num <= 1) {
done();
} else {
createPost(num - 1);
}
},
error: function(err) {
done(err);
}
});
};
createPost(5);
});
it("should create a Person",function(done){
var Person = AV.Object.extend("Person");
p = new Person();
p.set("pname","person1");
p.save(null,{
success:function(){
done();
}
});
});
it("should create many to many relations",function(done){
var query = new AV.Query(Person);
query.first({
success:function(result){
var p=result;
var relation = p.relation("likes");
for(var i=0;i<posts.length;i++){
relation.add(posts[i]);
}
p.set("pname","plike1");
p.save(null,{
success:function(){
debug(p.toJSON());
debug(p.get("likes"));
done();
}
});
}
});
});
it("should save all successfully", function(done){
var Person=AV.Object.extend("Person");
var query=new AV.Query(Person);
query.first({
success:function(result){
debug(result instanceof Person);
var person = AV.Object.createWithoutData('Person', result.id);
person.set('age', 30);
AV.Object.saveAll([person],{
success: function(){
done();
},
error: function(err){
debug(result);
debug(err);
throw err;
}
});
}
});
});
it("should query relational data",function(done){
var Person=AV.Object.extend("Person");
var query=new AV.Query(Person);
query.first({
success:function(result){
debug(result instanceof Person);
debug(result.get("likes") instanceof AV.Relation);
var relation = result.relation("likes");
relation.query().find({
success:function(){
done();
}
});
debug(result.toJSON());
}
});
});
it("should fetch when save", function(done){
var query=new AV.Query(Person);
query.first({
success: function(person){
var person2 = new Person();
person2.id = person.id;
person2.set('age', 0);
person2.increment('age',9);
person2.save().then(function(){
person.fetchWhenSave(true);
person.increment('age', 10);
person.save().then(function(p){
expect(p.get('age')).to.be(19);
done();
},function(err){
throw err;
});
});
}
});
});
it("should fetch when save when creating new object.", function(done){
var p= new Person();
p.set('pname', 'dennis');
p.save().then(function(person) {
expect(person.get('company')).to.be('leancloud');
done();
}).catch(function(err) {
throw err;
});
});
/*
it("it should fetch relation post",function(done){
var post=myComment.get("parent");
post.fetch({
success:function(post){
done();
}
})
})
it("many to many relations create",function(done){
var user = AV.User.current();
relation = user.relation("likes");
relation.add(post);
user.save({
success:function(){
done();
},
error:function(err){
throw err;
}
});
})
it("many to many relations query",function(done){
relation.query().find({
success: function(list) {
done();
// list contains the posts that the current user likes.
},
error:function(err){
throw err;
}
});
})
*/
});
});//END RETRIEVING