|
| 1 | +/* Copyright 2010-present MongoDB Inc. |
| 2 | +* |
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +* you may not use this file except in compliance with the License. |
| 5 | +* You may obtain a copy of the License at |
| 6 | +* |
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +* |
| 9 | +* Unless required by applicable law or agreed to in writing, software |
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the License for the specific language governing permissions and |
| 13 | +* limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using FluentAssertions; |
| 17 | +using MongoDB.Driver.Linq; |
| 18 | +using Xunit; |
| 19 | + |
| 20 | +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators |
| 21 | +{ |
| 22 | + public class ConvertExpressionToAggregationExpressionTranslatorTests : Linq3IntegrationTest |
| 23 | + { |
| 24 | + [Fact] |
| 25 | + public void Should_translate_to_derived_class_on_method_call() |
| 26 | + { |
| 27 | + var collection = GetCollection(); |
| 28 | + var queryable = collection.AsQueryable() |
| 29 | + .Select(p => new DerivedClass |
| 30 | + { |
| 31 | + Id = p.Id, |
| 32 | + A = ((DerivedClass)p).A.ToUpper() |
| 33 | + }); |
| 34 | + |
| 35 | + var stages = Translate(collection, queryable); |
| 36 | + AssertStages( |
| 37 | + stages, |
| 38 | + "{ '$project' : { _id : '$_id', A : { '$toUpper' : '$A' } } }"); |
| 39 | + |
| 40 | + var result = queryable.Single(); |
| 41 | + result.Id.Should().Be(1); |
| 42 | + result.A.Should().Be("ABC"); |
| 43 | + } |
| 44 | + |
| 45 | + [Fact] |
| 46 | + public void Should_translate_to_derived_class_on_projection() |
| 47 | + { |
| 48 | + var collection = GetCollection(); |
| 49 | + var queryable = collection.AsQueryable() |
| 50 | + .Select(p => new DerivedClass() |
| 51 | + { |
| 52 | + Id = p.Id, |
| 53 | + A = ((DerivedClass)p).A |
| 54 | + }); |
| 55 | + |
| 56 | + var stages = Translate(collection, queryable); |
| 57 | + AssertStages( |
| 58 | + stages, |
| 59 | + "{ '$project' : { _id : '$_id', A : '$A' } }"); |
| 60 | + |
| 61 | + var result = queryable.Single(); |
| 62 | + result.Id.Should().Be(1); |
| 63 | + result.A.Should().Be("abc"); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public void Project_using_convert_underlying_type_to_enum_should_work() |
| 68 | + { |
| 69 | + var collection = GetCollection(); |
| 70 | + var queryable = collection.AsQueryable() |
| 71 | + .Select(p => new ProjectedModel |
| 72 | + { |
| 73 | + Id = p.Id, |
| 74 | + Enum = (Enum)p.EnumAsInt, |
| 75 | + EnumComparisonResult = (Enum)p.EnumAsInt == Enum.Two, |
| 76 | + }); |
| 77 | + |
| 78 | + var stages = Translate(collection, queryable); |
| 79 | + AssertStages( |
| 80 | + stages, |
| 81 | + "{ '$project' : { _id : '$_id', Enum : '$EnumAsInt', EnumComparisonResult : { $eq : ['$EnumAsInt', 2] } } }"); |
| 82 | + |
| 83 | + var result = queryable.Single(); |
| 84 | + result.Id.Should().Be(1); |
| 85 | + result.Enum.Should().Be(Enum.Two); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public void Project_using_convert_nullable_underlying_type_to_enum_should_work() |
| 90 | + { |
| 91 | + var collection = GetCollection(); |
| 92 | + var queryable = collection.AsQueryable() |
| 93 | + .Select(p => new ProjectedModel |
| 94 | + { |
| 95 | + Id = p.Id, |
| 96 | + Enum = (Enum)p.EnumAsNullableInt, |
| 97 | + EnumComparisonResult = (Enum)p.EnumAsNullableInt == Enum.Two, |
| 98 | + }); |
| 99 | + |
| 100 | + var stages = Translate(collection, queryable); |
| 101 | + AssertStages( |
| 102 | + stages, |
| 103 | + "{ '$project' : { _id : '$_id', Enum : '$EnumAsNullableInt', EnumComparisonResult : { $eq : ['$EnumAsNullableInt', 2] } } }"); |
| 104 | + |
| 105 | + var result = queryable.Single(); |
| 106 | + result.Id.Should().Be(1); |
| 107 | + result.Enum.Should().Be(Enum.Two); |
| 108 | + } |
| 109 | + |
| 110 | + [Fact] |
| 111 | + public void Project_using_convert_nullable_underlying_type_to_nullable_enum_should_work() |
| 112 | + { |
| 113 | + var collection = GetCollection(); |
| 114 | + var queryable = collection.AsQueryable() |
| 115 | + .Select(p => new ProjectedModel |
| 116 | + { |
| 117 | + Id = p.Id, |
| 118 | + NullableEnum = (Enum?)p.EnumAsNullableInt |
| 119 | + }); |
| 120 | + |
| 121 | + var stages = Translate(collection, queryable); |
| 122 | + AssertStages( |
| 123 | + stages, |
| 124 | + "{ '$project' : { _id : '$_id', NullableEnum : '$EnumAsNullableInt' } }"); |
| 125 | + |
| 126 | + var result = queryable.Single(); |
| 127 | + result.Id.Should().Be(1); |
| 128 | + result.NullableEnum.Should().Be(Enum.Two); |
| 129 | + } |
| 130 | + |
| 131 | + [Fact] |
| 132 | + public void Project_using_convert_enum_to_underlying_type_should_work() |
| 133 | + { |
| 134 | + var collection = GetCollection(); |
| 135 | + var queryable = collection.AsQueryable() |
| 136 | + .Select(p => new ProjectedModel |
| 137 | + { |
| 138 | + Id = p.Id, |
| 139 | + EnumAsInt = (int)p.Enum |
| 140 | + }); |
| 141 | + |
| 142 | + var stages = Translate(collection, queryable); |
| 143 | + AssertStages( |
| 144 | + stages, |
| 145 | + "{ '$project' : { _id : '$_id', EnumAsInt : '$Enum' } }"); |
| 146 | + |
| 147 | + var result = queryable.Single(); |
| 148 | + result.Id.Should().Be(1); |
| 149 | + result.EnumAsInt.Should().Be(2); |
| 150 | + } |
| 151 | + |
| 152 | + [Fact] |
| 153 | + public void Project_using_convert_nullable_enum_to_underlying_type_work() |
| 154 | + { |
| 155 | + var collection = GetCollection(); |
| 156 | + var queryable = collection.AsQueryable() |
| 157 | + .Select(p => new ProjectedModel |
| 158 | + { |
| 159 | + Id = p.Id, |
| 160 | + EnumAsInt = (int)p.NullableEnum |
| 161 | + }); |
| 162 | + |
| 163 | + var stages = Translate(collection, queryable); |
| 164 | + AssertStages( |
| 165 | + stages, |
| 166 | + "{ '$project' : { _id : '$_id', EnumAsInt : '$NullableEnum' } }"); |
| 167 | + |
| 168 | + var result = queryable.Single(); |
| 169 | + result.Id.Should().Be(1); |
| 170 | + result.EnumAsInt.Should().Be(2); |
| 171 | + } |
| 172 | + |
| 173 | + [Fact] |
| 174 | + public void Project_using_convert_nullable_enum_to_nullable_underlying_type_work() |
| 175 | + { |
| 176 | + var collection = GetCollection(); |
| 177 | + var queryable = collection.AsQueryable() |
| 178 | + .Select(p => new ProjectedModel |
| 179 | + { |
| 180 | + Id = p.Id, |
| 181 | + EnumAsNullableInt = (int)p.NullableEnum |
| 182 | + }); |
| 183 | + |
| 184 | + var stages = Translate(collection, queryable); |
| 185 | + AssertStages( |
| 186 | + stages, |
| 187 | + "{ '$project' : { _id : '$_id', EnumAsNullableInt : '$NullableEnum' } }"); |
| 188 | + |
| 189 | + var result = queryable.Single(); |
| 190 | + result.Id.Should().Be(1); |
| 191 | + result.EnumAsNullableInt.Should().Be(2); |
| 192 | + } |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | + private IMongoCollection<BaseClass> GetCollection() |
| 197 | + { |
| 198 | + var collection = GetCollection<BaseClass>("test"); |
| 199 | + CreateCollection(collection, new DerivedClass() |
| 200 | + { |
| 201 | + Id = 1, |
| 202 | + A = "abc", |
| 203 | + Enum = Enum.Two, |
| 204 | + NullableEnum = Enum.Two, |
| 205 | + EnumAsInt = 2, |
| 206 | + EnumAsNullableInt = 2 |
| 207 | + }); |
| 208 | + return collection; |
| 209 | + } |
| 210 | + |
| 211 | + private class BaseClass |
| 212 | + { |
| 213 | + public int Id { get; set; } |
| 214 | + public Enum Enum { get; set; } |
| 215 | + public Enum? NullableEnum { get; set; } |
| 216 | + public int EnumAsInt { get; set; } |
| 217 | + public int? EnumAsNullableInt { get; set; } |
| 218 | + } |
| 219 | + |
| 220 | + private class DerivedClass : BaseClass |
| 221 | + { |
| 222 | + public string A { get; set; } |
| 223 | + } |
| 224 | + |
| 225 | + private class ProjectedModel |
| 226 | + { |
| 227 | + public int Id { get; set; } |
| 228 | + public Enum Enum { get; set; } |
| 229 | + public Enum? NullableEnum { get; set; } |
| 230 | + public int EnumAsInt { get; set; } |
| 231 | + public int? EnumAsNullableInt { get; set; } |
| 232 | + public bool EnumComparisonResult { get; set; } |
| 233 | + } |
| 234 | + |
| 235 | + private enum Enum |
| 236 | + { |
| 237 | + One = 1, |
| 238 | + Two = 2 |
| 239 | + } |
| 240 | + } |
| 241 | +} |
0 commit comments