Ejemplo n.º 1
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"]);
            }
        }
Ejemplo n.º 2
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"]);
            }
        }