Esempio n. 1
0
        public virtual void TestViews()
        {
            Send("PUT", "/db", Status.Created, null);
            IDictionary <string, object> result;
            IDictionary <string, object> doc1 = new Dictionary <string, object>();

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

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

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

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

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

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

            row3["id"]  = "doc3";
            row3["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["offset"]     = 0;
            expectedResult["total_rows"] = 3;
            expectedResult["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["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["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["total_rows"]);
        }