|
| 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 System.Linq; |
| 18 | +using Xunit; |
| 19 | + |
| 20 | +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators.MethodTranslators |
| 21 | +{ |
| 22 | + public class StringConcatMethodToAggregationExpressionTranslatorTests : Linq3IntegrationTest |
| 23 | + { |
| 24 | + [Fact] |
| 25 | + public void Filter_using_string_concat_with_two_strings_should_work() |
| 26 | + { |
| 27 | + var collection = CreateCollection(); |
| 28 | + |
| 29 | + var queryable = collection.AsQueryable() |
| 30 | + .Where(i => string.Concat(i.A, ";") == "A1;"); |
| 31 | + |
| 32 | + var stages = Translate(collection, queryable); |
| 33 | + AssertStages( |
| 34 | + stages, |
| 35 | + "{ $match : { $expr : { $eq : [{ $concat : ['$A', ';'] }, 'A1;'] } } }"); |
| 36 | + |
| 37 | + var result = queryable.Single(); |
| 38 | + result.Id.Should().Be(1); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void Projection_using_string_concat_with_two_strings_should_work() |
| 43 | + { |
| 44 | + var collection = CreateCollection(); |
| 45 | + |
| 46 | + var queryable = collection.AsQueryable() |
| 47 | + .Where(i => i.Id == 1) |
| 48 | + .Select(i => new { T = string.Concat(i.A, ";") }); |
| 49 | + |
| 50 | + var stages = Translate(collection, queryable); |
| 51 | + AssertStages( |
| 52 | + stages, |
| 53 | + "{ $match : { _id : 1 } }", |
| 54 | + "{ $project : { T : { $concat : ['$A', ';'] }, _id : 0 } }"); |
| 55 | + |
| 56 | + var result = queryable.Single(); |
| 57 | + result.T.Should().Be("A1;"); |
| 58 | + } |
| 59 | + |
| 60 | + [Fact] |
| 61 | + public void Filter_using_string_concat_with_three_strings_should_work() |
| 62 | + { |
| 63 | + var collection = CreateCollection(); |
| 64 | + |
| 65 | + var queryable = collection.AsQueryable() |
| 66 | + .Where(i => string.Concat(i.A, ";", i.B) == "A1;B1"); |
| 67 | + |
| 68 | + var stages = Translate(collection, queryable); |
| 69 | + AssertStages( |
| 70 | + stages, |
| 71 | + "{ $match : { $expr : { $eq : [{ $concat : ['$A', ';', '$B'] }, 'A1;B1'] } } }"); |
| 72 | + |
| 73 | + var result = queryable.Single(); |
| 74 | + result.Id.Should().Be(1); |
| 75 | + } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + public void Projection_using_string_concat_with_three_strings_should_work() |
| 79 | + { |
| 80 | + var collection = CreateCollection(); |
| 81 | + |
| 82 | + var queryable = collection.AsQueryable() |
| 83 | + .Where(i => i.Id == 1) |
| 84 | + .Select(i => new { T = string.Concat(i.A, ";", i.B) }); |
| 85 | + |
| 86 | + var stages = Translate(collection, queryable); |
| 87 | + AssertStages( |
| 88 | + stages, |
| 89 | + "{ $match : { _id : 1 } }", |
| 90 | + "{ $project : { T : { $concat : ['$A', ';', '$B'] }, _id : 0 } }"); |
| 91 | + |
| 92 | + var result = queryable.Single(); |
| 93 | + result.T.Should().Be("A1;B1"); |
| 94 | + } |
| 95 | + |
| 96 | + [Fact] |
| 97 | + public void Filter_using_string_concat_with_four_strings_should_work() |
| 98 | + { |
| 99 | + var collection = CreateCollection(); |
| 100 | + |
| 101 | + var queryable = collection.AsQueryable() |
| 102 | + .Where(i => string.Concat(i.A, ";", i.B, i.C) == "A1;B1C1"); |
| 103 | + |
| 104 | + var stages = Translate(collection, queryable); |
| 105 | + AssertStages( |
| 106 | + stages, |
| 107 | + "{ $match : { $expr : { $eq : [{ $concat : ['$A', ';', '$B', '$C'] }, 'A1;B1C1'] } } }"); |
| 108 | + |
| 109 | + var result = queryable.Single(); |
| 110 | + result.Id.Should().Be(1); |
| 111 | + } |
| 112 | + |
| 113 | + [Fact] |
| 114 | + public void Projection_using_string_concat_with_four_strings_should_work() |
| 115 | + { |
| 116 | + var collection = CreateCollection(); |
| 117 | + |
| 118 | + var queryable = collection.AsQueryable() |
| 119 | + .Where(i => i.Id == 1) |
| 120 | + .Select(i => new { T = string.Concat(i.A, ";", i.B, i.C) }); |
| 121 | + |
| 122 | + var stages = Translate(collection, queryable); |
| 123 | + AssertStages( |
| 124 | + stages, |
| 125 | + "{ $match : { _id : 1 } }", |
| 126 | + "{ $project : { T : { $concat : ['$A', ';', '$B', '$C'] }, _id : 0 } }"); |
| 127 | + |
| 128 | + var result = queryable.Single(); |
| 129 | + result.T.Should().Be("A1;B1C1"); |
| 130 | + } |
| 131 | + |
| 132 | + [Fact] |
| 133 | + public void Filter_using_string_concat_with_params_array_should_work() |
| 134 | + { |
| 135 | + var collection = CreateCollection(); |
| 136 | + var queryable = collection.AsQueryable() |
| 137 | + .Where(i => string.Concat(i.A, ";", i.B, ";", i.C) == "A1;B1;C1"); |
| 138 | + |
| 139 | + var stages = Translate(collection, queryable); |
| 140 | + AssertStages( |
| 141 | + stages, |
| 142 | + "{ $match : { $expr : { $eq : [{ $concat : ['$A', ';', '$B', ';', '$C'] }, 'A1;B1;C1'] } } }"); |
| 143 | + |
| 144 | + var result = queryable.Single(); |
| 145 | + result.Id.Should().Be(1); |
| 146 | + } |
| 147 | + |
| 148 | + [Fact] |
| 149 | + public void Projection_using_string_concat_with_params_array_should_work() |
| 150 | + { |
| 151 | + var collection = CreateCollection(); |
| 152 | + |
| 153 | + var queryable = collection.AsQueryable() |
| 154 | + .Where(i => i.Id == 1) |
| 155 | + .Select(i => new { T = string.Concat(i.A, ";", i.B, ";", i.C) }); |
| 156 | + |
| 157 | + var stages = Translate(collection, queryable); |
| 158 | + AssertStages( |
| 159 | + stages, |
| 160 | + "{ $match : { '_id' : 1 } }", |
| 161 | + "{ $project : { T : { $concat : ['$A', ';', '$B', ';', '$C'] }, _id : 0 } }"); |
| 162 | + |
| 163 | + var result = queryable.Single(); |
| 164 | + result.T.Should().Be("A1;B1;C1"); |
| 165 | + } |
| 166 | + |
| 167 | + private IMongoCollection<Data> CreateCollection() |
| 168 | + { |
| 169 | + var collection = GetCollection<Data>("test"); |
| 170 | + CreateCollection( |
| 171 | + collection, |
| 172 | + new Data { Id = 1, A = "A1", B = "B1", C = "C1", D="D1" }, |
| 173 | + new Data { Id = 2, A = "A2", B = "B2", C = "C2", D="D2" }); |
| 174 | + return collection; |
| 175 | + } |
| 176 | + |
| 177 | + private class Data |
| 178 | + { |
| 179 | + public int Id { get; set; } |
| 180 | + public string A { get; set; } |
| 181 | + public string B { get; set; } |
| 182 | + public string C { get; set; } |
| 183 | + public string D { get; set; } |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments