forked from samus/mongodb-csharp
/
TestCollectionSafeMode.cs
77 lines (69 loc) · 2.76 KB
/
TestCollectionSafeMode.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
using System;
using NUnit.Framework;
namespace MongoDB.IntegrationTests
{
[TestFixture]
public class TestCollectionSafeMode : MongoTestBase
{
public override string TestCollections {
get {
return "safeinsert, safeupdate, safedelete, safemupdate";
}
}
[Test]
public void TestBadInsert(){
IMongoCollection col = InitCollection("safeinsert");
bool thrown = false;
try{
col.Insert(new Document {{"x",1},{"y",2}},true);
}catch(MongoDuplicateKeyException){
thrown = true;
}catch(Exception e){
Assert.Fail(String.Format("Wrong exception thrown: {0}", e.GetType().Name));
}
Assert.IsTrue(thrown);
}
[Test]
public void TestBadUpdate(){
IMongoCollection col = InitCollection("safeupdate");
bool thrown = false;
try{
col.Update(new Document {{"x", 1}}, new Document{{"x",2}},true);
}catch(MongoDuplicateKeyUpdateException){
thrown = true;
}catch(MongoDuplicateKeyException){
Assert.Fail("MongoDuplicateKeyException thown instead of MongoDuplicateKeyUpdateException");
}catch(Exception e){
Assert.Fail(String.Format("Wrong exception thrown: {0}", e.GetType().Name));
}
Assert.IsTrue(thrown);
}
[Test]
public void TestMultiUpdate(){
IMongoCollection col = InitCollection("safemupdate");
Document newy = new Document(){{"y", 2}};
col.UpdateAll(newy, new Document(){{"y",1}},true);
Assert.AreEqual(5, col.Count(newy));
bool thrown = false;
try{
col.UpdateAll(new Document{{"x",1}}, new Document{{"y",2}},true);
}catch(MongoDuplicateKeyUpdateException){
thrown = true;
}catch(MongoDuplicateKeyException){
Assert.Fail("MongoDuplicateKeyException thown instead of MongoDuplicateKeyUpdateException");
}catch(Exception e){
Assert.Fail(String.Format("Wrong exception thrown: {0}", e.GetType().Name));
}
Assert.IsTrue(thrown, "Exception not thrown.");
}
protected IMongoCollection InitCollection(string name)
{
IMongoCollection col = DB[name];
col.Metadata.CreateIndex(new Document{{"x", IndexOrder.Ascending}}, true);
for(int x = 0; x < 5; x++){
col.Insert(new Document{{"x", x}, {"y", 1}});
}
return col;
}
}
}