forked from samus/mongodb-csharp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TestOid.cs
executable file
·163 lines (135 loc) · 4.91 KB
/
TestOid.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
using System;
using NUnit.Framework;
namespace MongoDB.Driver
{
[TestFixture]
public class TestOid
{
[Test]
public void TestIDLength(){
bool thrown = false;
try{
new Oid("BAD0");
}catch(Exception){
thrown = true;
}
Assert.IsTrue(thrown,"No length exception thrown");
}
[Test]
public void TestIDCharacters(){
bool thrown = false;
try{
new Oid("BADBOYc30a57000000008ecb");
}catch(Exception){
thrown = true;
}
Assert.IsTrue(thrown,"No invalid characters exception thrown");
}
[Test]
public void TestQuoteCharacters(){
bool thrown = false;
Oid val = new Oid(@"""4a7067c30a57000000008ecb""");
try{
new Oid(val.ToString());
}catch(ArgumentException ae){
Assert.Fail("Creating an Oid from the json representation should not fail.");
}
}
[Test]
public void TestOidEquality(){
Oid val = new Oid("4a7067c30a57000000008ecb");
Oid other = new Oid("4a7067c30a57000000008ecb");
Assert.IsTrue(val.Equals(other), "Equals(Oid) did not work");
Assert.IsTrue(val == other, "== operator did not work");
}
[Test]
public void TestOidInEquality(){
Oid val = new Oid("4a7067c30a57000000008ecb");
Oid other = new Oid("5a7067c30a57000000008ecb");
Assert.IsFalse(val == null);
Assert.IsFalse(val == other);
Assert.IsFalse(val.Equals(other));
Assert.IsTrue(val != null);
Assert.IsTrue(val != other);
}
[Test]
public void TestOidComparisons(){
Oid lower = new Oid("4a7067c30a57000000008ecb");
Oid higher = new Oid("5a7067c30a57000000008ecb");
Assert.AreEqual(1, lower.CompareTo(null));
Assert.AreEqual(1, higher.CompareTo(lower));
Assert.IsTrue(lower < higher);
Assert.IsTrue(higher > lower);
}
[Test]
public void TestOidFromBytes(){
byte[] bytes = new byte[]{1,2,3,4,5,6,7,8,9,10,11,12};
string hex = "0102030405060708090a0b0c";
Oid bval = new Oid(bytes);
Oid sval = new Oid(hex);
Assert.AreEqual(bval, sval);
}
[Test]
public void TestNullValue(){
bool thrown = false;
try{
new Oid(String.Empty);
}catch(Exception){
thrown = true;
}
Assert.IsTrue(thrown,"Null value exception not thrown");
}
[Test]
public void TestCtor(){
bool thrown = false;
try{
new Oid("4a7067c30a57000000008ecb");
}catch(ArgumentException){
thrown = true;
}
Assert.IsFalse(thrown,"ID should be fine.");
}
[Test]
public void TestDecode(){
string hex = "4a7067c30a57000000008ecb";
Oid oid = new Oid(hex);
Assert.AreEqual("\"" + hex + "\"", oid.ToString());
}
[Test]
public void TestEquals(){
string hex = "4a7067c30a57000000008ecb";
Assert.AreEqual(new Oid(hex), new Oid(hex));
}
[Test]
public void TestNotEquals(){
string hex = "4a7067c30a57000000008ecb";
string hex2 = "4a7067c30a57000000008ecc";
Assert.AreNotEqual(new Oid(hex), new Oid(hex2));
}
[Test]
public void TestDate(){
string hex = "4B458B95D114BE541B000000";
Oid oid = new Oid(hex);
//Expected: 2010-01-07 02:24:56.633
DateTime expected = new DateTime(2010,1,7,7,21,57,DateTimeKind.Utc);
Assert.AreEqual(expected,oid.Created);
}
[Test]
public void TestToByteArray(){
byte[] bytes = new byte[]{1,2,3,4,5,6,7,8,9,10,11,12};
string hex = "0102030405060708090a0b0c";
Oid bval = new Oid(bytes);
byte[] bytes2 = bval.ToByteArray();
Assert.IsNotNull(bytes2);
Assert.AreEqual(12, bytes2.Length);
Assert.AreEqual(bytes, bytes2);
}
[Test]
public void TestNewOidFromToString(){
var hex = "4B458B95D114BE541B000000";
var firstOid = new Oid(hex);
var secondOid = new Oid(firstOid.ToString());
Assert.AreEqual(firstOid.ToString(), secondOid.ToString());
}
}
}