|
| 1 | +#if !NET452 |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Diagnostics.CodeAnalysis; |
| 5 | +using System.Globalization; |
| 6 | +using System.Linq.Dynamic.Core.CustomTypeProviders; |
| 7 | +using System.Reflection; |
| 8 | +using FluentAssertions; |
| 9 | +using NodaTime; |
| 10 | +using NodaTime.Text; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace System.Linq.Dynamic.Core.Tests.TypeConvertors |
| 14 | +{ |
| 15 | + public class NodaTimeConverterTests |
| 16 | + { |
| 17 | + private class Entity |
| 18 | + { |
| 19 | + public Guid Id { get; set; } |
| 20 | + public string FirstName { get; set; } |
| 21 | + public DateTime? MyDateTimeNullable { get; set; } |
| 22 | + public LocalDate BirthDate { get; set; } |
| 23 | + public LocalDate? BirthDateNullable { get; set; } |
| 24 | + public LocalTime? Time { get; set; } |
| 25 | + } |
| 26 | + |
| 27 | + private readonly IQueryable<Entity> entities; |
| 28 | + |
| 29 | + public NodaTimeConverterTests() |
| 30 | + { |
| 31 | + entities = new List<Entity>() |
| 32 | + { |
| 33 | + new Entity { Id = Guid.NewGuid(), FirstName = "Paul", MyDateTimeNullable = new DateTime(1987, 10, 12), BirthDate = new LocalDate(1987, 10, 12), BirthDateNullable = new LocalDate(1987, 10, 12), Time = new LocalTime(11, 1) }, |
| 34 | + new Entity { Id = Guid.NewGuid(), FirstName = "Abigail", BirthDate = new LocalDate(1970, 02, 13), Time = new LocalTime(12, 2) }, |
| 35 | + new Entity { Id = Guid.NewGuid(), FirstName = "Sophia", BirthDate = new LocalDate(1983, 05, 01), Time = new LocalTime(13, 3) } |
| 36 | + }.AsQueryable(); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public void FilterByLocalDate_WithTypeConverter() |
| 41 | + { |
| 42 | + // Arrange |
| 43 | + var config = new ParsingConfig |
| 44 | + { |
| 45 | + TypeConverters = new Dictionary<Type, TypeConverter> |
| 46 | + { |
| 47 | + { typeof(LocalDate), new LocalDateConverter() } |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + // Act |
| 52 | + var result = entities.AsQueryable().Where(config, "BirthDate == @0", "1987-10-12").ToList(); |
| 53 | + |
| 54 | + // Assert |
| 55 | + Assert.Single(result); |
| 56 | + } |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public void FilterByNullableLocalDate_WithTypeConverter() |
| 60 | + { |
| 61 | + // Arrange |
| 62 | + var config = new ParsingConfig |
| 63 | + { |
| 64 | + TypeConverters = new Dictionary<Type, TypeConverter> |
| 65 | + { |
| 66 | + { typeof(LocalDate), new LocalDateConverter() } |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + // Act |
| 71 | + var result = entities.AsQueryable().Where(config, "BirthDateNullable == @0", "1987-10-12").ToList(); |
| 72 | + |
| 73 | + // Assert |
| 74 | + Assert.Single(result); |
| 75 | + } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + public void FilterByLocalDate_WithDynamicExpressionParser() |
| 79 | + { |
| 80 | + // Arrange |
| 81 | + var config = new ParsingConfig |
| 82 | + { |
| 83 | + TypeConverters = new Dictionary<Type, TypeConverter> |
| 84 | + { |
| 85 | + { typeof(LocalDate), new LocalDateConverter() } |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + // Act |
| 90 | + var expr = DynamicExpressionParser.ParseLambda<Entity, bool>(config, false, "BirthDate == @0", "1987-10-12"); |
| 91 | + var result = entities.AsQueryable().Where(expr).ToList(); |
| 92 | + |
| 93 | + // Assert |
| 94 | + Assert.Single(result); |
| 95 | + } |
| 96 | + |
| 97 | + [Fact] |
| 98 | + public void FilterByNullableLocalDate_WithDynamicExpressionParser() |
| 99 | + { |
| 100 | + // Arrange |
| 101 | + var config = new ParsingConfig |
| 102 | + { |
| 103 | + TypeConverters = new Dictionary<Type, TypeConverter> |
| 104 | + { |
| 105 | + { typeof(LocalDate), new LocalDateConverter() } |
| 106 | + } |
| 107 | + }; |
| 108 | + |
| 109 | + // Act |
| 110 | + var expr = DynamicExpressionParser.ParseLambda<Entity, bool>(config, false, "BirthDateNullable == \"1987-10-12\""); |
| 111 | + var result = entities.AsQueryable().Where(expr).ToList(); |
| 112 | + |
| 113 | + // Assert |
| 114 | + Assert.Single(result); |
| 115 | + } |
| 116 | + |
| 117 | + [Theory] |
| 118 | + [InlineData("!= null", 1)] |
| 119 | + [InlineData("== null", 2)] |
| 120 | + public void FilterByNullableDateTime_WithDynamicExpressionParser_CompareWithNull(string equal, int numberOfEntities) |
| 121 | + { |
| 122 | + // Arrange |
| 123 | + var config = new ParsingConfig |
| 124 | + { |
| 125 | + TypeConverters = new Dictionary<Type, TypeConverter> |
| 126 | + { |
| 127 | + { typeof(LocalDate), new LocalDateConverter() } |
| 128 | + } |
| 129 | + }; |
| 130 | + |
| 131 | + // Act |
| 132 | + var expr = DynamicExpressionParser.ParseLambda<Entity, bool>(config, false, $"MyDateTimeNullable {equal}"); |
| 133 | + var result = entities.AsQueryable().Where(expr).ToList(); |
| 134 | + |
| 135 | + // Assert |
| 136 | + result.Should().HaveCount(numberOfEntities); |
| 137 | + } |
| 138 | + |
| 139 | + [Theory] |
| 140 | + [InlineData("!= null", 1)] |
| 141 | + [InlineData("== null", 2)] |
| 142 | + public void FilterByNullableLocalDate_WithDynamicExpressionParser_CompareWithNull(string equal, int numberOfEntities) |
| 143 | + { |
| 144 | + // Arrange |
| 145 | + var config = new ParsingConfig |
| 146 | + { |
| 147 | + TypeConverters = new Dictionary<Type, TypeConverter> |
| 148 | + { |
| 149 | + { typeof(LocalDate), new LocalDateConverter() } |
| 150 | + } |
| 151 | + }; |
| 152 | + |
| 153 | + // Act |
| 154 | + var expr = DynamicExpressionParser.ParseLambda<Entity, bool>(config, false, $"BirthDateNullable {equal}"); |
| 155 | + var result = entities.AsQueryable().Where(expr).ToList(); |
| 156 | + |
| 157 | + // Assert |
| 158 | + result.Should().HaveCount(numberOfEntities); |
| 159 | + } |
| 160 | + |
| 161 | + public class LocalDateConverter : TypeConverter |
| 162 | + { |
| 163 | + public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) => sourceType == typeof(string); |
| 164 | + |
| 165 | + public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) |
| 166 | + { |
| 167 | + var result = LocalDatePattern.Iso.Parse(value as string); |
| 168 | + |
| 169 | + return result.Success |
| 170 | + ? result.Value |
| 171 | + : throw new FormatException(value?.ToString()); |
| 172 | + } |
| 173 | + |
| 174 | + protected ParseResult<LocalDate> Convert(object value) => LocalDatePattern.Iso.Parse(value as string); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
| 178 | +#endif |
0 commit comments