本文整理汇总了Python中cloudant.document.Document.update_field方法的典型用法代码示例。如果您正苦于以下问题:Python Document.update_field方法的具体用法?Python Document.update_field怎么用?Python Document.update_field使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cloudant.document.Document
的用法示例。
在下文中一共展示了Document.update_field方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_document_update_field
# 需要导入模块: from cloudant.document import Document [as 别名]
# 或者: from cloudant.document.Document import update_field [as 别名]
def test_document_update_field(self):
"""
_test_document_update_field_
Tests for the field update functions.
"""
# Setup a routine for testing conflict handing.
errors = {'conflicts': 0}
def raise_conflict(conflicts=3):
if errors['conflicts'] < conflicts:
errors['conflicts'] += 1
err = requests.HTTPError()
err.response = mock.Mock()
err.response.status_code = 409
raise err
# Mock our our doc
doc = Document(self.database, "HOWARD")
mock_put_resp = mock.Mock()
mock_put_resp.side_effect = mock.Mock()
mock_put_resp.status_code = 200
mock_put_resp.raise_for_status = raise_conflict
mock_put_resp.json.side_effect = lambda: {'id': "ID", "rev": "updated"}
self.mock_session.put.return_value = mock_put_resp
mock_get_resp = mock.Mock()
mock_get_resp.status_code = 200
mock_get_resp.json.side_effect = lambda: {"foo": "baz"}
self.mock_session.get.return_value = mock_get_resp
# Verify that our mock doc has the old value
doc.fetch()
self.assertEqual(doc["foo"], "baz")
# And that we replace it with an updated value
doc.update_field(doc.field_set, "foo", "bar")
self.assertEqual(doc["foo"], "bar")
# And verify that we called mock_session.put
self.assertTrue(self.mock_session.put.called)
# Try again, verifing that excessive conflicts get raised
errors['conflicts'] = 0
mock_put_resp.raise_for_status = lambda: raise_conflict(conflicts=11)
self.assertRaises(
requests.HTTPError,
doc.update_field,
doc.field_set,
"foo",
"bar"
)
示例2: test_update_field
# 需要导入模块: from cloudant.document import Document [as 别名]
# 或者: from cloudant.document.Document import update_field [as 别名]
def test_update_field(self):
"""
Test that we can update a single field remotely using the
update_field method.
"""
doc = Document(self.db, 'julia006')
doc['name'] = 'julia'
doc['age'] = 6
doc['pets'] = ['cat', 'dog']
doc.create()
self.assertTrue(doc['_rev'].startswith('1-'))
self.assertEqual(doc['pets'], ['cat', 'dog'])
doc.update_field(doc.list_field_append, 'pets', 'fish')
self.assertTrue(doc['_rev'].startswith('2-'))
self.assertEqual(doc['pets'], ['cat', 'dog', 'fish'])