/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        public virtual void TestPostKeysView()
        {
            Send("PUT", "/db", Status.Created, null);
            IDictionary <string, object> result;
            Database db   = manager.GetDatabase("db");
            View     view = db.GetView("design/view");

            view.SetMapReduce(new _Mapper_463(), null, "1");
            IDictionary <string, object> key_doc1 = new Dictionary <string, object>();

            key_doc1.Put("parentId", "12345");
            result = (IDictionary <string, object>)SendBody("PUT", "/db/key_doc1", key_doc1, Status
                                                            .Created, null);
            view = db.GetView("design/view");
            view.SetMapReduce(new _Mapper_475(), null, "1");
            IList <object> keys = new AList <object>();

            keys.AddItem("12345");
            IDictionary <string, object> bodyObj = new Dictionary <string, object>();

            bodyObj.Put("keys", keys);
            URLConnection conn = SendRequest("POST", "/db/_design/design/_view/view", null, bodyObj
                                             );

            result = (IDictionary <string, object>)ParseJSONResponse(conn);
            NUnit.Framework.Assert.AreEqual(1, result.Get("total_rows"));
        }
Esempio n. 2
0
        //*** JavaScript View ***//

        private Couchbase.Lite.View CompileView(Database db, string viewName, JObject viewProps)
        {
            JToken language;

            if (!viewProps.TryGetValue("language", out language))
            {
                language = "javascript";
            }
            JToken mapSource;

            if (!viewProps.TryGetValue("map", out mapSource))
            {
                return(null);
            }

            IViewCompiler viewCompiler = Couchbase.Lite.View.Compiler;
            IViewCompiler test         = new JSViewCompilerCopy();

            Couchbase.Lite.View.Compiler = test;
            MapDelegate mapBlock = Couchbase.Lite.View.Compiler.CompileMap(mapSource.Value <string>(), language.Value <string>());

            if (mapBlock == null)
            {
                return(null);
            }

            string mapID = db.Name + ":" + viewName + ":" + mapSource.Value <string>().GetHashCode();

            JToken         reduceSource = null;
            ReduceDelegate reduceBlock  = null;

            if (viewProps.TryGetValue("reduce", out reduceSource))
            {
                // Couchbase.Lite.View.compiler est null et Couchbase.Lite.Listener.JSViewCompiler est inaccessible (même avec la reflection)

                reduceBlock = Couchbase.Lite.View.Compiler.CompileReduce(reduceSource.Value <string>(), language.Value <string>());
                if (reduceBlock == null)
                {
                    return(null);
                }
                mapID += ":" + reduceSource.Value <string>().GetHashCode();
            }

            Couchbase.Lite.View view = db.GetView(viewName);
            view.SetMapReduce(mapBlock, reduceBlock, mapID);
            JToken collation = null;

            if (viewProps.TryGetValue("collation", out collation))
            {
                if ("raw".Equals((String)collation))
                {
                    // ???
                }
            }

            return(view);
        }
Esempio n. 3
0
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        public virtual void TestViewReduce()
        {
            IDictionary <string, object> docProperties1 = new Dictionary <string, object>();

            docProperties1["_id"]  = "CD";
            docProperties1["cost"] = 8.99;
            PutDoc(database, docProperties1);
            IDictionary <string, object> docProperties2 = new Dictionary <string, object>();

            docProperties2["_id"]  = "App";
            docProperties2["cost"] = 1.95;
            PutDoc(database, docProperties2);
            IDictionary <string, object> docProperties3 = new Dictionary <string, object>();

            docProperties3["_id"]  = "Dessert";
            docProperties3["cost"] = 6.50;
            PutDoc(database, docProperties3);
            View view = database.GetView("totaler");

            view.SetMapReduce((document, emitter) => {
                NUnit.Framework.Assert.IsNotNull(document.Get("_id"));
                NUnit.Framework.Assert.IsNotNull(document.Get("_rev"));
                object cost = document.Get("cost");
                if (cost != null)
                {
                    emitter(document.Get("_id"), cost);
                }
            }, (IList <object> keys, IList <object> values, bool rereduce) => {
                return(View.TotalValues(values));
            }, "1");
            view.UpdateIndex();
            IList <IDictionary <string, object> > dumpResult = view.Dump();

            Log.V(Tag, "View dump: " + dumpResult);
            NUnit.Framework.Assert.AreEqual(3, dumpResult.Count);
            NUnit.Framework.Assert.AreEqual("\"App\"", dumpResult[0]["key"]);
            NUnit.Framework.Assert.AreEqual("1.95", dumpResult[0]["value"]);
            NUnit.Framework.Assert.AreEqual(2, dumpResult[0]["seq"]);
            NUnit.Framework.Assert.AreEqual("\"CD\"", dumpResult[1]["key"]);
            NUnit.Framework.Assert.AreEqual("8.99", dumpResult[1]["value"]);
            NUnit.Framework.Assert.AreEqual(1, dumpResult[1]["seq"]);
            NUnit.Framework.Assert.AreEqual("\"Dessert\"", dumpResult[2]["key"]);
            NUnit.Framework.Assert.AreEqual("6.5", dumpResult[2]["value"]);
            NUnit.Framework.Assert.AreEqual(3, dumpResult[2]["seq"]);
            QueryOptions options = new QueryOptions();

            options.SetReduce(true);
            IList <QueryRow> reduced = view.QueryWithOptions(options);

            NUnit.Framework.Assert.AreEqual(1, reduced.Count);
            object value       = reduced[0].GetValue();
            Number numberValue = (Number)value;

            NUnit.Framework.Assert.IsTrue(Math.Abs(numberValue - 17.44) < 0.001);
        }
Esempio n. 4
0
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        public virtual void TestViewGroupedStrings()
        {
            IDictionary <string, object> docProperties1 = new Dictionary <string, object>();

            docProperties1["name"] = "Alice";
            PutDoc(database, docProperties1);
            IDictionary <string, object> docProperties2 = new Dictionary <string, object>();

            docProperties2["name"] = "Albert";
            PutDoc(database, docProperties2);
            IDictionary <string, object> docProperties3 = new Dictionary <string, object>();

            docProperties3["name"] = "Naomi";
            PutDoc(database, docProperties3);
            IDictionary <string, object> docProperties4 = new Dictionary <string, object>();

            docProperties4["name"] = "Jens";
            PutDoc(database, docProperties4);
            IDictionary <string, object> docProperties5 = new Dictionary <string, object>();

            docProperties5["name"] = "Jed";
            PutDoc(database, docProperties5);
            View view = database.GetView("default/names");

            view.SetMapReduce(new _Mapper_852(), new _Reducer_862(), "1.0");
            view.UpdateIndex();
            QueryOptions options = new QueryOptions();

            options.SetGroupLevel(1);
            IList <QueryRow> rows = view.QueryWithOptions(options);
            IList <IDictionary <string, object> > expectedRows = new AList <IDictionary <string, object
                                                                                         > >();
            IDictionary <string, object> row1 = new Dictionary <string, object>();

            row1["key"]   = "A";
            row1["value"] = 2;
            expectedRows.AddItem(row1);
            IDictionary <string, object> row2 = new Dictionary <string, object>();

            row2["key"]   = "J";
            row2["value"] = 2;
            expectedRows.AddItem(row2);
            IDictionary <string, object> row3 = new Dictionary <string, object>();

            row3["key"]   = "N";
            row3["value"] = 1;
            expectedRows.AddItem(row3);
            NUnit.Framework.Assert.AreEqual(row1["key"], rows[0].Key);
            NUnit.Framework.Assert.AreEqual(row1["value"], rows[0].GetValue());
            NUnit.Framework.Assert.AreEqual(row2["key"], rows[1].Key);
            NUnit.Framework.Assert.AreEqual(row2["value"], rows[1].GetValue());
            NUnit.Framework.Assert.AreEqual(row3["key"], rows[2].Key);
            NUnit.Framework.Assert.AreEqual(row3["value"], rows[2].GetValue());
        }
        /// <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. 6
0
        public virtual void TestViewCreation()
        {
            NUnit.Framework.Assert.IsNull(database.GetExistingView("aview"));
            View view = database.GetView("aview");

            NUnit.Framework.Assert.IsNotNull(view);
            NUnit.Framework.Assert.AreEqual(database, view.Database);
            NUnit.Framework.Assert.AreEqual("aview", view.Name);
            NUnit.Framework.Assert.IsNull(view.Map);
            NUnit.Framework.Assert.AreEqual(view, database.GetExistingView("aview"));
            bool changed = view.SetMapReduce(() => {}, null, "1");

            //no-op
            NUnit.Framework.Assert.IsTrue(changed);
            NUnit.Framework.Assert.AreEqual(1, database.GetAllViews().Count);
            NUnit.Framework.Assert.AreEqual(view, database.GetAllViews()[0]);
            changed = view.SetMapReduce(() => {}, null, "1");
            //no-op
            NUnit.Framework.Assert.IsFalse(changed);
            changed = view.SetMapReduce(() => {}, null, "2");
            //no-op
            NUnit.Framework.Assert.IsTrue(changed);
        }
Esempio n. 7
0
        public static View CreateView(Database db)
        {
            View view = db.GetView("aview");

            view.SetMapReduce((IDictionary <string, object> document, EmitDelegate emitter) =>
            {
                NUnit.Framework.Assert.IsNotNull(document["_id"]);
                NUnit.Framework.Assert.IsNotNull(document["_rev"]);
                if (document["key"] != null)
                {
                    emitter.Emit(document["key"], null);
                }
            }, null, "1");
            return(view);
        }
Esempio n. 8
0
        public void TestPullerWithLiveQuery()
        {
            // Even though this test is passed, there is a runtime exception
            // thrown regarding the replication's number of changes count versus
            // number of completed changes count. Investigation is required.
            Log.D(Database.Tag, "testPullerWithLiveQuery");
            string docIdTimestamp = System.Convert.ToString(Runtime.CurrentTimeMillis());
            string doc1Id         = string.Format("doc1-{0}", docIdTimestamp);
            string doc2Id         = string.Format("doc2-{0}", docIdTimestamp);

            AddDocWithId(doc1Id, "attachment2.png");
            AddDocWithId(doc2Id, "attachment2.png");

            int  numDocsBeforePull = database.DocumentCount;
            View view = database.GetView("testPullerWithLiveQueryView");

            view.SetMapReduce((document, emitter) => {
                if (document.Get("_id") != null)
                {
                    emitter(document.Get("_id"), null);
                }
            }, null, "1");

            LiveQuery allDocsLiveQuery = view.CreateQuery().ToLiveQuery();

            allDocsLiveQuery.Changed += (sender, e) => {
                int numTimesCalled = 0;
                if (e.Error != null)
                {
                    throw new RuntimeException(e.Error);
                }
                if (numTimesCalled++ > 0)
                {
                    NUnit.Framework.Assert.IsTrue(e.Rows.Count > numDocsBeforePull);
                }
                Log.D(Database.Tag, "rows " + e.Rows);
            };

            // the first time this is called back, the rows will be empty.
            // but on subsequent times we should expect to get a non empty
            // row set.
            allDocsLiveQuery.Start();
            DoPullReplication();
            allDocsLiveQuery.Stop();
        }
Esempio n. 9
0
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        public virtual void TestViewLinkedDocs()
        {
            PutLinkedDocs(database);
            View view = database.GetView("linked");

            view.SetMapReduce(new _Mapper_1079(), null, "1");
            view.UpdateIndex();
            QueryOptions options = new QueryOptions();

            options.SetIncludeDocs(true);
            // required for linked documents
            IList <QueryRow> rows = view.QueryWithOptions(options);

            NUnit.Framework.Assert.IsNotNull(rows);
            NUnit.Framework.Assert.AreEqual(5, rows.Count);
            object[][] expected = new object[][] { new object[] { "22222", "hello", 0, null,
                                                                  "22222" }, new object[] { "22222", "hello", 1, "11111", "11111" }, new object[]
                                                   { "33333", "world", 0, null, "33333" }, new object[] { "33333", "world", 1, "22222"
                                                                                                          , "22222" }, new object[] { "33333", "world", 2, "11111", "11111" } };
            for (int i = 0; i < rows.Count; i++)
            {
                QueryRow row = rows[i];
                IDictionary <string, object> rowAsJson = row.AsJSONDictionary();
                Log.D(Tag, string.Empty + rowAsJson);
                IList <object> key = (IList <object>)rowAsJson["key"];
                IDictionary <string, object> doc = (IDictionary <string, object>)rowAsJson.Get("doc"
                                                                                               );
                string id = (string)rowAsJson["id"];
                NUnit.Framework.Assert.AreEqual(expected[i][0], id);
                NUnit.Framework.Assert.AreEqual(2, key.Count);
                NUnit.Framework.Assert.AreEqual(expected[i][1], key[0]);
                NUnit.Framework.Assert.AreEqual(expected[i][2], key[1]);
                if (expected[i][3] == null)
                {
                    NUnit.Framework.Assert.IsNull(row.GetValue());
                }
                else
                {
                    NUnit.Framework.Assert.AreEqual(expected[i][3], ((IDictionary <string, object>)row
                                                                     .GetValue())["_id"]);
                }
                NUnit.Framework.Assert.AreEqual(expected[i][4], doc["_id"]);
            }
        }
Esempio n. 10
0
        public void TestViewCollationRaw()
        {
            IList <object> list1 = new List <object>();

            list1.AddItem("a");
            IList <object> list2 = new List <object>();

            list2.AddItem("b");
            IList <object> list3 = new List <object>();

            list3.AddItem("b");
            list3.AddItem("c");
            IList <object> list4 = new List <object>();

            list4.AddItem("b");
            list4.AddItem("c");
            list4.AddItem("a");
            IList <object> list5 = new List <object>();

            list5.AddItem("b");
            list5.AddItem("d");
            IList <object> list6 = new List <object>();

            list6.AddItem("b");
            list6.AddItem("d");
            list6.AddItem("e");

            // Based on CouchDB's "view_collation.js" test
            IList <object> testKeys = new List <object>();

            testKeys.AddItem(0);
            testKeys.AddItem(2.5);
            testKeys.AddItem(10);
            testKeys.AddItem(false);
            testKeys.AddItem(null);
            testKeys.AddItem(true);
            testKeys.AddItem(list1);
            testKeys.AddItem(list2);
            testKeys.AddItem(list3);
            testKeys.AddItem(list4);
            testKeys.AddItem(list5);
            testKeys.AddItem(list6);
            testKeys.AddItem(" ");
            testKeys.AddItem("A");
            testKeys.AddItem("B");
            testKeys.AddItem("_");
            testKeys.AddItem("a");
            testKeys.AddItem("aa");
            testKeys.AddItem("b");
            testKeys.AddItem("ba");
            testKeys.AddItem("bb");
            testKeys.AddItem("~");

            int i = 0;

            foreach (object key in testKeys)
            {
                IDictionary <string, object> docProperties = new Dictionary <string, object>();
                docProperties.Put("_id", Sharpen.Extensions.ToString(i++));
                docProperties["name"] = key;
                PutDoc(database, docProperties);
            }

            View view = database.GetView("default/names");

            view.SetMapReduce((document, emitter) =>
                              emitter(document["name"], null), null, "1.0");

            view.Collation = ViewCollation.Raw;

            QueryOptions options = new QueryOptions();

            IList <QueryRow> rows = view.QueryWithOptions(options).ToList();

            i = 0;
            foreach (QueryRow row in rows)
            {
                Assert.AreEqual(testKeys[i++], row.Key);
            }
            database.Close();
        }
Esempio n. 11
0
        public void TestViewGroupedStrings()
        {
            IDictionary <string, object> docProperties1 = new Dictionary <string, object>();

            docProperties1["name"] = "Alice";
            PutDoc(database, docProperties1);
            IDictionary <string, object> docProperties2 = new Dictionary <string, object>();

            docProperties2["name"] = "Albert";
            PutDoc(database, docProperties2);
            IDictionary <string, object> docProperties3 = new Dictionary <string, object>();

            docProperties3["name"] = "Naomi";
            PutDoc(database, docProperties3);
            IDictionary <string, object> docProperties4 = new Dictionary <string, object>();

            docProperties4["name"] = "Jens";
            PutDoc(database, docProperties4);
            IDictionary <string, object> docProperties5 = new Dictionary <string, object>();

            docProperties5["name"] = "Jed";
            PutDoc(database, docProperties5);

            View view = database.GetView("default/names");

            view.SetMapReduce((document, emitter) =>
            {
                string name = (string)document["name"];
                if (name != null)
                {
                    emitter(Sharpen.Runtime.Substring(name, 0, 1), 1);
                }
            }, (keys, values, rereduce) => View.TotalValues(values.ToList()), "1.0");

            view.UpdateIndex();
            QueryOptions options = new QueryOptions();

            options.SetGroupLevel(1);

            IList <QueryRow> rows = view.QueryWithOptions(options).ToList();
            IList <IDictionary <string, object> > expectedRows = new List <IDictionary <string, object> >();
            IDictionary <string, object>          row1         = new Dictionary <string, object>();

            row1["key"]   = "A";
            row1["value"] = 2;
            expectedRows.AddItem(row1);

            IDictionary <string, object> row2 = new Dictionary <string, object>();

            row2["key"]   = "J";
            row2["value"] = 2;
            expectedRows.AddItem(row2);

            IDictionary <string, object> row3 = new Dictionary <string, object>();

            row3["key"]   = "N";
            row3["value"] = 1;
            expectedRows.AddItem(row3);

            Assert.AreEqual(row1["key"], rows[0].Key);
            Assert.AreEqual(row1["value"], rows[0].Value);
            Assert.AreEqual(row2["key"], rows[1].Key);
            Assert.AreEqual(row2["value"], rows[1].Value);
            Assert.AreEqual(row3["key"], rows[2].Key);
            Assert.AreEqual(row3["value"], rows[2].Value);
        }
Esempio n. 12
0
        public void TestViewGrouped()
        {
            IDictionary <string, object> docProperties1 = new Dictionary <string, object>();

            docProperties1["_id"]    = "1";
            docProperties1["artist"] = "Gang Of Four";
            docProperties1["album"]  = "Entertainment!";
            docProperties1["track"]  = "Ether";
            docProperties1["time"]   = 231;
            PutDoc(database, docProperties1);

            IDictionary <string, object> docProperties2 = new Dictionary <string, object>();

            docProperties2["_id"]    = "2";
            docProperties2["artist"] = "Gang Of Four";
            docProperties2["album"]  = "Songs Of The Free";
            docProperties2["track"]  = "I Love A Man In Uniform";
            docProperties2["time"]   = 248;
            PutDoc(database, docProperties2);

            IDictionary <string, object> docProperties3 = new Dictionary <string, object>();

            docProperties3["_id"]    = "3";
            docProperties3["artist"] = "Gang Of Four";
            docProperties3["album"]  = "Entertainment!";
            docProperties3["track"]  = "Natural's Not In It";
            docProperties3["time"]   = 187;
            PutDoc(database, docProperties3);

            IDictionary <string, object> docProperties4 = new Dictionary <string, object>();

            docProperties4["_id"]    = "4";
            docProperties4["artist"] = "PiL";
            docProperties4["album"]  = "Metal Box";
            docProperties4["track"]  = "Memories";
            docProperties4["time"]   = 309;
            PutDoc(database, docProperties4);

            IDictionary <string, object> docProperties5 = new Dictionary <string, object>();

            docProperties5["_id"]    = "5";
            docProperties5["artist"] = "Gang Of Four";
            docProperties5["album"]  = "Entertainment!";
            docProperties5["track"]  = "Not Great Men";
            docProperties5["time"]   = 187;
            PutDoc(database, docProperties5);

            View view = database.GetView("grouper");

            view.SetMapReduce((IDictionary <string, object> document, EmitDelegate emitter) =>
            {
                IList <object> key = new List <object>();
                key.AddItem(document["artist"]);
                key.AddItem(document["album"]);
                key.AddItem(document["track"]);
                emitter(key, document["time"]);
            }, (IEnumerable <object> keys, IEnumerable <object> values, bool rereduce) => {
                return(View.TotalValues(values.ToList()));
            }, "1");

            view.UpdateIndex();
            QueryOptions options = new QueryOptions();

            options.SetReduce(true);

            IList <QueryRow> rows = view.QueryWithOptions(options).ToList();
            IList <IDictionary <string, object> > expectedRows = new List <IDictionary <string, object> >();
            IDictionary <string, object>          row1         = new Dictionary <string, object>();

            row1["key"]   = null;
            row1["value"] = 1162.0;
            expectedRows.AddItem(row1);
            Assert.AreEqual(row1["key"], rows[0].Key);
            Assert.AreEqual(row1["value"], rows[0].Value);

            //now group
            options.SetGroup(true);
            rows         = view.QueryWithOptions(options).ToList();
            expectedRows = new List <IDictionary <string, object> >();
            row1         = new Dictionary <string, object>();
            IList <string> key1 = new List <string>();

            key1.AddItem("Gang Of Four");
            key1.AddItem("Entertainment!");
            key1.AddItem("Ether");
            row1["key"]   = key1;
            row1["value"] = 231.0;
            expectedRows.AddItem(row1);

            IDictionary <string, object> row2 = new Dictionary <string, object>();
            IList <string> key2 = new List <string>();

            key2.AddItem("Gang Of Four");
            key2.AddItem("Entertainment!");
            key2.AddItem("Natural's Not In It");
            row2["key"]   = key2;
            row2["value"] = 187.0;
            expectedRows.AddItem(row2);

            IDictionary <string, object> row3 = new Dictionary <string, object>();
            IList <string> key3 = new List <string>();

            key3.AddItem("Gang Of Four");
            key3.AddItem("Entertainment!");
            key3.AddItem("Not Great Men");
            row3["key"]   = key3;
            row3["value"] = 187.0;
            expectedRows.AddItem(row3);

            IDictionary <string, object> row4 = new Dictionary <string, object>();
            IList <string> key4 = new List <string>();

            key4.AddItem("Gang Of Four");
            key4.AddItem("Songs Of The Free");
            key4.AddItem("I Love A Man In Uniform");
            row4["key"]   = key4;
            row4["value"] = 248.0;
            expectedRows.AddItem(row4);

            IDictionary <string, object> row5 = new Dictionary <string, object>();
            IList <string> key5 = new List <string>();

            key5.AddItem("PiL");
            key5.AddItem("Metal Box");
            key5.AddItem("Memories");
            row5["key"]   = key5;
            row5["value"] = 309.0;
            expectedRows.AddItem(row5);
            Assert.AreEqual(row1["key"], ((JArray)rows[0].Key).Values <String>().ToList());
            Assert.AreEqual(row1["value"], rows[0].Value);
            Assert.AreEqual(row2["key"], ((JArray)rows[1].Key).Values <String>().ToList());
            Assert.AreEqual(row2["value"], rows[1].Value);
            Assert.AreEqual(row3["key"], ((JArray)rows[2].Key).Values <String>().ToList());
            Assert.AreEqual(row3["value"], rows[2].Value);
            Assert.AreEqual(row4["key"], ((JArray)rows[3].Key).Values <String>().ToList());
            Assert.AreEqual(row4["value"], rows[3].Value);
            Assert.AreEqual(row5["key"], ((JArray)rows[4].Key).Values <String>().ToList());
            Assert.AreEqual(row5["value"], rows[4].Value);

            //group level 1
            options.SetGroupLevel(1);
            rows         = view.QueryWithOptions(options).ToList();
            expectedRows = new List <IDictionary <string, object> >();
            row1         = new Dictionary <string, object>();
            key1         = new List <string>();
            key1.AddItem("Gang Of Four");
            row1["key"]   = key1;
            row1["value"] = 853.0;

            expectedRows.AddItem(row1);
            row2 = new Dictionary <string, object>();
            key2 = new List <string>();
            key2.AddItem("PiL");
            row2["key"]   = key2;
            row2["value"] = 309.0;
            expectedRows.AddItem(row2);
            Assert.AreEqual(row1["key"], ((JArray)rows[0].Key).Values <String>().ToList());
            Assert.AreEqual(row1["value"], rows[0].Value);
            Assert.AreEqual(row2["key"], ((JArray)rows[1].Key).Values <String>().ToList());
            Assert.AreEqual(row2["value"], rows[1].Value);

            //group level 2
            options.SetGroupLevel(2);
            rows         = view.QueryWithOptions(options).ToList();
            expectedRows = new List <IDictionary <string, object> >();
            row1         = new Dictionary <string, object>();
            key1         = new List <string>();
            key1.AddItem("Gang Of Four");
            key1.AddItem("Entertainment!");
            row1["key"]   = key1;
            row1["value"] = 605.0;
            expectedRows.AddItem(row1);
            row2 = new Dictionary <string, object>();
            key2 = new List <string>();
            key2.AddItem("Gang Of Four");
            key2.AddItem("Songs Of The Free");
            row2["key"]   = key2;
            row2["value"] = 248.0;
            expectedRows.AddItem(row2);
            row3 = new Dictionary <string, object>();
            key3 = new List <string>();
            key3.AddItem("PiL");
            key3.AddItem("Metal Box");
            row3["key"]   = key3;
            row3["value"] = 309.0;
            expectedRows.AddItem(row3);
            Assert.AreEqual(row1["key"], ((JArray)rows[0].Key).Values <String>().ToList());
            Assert.AreEqual(row1["value"], rows[0].Value);
            Assert.AreEqual(row2["key"], ((JArray)rows[1].Key).Values <String>().ToList());
            Assert.AreEqual(row2["value"], rows[1].Value);
            Assert.AreEqual(row3["key"], ((JArray)rows[2].Key).Values <String>().ToList());
            Assert.AreEqual(row3["value"], rows[2].Value);
        }
Esempio n. 13
0
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        public virtual void TestViewLinkedDocs()
        {
            PutLinkedDocs(database);
            View view = database.GetView("linked");

            view.SetMapReduce((document, emitter) =>
            {
                if (document.ContainsKey("value"))
                {
                    emitter(new object[] { document["value"], 0 }, null);
                }
                if (document.ContainsKey("ancestors"))
                {
                    IList <object> ancestors = (IList <object>)document["ancestors"];
                    for (int i = 0; i < ancestors.Count; i++)
                    {
                        IDictionary <string, object> value = new Dictionary <string, object>();
                        value["_id"] = ancestors[i];
                        emitter(new object[] { document["value"], i + 1 }, value);
                    }
                }
            }, null, "1.0");

            view.UpdateIndex();
            QueryOptions options = new QueryOptions();

            options.SetIncludeDocs(true);

            // required for linked documents
            IList <QueryRow> rows = view.QueryWithOptions(options).ToList();

            Assert.IsNotNull(rows);
            Assert.AreEqual(5, rows.Count);

            object[][] expected = new object[][] {
                new object[] { "22222", "hello", 0, null, "22222" },
                new object[] { "22222", "hello", 1, "11111", "11111" },
                new object[] { "33333", "world", 0, null, "33333" },
                new object[] { "33333", "world", 1, "22222", "22222" },
                new object[] { "33333", "world", 2, "11111", "11111" }
            };

            for (int i = 0; i < rows.Count; i++)
            {
                QueryRow row = rows[i];
                IDictionary <string, object> rowAsJson = row.AsJSONDictionary();
                Log.D(Tag, string.Empty + rowAsJson);
                IList <object> key = (IList <object>)rowAsJson["key"];
                IDictionary <string, object> doc = (IDictionary <string, object>)rowAsJson.Get("doc"
                                                                                               );
                string id = (string)rowAsJson["id"];
                Assert.AreEqual(expected[i][0], id);
                Assert.AreEqual(2, key.Count);
                Assert.AreEqual(expected[i][1], key[0]);
                Assert.AreEqual(expected[i][2], key[1]);
                if (expected[i][3] == null)
                {
                    Assert.IsNull(row.Value);
                }
                else
                {
                    Assert.AreEqual(expected[i][3], ((IDictionary <string, object>)row.Value)["_id"]);
                }
                Assert.AreEqual(expected[i][4], doc["_id"]);
            }
        }
Esempio n. 14
0
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        public virtual void TestViews()
        {
            Send("PUT", "/db", Status.Created, null);
            IDictionary <string, object> result;
            IDictionary <string, object> doc1 = new Dictionary <string, object>();

            doc1.Put("message", "hello");
            result = (IDictionary <string, object>)SendBody("PUT", "/db/doc1", doc1, Status.Created
                                                            , null);
            string revID = (string)result.Get("rev");
            IDictionary <string, object> doc3 = new Dictionary <string, object>();

            doc3.Put("message", "bonjour");
            result = (IDictionary <string, object>)SendBody("PUT", "/db/doc3", doc3, Status.Created
                                                            , null);
            string revID3 = (string)result.Get("rev");
            IDictionary <string, object> doc2 = new Dictionary <string, object>();

            doc2.Put("message", "guten tag");
            result = (IDictionary <string, object>)SendBody("PUT", "/db/doc2", doc2, Status.Created
                                                            , null);
            string   revID2 = (string)result.Get("rev");
            Database db     = manager.GetDatabase("db");
            View     view   = db.GetView("design/view");

            view.SetMapReduce(new _Mapper_372(), null, "1");
            // Build up our expected result
            IDictionary <string, object> row1 = new Dictionary <string, object>();

            row1.Put("id", "doc1");
            row1.Put("key", "hello");
            IDictionary <string, object> row2 = new Dictionary <string, object>();

            row2.Put("id", "doc2");
            row2.Put("key", "guten tag");
            IDictionary <string, object> row3 = new Dictionary <string, object>();

            row3.Put("id", "doc3");
            row3.Put("key", "bonjour");
            IList <IDictionary <string, object> > expectedRows = new AList <IDictionary <string, object
                                                                                         > >();

            expectedRows.AddItem(row3);
            expectedRows.AddItem(row2);
            expectedRows.AddItem(row1);
            IDictionary <string, object> expectedResult = new Dictionary <string, object>();

            expectedResult.Put("offset", 0);
            expectedResult.Put("total_rows", 3);
            expectedResult.Put("rows", expectedRows);
            // Query the view and check the result:
            Send("GET", "/db/_design/design/_view/view", Status.Ok, expectedResult);
            // Check the ETag:
            URLConnection conn = SendRequest("GET", "/db/_design/design/_view/view", null, null
                                             );
            string etag = conn.GetHeaderField("Etag");

            NUnit.Framework.Assert.AreEqual(string.Format("\"%d\"", view.GetLastSequenceIndexed
                                                              ()), etag);
            // Try a conditional GET:
            IDictionary <string, string> headers = new Dictionary <string, string>();

            headers.Put("If-None-Match", etag);
            conn = SendRequest("GET", "/db/_design/design/_view/view", headers, null);
            NUnit.Framework.Assert.AreEqual(Status.NotModified, conn.GetResponseCode());
            // Update the database:
            IDictionary <string, object> doc4 = new Dictionary <string, object>();

            doc4.Put("message", "aloha");
            result = (IDictionary <string, object>)SendBody("PUT", "/db/doc4", doc4, Status.Created
                                                            , null);
            // Try a conditional GET:
            conn = SendRequest("GET", "/db/_design/design/_view/view", headers, null);
            NUnit.Framework.Assert.AreEqual(Status.Ok, conn.GetResponseCode());
            result = (IDictionary <string, object>)ParseJSONResponse(conn);
            NUnit.Framework.Assert.AreEqual(4, result.Get("total_rows"));
        }
Esempio n. 15
0
        void setupSoldView()
        {
            viewSold = db.GetView ("sold-by-employee");

            var mapBlock = new MapDelegate ((doc, emit) =>
                {
                    object type;
                    doc.TryGetValue ("type", out type);
                    if (type != null) {
                        string t = type.ToString ();
                        if (t == "sale") {
                            object name;
                            doc.TryGetValue ("employee", out name);

                            object quantity;
                            doc.TryGetValue ("quantity", out quantity);
                            string q = quantity.ToString ();
                            int quant;
                            var ok = Int32.TryParse(q, out quant);

                            object price;
                            doc.TryGetValue ("price", out price);
                            string ps = price.ToString ();
                            Single pnum;
                            var ok2 = Single.TryParse(ps, out pnum);

                            if (ok && ok2 && name != null)
                                emit (name, quant * pnum);
                        }
                    }
                });

            viewSold.SetMapReduce (mapBlock, Couchbase.Lite.Views.BuiltinReduceFunctions.Sum, "1.3");
        }
Esempio n. 16
0
        void setupScannedView()
        {
            viewScanned = db.GetView ("scanned-by-employee");

            var mapBlock = new MapDelegate ((doc, emit) =>
                {
                    object name;
                    doc.TryGetValue ("employee", out name);

                    object quantity;
                    doc.TryGetValue ("quantity", out quantity);
                    string q = quantity.ToString ();
                    int quant;
                    var ok = Int32.TryParse(q, out quant);

                    if (ok && name != null)
                        emit (name, quant);
                });

            viewScanned.SetMapReduce (mapBlock, Couchbase.Lite.Views.BuiltinReduceFunctions.Sum, "1.1");
        }