Skip to content

Commit

Permalink
Merge remote branch 'samus\master'
Browse files Browse the repository at this point in the history
  • Loading branch information
craiggwilson committed Aug 10, 2010
2 parents af265ba + f264954 commit 30d8109
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 12 deletions.
Expand Up @@ -354,6 +354,20 @@ where Regex.IsMatch(p.FirstName, "Joe")
Assert.AreEqual(new Document("FirstName", new MongoRegex("Joe")), queryObject.Query);
}

[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()
{
Expand Down
18 changes: 14 additions & 4 deletions source/MongoDB.Tests/IntegrationTests/Linq/MongoQueryTests.cs
Expand Up @@ -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 }
Expand Down Expand Up @@ -76,23 +76,23 @@ 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]
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]
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]
Expand Down Expand Up @@ -370,6 +370,16 @@ where Regex.IsMatch(p.FirstName, "Joe")
Assert.AreEqual(1, people.Count);
}

[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()
{
Expand Down
Expand Up @@ -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 changes: 16 additions & 0 deletions source/MongoDB.Tests/UnitTests/TestMongoRegex.cs
Expand Up @@ -45,6 +45,14 @@ public void CanBeConstructedFromRegex()
Assert.AreEqual("img", regex.RawOptions);
}

[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()
{
Expand All @@ -53,6 +61,14 @@ public void CanBeConstructedWithMongoRegexOption()
Assert.AreEqual("img", regex.RawOptions);
}

[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()
{
Expand Down
1 change: 1 addition & 0 deletions source/MongoDB/Attributes/MongoDefaultAttribute.cs
Expand Up @@ -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;
Expand Down
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using MongoDB.Configuration.Mapping.Util;

namespace MongoDB.Configuration.DictionaryAdapters
{
Expand Down Expand Up @@ -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>
Expand All @@ -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;
}

}
}
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using MongoDB.Configuration.Mapping.Util;

namespace MongoDB.Configuration.DictionaryAdapters
{
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using MongoDB.Configuration.Mapping.Util;

namespace MongoDB.Configuration.DictionaryAdapters
{
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
Expand Down
16 changes: 15 additions & 1 deletion source/MongoDB/Configuration/Mapping/Util/ValueConverter.cs
Expand Up @@ -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));
Expand Down Expand Up @@ -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 changes: 5 additions & 1 deletion source/MongoDB/Linq/Translators/DocumentFormatter.cs
Expand Up @@ -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;
}
Expand Down
10 changes: 10 additions & 0 deletions source/MongoDB/MongoRegex.cs
Expand Up @@ -38,6 +38,16 @@ public MongoRegex(string expression, MongoRegexOption options)
Options = 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>
Expand Down
2 changes: 1 addition & 1 deletion source/MongoDB/MongoRegexOption.cs
Expand Up @@ -25,6 +25,6 @@ public enum MongoRegexOption
/// <summary>
/// g - Eliminates unescaped white space from the pattern.
/// </summary>
IgnorePatternWhitespace = 3
IgnorePatternWhitespace = 4
}
}

0 comments on commit 30d8109

Please sign in to comment.