/// <summary>https://github.com/couchbase/couchbase-lite-java-core/issues/164</summary> /// <exception cref="System.Exception"></exception> public virtual void TestRevisionIdEquivalentRevisions() { // two revisions with the same content and the same json // should have the exact same revision id, because their content // will have an identical hash IDictionary <string, object> properties = new Dictionary <string, object>(); properties.Put("testName", "testCreateRevisions"); properties.Put("tag", 1337); IDictionary <string, object> properties2 = new Dictionary <string, object>(); properties2.Put("testName", "testCreateRevisions"); properties2.Put("tag", 1338); Document doc = database.CreateDocument(); UnsavedRevision newRev = doc.CreateRevision(); newRev.SetUserProperties(properties); SavedRevision rev1 = newRev.Save(); UnsavedRevision newRev2a = rev1.CreateRevision(); newRev2a.SetUserProperties(properties2); SavedRevision rev2a = newRev2a.Save(); UnsavedRevision newRev2b = rev1.CreateRevision(); newRev2b.SetUserProperties(properties2); SavedRevision rev2b = newRev2b.Save(true); NUnit.Framework.Assert.AreEqual(rev2a.GetId(), rev2b.GetId()); }
/// <exception cref="System.Exception"></exception> public static SavedRevision CreateRevisionWithRandomProps(SavedRevision createRevFrom , bool allowConflict) { IDictionary <string, object> properties = new Dictionary <string, object>(); properties.Put(UUID.RandomUUID().ToString(), "val"); UnsavedRevision unsavedRevision = createRevFrom.CreateRevision(); unsavedRevision.SetUserProperties(properties); return(unsavedRevision.Save(allowConflict)); }
/// <summary>https://github.com/couchbase/couchbase-lite-android/issues/134</summary> /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception> /// <exception cref="System.IO.IOException"></exception> public virtual void TestGetAttachmentBodyUsingPrefetch() { // add a doc with an attachment Document doc = database.CreateDocument(); UnsavedRevision rev = doc.CreateRevision(); IDictionary <string, object> properties = new Dictionary <string, object>(); properties["foo"] = "bar"; rev.SetUserProperties(properties); byte[] attachBodyBytes = Sharpen.Runtime.GetBytesForString("attach body"); Attachment attachment = new Attachment(new ByteArrayInputStream(attachBodyBytes), "text/plain"); string attachmentName = "test_attachment.txt"; rev.AddAttachment(attachment, attachmentName); rev.Save(); // do query that finds that doc with prefetch View view = database.GetView("aview"); view.SetMapReduce((IDictionary <string, object> document, EmitDelegate emitter) => { string id = (string)document["_id"]; emitter.Emit(id, null); }, null, "1"); // try to get the attachment Query query = view.CreateQuery(); query.Prefetch = true; QueryEnumerator results = query.Run(); while (results.MoveNext()) { QueryRow row = results.Current; // This returns the revision just fine, but the sequence number // is set to 0. SavedRevision revision = row.Document.CurrentRevision; IList <string> attachments = revision.AttachmentNames; // This returns an Attachment object which looks ok, except again // its sequence number is 0. The metadata property knows about // the length and mime type of the attachment. It also says // "stub" -> "true". Attachment attachmentRetrieved = revision.GetAttachment(attachmentName); // This throws a CouchbaseLiteException with StatusCode.NOT_FOUND. InputStream @is = attachmentRetrieved.GetContent(); NUnit.Framework.Assert.IsNotNull(@is); byte[] attachmentDataRetrieved = TextUtils.Read(@is); string attachmentDataRetrievedString = Sharpen.Runtime.GetStringForBytes(attachmentDataRetrieved ); string attachBodyString = Sharpen.Runtime.GetStringForBytes(attachBodyBytes); NUnit.Framework.Assert.AreEqual(attachBodyString, attachmentDataRetrievedString); } }
/// <exception cref="System.Exception"></exception> public virtual void TestWinningRevIDOfDoc() { IDictionary <string, object> properties = new Dictionary <string, object>(); properties.Put("testName", "testCreateRevisions"); properties.Put("tag", 1337); IDictionary <string, object> properties2a = new Dictionary <string, object>(); properties2a.Put("testName", "testCreateRevisions"); properties2a.Put("tag", 1338); IDictionary <string, object> properties2b = new Dictionary <string, object>(); properties2b.Put("testName", "testCreateRevisions"); properties2b.Put("tag", 1339); IList <bool> outIsDeleted = new AList <bool>(); IList <bool> outIsConflict = new AList <bool>(); // Create a conflict on purpose Document doc = database.CreateDocument(); UnsavedRevision newRev1 = doc.CreateRevision(); newRev1.SetUserProperties(properties); SavedRevision rev1 = newRev1.Save(); long docNumericId = database.GetDocNumericID(doc.GetId()); NUnit.Framework.Assert.IsTrue(docNumericId != 0); NUnit.Framework.Assert.AreEqual(rev1.GetId(), database.WinningRevIDOfDoc(docNumericId , outIsDeleted, outIsConflict)); NUnit.Framework.Assert.IsTrue(outIsConflict.Count == 0); outIsDeleted = new AList <bool>(); outIsConflict = new AList <bool>(); UnsavedRevision newRev2a = rev1.CreateRevision(); newRev2a.SetUserProperties(properties2a); SavedRevision rev2a = newRev2a.Save(); NUnit.Framework.Assert.AreEqual(rev2a.GetId(), database.WinningRevIDOfDoc(docNumericId , outIsDeleted, outIsConflict)); NUnit.Framework.Assert.IsTrue(outIsConflict.Count == 0); outIsDeleted = new AList <bool>(); outIsConflict = new AList <bool>(); UnsavedRevision newRev2b = rev1.CreateRevision(); newRev2b.SetUserProperties(properties2b); SavedRevision rev2b = newRev2b.Save(true); database.WinningRevIDOfDoc(docNumericId, outIsDeleted, outIsConflict); NUnit.Framework.Assert.IsTrue(outIsConflict.Count > 0); }
/// <summary>https://github.com/couchbase/couchbase-lite-java-core/issues/106</summary> /// <exception cref="System.Exception"></exception> public virtual void TestResolveConflict() { IDictionary <string, object> properties = new Dictionary <string, object>(); properties.Put("testName", "testCreateRevisions"); properties.Put("tag", 1337); // Create a conflict on purpose Document doc = database.CreateDocument(); UnsavedRevision newRev1 = doc.CreateRevision(); newRev1.SetUserProperties(properties); SavedRevision rev1 = newRev1.Save(); SavedRevision rev2a = CreateRevisionWithRandomProps(rev1, false); SavedRevision rev2b = CreateRevisionWithRandomProps(rev1, true); SavedRevision winningRev = null; SavedRevision losingRev = null; if (doc.GetCurrentRevisionId().Equals(rev2a.GetId())) { winningRev = rev2a; losingRev = rev2b; } else { winningRev = rev2b; losingRev = rev2a; } NUnit.Framework.Assert.AreEqual(2, doc.GetConflictingRevisions().Count); NUnit.Framework.Assert.AreEqual(2, doc.GetLeafRevisions().Count); // let's manually choose the losing rev as the winner. First, delete winner, which will // cause losing rev to be the current revision. SavedRevision deleteRevision = winningRev.DeleteDocument(); IList <SavedRevision> conflictingRevisions = doc.GetConflictingRevisions(); NUnit.Framework.Assert.AreEqual(1, conflictingRevisions.Count); NUnit.Framework.Assert.AreEqual(2, doc.GetLeafRevisions().Count); NUnit.Framework.Assert.AreEqual(3, deleteRevision.GetGeneration()); NUnit.Framework.Assert.AreEqual(losingRev.GetId(), doc.GetCurrentRevision().GetId ()); // Finally create a new revision rev3 based on losing rev SavedRevision rev3 = CreateRevisionWithRandomProps(losingRev, true); NUnit.Framework.Assert.AreEqual(rev3.GetId(), doc.GetCurrentRevisionId()); IList <SavedRevision> conflictingRevisions1 = doc.GetConflictingRevisions(); NUnit.Framework.Assert.AreEqual(1, conflictingRevisions1.Count); NUnit.Framework.Assert.AreEqual(2, doc.GetLeafRevisions().Count); }
/// <summary>https://github.com/couchbase/couchbase-lite-java-core/issues/164</summary> /// <exception cref="System.Exception"></exception> public virtual void TestRevisionIdDifferentRevisions() { // two revisions with different json should have different rev-id's // because their content will have a different hash (even though // they have the same generation number) IDictionary <string, object> properties = new Dictionary <string, object>(); properties.Put("testName", "testCreateRevisions"); properties.Put("tag", 1337); Document doc = database.CreateDocument(); UnsavedRevision newRev = doc.CreateRevision(); newRev.SetUserProperties(properties); SavedRevision rev1 = newRev.Save(); SavedRevision rev2a = CreateRevisionWithRandomProps(rev1, false); SavedRevision rev2b = CreateRevisionWithRandomProps(rev1, true); NUnit.Framework.Assert.AreNotSame(rev2a.GetId(), rev2b.GetId()); }