public void TestPutDeletedDocument() { Document document = database.CreateDocument(); var properties = new Dictionary <string, object>(); properties["foo"] = "foo"; properties["bar"] = false; document.PutProperties(properties); Assert.IsNotNull(document.CurrentRevision); var docId = document.Id; properties["_rev"] = document.CurrentRevisionId; properties["_deleted"] = true; properties["mykey"] = "myval"; var newRev = document.PutProperties(properties); newRev.LoadProperties(); Assert.IsTrue(newRev.Properties.ContainsKey("mykey")); Assert.IsTrue(document.Deleted); var featchedDoc = database.GetExistingDocument(docId); Assert.IsNull(featchedDoc); var queryAllDocs = database.CreateAllDocumentsQuery(); var queryEnumerator = queryAllDocs.Run(); foreach (QueryRow row in queryEnumerator) { Assert.AreNotEqual(row.Document.Id, docId); } }
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); }
public void Test01CreateDocs() { RunTest("Test01CreateDocs", (parameters) => { var numDocs = Convert.ToInt32(parameters[NUMDOCS_KEY]); var docSize = Convert.ToInt32(parameters[DOCSIZE_KEY]); var props = CreateTestProperties(docSize); var stopwatch = Stopwatch.StartNew(); database.RunInTransaction(() => { for (var i = 0; i < numDocs; i++) { Document document = database.CreateDocument(); document.PutProperties(props); } return(true); }); stopwatch.Stop(); return(stopwatch.ElapsedMilliseconds); }); }
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); }
// 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); }
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception> public virtual void TestDeleteDocument() { 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()); string docId = document.GetId(); document.Delete(); NUnit.Framework.Assert.IsTrue(document.IsDeleted()); Document fetchedDoc = database.GetExistingDocument(docId); NUnit.Framework.Assert.IsNull(fetchedDoc); // query all docs and make sure we don't see that document database.GetAllDocs(new QueryOptions()); Query queryAllDocs = database.CreateAllDocumentsQuery(); QueryEnumerator queryEnumerator = queryAllDocs.Run(); for (IEnumerator <QueryRow> it = queryEnumerator; it.HasNext();) { QueryRow row = it.Next(); NUnit.Framework.Assert.IsFalse(row.GetDocument().GetId().Equals(docId)); } }
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception> public static void UpdateCheckedStatus(Couchbase.Lite.Document task, bool @checked ) { IDictionary <string, object> properties = new Dictionary <string, object>(); properties.PutAll(task.GetProperties()); properties.Put("checked", @checked); task.PutProperties(properties); }
/// <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()); }
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception> public static void RemoveMemberFromList(Couchbase.Lite.Document list, Couchbase.Lite.Document user) { IDictionary <string, object> newProperties = new Dictionary <string, object>(); newProperties.PutAll(list.Properties); IList <string> members = (IList <string>)newProperties.Get("members"); if (members != null) { members.Remove(user.Id); } newProperties.Put("members", members); list.PutProperties(newProperties); }
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception> public static Couchbase.Lite.Document CreateNewList(Database database, string title, string userId) { var dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); var calendar = Calendar.CurrentEra; string currentTimeString = dateFormatter.Format(calendar.GetTime()); IDictionary <string, object> properties = new Dictionary <string, object>(); properties.Put("type", "list"); properties.Put("title", title); properties.Put("created_at", currentTimeString); properties.Put("owner", "profile:" + userId); properties.Put("members", new AList <string>()); Couchbase.Lite.Document document = database.CreateDocument(); document.PutProperties(properties); return(document); }
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception> public static Couchbase.Lite.Document CreateProfile(Database database, string userId , string name) { SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" ); Calendar calendar = GregorianCalendar.GetInstance(); string currentTimeString = dateFormatter.Format(calendar.GetTime()); IDictionary <string, object> properties = new Dictionary <string, object>(); properties.Put("type", DocType); properties.Put("user_id", userId); properties.Put("name", name); Couchbase.Lite.Document document = database.GetDocument("profile:" + userId); document.PutProperties(properties); return(document); }
public void Test02CreateDocsUnoptimizedWay() { RunTest("Test02CreateDocsUnoptimizedWay", (parameters) => { var numDocs = Convert.ToInt32(parameters[NUMDOCS_KEY]); var docSize = Convert.ToInt32(parameters[DOCSIZE_KEY]); var props = CreateTestProperties(docSize); var stopwatch = Stopwatch.StartNew(); for (var i = 0; i < numDocs; i++) { Document document = database.CreateDocument(); document.PutProperties(props); } stopwatch.Stop(); return(stopwatch.ElapsedMilliseconds); }); }
public void Test08DocRevisions() { RunTest("Test08DocRevisions", (parameters) => { var numDocs = Convert.ToInt32(parameters[NUMDOCS_KEY]); var docSize = Convert.ToInt32(parameters[DOCSIZE_KEY]); var docs = new Document[numDocs]; var props = CreateTestProperties(docSize); props["toggle"] = true; database.RunInTransaction(() => { for (var i = 0; i < docs.Length; i++) { var doc = database.CreateDocument(); doc.PutProperties(props); docs[i] = doc; } return(true); }); var stopwatch = Stopwatch.StartNew(); for (var j = 0; j < docs.Length; j++) { Document doc = docs[j]; var contents = new Dictionary <string, object>(doc.Properties); contents["toggle"] = !(Boolean)contents["toggle"]; doc.PutProperties(contents); } stopwatch.Stop(); return(stopwatch.ElapsedMilliseconds); }); }
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception> public static void AddMemberToList(Couchbase.Lite.Document list, Couchbase.Lite.Document user) { IDictionary <string, object> newProperties = new Dictionary <string, object>(); newProperties.PutAll(list.Properties); IList <string> members = (IList <string>)newProperties.Get("members"); if (members == null) { members = new AList <string>(); } members.AddItem(user.Id); newProperties.Put("members", members); try { list.PutProperties(newProperties); } catch (CouchbaseLiteException e) { Log.E(Application.Tag, "Cannot add member to the list", e); } }
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception> public static void AssignOwnerToListsIfNeeded(Database database, Couchbase.Lite.Document user) { QueryEnumerator enumerator = GetQuery(database).Run(); if (enumerator == null) { return; } foreach (var row in enumerator) { Couchbase.Lite.Document document = row.Document; string owner = (string)document.GetProperty("owner"); if (owner != null) { continue; } IDictionary <string, object> properties = new Dictionary <string, object>(); properties.PutAll(document.Properties); properties.Put("owner", user.Id); document.PutProperties(properties); } }
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception> private void PutDocViaUntitledDoc(Database db, IDictionary <string, object> props) { Document document = db.CreateDocument(); document.PutProperties(props); }
/// <summary>Creates and saves a new revision with the given properties.</summary> /// <remarks> /// Creates and saves a new <see cref="Couchbase.Lite.Revision"/> with the specified properties. To succeed the specified properties must include a '_rev' property whose value maches the current %Revision's% id. /// This will fail with a 412 error if the receiver is not the current revision of the document. /// </remarks> /// <returns> /// The new <see cref="Couchbase.Lite.SavedRevision"/>. /// </returns> /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception> public SavedRevision CreateRevision(IDictionary <String, Object> properties) { return(Document.PutProperties(properties, RevisionInternal.GetRevId(), allowConflict: false)); }
/// <summary> /// Saves the <see cref="Couchbase.Lite.UnsavedRevision"/>. This will fail if its parent is not the current /// <see cref="Couchbase.Lite.Revision"/> of the associated <see cref="Couchbase.Lite.Document"/>. /// </summary> /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception> public SavedRevision Save() { return(Document.PutProperties(Properties, ParentId, allowConflict: false)); }
/// <summary> /// Saves the <see cref="Couchbase.Lite.UnsavedRevision"/>. This will fail if its parent is not the current /// <see cref="Couchbase.Lite.Revision"/> of the associated <see cref="Couchbase.Lite.Document"/>. /// </summary> /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception> /// <param name="allowConflict">Allow In Conflict</param> public SavedRevision Save(Boolean allowConflict) { return(Document.PutProperties(Properties, ParentId, allowConflict)); }
/// <summary> /// Saves the <see cref="Couchbase.Lite.UnsavedRevision"/>. /// This will fail if its parent is not the current <see cref="Couchbase.Lite.Revision"/> /// of the associated <see cref="Couchbase.Lite.Document"/>. /// </summary> /// <exception cref="Couchbase.Lite.CouchbaseLiteException"> /// Thrown if an issue occurs while saving the <see cref="Couchbase.Lite.UnsavedRevision"/>. /// </exception> public SavedRevision Save() { return(Document.PutProperties(Properties, ParentId.AsRevID(), false)); }
/// <summary> /// Creates and saves a new <see cref="Couchbase.Lite.Revision"/> with the specified properties. /// To succeed the specified properties must include a '_rev' property whose value maches the current Revision's id. /// </summary> /// <returns> /// The new <see cref="Couchbase.Lite.SavedRevision"/>. /// </returns> /// <param name="properties"> /// The properties to set on the new Revision. /// </param> /// <exception cref="Couchbase.Lite.CouchbaseLiteException"> /// Thrown if an error occurs while creating or saving the new <see cref="Couchbase.Lite.Revision"/>. /// </exception> public SavedRevision CreateRevision(IDictionary <String, Object> properties) { return(Document.PutProperties(properties, RevisionInternal.RevID, false)); }
/// <summary> /// Saves the <see cref="Couchbase.Lite.UnsavedRevision"/>, optionally allowing /// the save when there is a conflict. /// </summary> /// <param name="allowConflict"> /// Whether or not to allow saving when there is a conflict. /// </param> /// <exception cref="Couchbase.Lite.CouchbaseLiteException"> /// Thrown if an issue occurs while saving the <see cref="Couchbase.Lite.UnsavedRevision"/>. /// </exception> public SavedRevision Save(bool allowConflict) { return(Document.PutProperties(Properties, ParentId.AsRevID(), allowConflict)); }