Example #1
0
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        public virtual void TestRevTree()
        {
            RevisionInternal             rev           = new RevisionInternal("MyDocId", "4-foxy", false, database);
            IDictionary <string, object> revProperties = new Dictionary <string, object>();

            revProperties.Put("_id", rev.GetDocId());
            revProperties.Put("_rev", rev.GetRevId());
            revProperties["message"] = "hi";
            rev.SetProperties(revProperties);
            IList <string> revHistory = new AList <string>();

            revHistory.AddItem(rev.GetRevId());
            revHistory.AddItem("3-thrice");
            revHistory.AddItem("2-too");
            revHistory.AddItem("1-won");
            database.ForceInsert(rev, revHistory, null);
            NUnit.Framework.Assert.AreEqual(1, database.DocumentCount);
            VerifyHistory(database, rev, revHistory);
            RevisionInternal conflict = new RevisionInternal("MyDocId", "5-epsilon", false, database
                                                             );
            IDictionary <string, object> conflictProperties = new Dictionary <string, object>();

            conflictProperties.Put("_id", conflict.GetDocId());
            conflictProperties.Put("_rev", conflict.GetRevId());
            conflictProperties["message"] = "yo";
            conflict.SetProperties(conflictProperties);
            IList <string> conflictHistory = new AList <string>();

            conflictHistory.AddItem(conflict.GetRevId());
            conflictHistory.AddItem("4-delta");
            conflictHistory.AddItem("3-gamma");
            conflictHistory.AddItem("2-too");
            conflictHistory.AddItem("1-won");
            database.ForceInsert(conflict, conflictHistory, null);
            NUnit.Framework.Assert.AreEqual(1, database.DocumentCount);
            VerifyHistory(database, conflict, conflictHistory);
            // Add an unrelated document:
            RevisionInternal other = new RevisionInternal("AnotherDocID", "1-ichi", false, database
                                                          );
            IDictionary <string, object> otherProperties = new Dictionary <string, object>();

            otherProperties["language"] = "jp";
            other.SetProperties(otherProperties);
            IList <string> otherHistory = new AList <string>();

            otherHistory.AddItem(other.GetRevId());
            database.ForceInsert(other, otherHistory, null);
            // Fetch one of those phantom revisions with no body:
            RevisionInternal rev2 = database.GetDocumentWithIDAndRev(rev.GetDocId(), "2-too",
                                                                     EnumSet.NoneOf <TDContentOptions>());

            NUnit.Framework.Assert.AreEqual(rev.GetDocId(), rev2.GetDocId());
            NUnit.Framework.Assert.AreEqual("2-too", rev2.GetRevId());
            //Assert.assertNull(rev2.getContent());
            // Make sure no duplicate rows were inserted for the common revisions:
            NUnit.Framework.Assert.AreEqual(8, database.GetLastSequenceNumber());
            // Make sure the revision with the higher revID wins the conflict:
            RevisionInternal current = database.GetDocumentWithIDAndRev(rev.GetDocId(), null,
                                                                        EnumSet.NoneOf <TDContentOptions>());

            NUnit.Framework.Assert.AreEqual(conflict, current);
            // Get the _changes feed and verify only the winner is in it:
            ChangesOptions options         = new ChangesOptions();
            RevisionList   changes         = database.ChangesSince(0, options, null);
            RevisionList   expectedChanges = new RevisionList();

            expectedChanges.AddItem(conflict);
            expectedChanges.AddItem(other);
            NUnit.Framework.Assert.AreEqual(changes, expectedChanges);
            options.SetIncludeConflicts(true);
            changes         = database.ChangesSince(0, options, null);
            expectedChanges = new RevisionList();
            expectedChanges.AddItem(rev);
            expectedChanges.AddItem(conflict);
            expectedChanges.AddItem(other);
            NUnit.Framework.Assert.AreEqual(changes, expectedChanges);
        }
Example #2
0
        public void TestRevTree()
        {
            var rev           = new RevisionInternal("MyDocId", "4-foxy", false);
            var revProperties = new Dictionary <string, object>();

            revProperties.Put("_id", rev.GetDocId());
            revProperties.Put("_rev", rev.GetRevId());
            revProperties["message"] = "hi";
            rev.SetProperties(revProperties);

            var revHistory = new List <string>();

            revHistory.AddItem(rev.GetRevId());
            revHistory.AddItem("3-thrice");
            revHistory.AddItem("2-too");
            revHistory.AddItem("1-won");
            database.ForceInsert(rev, revHistory, null);
            Assert.AreEqual(1, database.DocumentCount);

            VerifyHistory(database, rev, revHistory);
            var conflict           = new RevisionInternal("MyDocId", "5-epsilon", false);
            var conflictProperties = new Dictionary <string, object>();

            conflictProperties.Put("_id", conflict.GetDocId());
            conflictProperties.Put("_rev", conflict.GetRevId());
            conflictProperties["message"] = "yo";
            conflict.SetProperties(conflictProperties);

            var conflictHistory = new List <string>();

            conflictHistory.AddItem(conflict.GetRevId());
            conflictHistory.AddItem("4-delta");
            conflictHistory.AddItem("3-gamma");
            conflictHistory.AddItem("2-too");
            conflictHistory.AddItem("1-won");
            database.ForceInsert(conflict, conflictHistory, null);
            Assert.AreEqual(1, database.DocumentCount);
            VerifyHistory(database, conflict, conflictHistory);

            // Add an unrelated document:
            var other           = new RevisionInternal("AnotherDocID", "1-ichi", false);
            var otherProperties = new Dictionary <string, object>();

            otherProperties["language"] = "jp";
            other.SetProperties(otherProperties);
            var otherHistory = new List <string>();

            otherHistory.AddItem(other.GetRevId());
            database.ForceInsert(other, otherHistory, null);

            // Fetch one of those phantom revisions with no body:
            var rev2 = database.GetDocument(rev.GetDocId(), "2-too",
                                            true);

            Assert.IsNull(rev2);

            // Make sure no duplicate rows were inserted for the common revisions:
            Assert.AreEqual(8, database.LastSequenceNumber);
            // Make sure the revision with the higher revID wins the conflict:
            var current = database.GetDocument(rev.GetDocId(), null,
                                               true);

            Assert.AreEqual(conflict, current);

            // Get the _changes feed and verify only the winner is in it:
            var options         = new ChangesOptions();
            var changes         = database.ChangesSince(0, options, null, null);
            var expectedChanges = new RevisionList();

            expectedChanges.AddItem(conflict);
            expectedChanges.AddItem(other);
            Assert.AreEqual(expectedChanges, changes);
            options.SetIncludeConflicts(true);
            changes         = database.ChangesSince(0, options, null, null);
            expectedChanges = new RevisionList();
            expectedChanges.AddItem(rev);
            expectedChanges.AddItem(conflict);
            expectedChanges.AddItem(other);
            var expectedChangesAlt = new RevisionList();

            expectedChangesAlt.AddItem(conflict);
            expectedChangesAlt.AddItem(rev);
            expectedChangesAlt.AddItem(other);
            Assert.IsTrue(expectedChanges.SequenceEqual(changes) || expectedChangesAlt.SequenceEqual(changes));
        }