public void TestDeleteDocument()
        {
            var 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;

            document.Delete();
            Assert.IsTrue(document.Deleted);
            Document fetchedDoc = database.GetExistingDocument(docId);

            Assert.IsNull(fetchedDoc);

            // query all docs and make sure we don't see that document
            Query           queryAllDocs    = database.CreateAllDocumentsQuery();
            QueryEnumerator queryEnumerator = queryAllDocs.Run();

            foreach (var row in queryEnumerator)
            {
                Assert.IsFalse(row.Document.Id.Equals(docId));
            }
        }
        /// <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));
            }
        }
Esempio n. 3
0
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        public virtual void TestIndexUpdateMode()
        {
            View  view  = CreateView(database);
            Query query = view.CreateQuery();

            query.SetIndexUpdateMode(Query.IndexUpdateMode.Before);
            int numRowsBefore = query.Run().Count;

            NUnit.Framework.Assert.AreEqual(0, numRowsBefore);
            // do a query and force re-indexing, number of results should be +4
            PutNDocs(database, 1);
            query.SetIndexUpdateMode(Query.IndexUpdateMode.Before);
            NUnit.Framework.Assert.AreEqual(1, query.Run().Count);
            // do a query without re-indexing, number of results should be the same
            PutNDocs(database, 4);
            query.SetIndexUpdateMode(Query.IndexUpdateMode.Never);
            NUnit.Framework.Assert.AreEqual(1, query.Run().Count);
            // do a query and force re-indexing, number of results should be +4
            query.SetIndexUpdateMode(Query.IndexUpdateMode.Before);
            NUnit.Framework.Assert.AreEqual(5, query.Run().Count);
            // do a query which will kick off an async index
            PutNDocs(database, 1);
            query.SetIndexUpdateMode(Query.IndexUpdateMode.After);
            query.Run().Count;
            // wait until indexing is (hopefully) done
            try
            {
                Sharpen.Thread.Sleep(1 * 1000);
            }
            catch (Exception e)
            {
                Sharpen.Runtime.PrintStackTrace(e);
            }
            NUnit.Framework.Assert.AreEqual(6, query.Run().Count);
        }
        /// <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);
            }
        }
Esempio n. 5
0
 /// <summary>
 /// Runs the query returning an enumerator over the resulting rows.
 /// </summary>
 /// <returns>A <see cref="EntityQueryEnumerator{TEntity}"/>.</returns>
 public EntityQueryEnumerator <TEntity> Run()
 {
     return(new EntityQueryEnumerator <TEntity>(query.Run(), postFilter));
 }