Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge remote branch 'samus\master'

  • Loading branch information...
commit 30d8109551a73b226165d1bc33f1b05c9a7f3600 2 parents af265ba + f264954
Craig Wilson craiggwilson authored
14 source/MongoDB.Tests/IntegrationTests/Linq/MongoQueryProviderTests.cs
View
@@ -355,6 +355,20 @@ where Regex.IsMatch(p.FirstName, "Joe")
}
[Test]
+ public void Regex_IsMatch_CaseInsensitive()
+ {
+ var people = from p in Collection.Linq()
+ where Regex.IsMatch(p.FirstName, "Joe", RegexOptions.IgnoreCase)
+ select p;
+
+ var queryObject = ((IMongoQueryable)people).GetQueryObject();
+ Assert.AreEqual(0, queryObject.Fields.Count);
+ Assert.AreEqual(0, queryObject.NumberToLimit);
+ Assert.AreEqual(0, queryObject.NumberToSkip);
+ Assert.AreEqual(new Document("FirstName", new MongoRegex("Joe", MongoRegexOption.IgnoreCase)), queryObject.Query);
+ }
+
+ [Test]
public void SingleEqualConstraint()
{
var people = Collection.Linq().Where(p => "Jack" == p.FirstName);
18 source/MongoDB.Tests/IntegrationTests/Linq/MongoQueryTests.cs
View
@@ -37,7 +37,7 @@ public override void TestSetup()
FirstName = "Jane",
LastName = "McJane",
Age = 35,
- PrimaryAddress = new Address { City = "Paris", IsInternational = true, AddressType = AddressType.Private },
+ PrimaryAddress = new Address { City = "Paris", IsInternational = false, AddressType = AddressType.Private },
Addresses = new List<Address>
{
new Address { City = "Paris", AddressType = AddressType.Private }
@@ -76,7 +76,7 @@ public void Boolean()
{
var people = Enumerable.ToList(Collection.Linq().Where(x => x.PrimaryAddress.IsInternational));
- Assert.AreEqual(3, people.Count);
+ Assert.AreEqual(2, people.Count);
}
[Test]
@@ -84,7 +84,7 @@ public void Boolean_Inverse()
{
var people = Enumerable.ToList(Collection.Linq().Where(x => !x.PrimaryAddress.IsInternational));
- Assert.AreEqual(0, people.Count);
+ Assert.AreEqual(1, people.Count);
}
[Test]
@@ -92,7 +92,7 @@ public void Boolean_In_Conjunction()
{
var people = Enumerable.ToList(Collection.Linq().Where(x => x.PrimaryAddress.IsInternational && x.Age > 21));
- Assert.AreEqual(2, people.Count);
+ Assert.AreEqual(1, people.Count);
}
[Test]
@@ -371,6 +371,16 @@ where Regex.IsMatch(p.FirstName, "Joe")
}
[Test]
+ public void Regex_IsMatch_CaseInsensitive()
+ {
+ var people = (from p in Collection.Linq()
+ where Regex.IsMatch(p.FirstName, "joe", RegexOptions.IgnoreCase)
+ select p).ToList();
+
+ Assert.AreEqual(1, people.Count);
+ }
+
+ [Test]
public void Single()
{
var person = Collection.Linq().Where(x => x.Age == 21).Single();
47 source/MongoDB.Tests/UnitTests/Serialization/SerializationFactoryTests.cs
View
@@ -314,5 +314,52 @@ public void CanReadEmbeddedDocument()
Assert.AreEqual(1, embedded.Count);
Assert.AreEqual(10, embedded["value"]);
}
+
+ public class DictionaryWithEnumAsKeyHelper
+ {
+ public Dictionary<DateTimeKind, int> Dict { get; set; }
+ }
+
+ [Test]
+ public void SerializesAnEnumAsIntWhenItsUsedAsDictionaryKey()
+ {
+ var obj = new DictionaryWithEnumAsKeyHelper { Dict = new Dictionary<DateTimeKind, int> { { DateTimeKind.Utc, 9 } } };
+ var bson = Serialize<DictionaryWithEnumAsKeyHelper>(obj);
+ var doc = Deserialize<Document>(bson);
+
+ Assert.IsNotNull(doc);
+ var dict = doc["Dict"] as Document;
+ Assert.IsNotNull(dict);
+ Assert.AreEqual(1, dict.Count);
+ Assert.AreEqual(9, dict[Convert.ToString((int)DateTimeKind.Utc)]);
+ }
+
+ [Test]
+ public void CanDeserializeADictionaryWithEnumAsKey()
+ {
+ var bson = Serialize<Document>(new Document("Dict", new Document(( (int)DateTimeKind.Utc ).ToString(), 9)));
+ var prop = Deserialize<DictionaryWithEnumAsKeyHelper>(bson);
+
+ Assert.IsNotNull(prop);
+ Assert.IsNotNull(prop.Dict);
+ Assert.AreEqual(1,prop.Dict.Count);
+ Assert.AreEqual(9,prop.Dict[DateTimeKind.Utc]);
+ }
+
+ public class NullDictionaryPropertyHelper
+ {
+ public Dictionary<string, string> Dict { get; set; }
+ }
+
+ [Test]
+ public void CanDeserializeAndNullDictionaryProperty()
+ {
+ var bson = Serialize<Document>(new Document("Dict", null));
+ var prop = Deserialize<NullDictionaryPropertyHelper>(bson);
+
+ Assert.IsNotNull(prop);
+ Assert.IsNull(prop.Dict);
+ }
+
}
}
16 source/MongoDB.Tests/UnitTests/TestMongoRegex.cs
View
@@ -46,6 +46,14 @@ public void CanBeConstructedFromRegex()
}
[Test]
+ public void MongoRegexOptionFlagsAreIndependent()
+ {
+ var regex = new MongoRegex("expression", MongoRegexOption.IgnoreCase);
+ Assert.AreEqual("expression", regex.Expression);
+ Assert.AreEqual("i", regex.RawOptions);
+ }
+
+ [Test]
public void CanBeConstructedWithMongoRegexOption()
{
var regex = new MongoRegex("expression", MongoRegexOption.IgnoreCase | MongoRegexOption.IgnorePatternWhitespace | MongoRegexOption.Multiline);
@@ -54,6 +62,14 @@ public void CanBeConstructedWithMongoRegexOption()
}
[Test]
+ public void CanBeConstructedWithRegexOptions()
+ {
+ var regex = new MongoRegex("expression", RegexOptions.IgnoreCase | RegexOptions.Multiline);
+ Assert.AreEqual("expression", regex.Expression);
+ Assert.AreEqual("im", regex.RawOptions);
+ }
+
+ [Test]
public void CanReadOptions()
{
var regex = new MongoRegex("expression", "img");
1  source/MongoDB/Attributes/MongoDefaultAttribute.cs
View
@@ -21,6 +21,7 @@ public MongoDefaultAttribute(object value)
/// Initializes a new instance of the <see cref="MongoDefaultAttribute"/> class.
/// </summary>
/// <param name="value">The value.</param>
+ /// <param name="persistDefaultValue">if set to <c>true</c> [persist default value].</param>
public MongoDefaultAttribute(object value, bool persistDefaultValue)
{
Value = value;
9 source/MongoDB/Configuration/DictionaryAdapters/GenericDictionaryDictionaryAdapter.cs
View
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using MongoDB.Configuration.Mapping.Util;
namespace MongoDB.Configuration.DictionaryAdapters
{
@@ -34,7 +35,10 @@ public Type ValueType
/// <returns></returns>
public object CreateDictionary(Document document)
{
- return document.ToDictionary(pair => (TKey)Convert.ChangeType(pair.Key, typeof(TKey)), pair => (TValue)pair.Value);
+ if(document==null)
+ return null;
+
+ return document.ToDictionary(pair => (TKey)ValueConverter.Convert(pair.Key, typeof(TKey)), pair => (TValue)pair.Value);
}
/// <summary>
@@ -52,9 +56,10 @@ public Document GetDocument(object dictionary)
var doc = new Document();
foreach (var e in instance)
- doc.Add(e.Key.ToString(), e.Value);
+ doc.Add(ValueConverter.ConvertKey(e.Key), e.Value);
return doc;
}
+
}
}
8 source/MongoDB/Configuration/DictionaryAdapters/GenericSortedListDictionaryAdapter.cs
View
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using MongoDB.Configuration.Mapping.Util;
namespace MongoDB.Configuration.DictionaryAdapters
{
@@ -33,10 +34,13 @@ public Type ValueType
/// <returns></returns>
public object CreateDictionary(Document document)
{
+ if(document == null)
+ return null;
+
var list = new SortedList<TKey, TValue>();
foreach(var pair in document)
- list.Add((TKey)Convert.ChangeType(pair.Key, typeof(TKey)), (TValue)pair.Value);
+ list.Add((TKey)ValueConverter.Convert(pair.Key, typeof(TKey)), (TValue)pair.Value);
return list;
}
@@ -56,7 +60,7 @@ public Document GetDocument(object dictionary)
var doc = new Document();
foreach (var e in instance)
- doc.Add(e.Key.ToString(), e.Value);
+ doc.Add(ValueConverter.ConvertKey(e.Key), e.Value);
return doc;
}
6 source/MongoDB/Configuration/DictionaryAdapters/HashtableDictionaryAdapter.cs
View
@@ -1,5 +1,6 @@
using System;
using System.Collections;
+using MongoDB.Configuration.Mapping.Util;
namespace MongoDB.Configuration.DictionaryAdapters
{
@@ -33,6 +34,9 @@ public Type ValueType
/// <returns></returns>
public object CreateDictionary(Document document)
{
+ if(document == null)
+ return null;
+
var hashtable = new Hashtable();
foreach (var pair in document)
@@ -54,7 +58,7 @@ public Document GetDocument(object collection)
var doc = new Document();
foreach (DictionaryEntry entry in hashtable)
- doc.Add(entry.Key.ToString(), entry.Value);
+ doc.Add(ValueConverter.ConvertKey(entry.Key), entry.Value);
return doc;
}
16 source/MongoDB/Configuration/Mapping/Util/ValueConverter.cs
View
@@ -17,7 +17,10 @@ public static object Convert(object value, Type type)
var code = System.Convert.GetTypeCode(value);
if(type.IsEnum)
- value = Enum.ToObject(type, value);
+ if(value is string)
+ value = Enum.Parse(type, (string)value);
+ else
+ value = Enum.ToObject(type, value);
else if(type.IsGenericType &&
type.GetGenericTypeDefinition() == typeof(Nullable<>))
value = System.Convert.ChangeType(value, Nullable.GetUnderlyingType(type));
@@ -47,5 +50,16 @@ public static Array ConvertArray(object[] elements, Type type)
return array;
}
+
+ public static string ConvertKey(object key)
+ {
+ if(key == null)
+ throw new ArgumentNullException("key");
+
+ if(key is Enum)
+ return System.Convert.ToInt64(key).ToString();
+
+ return key.ToString();
+ }
}
}
6 source/MongoDB/Linq/Translators/DocumentFormatter.cs
View
@@ -225,7 +225,11 @@ protected override Expression VisitMethodCall(MethodCallExpression m)
else
throw new InvalidQueryException(string.Format("Only the static Regex.IsMatch is supported.", m.Method.Name));
- AddCondition(new MongoRegex(value));
+ var regexOptions = RegexOptions.None;
+ if (m.Arguments.Count > 2)
+ regexOptions = EvaluateConstant<RegexOptions>(m.Arguments[2]);
+
+ AddCondition(new MongoRegex(value, regexOptions));
PopConditionScope();
return m;
}
10 source/MongoDB/MongoRegex.cs
View
@@ -39,6 +39,16 @@ public MongoRegex(string expression, MongoRegexOption options)
}
/// <summary>
+ /// Initializes a new instance of the <see cref="MongoRegex"/> class.
+ /// </summary>
+ /// <param name="expression">The Regex expression.</param>
+ /// <param name="options">The Regex options.</param>
+ public MongoRegex(string expression, RegexOptions options)
+ : this(new Regex(expression, options))
+ {
+ }
+
+ /// <summary>
/// Initializes a new instance of the <see cref = "MongoRegex" /> class.
/// </summary>
/// <param name = "regex">The regex.</param>
2  source/MongoDB/MongoRegexOption.cs
View
@@ -25,6 +25,6 @@ public enum MongoRegexOption
/// <summary>
/// g - Eliminates unescaped white space from the pattern.
/// </summary>
- IgnorePatternWhitespace = 3
+ IgnorePatternWhitespace = 4
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.