Beispiel #1
0
        public void TestJsBuiltInReduceFunction()
        {
            var c           = new JSViewCompiler();
            var reduceBlock = c.CompileReduce("_count", "javascript");

            Assert.IsNotNull(reduceBlock);
            Assert.AreEqual(BuiltinReduceFunctions.Count, reduceBlock);

            reduceBlock = c.CompileReduce("_stats", "javascript");
            Assert.IsNotNull(reduceBlock);
            Assert.AreEqual(BuiltinReduceFunctions.Stats, reduceBlock);

            var keys = new List <object> {
                "master", "schlage", "medeco"
            };
            var values = new List <object> {
                19, -75, 3.1416
            };
            var result   = reduceBlock(keys, values, false);
            var expected = new Dictionary <string, object> {
                { "count", 3 },
                { "sum", -52.8584 },
                { "sumsqr", 5995.86965056 },
                { "max", 19 },
                { "min", -75 }
            };

            CollectionAssert.AreEquivalent(expected, (IEnumerable)result);

            reduceBlock = c.CompileReduce("_frob", "javascript");
            Assert.IsNull(reduceBlock);
        }
Beispiel #2
0
        public void TestJsMapFunction()
        {
            var c        = new JSViewCompiler();
            var mapBlock = c.CompileMap("function(doc){emit(doc.key, doc);}", "javascript");

            Assert.IsNotNull(mapBlock);

            var doc = new Dictionary <string, object> {
                { "_id", "doc1" },
                { "_rev", @"1-xyzzy" },
                { "key", "value" }
            };

            var          emitted = new List <object>();
            EmitDelegate emit    = (key, value) =>
            {
                Console.WriteLine("Emitted: {0} -> {1}", key, value);
                emitted.Add(key);
                emitted.Add(value);
            };

            mapBlock(doc, emit);

            CollectionAssert.AreEqual(new List <object> {
                "value", doc
            }, emitted);
        }
Beispiel #3
0
        public void TestJsReduceFunction()
        {
            var c           = new JSViewCompiler();
            var reduceBlock = c.CompileReduce("function(k,v,r){return [k,v,r];}", "javascript");

            Assert.IsNotNull(reduceBlock);

            var keys = new List <object> {
                "master", "schlage", "medeco"
            };
            var values = new List <object> {
                1, 2, 3
            };
            var result = reduceBlock(keys, values, false);

            CollectionAssert.AreEqual(new List <object> {
                keys, values, false
            }, (IEnumerable)result);
        }
Beispiel #4
0
        public void TestJsLogFunction()
        {
            // This case will test that calling log() function doesn't cause any errors running the JS map
            // map function.
            var c        = new JSViewCompiler();
            var mapBlock = c.CompileMap("function(doc){log('Log Message'); emit(doc.key, doc);}", "javascript");

            Assert.IsNotNull(mapBlock);

            var doc = new Dictionary <string, object> {
                { "_id", "doc1" },
                { "_rev", @"1-xyzzy" },
                { "key", "value" }
            };

            var          emitted = new List <object>();
            EmitDelegate emit    = (key, value) => emitted.Add(value);

            mapBlock(doc, emit);

            CollectionAssert.AreEqual(new List <object> {
                doc
            }, emitted);
        }
Beispiel #5
0
 static View()
 {
     Compiler = new JSViewCompiler();
 }