Ejemplo n.º 1
0
        /// <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));
            }
        }
Ejemplo n.º 2
0
        /// <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.Put("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(new _Mapper_432(), null, "1");
            // try to get the attachment
            Query query = view.CreateQuery();

            query.SetPrefetch(true);
            QueryEnumerator results = query.Run();

            while (results.HasNext())
            {
                QueryRow row = results.Next();
                // This returns the revision just fine, but the sequence number
                // is set to 0.
                SavedRevision  revision    = row.GetDocument().GetCurrentRevision();
                IList <string> attachments = revision.GetAttachmentNames();
                // 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 Status.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);
            }
        }