This repository has been archived by the owner on Dec 30, 2020. It is now read-only.
/
TestMapReduceBuilder.cs
99 lines (89 loc) · 3.91 KB
/
TestMapReduceBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections.Generic;
using NUnit.Framework;
namespace MongoDB.Driver
{
[TestFixture()]
public class TestMapReduceBuilder : MongoTestBase
{
IMongoCollection mrcol;
string mapfunction = "function(){\n" +
" this.tags.forEach(\n" +
" function(z){\n" +
" emit( z , { count : 1 } );\n" +
" });\n" +
"};";
string reducefunction = "function( key , values ){\n" +
" var total = 0;\n" +
" for ( var i=0; i<values.length; i++ )\n" +
" total += values[i].count;\n" +
" return { count : total };\n" +
"};";
public override string TestCollections {
get {
return "mr";
}
}
public override void OnInit (){
mrcol = DB["mr"];
mrcol.Insert(new Document().Add("_id", 1).Add("tags", new String[] { "dog", "cat" }));
mrcol.Insert(new Document().Add("_id", 2).Add("tags", new String[] { "dog" }));
mrcol.Insert(new Document().Add("_id", 3).Add("tags", new String[] { "mouse", "cat", "dog" }));
mrcol.Insert(new Document().Add("_id", 4).Add("tags", new String[] { }));
}
[Test()]
public void TestCreateMapReduceWithStringFunctions(){
MapReduce mr = mrcol.MapReduce();
MapReduceBuilder mrb = new MapReduceBuilder(mr);
mrb.Map(mapfunction).Reduce(reducefunction);
Assert.IsNotNull(mr.Map);
Assert.IsNotNull(mr.Reduce);
}
[Test()]
public void TestBuilderSetsAllProperties(){
Document query = new Document().Add("x",1);
Document scope = new Document().Add("y",2);
Document sort = new Document().Add("z",3);
MapReduceBuilder mrb = mrcol.MapReduceBuilder();
mrb.Map(mapfunction)
.Reduce(reducefunction)
.KeepTemp(true)
.Limit(5)
.Out("outtest")
.Query(query)
.Scope(scope)
.Sort(sort)
.Verbose(false);
MapReduce mr = mrb.MapReduce;
Assert.AreEqual(query.ToString(), mr.Query.ToString());
Assert.AreEqual(scope.ToString(), mr.Scope.ToString());
Assert.AreEqual(sort.ToString(), mr.Sort.ToString());
Assert.AreEqual(true, mr.KeepTemp);
Assert.AreEqual(5, mr.Limit);
Assert.AreEqual("outtest", mr.Out);
Assert.AreEqual(false, mr.Verbose);
}
[Test()]
public void TestExecuteSimple(){
MapReduceBuilder mrb = mrcol.MapReduceBuilder();
MapReduce mr = mrb.Map(mapfunction).Reduce(reducefunction).Execute();
Assert.IsNotNull(mr.Result);
Assert.IsTrue(mr.Result.Ok);
Assert.AreEqual(4, mr.Result.InputCount);
Assert.AreEqual(6, mr.Result.EmitCount);
Assert.AreEqual(3, mr.Result.OutputCount);
}
[Test()]
public void TestExecuteUsing(){
String tempcollname = null;
using(MapReduceBuilder mrb = mrcol.MapReduceBuilder().Map(mapfunction).Reduce(reducefunction)){
MapReduce mr = mrb.Execute();
Assert.IsNotNull(mr.Result);
Assert.IsTrue(mr.Result.Ok);
tempcollname = DB.Name + "." + mr.Result.CollectionName;
Assert.IsTrue(DB.GetCollectionNames().Contains(tempcollname));
}
Assert.IsFalse(DB.GetCollectionNames().Contains(tempcollname));
}
}
}