Skip to content

Commit

Permalink
Add failing tests for Dictionarys which has non string based key.
Browse files Browse the repository at this point in the history
  • Loading branch information
lanwin committed May 19, 2010
1 parent 95f794b commit 5596d8e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,36 +143,64 @@ public void CanDeserializeGenericDictionary()
Assert.Contains(new KeyValuePair<string, int>("key2", 20), prop.Property);
}

public class GenericDictionaryWithComplexType
public class GenericStringDictionaryWithComplexType
{
public Dictionary<string, GenericDictionaryComplexType> Property { get; set; }
public Dictionary<string, GenericDictionaryComplexType> Dict { get; set; }
}


public class GenericDictionaryComplexType
{
public string Name { get; set; }
}

[Test]
public void CanSerializeGenericDictionaryWithComplexType()
public void CanSerializeStringGenericDictionaryWithComplexType()
{
var expectedBson = Serialize<Document>(new Document("Property", new Document() { { "key1", new Document("Name", "a") }, { "key2", new Document("Name", "b") } }));
var obj = new GenericDictionaryWithComplexType { Property = new Dictionary<string, GenericDictionaryComplexType> { { "key1", new GenericDictionaryComplexType() { Name = "a" } }, { "key2", new GenericDictionaryComplexType() { Name = "b" } } } };
var bson = Serialize<GenericDictionaryWithComplexType>(obj);
var expectedBson = Serialize<Document>(new Document("Dict", new Document { { "key1", new Document("Name", "a") }, { "key2", new Document("Name", "b") } }));
var obj = new GenericStringDictionaryWithComplexType { Dict = new Dictionary<string, GenericDictionaryComplexType> { { "key1", new GenericDictionaryComplexType { Name = "a" } }, { "key2", new GenericDictionaryComplexType { Name = "b" } } } };
var bson = Serialize<GenericStringDictionaryWithComplexType>(obj);
Assert.AreEqual(expectedBson, bson);
}

[Test]
public void CanDeserializeGenericDictionaryWithComplexType()
public void CanDeserializeStringGenericDictionaryWithComplexType()
{
var bson = Serialize<Document>(new Document("Property", new Document() { { "key1", new Document("Name", "a") }, { "key2", new Document("Name", "b") } }));
var prop = Deserialize<GenericDictionaryWithComplexType>(bson);
var bson = Serialize<Document>(new Document("Dict", new Document { { "key1", new Document("Name", "a") }, { "key2", new Document("Name", "b") } }));
var prop = Deserialize<GenericStringDictionaryWithComplexType>(bson);

Assert.IsNotNull(prop);
Assert.IsNotNull(prop.Property);
Assert.AreEqual(2, prop.Property.Count);
Assert.IsTrue(prop.Property["key1"].Name == "a");
Assert.IsTrue(prop.Property["key2"].Name == "b");
Assert.IsNotNull(prop.Dict);
Assert.AreEqual(2, prop.Dict.Count);
Assert.IsTrue(prop.Dict["key1"].Name == "a");
Assert.IsTrue(prop.Dict["key2"].Name == "b");
}

public class GenericIntDictionaryWithComplexType
{
public Dictionary<int, GenericDictionaryComplexType> Dict { get; set; }
}

[Test]
public void CanSerializeIntGenericDictionaryWithComplexType()
{
var expectedBson = Serialize<Document>(new Document("Dict", new Document { { "1", new Document("Name", "a") }, { "2", new Document("Name", "b") } }));
var obj = new GenericIntDictionaryWithComplexType { Dict = new Dictionary<int, GenericDictionaryComplexType> { { 1, new GenericDictionaryComplexType { Name = "a" } }, { 2, new GenericDictionaryComplexType { Name = "b" } } } };
var bson = Serialize<GenericIntDictionaryWithComplexType>(obj);
Assert.AreEqual(expectedBson, bson);
}

[Test]
public void CanDeserializeIntGenericDictionaryWithComplexType()
{
var bson = Serialize<Document>(new Document("Dict", new Document { { "key1", new Document("Name", "a") }, { "key2", new Document("Name", "b") } }));
var prop = Deserialize<GenericIntDictionaryWithComplexType>(bson);

Assert.IsNotNull(prop);
Assert.IsNotNull(prop.Dict);
Assert.AreEqual(2, prop.Dict.Count);
Assert.IsTrue(prop.Dict[1].Name == "a");
Assert.IsTrue(prop.Dict[2].Name == "b");
}

public class HashSetHelper
Expand Down
1 change: 1 addition & 0 deletions source/MongoDB/MongoDB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<Compile Include="..\..\AssemblyInfoGlobal.cs">
<Link>AssemblyInfoGlobal.cs</Link>
</Compile>
<Compile Include="BinarySubtype.cs" />
<Compile Include="Bson\BsonType.cs" />
<Compile Include="Bson\BsonInfo.cs" />
<Compile Include="Bson\BsonDocumentBuilder.cs" />
Expand Down

0 comments on commit 5596d8e

Please sign in to comment.