def delete(self, selector): q_res = self.c_db.get_query_result(selector) for document in q_res: id = document['_id'] rev = document['_rev'] doc = Document(self.db, id) doc['_rev'] = rev doc.delete()
def test_delete_document_success_with_encoded_url(self): """ Test that we can remove a document from the remote database successfully when the document id requires an encoded url. """ doc = Document(self.db, 'http://example.com') doc['name'] = 'julia' doc['age'] = 6 doc['pets'] = ['cat', 'dog'] doc.create() self.assertTrue(doc.exists()) doc.delete() self.assertFalse(doc.exists()) self.assertEqual(doc, {'_id': 'http://example.com'})
def test_delete_document_success(self): """ Test that we can remove a document from the remote database successfully. """ doc = Document(self.db, 'julia006') doc['name'] = 'julia' doc['age'] = 6 doc['pets'] = ['cat', 'dog'] doc.create() self.assertTrue(doc.exists()) doc.delete() self.assertFalse(doc.exists()) self.assertEqual(doc, {'_id': 'julia006'})
def test_delete_document_failure(self): """ Test failure condition when attempting to remove a document from the remote database. """ doc = Document(self.db, 'julia006') doc['name'] = 'julia' doc['age'] = 6 doc['pets'] = ['cat', 'dog'] try: doc.delete() self.fail('Above statement should raise an Exception') except CloudantDocumentException as err: self.assertEqual( str(err), 'Attempting to delete a doc with no _rev. ' 'Try running .fetch and re-try.')
def test_delete_document_failure(self): """ Test failure condition when attempting to remove a document from the remote database. """ doc = Document(self.db, 'julia006') doc['name'] = 'julia' doc['age'] = 6 doc['pets'] = ['cat', 'dog'] try: doc.delete() self.fail('Above statement should raise an Exception') except CloudantException as err: self.assertEqual( str(err), 'Attempting to delete a doc with no _rev. ' 'Try running .fetch first!' )
def test_document_crud(self): """test basic crud operations with mocked backend""" doc = Document(self.database, "DUCKUMENT") # exists mock_resp = mock.Mock() mock_resp.status_code = 200 self.mock_session.get.return_value = mock_resp self.assertTrue(doc.exists()) self.assertTrue(self.mock_session.get.called) self.mock_session.get.assert_has_calls( [ mock.call('https://bob.cloudant.com/unittest/DUCKUMENT') ] ) self.mock_session.get.reset_mock() # create mock_resp = mock.Mock() mock_resp.raise_for_status = mock.Mock() mock_resp.status_code = 200 mock_resp.json = mock.Mock() mock_resp.json.return_value = {'id': 'DUCKUMENT', 'rev': 'DUCK2'} self.mock_session.post.return_value = mock_resp doc.create() self.assertEqual(doc['_rev'], 'DUCK2') self.assertEqual(doc['_id'], 'DUCKUMENT') self.assertTrue(self.mock_session.post.called) self.mock_session.post.reset_mock() # fetch mock_resp = mock.Mock() mock_resp.status_code = 200 mock_resp.raise_for_status = mock.Mock() mock_resp.json = mock.Mock() mock_resp.json.return_value = { '_id': 'DUCKUMENT', '_rev': 'DUCK2', 'herp': 'HERP', 'derp': 'DERP' } self.mock_session.get.return_value = mock_resp doc.fetch() self.assertTrue('herp' in doc) self.assertTrue('derp' in doc) self.assertEqual(doc['herp'], 'HERP') self.assertEqual(doc['derp'], 'DERP') self.assertTrue(self.mock_session.get.called) self.mock_session.get.assert_has_calls( [ mock.call('https://bob.cloudant.com/unittest/DUCKUMENT') ] ) self.mock_session.get.reset_mock() # save mock_put_resp = mock.Mock() mock_put_resp.status_code = 200 mock_put_resp.raise_for_status = mock.Mock() mock_put_resp.json = mock.Mock() mock_put_resp.json.return_value = {'id': 'DUCKUMENT', 'rev': 'DUCK3'} self.mock_session.put.return_value = mock_put_resp mock_get_resp = mock.Mock() mock_get_resp.status_code = 200 self.mock_session.get.return_value = mock_get_resp doc.save() self.assertEqual(doc['_rev'], 'DUCK3') self.assertEqual(doc['_id'], 'DUCKUMENT') self.assertTrue(self.mock_session.get.called) self.assertTrue(self.mock_session.put.called) self.mock_session.get.assert_has_calls( [ mock.call('https://bob.cloudant.com/unittest/DUCKUMENT') ] ) self.mock_session.put.assert_has_calls( [ mock.call( 'https://bob.cloudant.com/unittest/DUCKUMENT', headers={'Content-Type': 'application/json'}, data=mock.ANY ) ] ) self.mock_session.get.reset_mock() self.mock_session.put.reset_mock() # delete mock_resp = mock.Mock() mock_resp.status_code = 200 mock_resp.raise_for_status = mock.Mock() self.mock_session.delete.return_value = mock_resp doc.delete() self.assertTrue(self.mock_session.delete.called) self.mock_session.delete.assert_has_calls( [ mock.call( 'https://bob.cloudant.com/unittest/DUCKUMENT', params={'rev': 'DUCK3'} ) ] ) self.mock_session.delete.reset_mock() # test delete with no rev explodes as expected self.assertRaises(CloudantException, doc.delete)