Browse Source

Added support for Enums to BsonTypeMapper. Added unit tests for BsonTypeMapper.

pull/27/head
rstam 15 years ago
parent
commit
3bcbd156ec
  1. 38
      Bson/ObjectModel/BsonTypeMapper.cs
  2. 1
      BsonUnitTests/BsonUnitTests.csproj
  3. 371
      BsonUnitTests/ObjectModel/BsonTypeMapperTests.cs

38
Bson/ObjectModel/BsonTypeMapper.cs

@ -27,10 +27,14 @@ namespace MongoDB.Bson {
// table of from mappings used by MapToBsonValue
private static Dictionary<Type, Conversion> fromMappings = new Dictionary<Type, Conversion> {
{ typeof(bool), Conversion.NewBsonBoolean },
{ typeof(byte[]), Conversion.ByteArrayToBsonBinary },
{ typeof(BsonArray), Conversion.None },
{ typeof(BsonBinaryData), Conversion.None },
{ typeof(BsonBoolean), Conversion.None },
{ typeof(BsonDateTime), Conversion.None },
{ typeof(BsonDocument), Conversion.None },
{ typeof(BsonDouble), Conversion.None },
{ typeof(BsonInt32), Conversion.None },
{ typeof(BsonInt64), Conversion.None },
{ typeof(BsonJavaScript), Conversion.None },
{ typeof(BsonJavaScriptWithScope), Conversion.None },
{ typeof(BsonMaxKey), Conversion.None },
@ -38,17 +42,27 @@ namespace MongoDB.Bson {
{ typeof(BsonNull), Conversion.None },
{ typeof(BsonObjectId), Conversion.None },
{ typeof(BsonRegularExpression), Conversion.None },
{ typeof(BsonString), Conversion.None },
{ typeof(BsonSymbol), Conversion.None },
{ typeof(BsonTimestamp), Conversion.None },
{ typeof(BsonValue), Conversion.None },
{ typeof(byte), Conversion.ByteToBsonInt32 },
{ typeof(byte[]), Conversion.ByteArrayToBsonBinary },
{ typeof(DateTime), Conversion.DateTimeToBsonDateTime },
{ typeof(double), Conversion.NewBsonDouble },
{ typeof(float), Conversion.SingleToBsonDouble },
{ typeof(Guid), Conversion.GuidToBsonBinary },
{ typeof(int), Conversion.NewBsonInt32 },
{ typeof(long), Conversion.NewBsonInt64 },
{ typeof(ObjectId), Conversion.NewBsonObjectId },
{ typeof(Regex), Conversion.RegexToBsonRegularExpression },
{ typeof(string), Conversion.NewBsonString }
};
{ typeof(sbyte), Conversion.SByteToBsonInt32 },
{ typeof(short), Conversion.Int16ToBsonInt32 },
{ typeof(string), Conversion.NewBsonString },
{ typeof(uint), Conversion.UInt32ToBsonInt64 },
{ typeof(ushort), Conversion.UInt16ToBsonInt32 },
{ typeof(ulong), Conversion.UInt64ToBsonInt64 }
};
// table of from/to mappings used by MapToBsonValue
private static Dictionary<Mapping, Conversion> fromToMappings = new Dictionary<Mapping, Conversion>() {
@ -177,8 +191,24 @@ namespace MongoDB.Bson {
throw new ArgumentNullException("Value to be mapped to BsonValue cannot be null");
}
var valueType = value.GetType();
if (valueType.IsEnum) {
valueType = Enum.GetUnderlyingType(valueType);
switch (Type.GetTypeCode(valueType)) {
case TypeCode.Byte: value = (int) (byte) value; break;
case TypeCode.Int16: value = (int) (short) value; break;
case TypeCode.Int32: value = (int) value; break;
case TypeCode.Int64: value = (long) value; break;
case TypeCode.SByte: value = (int) (sbyte) value; break;
case TypeCode.UInt16: value = (int) (ushort) value; break;
case TypeCode.UInt32: value = (long) (uint) value; break;
case TypeCode.UInt64: value = (long) (ulong) value; break;
}
valueType = value.GetType();
}
Conversion conversion;
if (fromMappings.TryGetValue(value.GetType(), out conversion)) {
if (fromMappings.TryGetValue(valueType, out conversion)) {
bsonValue = Convert(value, conversion);
return true;
}

1
BsonUnitTests/BsonUnitTests.csproj

@ -102,6 +102,7 @@
<Compile Include="ObjectModel\BsonEqualsTests.cs" />
<Compile Include="IO\BsonJsonWriterTests.cs" />
<Compile Include="ObjectModel\BsonObjectIdTests.cs" />
<Compile Include="ObjectModel\BsonTypeMapperTests.cs" />
<Compile Include="ObjectModel\BsonValueTests.cs" />
<Compile Include="ObjectModel\ObjectIdTests.cs" />
<Compile Include="IO\BsonReaderTests.cs" />

371
BsonUnitTests/ObjectModel/BsonTypeMapperTests.cs

@ -0,0 +1,371 @@
/* Copyright 2010 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using NUnit.Framework;
using MongoDB.Bson;
namespace MongoDB.BsonUnitTests {
[TestFixture]
public class BsonTypeMapperTests {
private enum ByteEnum : byte {
V = 1
}
private enum Int16Enum : short {
V = 1
}
private enum Int32Enum : int {
V = 1
}
private enum Int64Enum : long {
V = 1
}
private enum SByteEnum : sbyte {
V = 1
}
private enum UInt16Enum : ushort {
V = 1
}
private enum UInt32Enum : uint {
V = 1
}
private enum UInt64Enum : ulong {
V = 1
}
[Test]
public void TestMapBoolean() {
var value = true;
var bsonValue = (BsonBoolean) BsonTypeMapper.MapToBsonValue(value);
Assert.IsTrue(bsonValue.Value);
}
[Test]
public void TestMapBsonArray() {
var value = new BsonArray();
var bsonValue = (BsonArray) BsonTypeMapper.MapToBsonValue(value);
Assert.AreSame(value, bsonValue);
}
[Test]
public void TestMapBsonBinaryData() {
var value = new BsonBinaryData(new byte[] { 1, 2, 3 });
var bsonValue = (BsonBinaryData) BsonTypeMapper.MapToBsonValue(value);
Assert.AreSame(value, bsonValue);
}
[Test]
public void TestMapBsonBoolean() {
var value = BsonBoolean.True;
var bsonValue = (BsonBoolean) BsonTypeMapper.MapToBsonValue(value);
Assert.AreSame(value, bsonValue);
}
[Test]
public void TestMapBsonDateTime() {
var value = new BsonDateTime(DateTime.UtcNow);
var bsonValue = (BsonDateTime) BsonTypeMapper.MapToBsonValue(value);
Assert.AreSame(value, bsonValue);
}
[Test]
public void TestMapBsonDocument() {
var value = new BsonDocument();
var bsonValue = (BsonDocument) BsonTypeMapper.MapToBsonValue(value);
Assert.AreSame(value, bsonValue);
}
[Test]
public void TestMapBsonDouble() {
var value = new BsonDouble(1.2);
var bsonValue = (BsonDouble) BsonTypeMapper.MapToBsonValue(value);
Assert.AreSame(value, bsonValue);
}
[Test]
public void TestMapBsonInt32() {
var value = new BsonInt32(1);
var bsonValue = (BsonInt32) BsonTypeMapper.MapToBsonValue(value);
Assert.AreSame(value, bsonValue);
}
[Test]
public void TestMapBsonInt64() {
var value = new BsonInt64(1L);
var bsonValue = (BsonInt64) BsonTypeMapper.MapToBsonValue(value);
Assert.AreSame(value, bsonValue);
}
[Test]
public void TestMapJavaScript() {
var value = new BsonJavaScript("code");
var bsonValue = (BsonJavaScript) BsonTypeMapper.MapToBsonValue(value);
Assert.AreSame(value, bsonValue);
}
[Test]
public void TestMapJavaScriptWithScope() {
var value = new BsonJavaScriptWithScope("code", new BsonDocument());
var bsonValue = (BsonJavaScriptWithScope) BsonTypeMapper.MapToBsonValue(value);
Assert.AreSame(value, bsonValue);
}
[Test]
public void TestMapBsonMaxKey() {
var value = BsonMaxKey.Value;
var bsonValue = (BsonMaxKey) BsonTypeMapper.MapToBsonValue(value);
Assert.AreSame(value, bsonValue);
}
[Test]
public void TestMapBsonMinKey() {
var value = BsonMinKey.Value;
var bsonValue = (BsonMinKey) BsonTypeMapper.MapToBsonValue(value);
Assert.AreSame(value, bsonValue);
}
[Test]
public void TestMapBsonNull() {
var value = BsonNull.Value;
var bsonValue = (BsonNull) BsonTypeMapper.MapToBsonValue(value);
Assert.AreSame(value, bsonValue);
}
[Test]
public void TestMapBsonObjectId() {
var value = BsonObjectId.GenerateNewId();
var bsonValue = (BsonObjectId) BsonTypeMapper.MapToBsonValue(value);
Assert.AreSame(value, bsonValue);
}
[Test]
public void TestMapBsonRegularExpression() {
var value = new BsonRegularExpression("pattern", "options");
var bsonValue = (BsonRegularExpression) BsonTypeMapper.MapToBsonValue(value);
Assert.AreSame(value, bsonValue);
}
[Test]
public void TestMapBsonString() {
var value = new BsonString("hello");
var bsonValue = (BsonString) BsonTypeMapper.MapToBsonValue(value);
Assert.AreSame(value, bsonValue);
}
[Test]
public void TestMapBsonSymbol() {
var value = BsonSymbol.Create("symbol");
var bsonValue = (BsonSymbol) BsonTypeMapper.MapToBsonValue(value);
Assert.AreSame(value, bsonValue);
}
[Test]
public void TestMapBsonTimestamp() {
var value = new BsonTimestamp(1234L);
var bsonValue = (BsonTimestamp) BsonTypeMapper.MapToBsonValue(value);
Assert.AreSame(value, bsonValue);
}
[Test]
public void TestMapBsonValue() {
var value = BsonValue.Create(1234);
var bsonValue = (BsonValue) BsonTypeMapper.MapToBsonValue(value);
Assert.AreSame(value, bsonValue);
}
[Test]
public void TestMapByte() {
var value = (byte) 1;
var bsonValue = (BsonInt32) BsonTypeMapper.MapToBsonValue(value);
Assert.AreEqual(value, bsonValue.Value);
}
[Test]
public void TestMapByteArray() {
var value = new byte[] { 1, 2, 3 };
var bsonValue = (BsonBinaryData) BsonTypeMapper.MapToBsonValue(value);
Assert.AreSame(value, bsonValue.Bytes);
Assert.AreEqual(BsonBinarySubType.Binary, bsonValue.SubType);
}
[Test]
public void TestMapByteEnum() {
var value = ByteEnum.V;
var bsonValue = (BsonInt32) BsonTypeMapper.MapToBsonValue(value);
Assert.AreEqual((int) (byte) value, bsonValue.Value);
}
[Test]
public void TestMapDateTime() {
var value = DateTime.UtcNow;
var bsonValue = (BsonDateTime) BsonTypeMapper.MapToBsonValue(value);
Assert.AreEqual(value, bsonValue.Value);
}
[Test]
public void TestMapDouble() {
var value = 1.2;
var bsonValue = (BsonDouble) BsonTypeMapper.MapToBsonValue(value);
Assert.AreEqual(value, bsonValue.Value);
}
[Test]
public void TestMapGuid() {
var value = Guid.NewGuid();
var bsonValue = (BsonBinaryData) BsonTypeMapper.MapToBsonValue(value);
Assert.IsTrue(value.ToByteArray().SequenceEqual(bsonValue.Bytes));
Assert.AreEqual(BsonBinarySubType.Uuid, bsonValue.SubType);
}
[Test]
public void TestMapInt16() {
var value = (short) 1;
var bsonValue = (BsonInt32) BsonTypeMapper.MapToBsonValue(value);
Assert.AreEqual(value, bsonValue.Value);
}
[Test]
public void TestMapInt16Enum() {
var value = Int16Enum.V;
var bsonValue = (BsonInt32) BsonTypeMapper.MapToBsonValue(value);
Assert.AreEqual((int) (short) value, bsonValue.Value);
}
[Test]
public void TestMapInt32() {
var value = 1;
var bsonValue = (BsonInt32) BsonTypeMapper.MapToBsonValue(value);
Assert.AreEqual(value, bsonValue.Value);
}
[Test]
public void TestMapInt32Enum() {
var value = Int32Enum.V;
var bsonValue = (BsonInt32) BsonTypeMapper.MapToBsonValue(value);
Assert.AreEqual((int) value, bsonValue.Value);
}
[Test]
public void TestMapInt64() {
var value = 1L;
var bsonValue = (BsonInt64) BsonTypeMapper.MapToBsonValue(value);
Assert.AreEqual(value, bsonValue.Value);
}
[Test]
public void TestMapInt64Enum() {
var value = Int64Enum.V;
var bsonValue = (BsonInt64) BsonTypeMapper.MapToBsonValue(value);
Assert.AreEqual((long) value, bsonValue.Value);
}
[Test]
public void TestMapObjectId() {
var value = ObjectId.GenerateNewId(); ;
var bsonValue = (BsonObjectId) BsonTypeMapper.MapToBsonValue(value);
Assert.AreEqual(value, bsonValue.Value);
}
[Test]
public void TestMapRegex() {
var value = new Regex("pattern");
var bsonValue = (BsonRegularExpression) BsonTypeMapper.MapToBsonValue(value);
Assert.AreEqual("pattern", bsonValue.Pattern);
Assert.AreEqual("", bsonValue.Options);
}
[Test]
public void TestMapSByte() {
var value = (sbyte) 1;
var bsonValue = (BsonInt32) BsonTypeMapper.MapToBsonValue(value);
Assert.AreEqual(value, bsonValue.Value);
}
[Test]
public void TestMapSByteEnum() {
var value = SByteEnum.V;
var bsonValue = (BsonInt32) BsonTypeMapper.MapToBsonValue(value);
Assert.AreEqual((int) (sbyte) value, bsonValue.Value);
}
[Test]
public void TestMapSingle() {
var value = (float) 1.2;
var bsonValue = (BsonDouble) BsonTypeMapper.MapToBsonValue(value);
Assert.AreEqual(value, bsonValue.Value);
}
[Test]
public void TestMapString() {
var value = "hello";
var bsonValue = (BsonString) BsonTypeMapper.MapToBsonValue(value);
Assert.AreEqual(value, bsonValue.Value);
}
[Test]
public void TestMapUInt16() {
var value = (ushort) 1;
var bsonValue = (BsonInt32) BsonTypeMapper.MapToBsonValue(value);
Assert.AreEqual(value, bsonValue.Value);
}
[Test]
public void TestMapUInt16Enum() {
var value = UInt16Enum.V;
var bsonValue = (BsonInt32) BsonTypeMapper.MapToBsonValue(value);
Assert.AreEqual((int) (ushort) value, bsonValue.Value);
}
[Test]
public void TestMapUInt32() {
var value = (uint) 1;
var bsonValue = (BsonInt64) BsonTypeMapper.MapToBsonValue(value);
Assert.AreEqual(value, bsonValue.Value);
}
[Test]
public void TestMapUInt32Enum() {
var value = UInt32Enum.V;
var bsonValue = (BsonInt64) BsonTypeMapper.MapToBsonValue(value);
Assert.AreEqual((long) (uint) value, bsonValue.Value);
}
[Test]
public void TestMapUInt64() {
var value = (ulong) 1;
var bsonValue = (BsonInt64) BsonTypeMapper.MapToBsonValue(value);
Assert.AreEqual(value, bsonValue.Value);
}
[Test]
public void TestMapUInt64Enum() {
var value = UInt64Enum.V;
var bsonValue = (BsonInt64) BsonTypeMapper.MapToBsonValue(value);
Assert.AreEqual((long) (ulong) value, bsonValue.Value);
}
}
}
Loading…
Cancel
Save