Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unstable #41

Open
wants to merge 10 commits into
base: unstable
Choose a base branch
from
86 changes: 86 additions & 0 deletions NoRM.Tests/MongoCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,86 @@ public void InsertingANewEntityWithNullableIntGeneratesAKey()
Assert.NotNull(testint._id);
Assert.AreNotEqual(0, testint._id.Value);
}
}



[Test]
public void when_inserting_a_new_entity_with_int_id_equals_zero_generates_a_key()
{
using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
{
var testint = new IntId {Name = "first"} ;
mongo.GetCollection<IntId>("Fake").Insert(testint);

Assert.NotNull(testint.Id);
Assert.AreNotEqual(0, testint.Id);
}
}

[Test]
public void when_inserting_a_22_new_entities_with_int_id_equals_zero_generates_a_unique_key_for_each_of_them()
{
using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
{
var idents = new List<int>();
for (int i = 0; i <22; i++)
{
var testint = new IntId() ;
mongo.GetCollection<IntId>("Fake").Insert(testint);
idents.Add(testint.Id);
}

var list = mongo.GetCollection<IntId>("Fake").Find(new { _id = Q.In(idents.ToArray()) });

foreach (var item in list)
{
Assert.True(idents.Contains(item.Id));
}

Assert.AreEqual(idents.Distinct().Count(), list.Select(x => x.Id).Distinct().Count());
}
}


[Test]
public void when_inserting_a_new_entity_with_long_type_id_equals_zero_generates_a_key()
{
using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
{
var testint = new LongId() { Name = "first" };
mongo.GetCollection<LongId>("Fake").Insert(testint);

Assert.NotNull(testint.Id);
Assert.AreNotEqual(0, testint.Id);
}
}

[Test]
public void when_inserting_a_22_new_entities_with_long_type_id_equals_zero_generates_a_unique_key_for_each_of_them()
{
using (var mongo = Mongo.Create(TestHelper.ConnectionString()))
{
var idents = new List<long>();
for (int i = 0; i < 22; i++)
{
var testint = new LongId();
mongo.GetCollection<LongId>("Fake").Insert(testint);
idents.Add(testint.Id);
}

var list = mongo.GetCollection<LongId>("Fake").Find(new { _id = Q.In(idents.ToArray()) });

foreach (var item in list)
{
Assert.True(idents.Contains(item.Id));
}

Assert.AreEqual(idents.Distinct().Count(), list.Select(x => x.Id).Distinct().Count());
}
}


[Test]
public void InsertingANewEntityWithNullableIntGeneratesAKeyComplex()
{
Expand Down Expand Up @@ -520,6 +598,8 @@ public void StringAsIdentifierDoesTranslation()
}
}



private class StringIdentifier
{
[MongoIdentifier]
Expand All @@ -531,6 +611,12 @@ private class IntId
{
public int Id { get; set; }
public string Name { get; set; }
}

private class LongId
{
public long Id { get; set; }
public string Name { get; set; }
}
}
}
34 changes: 34 additions & 0 deletions NoRM.Tests/MongoConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,40 @@ public void Are_Queries_Fully_Linqified()
}
}

[Test]
public void should_ignore_name_property_when_inserting__as_specified_in_mappings()
{

MongoConfiguration.Initialize(c => c.AddMap<ShopperMapWithIgnoreImmutableAndIgnoreIfNullConfigurationForProperties>());
using (
Shoppers shoppers =
new Shoppers(Mongo.Create(TestHelper.ConnectionString("pooling=false", "test", null, null))))
{
shoppers.Drop<Shopper>();
shoppers.Add(new Shopper
{
Id = ObjectId.NewObjectId(),
Name = "John",

});

shoppers.Add(new Shopper
{
Id = ObjectId.NewObjectId(),
Name = "Jane",

});


var deepQuery = shoppers.ToList();

Assert.IsNull(deepQuery[0].Name);
Assert.IsNull(deepQuery[1].Name);


}
}

[Test]
public void Can_correctly_determine_collection_name()
{
Expand Down
4 changes: 2 additions & 2 deletions NoRM.Tests/NoRM.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand Down
19 changes: 19 additions & 0 deletions NoRM.Tests/TestClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,25 @@ public ShopperMap()
}
}

public class ShopperMapWithIgnoreImmutableAndIgnoreIfNullConfigurationForProperties:MongoConfigurationMap
{
public ShopperMapWithIgnoreImmutableAndIgnoreIfNullConfigurationForProperties()
{
For<Shopper>(
config=> config.ForProperty(x => x.Name).Ignore()
);

For<Cart>(
config=>
{

config.ForProperty(x => x.Product).IgnoreIfNull();
config.ForProperty(x => x.Name).Immutable();

}
);
}
}
internal class IdMap0
{
public ObjectId _ID { get; set; }
Expand Down
63 changes: 38 additions & 25 deletions NoRM/BSON/BsonDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,41 +159,50 @@ private void NewDocument(int length)
/// <param retval="storedType">Type of the stored.</param>
/// <returns></returns>
private object DeserializeValue(Type type, BSONTypes storedType)
{
return DeserializeValue(type, storedType, null);
}

/// <summary>
/// Applies optional type conversion and deserializes the value.
/// </summary>
/// <param retval="type">The type.</param>
/// <param retval="storedType">Type of the stored.</param>
/// <param retval="container">The container.</param>
/// <returns></returns>
private object DeserializeValue(Type type, BSONTypes storedType, object container)
{
IBsonTypeConverter converter = Configuration.GetTypeConverterFor(type);
if (converter != null)
{
Type serializedType = converter.SerializedType;
object value = DeserializeValueAfterConversion(serializedType, storedType, container);
object value = DeserializeValueAfterConversion(serializedType, storedType);
return converter.ConvertFromBson(value);
}
else
{
return DeserializeValueAfterConversion(type, storedType, container);
}
return DeserializeValueAfterConversion(type, storedType);
}
}

/// <summary>
/// Applies optional type conversion and deserializes the value.
/// </summary>
/// <param retval="type">The type.</param>
/// <param retval="storedType">Type of the stored.</param>
/// <param retval="container">The container.</param>
/// <returns></returns>
private object DeserializeList(Type type, object container)
{
IBsonTypeConverter converter = Configuration.GetTypeConverterFor(type);
if (converter != null)
{
Type serializedType = converter.SerializedType;
object value = ReadList(serializedType, container);
return converter.ConvertFromBson(value);
}
else
{
return ReadList(type, container);
}
}


/// <summary>
/// Deserializes the value after any type conversion has been applied.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="storedType">Type of the stored.</param>
/// <param name="container">The container.</param>
/// <returns></returns>
private object DeserializeValueAfterConversion(Type type, BSONTypes storedType, object container)
private object DeserializeValueAfterConversion(Type type, BSONTypes storedType)
{
if (storedType == BSONTypes.Null)
{
Expand Down Expand Up @@ -226,7 +235,7 @@ private object DeserializeValueAfterConversion(Type type, BSONTypes storedType,
}
if (_IEnumerableType.IsAssignableFrom(type) || storedType == BSONTypes.Array)
{
return ReadList(type, container);
return ReadList(type, null);
}
if (type == typeof(bool))
{
Expand Down Expand Up @@ -342,18 +351,22 @@ private object ReadObject(Type type)
NewDocument(length);
}
}
object container = null;
if (property != null && property.Setter == null)
{
container = property.Getter(instance);

var propertyType = property != null ? property.Type : _typeMap.ContainsKey(storageType) ? _typeMap[storageType] : typeof(object);
object value = null;
if (!isNull)
{
if (_IEnumerableType.IsAssignableFrom(type) || storageType == BSONTypes.Array)
value = DeserializeList(propertyType, property.Getter(instance));
else
value = DeserializeValue(propertyType, storageType);
}
var propertyType = property != null ? property.Type : _typeMap.ContainsKey(storageType) ? _typeMap[storageType] : typeof(object);
var value = isNull ? null : DeserializeValue(propertyType, storageType, container);

if (property == null)
{
((IExpando)instance)[name] = value;
}
else if (container == null && value != null && property.Setter != null)
else if (property.Setter != null)
{
property.Setter(instance, value);
}
Expand Down
12 changes: 8 additions & 4 deletions NoRM/BSON/MagicProperty.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using System.Reflection;
using Norm.Attributes;
using System.ComponentModel;

using System.ComponentModel;
using Norm.Configuration;

namespace Norm.BSON
{
/// <summary>
Expand All @@ -27,8 +28,11 @@ public class MagicProperty
public MagicProperty(PropertyInfo property, Type declaringType)
{
_property = property;
this.IgnoreIfNull = property.GetCustomAttributes(_ignoredIfNullType, true).Length > 0;
this.Immutable = property.GetCustomAttributes(_immutableType, true).Length > 0;
this.IgnoreIfNull = property.GetCustomAttributes(_ignoredIfNullType, true).Length > 0 ||
MongoConfiguration.IsPropertyIgnoredWhenNull(declaringType,property.Name);
this.Immutable = property.GetCustomAttributes(_immutableType, true).Length > 0 ||
MongoConfiguration.IsPropertyImmutable(declaringType,property.Name);

var props = property.GetCustomAttributes(_defaultValueType, true);
if (props.Length > 0)
{
Expand Down
3 changes: 2 additions & 1 deletion NoRM/BSON/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ public static PropertyInfo FindIdProperty(Type type)
foreach (var property in properties)
{
if (property.GetCustomAttributes(_ignoredType, true).Length > 0 ||
property.GetIndexParameters().Length > 0)
property.GetIndexParameters().Length > 0 ||
MongoConfiguration.IsPropertyIgnored(property.DeclaringType, property.Name))
{
continue;
}
Expand Down