|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using FluentAssertions; |
| 7 | +using Xunit; |
| 8 | +using MongoDB.Bson.Serialization; |
| 9 | +using MongoDB.Driver.Linq.Translators; |
| 10 | +using System.ComponentModel; |
| 11 | +using System.Linq.Expressions; |
| 12 | +using MongoDB.Bson; |
| 13 | +using System.Globalization; |
| 14 | +using System.Reflection; |
| 15 | + |
| 16 | +namespace MongoDB.Driver.Tests.Linq.Translators |
| 17 | +{ |
| 18 | + public class PredicateImplicitTypeCastTests : IntegrationTestBase |
| 19 | + { |
| 20 | + [Fact] |
| 21 | + public void Implicit_Type_Casting_Primitive_Types() |
| 22 | + { |
| 23 | + Assert( |
| 24 | + x => x.B == (object)10, |
| 25 | + 0, |
| 26 | + "{B: '10'}"); |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public void Implicit_Type_Casting_With_Custom_TypeConverter() |
| 31 | + { |
| 32 | + //register custom type converter for C type |
| 33 | + TypeDescriptor.AddAttributes(typeof(C), new TypeConverterAttribute(typeof(CExampleTypeCoverter))); |
| 34 | + //cast string to object so it can be used in predicate |
| 35 | + var objectToCast = (object)"Dexter"; |
| 36 | + Assert( |
| 37 | + x => x.C == objectToCast, |
| 38 | + 0, |
| 39 | + "{C: { '_t' : 'C', D: 'Dexter', E: null, S: null, X: null}}"); |
| 40 | + } |
| 41 | + |
| 42 | + public void Assert(Expression<Func<Root, bool>> filter, int expectedCount, string expectedFilter) |
| 43 | + { |
| 44 | + var serializer = BsonSerializer.SerializerRegistry.GetSerializer<Root>(); |
| 45 | + var filterDocument = PredicateTranslator.Translate(filter, serializer, BsonSerializer.SerializerRegistry); |
| 46 | + |
| 47 | + var list = __collection.FindSync(filterDocument).ToList(); |
| 48 | + |
| 49 | + filterDocument.Should().Be(BsonDocument.Parse(expectedFilter)); |
| 50 | + list.Count.Should().Be(expectedCount); |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Custom type converter for <see cref="C"/> test class. |
| 55 | + /// </summary> |
| 56 | + public class CExampleTypeCoverter : TypeConverter |
| 57 | + { |
| 58 | + public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) |
| 59 | + { |
| 60 | + if (sourceType == typeof(string)) return true; |
| 61 | + return base.CanConvertFrom(context, sourceType); |
| 62 | + } |
| 63 | + |
| 64 | + public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) |
| 65 | + { |
| 66 | + if (value is string) return CreateFromString(value); |
| 67 | + return base.ConvertFrom(context, culture, value); |
| 68 | + } |
| 69 | + |
| 70 | + public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) |
| 71 | + { |
| 72 | + return (typeof(C).GetTypeInfo().IsAssignableFrom(destinationType)); |
| 73 | + } |
| 74 | + |
| 75 | + public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) |
| 76 | + { |
| 77 | + if (typeof(C).GetTypeInfo().IsAssignableFrom(destinationType)) |
| 78 | + { |
| 79 | + if (value is string) |
| 80 | + { |
| 81 | + return CreateFromString(value); |
| 82 | + } |
| 83 | + } |
| 84 | + return base.ConvertTo(context, culture, value, destinationType); |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Creates new <see cref="C"/> instance from string. |
| 89 | + /// </summary> |
| 90 | + /// <param name="value">String value used for conversion.</param> |
| 91 | + private C CreateFromString(object value) |
| 92 | + { |
| 93 | + return new C { D = (string)value }; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments