Esempio n. 1
0
 def test_create_document_with_docid(self):
     """
     Test creating a document providing an id
     """
     doc = Document(self.db, 'julia006')
     doc['name'] = 'julia'
     doc['age'] = 6
     self.assertFalse(doc.exists())
     self.assertIsNone(doc.get('_rev'))
     doc.create()
     self.assertTrue(doc.exists())
     self.assertTrue(doc.get('_rev').startswith('1-'))
Esempio n. 2
0
 def test_create_document_with_docid_encoded_url(self):
     """
     Test creating a document providing an id that has an encoded url
     """
     doc = Document(self.db, 'http://example.com')
     doc['name'] = 'julia'
     doc['age'] = 6
     self.assertFalse(doc.exists())
     self.assertIsNone(doc.get('_rev'))
     doc.create()
     self.assertTrue(doc.exists())
     self.assertTrue(doc.get('_rev').startswith('1-'))
 def test_create_document_with_docid_encoded_url(self):
     """
     Test creating a document providing an id that has an encoded url
     """
     doc = Document(self.db, 'http://example.com')
     doc['name'] = 'julia'
     doc['age'] = 6
     self.assertFalse(doc.exists())
     self.assertIsNone(doc.get('_rev'))
     doc.create()
     self.assertTrue(doc.exists())
     self.assertTrue(doc.get('_rev').startswith('1-'))
 def test_create_document_with_docid(self):
     """
     Test creating a document providing an id
     """
     doc = Document(self.db, 'julia006')
     doc['name'] = 'julia'
     doc['age'] = 6
     self.assertFalse(doc.exists())
     self.assertIsNone(doc.get('_rev'))
     doc.create()
     self.assertTrue(doc.exists())
     self.assertTrue(doc.get('_rev').startswith('1-'))
Esempio n. 5
0
    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 or self.client.is_iam_authenticated:
            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 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 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.'))
Esempio n. 8
0
 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')
Esempio n. 9
0
 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'))
Esempio n. 10
0
 def test_setting_id(self):
     """
     Ensure that proper processing occurs when setting the _id
     """
     doc = Document(self.db)
     self.assertIsNone(doc.get('_id'))
     doc['_id'] = 'julia006'
     self.assertEqual(doc['_id'], 'julia006')
 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')
Esempio n. 12
0
 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_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')
Esempio n. 16
0
    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']
        self.assertTrue(all(x in 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 not (repl_doc.get('_replication_state')
            in ('completed', 'error')):
            for change in self.replicator.database.changes():
                if change.get('id') == repl_id:
                    repl_doc = Document(self.replicator.database, repl_id)
                    repl_doc.fetch()
                    if (repl_doc.get('_replication_state')
                        in ('completed', 'error')):
                        break
        self.assertEqual(repl_doc['_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'
            ])
        )
Esempio n. 17
0
 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_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)
Esempio n. 19
0
    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.'))