def test_follow_replication(self):
        """test follow replication feature"""

        with mock.patch('cloudant.replicator.ReplicatorDatabase.changes') as mock_changes:

            mock_changes.return_value = [
                {"id": "not_this replication"},
                {"id": "not_this replication"},
                {"id": "replication_1", "_replication_state": "not finished"},
                {"id": "replication_1", "_replication_state": "completed"},
            ]

            mock_account = mock.Mock()
            repl = ReplicatorDatabase(mock_account)

            mock_doc = mock.Mock()
            mock_doc.fetch = mock.Mock()
            mock_doc.get = mock.Mock()
            mock_doc.get.side_effect = ['triggered', 'triggered', 'triggered', 'completed']

            repl['replication_1'] = mock_doc

            for x, i in enumerate(repl.follow_replication('replication_1')):
                pass
            # expect 4 iterations
            self.assertEqual(x, 3)
    def test_follow_replication(self):
        """
        _test_follow_replication_

        Test to make sure that we can follow a replication.

        """
        dbsource = u"test_follow_replication_source_{}".format(unicode(uuid.uuid4()))
        dbtarget = u"test_follow_replication_target_{}".format(unicode(uuid.uuid4()))

        self.dbs = [dbsource, dbtarget]

        with cloudant(self.user, self.passwd, account=self.user) as c:
            dbs = c.create_database(dbsource)
            dbt = c.create_database(dbtarget)

            doc1 = dbs.create_document({"_id": "doc1", "testing": "document 1"})
            doc2 = dbs.create_document({"_id": "doc2", "testing": "document 1"})
            doc3 = dbs.create_document({"_id": "doc3", "testing": "document 1"})

            replicator = ReplicatorDatabase(c)
            repl_id = u"test_follow_replication_{}".format(unicode(uuid.uuid4()))
            self.replication_ids.append(repl_id)

            ret = replicator.create_replication(source_db=dbs, target_db=dbt, repl_id=repl_id, continuous=False)
            updates = [update for update in replicator.follow_replication(repl_id)]
            self.assertTrue(len(updates) > 0)
            self.assertEqual(updates[-1]["_replication_state"], "completed")
Example #3
0
    def test_follow_replication_with_errors(self):
        """
        _test_follow_replication_with_errors_

        Test to make sure that we exit the follow loop when we submit
        a bad replication.

        """
        dbsource = u"test_follow_replication_source_error_{}".format(
            unicode(uuid.uuid4()))
        dbtarget = u"test_follow_replication_target_error_{}".format(
            unicode(uuid.uuid4()))

        self.dbs = [dbsource, dbtarget]

        with cloudant(self.user, self.passwd) as c:
            dbs = c.create_database(dbsource)
            dbt = c.create_database(dbtarget)

            doc1 = dbs.create_document(
                {"_id": "doc1", "testing": "document 1"}
            )
            doc2 = dbs.create_document(
                {"_id": "doc2", "testing": "document 1"}
            )
            doc3 = dbs.create_document(
                {"_id": "doc3", "testing": "document 1"}
            )

            replicator = ReplicatorDatabase(c)
            repl_id = u"test_follow_replication_{}".format(
                unicode(uuid.uuid4()))
            self.replication_ids.append(repl_id)

            ret = replicator.create_replication(
                source_db=dbs,
                target_db=dbt,
                # Deliberately override these good params with bad params
                source=dbsource + "foo",
                target=dbtarget + "foo",
                repl_id=repl_id,
                continuous=False,
            )
            updates = [
                update for update in replicator.follow_replication(repl_id)
            ]
            self.assertTrue(len(updates) > 0)
            self.assertEqual(updates[-1]['_replication_state'], 'error')