forked from samus/mongodb-csharp
/
TestDatabaseJS.cs
203 lines (175 loc) · 6.36 KB
/
TestDatabaseJS.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using NUnit.Framework;
namespace MongoDB.Driver{
[TestFixture()]
public class TestDatabaseJS
{
Mongo db = new Mongo();
Database tests;
DatabaseJS js;
[Test()]
public void TestCanGetDatabaseJSObject(){
Assert.IsNotNull(tests.JS);
}
[Test()]
public void TestCanGetAFunction(){
string name = "fget";
AddFunction(name);
Assert.IsNotNull(js[name]);
Assert.IsNotNull(js.GetFunction(name));
}
[Test()]
public void TestCanListFunctions(){
string name = "flist";
AddFunction(name);
List<String> list = js.GetFunctionNames();
Assert.IsTrue(list.Count > 0);
bool found = false;
foreach(string l in list){
if(l == name) found = true;
}
Assert.IsTrue(found, "Didn't find the function that was inserted.");
}
[Test()]
public void TestCanAddAFunctionStrStr(){
string name = "faddss";
string func = "function(x, y){return x + y;}";
js.Add(name,func);
Assert.IsNotNull(js[name]);
}
[Test()]
public void TestCanAddAFunctionStrCode(){
string name = "faddsc";
Code func = new Code("function(x, y){return x + y;}");
js.Add(name,func);
Assert.IsNotNull(js[name]);
}
[Test()]
public void TestCanAddAFunctionDoc(){
string name = "fadddoc";
Code func = new Code("function(x, y){return x + y;}");
Document doc = new Document().Append("_id", name).Append("value", func);
js.Add(doc);
Assert.IsNotNull(js[name]);
}
[Test]
public void TestCannotAddAFunctionTwice(){
string name = "faddtwice";
Code func = new Code("function(x,y){return x + y;}");
js.Add(name, func);
bool thrown = false;
try{
js.Add(name, func);
}catch(ArgumentException){
thrown = true;
}
Assert.IsTrue(thrown, "Shouldn't be able to add a function twice");
}
[Test]
public void TestCanAddFunctionByAssignment(){
string name = "fassignadd";
Code func = new Code("function(x,y){return x + y;}");
Document doc = new Document().Append("_id", name).Append("value", func);
js[name] = doc;
Assert.IsNotNull(js[name]);
}
[Test]
public void TestContains(){
string name = "fcontains";
AddFunction(name);
Assert.IsTrue(js.Contains(name));
Assert.IsFalse(js.Contains("none"));
Assert.IsTrue(js.Contains(new Document().Append("_id", name).Append("value", new Code("dfs"))));
}
[Test]
public void TestCopyTo(){
int cnt = 5;
Document[] funcs = new Document[cnt];
Code func = new Code("function(x,y){return x +y;}");
for(int i = 0; i < cnt; i++){
string name = "_" + i + "fcopyTo";
Document doc = new Document().Append("_id", name).Append("value", func);
js[name] = doc;
}
js.CopyTo(funcs, 1);
Assert.IsNull(funcs[0]);
Assert.IsNotNull(funcs[1]);
Assert.IsNotNull(funcs[4]);
Assert.IsTrue(((string)funcs[1]["_id"]).StartsWith("_1")); //as long as no other _ named functions get in.
}
[Test]
public void TestRemoveByName(){
String name = "fremoven";
AddFunction(name);
Assert.IsTrue(js.Contains(name));
js.Remove(name);
Assert.IsFalse(js.Contains(name));
}
[Test]
public void TestRemoveByDoc(){
String name = "fremoved";
Document func = new Document().Append("_id", name);
AddFunction(name);
Assert.IsTrue(js.Contains(name));
js.Remove(func);
Assert.IsFalse(js.Contains(name));
}
[Test]
public void TestForEach(){
string name = "foreach";
AddFunction(name);
bool found = false;
foreach(Document doc in js){
if(name.Equals(doc["_id"]))found = true;
}
Assert.IsTrue(found, "Added function wasn't found during foreach");
}
[Test]
public void TestClear(){
AddFunction("clear");
Assert.IsTrue(js.Count > 0);
js.Clear();
Assert.IsTrue(js.Count == 0);
}
[Test]
public void TestExec(){
js.Add("lt4", new Code("function(doc){return doc.j < 4;}"));
int cnt = 0;
foreach(Document doc in tests["reads"].Find("lt4(this)").Documents){
cnt++;
}
Assert.AreEqual(3,cnt);
}
[Test]
public void TestExecWithScope(){
js.Add("lt", new Code("function(doc){ return doc.j < limit;}"));
int cnt = 0;
Document scope = new Document().Append("limit", 5);
Document query = new Document().Append("$where", new CodeWScope("lt(this)",scope));
foreach(Document doc in tests["reads"].Find(query).Documents){
cnt++;
}
Assert.AreEqual(4,cnt);
}
[TestFixtureSetUp]
public void Init(){
db.Connect();
initDB();
tests = db["tests"];
js = tests.JS;
}
[TestFixtureTearDown]
public void Dispose(){
db.Disconnect();
}
protected void initDB(){
//drop any previously created collections.
db["tests"]["system.js"].Delete(new Document());
}
protected void AddFunction(string name){
Code func = new Code("function(x,y){return x + y;}");
tests["system.js"].Insert(new Document().Append("_id", name).Append("value", func));
}
}
}