Skip to content

Commit 787a0da

Browse files
authored
Merge pull request #400 from weakish/fix-is_new
fix(LeanObject): test existence more accurately
2 parents fea3240 + 81aba2c commit 787a0da

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

leancloud/object_.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,12 +517,21 @@ def is_new(self):
517517
"""
518518
判断当前对象是否已经保存至服务器。
519519
520+
该方法为 SDK 内部使用(save 调用此方法 dispatch 保存操作为 REST API 的 POST 和 PUT 请求)。
521+
查询对象是否在服务器上存在请使用 is_existed 方法。
522+
523+
520524
:rtype: bool
521525
"""
522526
return False if self.id else True
523527

524528
def is_existed(self):
525-
return bool(self.id)
529+
"""
530+
判断当前对象是否在服务器上已经存在。
531+
532+
:rtype: bool
533+
"""
534+
return self.has("createdAt")
526535

527536
def get_acl(self):
528537
"""

tests/test_object.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,9 @@ def test_fetch(): # type: () -> None
237237
album.save()
238238

239239
album_1 = Album.create_without_data(album.id)
240+
assert album_1.is_existed() is False
240241
album_1.fetch(include=['parent'], select=['name', 'parent'])
242+
assert album_1.is_existed() is True
241243
assert album_1.get('parent').get('name') == 'Nightwish'
242244
assert not album_1.has('title')
243245

@@ -252,6 +254,11 @@ def test_has(): # type: () -> None
252254
assert album.has('bar') is False
253255

254256

257+
def test_existence(): # type: () -> None
258+
album = Album()
259+
assert album.is_existed() is False
260+
261+
255262
def test_get_set_acl(): # type: () -> None
256263
acl = leancloud.ACL()
257264
album = Album()

tests/test_query.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ def test_basic_query(): # type: () -> None
169169
# get
170170
GameScore.query.get(game_score.id)
171171

172+
# get nonexist
173+
assert GameScore.query.get("NonexistentID").is_existed() is False
174+
172175
# count
173176
eq_(GameScore.query.count(), 10)
174177

0 commit comments

Comments
 (0)