public static Document CreateDocumentWithProperties(Database db, IDictionary <string , object> properties) { Document doc = db.CreateDocument(); NUnit.Framework.Assert.IsNotNull(doc); NUnit.Framework.Assert.IsNull(doc.GetCurrentRevisionId()); NUnit.Framework.Assert.IsNull(doc.GetCurrentRevision()); NUnit.Framework.Assert.IsNotNull("Document has no ID", doc.GetId()); // 'untitled' docs are no longer untitled (8/10/12) try { doc.PutProperties(properties); } catch (Exception e) { Log.E(Tag, "Error creating document", e); NUnit.Framework.Assert.IsTrue("can't create new document in db:" + db.GetName() + " with properties:" + properties.ToString(), false); } NUnit.Framework.Assert.IsNotNull(doc.GetId()); NUnit.Framework.Assert.IsNotNull(doc.GetCurrentRevisionId()); NUnit.Framework.Assert.IsNotNull(doc.GetUserProperties()); // this won't work until the weakref hashmap is implemented which stores all docs // Assert.assertEquals(db.getDocument(doc.getId()), doc); NUnit.Framework.Assert.AreEqual(db.GetDocument(doc.GetId()).GetId(), doc.GetId()); return(doc); }
public static Document CreateDocumentWithProperties(Database db, IDictionary <string , object> properties) { Document doc = db.CreateDocument(); NUnit.Framework.Assert.IsNotNull(doc); NUnit.Framework.Assert.IsNull(doc.GetCurrentRevisionId()); NUnit.Framework.Assert.IsNull(doc.GetCurrentRevision()); NUnit.Framework.Assert.IsNotNull("Document has no ID", doc.GetId()); // 'untitled' docs are no longer untitled (8/10/12) try { doc.PutProperties(properties); } catch (Exception e) { Log.E(Tag, "Error creating document", e); NUnit.Framework.Assert.IsTrue("can't create new document in db:" + db.GetName() + " with properties:" + properties.ToString(), false); } NUnit.Framework.Assert.IsNotNull(doc.GetId()); NUnit.Framework.Assert.IsNotNull(doc.GetCurrentRevisionId()); NUnit.Framework.Assert.IsNotNull(doc.GetUserProperties()); // should be same doc instance, since there should only ever be a single Document instance for a given document NUnit.Framework.Assert.AreEqual(db.GetDocument(doc.GetId()), doc); NUnit.Framework.Assert.AreEqual(db.GetDocument(doc.GetId()).GetId(), doc.GetId()); return(doc); }
/// <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); }
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception> public virtual void TestNewDocumentHasCurrentRevision() { Document document = database.CreateDocument(); IDictionary <string, object> properties = new Dictionary <string, object>(); properties.Put("foo", "foo"); properties.Put("bar", false); document.PutProperties(properties); NUnit.Framework.Assert.IsNotNull(document.GetCurrentRevisionId()); NUnit.Framework.Assert.IsNotNull(document.GetCurrentRevision()); }
/// <summary>https://github.com/couchbase/couchbase-lite-java-core/issues/106</summary> /// <exception cref="System.Exception"></exception> public virtual void TestResolveConflict() { IDictionary <string, object> result; // Create a conflict on purpose Document doc = database.CreateDocument(); SavedRevision rev1 = doc.CreateRevision().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); result = (IDictionary <string, object>)Send("GET", string.Format("/%s/%s?conflicts=true" , DefaultTestDb, doc.GetId()), Status.Ok, null); IList <string> conflicts = (IList)result.Get("_conflicts"); NUnit.Framework.Assert.AreEqual(1, conflicts.Count); string conflictingRevId = conflicts[0]; NUnit.Framework.Assert.AreEqual(losingRev.GetId(), conflictingRevId); long docNumericID = database.GetDocNumericID(doc.GetId()); NUnit.Framework.Assert.IsTrue(docNumericID != 0); NUnit.Framework.Assert.IsNotNull(database.GetDocument(doc.GetId())); Log.D(Tag, "docNumericID for " + doc.GetId() + " is: " + docNumericID); result = (IDictionary <string, object>)Send("DELETE", string.Format("/%s/%s?rev=%s" , DefaultTestDb, doc.GetId(), conflictingRevId), Status.Ok, null); result = (IDictionary <string, object>)Send("GET", string.Format("/%s/%s?conflicts=true" , DefaultTestDb, doc.GetId()), Status.Ok, null); conflicts = (IList)result.Get("_conflicts"); NUnit.Framework.Assert.AreEqual(0, conflicts.Count); }
public void Changed(Database.ChangeEvent @event) { NUnit.Framework.Assert.AreEqual(1, @event.GetChanges().Count); DocumentChange change = @event.GetChanges()[0]; NUnit.Framework.Assert.AreEqual(DocumentId, change.GetDocumentId()); NUnit.Framework.Assert.AreEqual(rev3.GetRevId(), change.GetRevisionId()); NUnit.Framework.Assert.IsTrue(change.IsCurrentRevision()); NUnit.Framework.Assert.IsFalse(change.IsConflict()); Document doc = this._enclosing.database.GetDocument(change.GetDocumentId()); NUnit.Framework.Assert.AreEqual(rev3.GetRevId(), doc.GetCurrentRevisionId()); try { NUnit.Framework.Assert.AreEqual(3, doc.GetRevisionHistory().Count); } catch (CouchbaseLiteException ex) { Assert.Fail("CouchbaseLiteException in change listener: " + ex.ToString()); } }
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception> public virtual void TestReplaceDatabaseNamedWithAttachments() { InputStream dbStream = GetAsset("withattachments.cblite"); string[] attachmentlist = null; IDictionary <string, InputStream> attachments = new Dictionary <string, InputStream >(); InputStream blobStream = GetAsset("attachments/356a192b7913b04c54574d18c28d46e6395428ab.blob" ); attachments.Put("356a192b7913b04c54574d18c28d46e6395428ab.blob", blobStream); manager.ReplaceDatabase("replaced2", dbStream, attachments); //Validate the number of files in the DB NUnit.Framework.Assert.AreEqual(1, manager.GetDatabase("replaced2").GetDocumentCount ()); //get the attachment from the document Document doc = manager.GetDatabase("replaced2").GetExistingDocument("168e0c56-4588-4df4-8700-4d5115fa9c74" ); NUnit.Framework.Assert.IsNotNull(doc); RevisionInternal gotRev1 = database.GetDocumentWithIDAndRev(doc.GetId(), doc.GetCurrentRevisionId (), EnumSet.NoneOf <Database.TDContentOptions>()); }
// Reproduces issue #167 // https://github.com/couchbase/couchbase-lite-android/issues/167 /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception> public virtual void TestLoadRevisionBody() { Document document = database.CreateDocument(); IDictionary <string, object> properties = new Dictionary <string, object>(); properties.Put("foo", "foo"); properties.Put("bar", false); document.PutProperties(properties); NUnit.Framework.Assert.IsNotNull(document.GetCurrentRevision()); bool deleted = false; RevisionInternal revisionInternal = new RevisionInternal(document.GetId(), document .GetCurrentRevisionId(), deleted, database); EnumSet <Database.TDContentOptions> contentOptions = EnumSet.Of(Database.TDContentOptions .TDIncludeAttachments, Database.TDContentOptions.TDBigAttachmentsFollow); database.LoadRevisionBody(revisionInternal, contentOptions); // now lets purge the document, and then try to load the revision body again NUnit.Framework.Assert.IsTrue(document.Purge()); bool gotExpectedException = false; try { database.LoadRevisionBody(revisionInternal, contentOptions); } catch (CouchbaseLiteException e) { if (e.GetCBLStatus().GetCode() == Status.NotFound) { gotExpectedException = true; } } NUnit.Framework.Assert.IsTrue(gotExpectedException); }