当前位置: 首页>>代码示例>>Python>>正文


Python Document.update_field方法代码示例

本文整理汇总了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"
        )
开发者ID:iblis17,项目名称:python-cloudant,代码行数:56,代码来源:document_test.py

示例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'])
开发者ID:dqdgardener,项目名称:python-cloudant,代码行数:17,代码来源:document_tests.py


注:本文中的cloudant.document.Document.update_field方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。