def test_attachment_delete(self): """ _test_attachment_delete_ """ doc = Document(self.database, "DUCKUMENT") doc_id = 'DUCKUMENT' attachment = 'herpderp.txt' mock_get = mock.Mock() mock_get.raise_for_status = mock.Mock() mock_get.status_code = 200 mock_get.json = mock.Mock() mock_get.json.return_value = {'_id': doc_id, '_rev': '2-def'} self.mock_session.get.return_value = mock_get mock_del = mock.Mock() mock_del.raise_for_status = mock.Mock() mock_del.status_code = 200 mock_del.json = mock.Mock() mock_del.json.return_value = {'id': doc_id, 'rev': '3-ghi', 'ok': True} self.mock_session.delete.return_value = mock_del resp = doc.delete_attachment(attachment) self.assertEqual(resp['id'], doc_id) self.assertTrue(self.mock_session.get.called) self.assertTrue(self.mock_session.delete.called)
def test_appended_error_message_using_save_with_invalid_key(self): """ Test that saving a document with an invalid remote key will throw an HTTPError with additional error details from util method append_response_error_content. """ # First create the document doc = Document(self.db, 'julia006') # Add an invalid key and try to save document doc['_invalid_key'] = 'jules' with self.assertRaises(requests.HTTPError) as cm: doc.save() err = cm.exception # Should be a 400 error code, but CouchDB 1.6 issues a 500 if err.response.status_code == 500: #Check this is CouchDB 1.6 self.assertTrue(self.client.r_session.head(self.url).headers['Server'].find('CouchDB/1.6.') >= 0, '500 returned but was not CouchDB 1.6.x') self.assertEqual( str(err.response.reason), 'Internal Server Error doc_validation Bad special document member: _invalid_key' ) else: self.assertEqual( str(err.response.reason), 'Bad Request doc_validation Bad special document member: _invalid_key' ) self.assertEqual( err.response.status_code, 400 )
def test_attachment_get(self): """ _test_attachment_get_ """ doc = Document(self.database, "DUCKUMENT") doc_id = 'DUCKUMENT' attachment = 'herpderp.txt' mock_get = mock.Mock() mock_get.raise_for_status = mock.Mock() mock_get.status_code = 200 mock_get.json = mock.Mock() mock_get.json.return_value = {'_id': doc_id, '_rev': '1-abc'} mock_get_attch = mock.Mock() mock_get_attch.raise_for_status = mock.Mock() mock_get_attch.status_code = 200 mock_get_attch.content = 'herp derp foo bar' self.mock_session.get.side_effect = [mock_get, mock_get_attch] resp = doc.get_attachment(attachment, attachment_type='binary') self.assertEqual(resp, mock_get_attch.content) self.assertEqual(self.mock_session.get.call_count, 2)
def test_attachment_put(self): """ _test_attachment_put_ """ doc = Document(self.database, "DUCKUMENT") doc_id = 'DUCKUMENT' attachment = 'herpderp.txt' data = '/path/to/herpderp.txt' mock_get = mock.Mock() mock_get.raise_for_status = mock.Mock() mock_get.status_code = 200 mock_get.json = mock.Mock() mock_get.json.return_value = {'_id': doc_id, '_rev': '1-abc'} self.mock_session.get.return_value = mock_get mock_put = mock.Mock() mock_put.raise_for_status = mock.Mock() mock_put.status_code = 201 mock_put.json = mock.Mock() mock_put.json.return_value = {'id': doc_id, 'rev': '2-def', 'ok': True} self.mock_session.put.return_value = mock_put resp = doc.put_attachment( attachment, content_type='text/plain', data=data ) self.assertEqual(resp['id'], doc_id) self.assertTrue(self.mock_session.get.called) self.assertTrue(self.mock_session.put.called)
def test_rewrite_rule(self): """ Test that design document URL is rewritten to the expected test document. """ ddoc = DesignDocument(self.db, '_design/ddoc001') ddoc['rewrites'] = [ {"from": "", "to": "/../../rewrite_doc", "method": "GET", "query": {} } ] self.assertIsInstance(ddoc.rewrites, list) self.assertIsInstance(ddoc.rewrites[0], dict) ddoc.save() doc = Document(self.db, 'rewrite_doc') doc.save() resp = self.client.r_session.get('/'.join([ddoc.document_url, '_rewrite'])) self.assertEquals( resp.json(), { '_id': 'rewrite_doc', '_rev': doc['_rev'] } )
def doc_exist(self, docid): """Test if document exists in a database @param docid: str, document id @return: boolean, True if document exist """ doc = Document(self.cloudant_database, docid) return doc.exists()
def test_constructor_with_docid(self): """ Test instantiating a Document providing an id """ doc = Document(self.db, 'julia006') self.assertIsInstance(doc, Document) self.assertEqual(doc.r_session, self.db.r_session) self.assertEqual(doc.get('_id'), 'julia006')
def test_removing_id(self): """ Ensure that proper processing occurs when removing the _id """ doc = Document(self.db) doc['_id'] = 'julia006' del doc['_id'] self.assertIsNone(doc.get('_id')) self.assertEqual(doc._document_id, None)
def test_constructor_without_docid(self): """ Test instantiating a Document without providing an id """ doc = Document(self.db) self.assertIsInstance(doc, Document) self.assertEqual(doc.r_session, self.db.r_session) self.assertIsNone(doc.get('_id')) self.assertIsNone(doc.document_url)
def test_setting_id(self): """ Ensure that proper processing occurs when setting the _id """ doc = Document(self.db) self.assertIsNone(doc.get('_id')) self.assertEqual(doc._document_id, None) doc['_id'] = 'julia006' self.assertEqual(doc['_id'], 'julia006') self.assertEqual(doc._document_id, 'julia006')
def test_fetch_non_existing_document(self): """ Test fetching document content from a non-existing document """ doc = Document(self.db, 'julia006') try: doc.fetch() self.fail('Above statement should raise an Exception') except requests.HTTPError as err: self.assertEqual(err.response.status_code, 404)
def test_retrieve_document_json(self): """ Test the document dictionary renders as json appropriately """ doc = Document(self.db) doc['_id'] = 'julia006' doc['name'] = 'julia' doc['age'] = 6 doc_as_json = doc.json() self.assertIsInstance(doc_as_json, str) self.assertEqual(json.loads(doc_as_json), doc)
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" )
def test_create_existing_document(self): """ Test creating an already existing document """ doc = Document(self.db, 'julia006') doc.create() try: doc.create() self.fail('Above statement should raise an Exception') except requests.HTTPError as err: self.assertEqual(err.response.status_code, 409)
def test_list_field_remove_successfully(self): """ Test the static helper method to successfully remove from a list field. """ doc = Document(self.db) self.assertEqual(doc, {}) doc.list_field_append(doc, 'pets', 'cat') doc.list_field_append(doc, 'pets', 'dog') self.assertEqual(doc, {'pets': ['cat', 'dog']}) doc.list_field_remove(doc, 'pets', 'dog') self.assertEqual(doc, {'pets': ['cat']})
def test_list_field_remove_failure(self): """ Test the static helper method to remove from a list field errors as expected. """ doc = Document(self.db) doc.field_set(doc, 'name', 'julia') try: doc.list_field_remove(doc, 'name', 'julia') self.fail('Above statement should raise an Exception') except CloudantException, err: self.assertEqual(str(err), 'The field name is not a list.')
def test_create_existing_document(self): """ Test creating an already existing document """ doc = Document(self.db, 'julia006') doc.create() with self.assertRaises(requests.HTTPError) as cm: doc.create() err = cm.exception self.assertEqual( err.response.status_code, 409 )
def __init__(self, trick_id='', document_id='', game=None): self.game = game if trick_id: # ID generated from Round object self['_id'] = f'trick-{trick_id}' Document.__init__(self, self.game.db.database) self['type'] = 'trick' # initialize self.reset() elif document_id: Document.__init__(self, self.game.db.database, document_id=document_id) # get document data from CouchDB self.fetch()
def register(): username = request.json['username'] password = request.json['password'] if Document(db, 'usercontrol').exists(): print username with Document(db, 'usercontrol') as document: document[username] = password print 'update', username else: db.create_document({'_id': 'usercontrol', username: password}) print 'create', username return ''
async def get_user_no_read(id): try: ch_feed = await get_changes(id) except HTTPError: return None try: if '_deleted' in ch_feed['doc'] and ch_feed['doc']['_deleted']: return None doc = Document(db, ch_feed['doc']['_id']) doc.update(ch_feed['doc']) return doc except: return None
def test_fetch_document_without_docid(self): """ Test fetching document content with no id provided """ doc = Document(self.db) try: doc.fetch() self.fail('Above statement should raise an Exception') except CloudantDocumentException as err: self.assertEqual( str(err), 'A document id is required to fetch document contents. ' 'Add an _id key and value to the document and re-try.')
def populate_categories(self): pass print("Portfolio.populate_categories()") # Load portfolio specification document from DB self.foliodoc = Document(self.db, self.name) self.foliodoc.fetch() for category in self.foliodoc['categories']: # print category for subcategory in self.foliodoc['categories'][category].keys(): # print "Subcategory name: {0} Target: {1}".format(subcategory, self.foliodoc['categories'][category][subcategory]) self.categories[subcategory] = dict( target=self.foliodoc['categories'][category][subcategory], actual=0, type=category)
def new_transaction_doc(self, symbol, quantity, price, fee, action): xactiondoc = Document(self.db) xactiondoc['type'] = 'transaction' xactiondoc['action'] = action xactiondoc['quantity'] = quantity xactiondoc['date'] = strftime("%Y-%m-%d %H:%M:%S") xactiondoc['fee'] = fee xactiondoc['price'] = price if action == 'deposit' or action == 'withdrawl': xactiondoc['symbol'] = 'Cash' xactiondoc['price'] = 1 else: #otherwise use symbol passed and check to see if updating cash is needed xactiondoc['symbol'] = symbol xactiondoc.save()
def open_doc(self, docid, **params): """Get document from database Args: @param docid: str, document id to retrieve @param wrapper: callable. function that takes dict as a param. Used to wrap an object. @param **params: See doc api for parameters to use: http://wiki.apache.org/couchdb/HTTP_Document_API @return: dict, representation of CouchDB document as a dict. """ wrapper = None if "wrapper" in params: wrapper = params.pop("wrapper") elif "schema" in params: schema = params.pop("schema") if not hasattr(schema, "wrap"): raise TypeError("invalid schema") wrapper = schema.wrap attachments = params.get('attachments', False) if six.PY2 and isinstance(docid, six.text_type): docid = docid.encode('utf-8') if six.PY3 and isinstance(docid, bytes): docid = docid.decode('utf-8') doc = Document(self.cloudant_database, docid) try: doc.fetch() except HTTPError as e: if e.response.status_code == 404: raise ResourceNotFound(json.loads(e.response.content.decode('utf-8'))['reason']) raise doc_dict = dict(doc) if attachments and '_attachments' in doc_dict: for attachment_name in doc_dict['_attachments']: attachment_data = doc.get_attachment(attachment_name, attachment_type='binary') doc_dict['_attachments'][attachment_name]['data'] = base64.b64encode(attachment_data) del doc_dict['_attachments'][attachment_name]['stub'] del doc_dict['_attachments'][attachment_name]['length'] if wrapper is not None: if not callable(wrapper): raise TypeError("wrapper isn't a callable") return wrapper(doc_dict) return doc_dict
def test_create_document_without_docid(self): """ Test creating a document remotely without providing an id """ doc = Document(self.db) doc['name'] = 'julia' doc['age'] = 6 self.assertFalse(doc.exists()) self.assertIsNone(doc.get('_id')) self.assertIsNone(doc.get('_rev')) doc.create() self.assertTrue(doc.exists()) self.assertIsNotNone(doc.get('_id')) self.assertTrue(doc.get('_rev').startswith('1-'))
def getConfig(self): thrownToPreventSave = False configNotFound = False returnConfig = None hpDatabase = self.couch[self.honeypot_db] conDatabase = self.couch[self.config_db] try: with Document(hpDatabase, self.uuid) as honeypotObj: if honeypotObj.exists(): try: with Document(conDatabase, honeypotObj["config_id"]) as configObj: if configObj.exists(): returnConfig = configObj thrownToPreventSave = True raise Exception( "Config exists, but will receive a update if we do not throw this" ) else: thrownToPreventSave = True configNotFound = True raise Exception( "No config exists in context manage - do not create it" ) except Exception as iE: if not thrownToPreventSave: # print(iE) raise iE elif configNotFound: print("ERROR: Could not fetch configuration. (", honeypotObj["config_id"], ") Setting to default.") self.setConfig(DEFAULT_CONFIG_DEFAULT_ID) returnConfig = self.getConfig() thrownToPreventSave = True raise Exception("Preventing honeypot save") else: # Error is the normal flow, where we only have # to raise an exception to avoid save raise iE else: print("ERROR: Could not fetch honeypot.") sys.exit(1) except Exception as oE: if not thrownToPreventSave: # print(oE) raise oE else: # Normal flow that simply protects against unneeded save return returnConfig
def test_fetch_document_without_docid(self): """ Test fetching document content with no id provided """ doc = Document(self.db) try: doc.fetch() self.fail('Above statement should raise an Exception') except CloudantException as err: self.assertEqual( str(err), 'A document id is required to fetch document contents. ' 'Add an _id key and value to the document and re-try.' )
def test_update_field_maxretries(self, m_save): """ Test that conflict retries work for updating a single field. """ # Create a doc doc = Document(self.db, 'julia006') doc['name'] = 'julia' doc['age'] = 6 doc.create() self.assertTrue(doc['_rev'].startswith('1-')) self.assertEqual(doc['age'], 6) # Mock conflicts when saving updates m_save.side_effect = requests.HTTPError( response=mock.Mock(status_code=409, reason='conflict')) # Tests that failing on retry eventually throws with self.assertRaises(requests.HTTPError) as cm: doc.update_field(doc.field_set, 'age', 7, max_tries=2) # There is an off-by-one error for "max_tries" # It really means max_retries i.e. 1 attempt # followed by a max of 2 retries self.assertEqual(m_save.call_count, 3) self.assertEqual(cm.exception.response.status_code, 409) self.assertEqual(cm.exception.response.reason, 'conflict') # Fetch again before asserting, otherwise we assert against # the locally updated age field doc.fetch() self.assertFalse(doc['_rev'].startswith('2-')) self.assertNotEqual(doc['age'], 7)
def test_field_set_and_replace(self): """ Test the static helper method to set or replace a field value. """ doc = Document(self.db) self.assertEqual(doc, {}) doc.field_set(doc, 'name', 'julia') self.assertEqual(doc, {'name': 'julia'}) doc.field_set(doc, 'name', 'jules') self.assertEqual(doc, {'name': 'jules'}) doc.field_set(doc, 'pets', ['cat', 'dog']) self.assertEqual(doc, {'name': 'jules', 'pets': ['cat', 'dog']}) doc.field_set(doc, 'pets', None) self.assertEqual(doc, {'name': 'jules'})
def test_fetch_doc_by_id(db, _id): """Test document fetch by Id.""" doc = Document(db, _id) # doc = Document(db, _id, decoder=json_decoder) if not doc.exists(): raise ValueError(f'Document with id {_id} not found') else: try: doc.fetch() print(f'SUCCESS FETCH: Document with id {_id}: {doc}') except Exception as e: print(f'FAILED FETCH: {e}') raise return doc
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'])
def create_doc(self, doc_id=None, is_ddoc=False): """ Function to create and return a Document or DesignDocument object. """ if is_ddoc: if doc_id is not None: doc = DesignDocument(self.db, doc_id) else: doc = DesignDocument(self.db) elif doc_id is not None: doc = Document(self.db, doc_id) else: doc = Document(self.db) self.assertIsNone(doc.get('_rev')) return doc
def test_create_document_using_save(self): """ Test that save functionality works. If a document does not exist remotely then create it. """ doc = Document(self.db, 'julia006') doc['name'] = 'julia' doc['age'] = 6 self.assertIsNone(doc.get('_rev')) doc.save() self.assertTrue(doc.exists()) self.assertTrue(doc['_rev'].startswith('1-')) remote_doc = Document(self.db, 'julia006') remote_doc.fetch() self.assertEqual(remote_doc, doc)
def test_document_exists_raises_httperror(self): """ Test document exists raises an HTTPError. """ # Mock HTTPError when running against CouchDB and Cloudant resp = requests.Response() resp.status_code = 400 self.client.r_session.head = mock.Mock(return_value=resp) doc = Document(self.db) doc['_id'] = 'julia006' with self.assertRaises(requests.HTTPError) as cm: doc.exists() err = cm.exception self.assertEqual(err.response.status_code, 400) self.client.r_session.head.assert_called_with(doc.document_url)
def test_document_context_manager(self): """ Test that the __enter__ and __exit__ methods perform as expected when initiated through a document context manager. """ new_doc = Document(self.db, 'julia006') new_doc.create() self.assertTrue(new_doc.exists()) del new_doc with Document(self.db, 'julia006') as doc: self.assertTrue(all(x in list(doc.keys()) for x in ['_id', '_rev'])) self.assertTrue(doc['_rev'].startswith('1-')) doc['name'] = 'julia' doc['age'] = 6 self.assertTrue(doc['_rev'].startswith('2-')) self.assertEqual(self.db['julia006'], doc)
def select_user_data(user_id, column): db_client.connect() labor_bot_db = db_client['labor_bot'] with Document(labor_bot_db, user_id) as document: user_doc = document[column] db_client.disconnect() return user_doc
def test_document_url(self): """ Test that the document url is populated correctly """ doc = Document(self.db, 'julia006') self.assertEqual(doc.document_url, '/'.join( (self.db.database_url, 'julia006')))
def test_document_request_fails_after_client_disconnects(self): """ Test that after disconnecting from a client any objects created based on that client are not able to make requests. """ self.client.connect() doc = Document(self.db, 'julia001') doc.save() self.client.disconnect() try: with self.assertRaises(AttributeError): doc.fetch() self.assertIsNone(doc.r_session) finally: self.client.connect()
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 main(args): # Connect Cloudant db_client = Cloudant(args["USERNAME"], args["PASSWORD"], url=args["URL"], adapter=Replay429Adapter(retries=10, initialBackoff=0.01)) db_client.connect() freee_tokens = db_client['freee_tokens'] doc_id = args['TOKEN_DOC_ID'] with Document(freee_tokens, doc_id) as document: refresh_token = document['refresh_token'] payload = { 'grant_type': 'refresh_token', 'refresh_token': refresh_token, 'client_id': args['CLIENT_ID'], 'client_secret': args['CLIENT_SECRET'], 'redirect_uri': 'urn:ietf:wg:oauth:2.0:oob' } response = requests.post( 'https://accounts.secure.freee.co.jp/public_api/token', data=payload).json() print(response) document['access_token'] = response['access_token'] document['expires_in'] = response['expires_in'] document['refresh_token'] = response['refresh_token'] document['created_at'] = response['created_at'] db_client.disconnect() return {'result': 'OK!'}
def get(self, key, default=None, remote=False): """ Overrides dictionary __getitem__ behavior to provide a document instance for the specified key from the current database. If the document instance does not exist locally, then a remote request is made and the document is subsequently added to the local cache and returned to the caller. If the document instance already exists locally then it is returned and a remote request is not performed. A KeyError will result if the document does not exist locally or in the remote database. :param str key: Document id used to retrieve the document from the database. :returns: A Document or DesignDocument object depending on the specified document id (key) """ if remote is False: if key in list(self.keys()): return super(CouchDatabase, self).__getitem__(key) if key.startswith('_design/'): doc = DesignDocument(self, key) else: doc = Document(self, key) if doc.exists(): doc.fetch() super(CouchDatabase, self).__setitem__(key, doc) return doc return default
def getTimeOfLastAccess(MAC_Addr): client = this.__client__ myDatabase = this.__myDatabase__ if Document(myDatabase, MAC_Addr).exists(): with Document(myDatabase, MAC_Addr) as document: document.fetch() if 'TimeOfLastAccess' in document: strTime = document['TimeOfLastAccess'] time = datetime.datetime.strptime(strTime, '%Y-%m-%d_%H:%M:%S.%f') else: time = datetime.datetime.fromisoformat( '2011-11-04 00:05:23.283') return time else: return None
def save_rating(movie_id: int, user_id: int, rating: Optional[float]): """Save user's rated movie Args: movie_ids (int): The movie id that was rated user_ids (int): The user id rating the movie rating (Optional[float]): The movie rating If the rating argument is not None: - If the rating doesn't exist in the database it will be created - If the rating does exist in the database it will be updated If the rating argument is None: - If the rating doesn't exist in the database no operation will be performed - If the rating does exist in the database it will be deleted """ db = cloudant_client[CL_RATINGDB] current_milli_time = lambda: int(round(time.time() * 1000)) id = 'user_{0}/movie_{1}'.format(user_id, movie_id) with Document(db, id) as document: if rating: document.update({ 'rating': rating, 'timestamp': current_milli_time() }) print('saved/updated rating', id) else: if document.exists(): document.update({'_deleted': True}) print('deleted rating', id)
def test_create_replication(self): """ Test that the replication document gets created and that the replication is successful. """ self.populate_db_with_documents(3) repl_id = 'test-repl-{}'.format(unicode_(uuid.uuid4())) repl_doc = self.replicator.create_replication( self.db, self.target_db, repl_id ) self.replication_ids.append(repl_id) # Test that the replication document was created expected_keys = ['_id', '_rev', 'source', 'target', 'user_ctx'] # If Admin Party mode then user_ctx will not be in the key list if self.client.admin_party: expected_keys.pop() self.assertTrue(all(x in list(repl_doc.keys()) for x in expected_keys)) self.assertEqual(repl_doc['_id'], repl_id) self.assertTrue(repl_doc['_rev'].startswith('1-')) # Now that we know that the replication document was created, # check that the replication occurred. repl_doc = Document(self.replicator.database, repl_id) repl_doc.fetch() if repl_doc.get('_replication_state') not in ('completed', 'error'): changes = self.replicator.database.changes( feed='continuous', heartbeat=1000) beats = 0 for change in changes: if beats == 300: changes.stop() if not change: beats += 1 continue elif change.get('id') == repl_id: beats = 0 repl_doc = Document(self.replicator.database, repl_id) repl_doc.fetch() if repl_doc.get('_replication_state') in ('completed', 'error'): changes.stop() self.assertEqual(repl_doc.get('_replication_state'), 'completed') self.assertEqual(self.db.all_docs(), self.target_db.all_docs()) self.assertTrue( all(x in self.target_db.keys(True) for x in [ 'julia000', 'julia001', 'julia002' ]) )
def select_freee_token(): # freee APIの設定 db_client.connect() freee_tokens = db_client['freee_tokens'] with Document(freee_tokens, os.environ.get('TOKEN_DOC_ID')) as document: freee_access_token = document['access_token'] db_client.disconnect() return freee_access_token
def test_design_document_url(self): """ Test that the document url is populated correctly when a design document id is provided. """ doc = Document(self.db, '_design/ddoc001') self.assertEqual(doc.document_url, '/'.join( (self.db.database_url, '_design/ddoc001')))
def test_design_document_url_encodes_correctly(self): """ Test that the document url is populated and encoded correctly """ doc = Document(self.db, '_design/http://example.com') self.assertEqual( doc.document_url, '/'.join( (self.db.database_url, '_design/http%3A%2F%2Fexample.com')))
def odata_from_db(cls, odata_id): logging.debug("Quering id={}".format(odata_id)) out = None with Document(cls.courses_db, odata_id) as doc: out = json.loads(doc.json()) return out
def test_update_field_success_on_retry(self): """ Test that conflict retries work for updating a single field. """ # Create a doc doc = Document(self.db, 'julia006') doc['name'] = 'julia' doc['age'] = 6 doc.create() self.assertTrue(doc['_rev'].startswith('1-')) self.assertEqual(doc['age'], 6) # Mock when saving the document # 1st call throw a 409 # 2nd call delegate to the real doc.save() with mock.patch('cloudant.document.Document.save', side_effect=[ requests.HTTPError(response=mock.Mock( status_code=409, reason='conflict')), doc.save() ]) as m_save: # A list of side effects containing only 1 element doc.update_field(doc.field_set, 'age', 7, max_tries=1) # Two calls to save, one with a 409 and one that succeeds self.assertEqual(m_save.call_count, 2) # Check that the _rev and age field were updated self.assertTrue(doc['_rev'].startswith('2-')) self.assertEqual(doc['age'], 7)
def get_meeting_ids(cls, section_id): if section_id in cls.meeting_lookup_table: return cls.meeting_lookup_table[section_id] logging.warn('PurdueIo API Section object not found in local table.' ' Querying online database: {}'.format(section_id)) with Document(cls.meeting_lookup_db, section_id) as doc: cls.meeting_lookup_table[section_id] = doc['list'] return doc['list']
def get_section_ids(cls, api_class_id): if api_class_id in cls.section_lookup_table: return cls.section_lookup_table[api_class_id] logging.warn('PurdueIo API Class object not found in local table.' ' Querying online database: {}'.format(api_class_id)) with Document(cls.section_lookup_db, api_class_id) as doc: cls.section_lookup_table[api_class_id] = doc['list'] return doc['list']
def test_update_document_with_encoded_url(self): """ Test that updating a document where the document id requires that the document url be encoded is successful. """ # First create the document doc = Document(self.db, 'http://example.com') doc['name'] = 'julia' doc['age'] = 6 doc.save() # Now test that the document gets updated doc['name'] = 'jules' doc.save() self.assertTrue(doc['_rev'].startswith('2-')) remote_doc = Document(self.db, 'http://example.com') remote_doc.fetch() self.assertEqual(remote_doc, doc) self.assertEqual(remote_doc['name'], 'jules')
def download_file(self, file_name, version): selector = { "file_name": file_name, "version": version } fields = ["version","_id","last_modified_time"]; data = self.database.get_query_result(selector=selector, fields=fields) for my_doc in data: print my_doc; id = my_doc["_id"] last_modified_time = my_doc["last_modified_time"] document_val = Document(self.database, id); with open(file_name, 'wb') as f: document_val.get_attachment(file_name, write_to=file_name, attachment_type='binary') fileDecrypt = security.FileEncryption(); fileDecrypt.decrypt_file(file_name, file_name);
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 tieToDatabase(self): """ Check if this device record (based on UUID) exists in the database. If not, create a new DB record for the honeypot. """ thrownToPreventSave = False database = self.couch[self.honeypot_db] try: with Document(database, self.uuid) as honeypotObj: changed = False if honeypotObj.exists(): if honeypotObj["hostname"] != self.hostname: honeypotObj["hostname"] = self.hostname print( "Honeypot record updated due to hostname change.") changed = True if honeypotObj["deleted"]: honeypotObj["deleted"] = False print( "Honeypot record updated due to rewake of a deleted HP." ) changed = True ipAddress = socket.gethostbyname(self.hostname) if honeypotObj["ip_addr"] != ipAddress: honeypotObj["ip_addr"] = ipAddress print("Honeypot record updated due to IP change.") changed = True if not changed: thrownToPreventSave = True raise Exception( "No changes made in context manager - no need to save to DB" ) else: # Honeypot not in system - create new record hpRecord = Honeypot(self.uuid, socket.gethostbyname(self.hostname)) hpRecord.hostname = self.hostname try: honeypotObj["ip_addr"] = hpRecord.ip_addr honeypotObj["auth_group_id"] = hpRecord.auth_group_id honeypotObj["config_id"] = hpRecord.config_id honeypotObj["tags"] = [hpRecord.config_id] honeypotObj["deleted"] = False honeypotObj["hostname"] = hpRecord.hostname print("Honeypot record created for: %s" % self.uuid) except AttributeError as aE: print("HP Attr Warning:", aE, hpRecord.json()) sys.exit(1) except Exception as e: print("HP DB Save Error:", hpRecord.json(), "Exception:", str(e)) sys.exit(1) except Exception as e: if not thrownToPreventSave: pprint(e) self.down = True
def test_save_non_exists(self): """cover save case where doc doesnt exist""" mock_resp = mock.Mock() mock_resp.status_code = 404 self.mock_session.get.return_value = mock_resp mock_post = mock.Mock() mock_post.raise_for_status = mock.Mock() mock_post.json = mock.Mock() mock_post.json.return_value = {'id': "created", "rev": "created"} mock_put = mock.Mock() mock_put.raise_for_status = mock.Mock() self.mock_session.post.return_value = mock_post self.mock_session.put.return_value = mock_put doc = Document(self.database, "DUCKUMENT") doc.save() self.assertEqual(doc['_id'], "created") self.assertEqual(doc['_rev'], "created")
def test_timeout_in_create_replication(self): """ Test that a read timeout exception is thrown when creating a replicator with a timeout value of 500 ms. """ # Setup client with a timeout self.set_up_client(auto_connect=True, timeout=.5) self.db = self.client[self.test_target_dbname] self.target_db = self.client[self.test_dbname] # Construct a replicator with the updated client self.replicator = Replicator(self.client) repl_id = 'test-repl-{}'.format(unicode_(uuid.uuid4())) repl_doc = self.replicator.create_replication( self.db, self.target_db, repl_id ) self.replication_ids.append(repl_id) # Test that the replication document was created expected_keys = ['_id', '_rev', 'source', 'target', 'user_ctx'] # If Admin Party mode then user_ctx will not be in the key list if self.client.admin_party: expected_keys.pop() self.assertTrue(all(x in list(repl_doc.keys()) for x in expected_keys)) self.assertEqual(repl_doc['_id'], repl_id) self.assertTrue(repl_doc['_rev'].startswith('1-')) # Now that we know that the replication document was created, # check that the replication timed out. repl_doc = Document(self.replicator.database, repl_id) repl_doc.fetch() if repl_doc.get('_replication_state') not in ('completed', 'error'): # assert that a connection error is thrown because the read timed out with self.assertRaises(ConnectionError) as cm: changes = self.replicator.database.changes( feed='continuous') for change in changes: continue self.assertTrue(str(cm.exception).endswith('Read timed out.'))