|
| 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 System.Linq; |
| 17 | +using FluentAssertions; |
| 18 | +using Xunit; |
| 19 | + |
| 20 | +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira |
| 21 | +{ |
| 22 | + public class CSharp4878Tests : Linq3IntegrationTest |
| 23 | + { |
| 24 | + [Fact] |
| 25 | + public void Repeat_with_constant_and_constant_should_work() |
| 26 | + { |
| 27 | + var collection = GetCollection(); |
| 28 | + |
| 29 | + var queryable = collection.AsQueryable().Select(x => Enumerable.Repeat(2, 3)); |
| 30 | + |
| 31 | + var stages = Translate(collection, queryable); |
| 32 | + AssertStages(stages, "{ $project : { _v : [2, 2, 2], _id : 0 } }"); |
| 33 | + |
| 34 | + var result = queryable.First(); |
| 35 | + result.Should().Equal(2, 2, 2); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public void Repeat_with_constant_and_expression_should_work() |
| 40 | + { |
| 41 | + var collection = GetCollection(); |
| 42 | + |
| 43 | + var queryable = collection.AsQueryable().Select(x => Enumerable.Repeat(2, x.Y)); |
| 44 | + |
| 45 | + var stages = Translate(collection, queryable); |
| 46 | + AssertStages(stages, "{ $project : { _v : { $map : { input : { $range : [0, '$Y'] }, as : 'i', in : 2 } }, _id : 0 } }"); |
| 47 | + |
| 48 | + var result = queryable.First(); |
| 49 | + result.Should().Equal(2, 2, 2); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public void Repeat_with_expression_and_constant_should_work() |
| 54 | + { |
| 55 | + var collection = GetCollection(); |
| 56 | + |
| 57 | + var queryable = collection.AsQueryable().Select(x => Enumerable.Repeat(x.X, 3)); |
| 58 | + |
| 59 | + var stages = Translate(collection, queryable); |
| 60 | + AssertStages(stages, "{ $project : { _v : { $map : { input : { $range : [0, 3] }, as : 'i', in : '$X' } }, _id : 0 } }"); |
| 61 | + |
| 62 | + var result = queryable.First(); |
| 63 | + result.Should().Equal(2, 2, 2); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public void Repeat_with_expression_and_expression_should_work() |
| 68 | + { |
| 69 | + var collection = GetCollection(); |
| 70 | + |
| 71 | + var queryable = collection.AsQueryable().Select(x => Enumerable.Repeat(x.X, x.Y)); |
| 72 | + |
| 73 | + var stages = Translate(collection, queryable); |
| 74 | + AssertStages(stages, "{ $project : { _v : { $map : { input : { $range : [0, '$Y'] }, as : 'i', in : '$X' } }, _id : 0 } }"); |
| 75 | + |
| 76 | + var result = queryable.First(); |
| 77 | + result.Should().Equal(2, 2, 2); |
| 78 | + } |
| 79 | + |
| 80 | + private IMongoCollection<C> GetCollection() |
| 81 | + { |
| 82 | + var collection = GetCollection<C>("test"); |
| 83 | + CreateCollection( |
| 84 | + collection, |
| 85 | + new C { Id = 1, X = 2, Y = 3 }); |
| 86 | + return collection; |
| 87 | + } |
| 88 | + |
| 89 | + private class C |
| 90 | + { |
| 91 | + public int Id { get; set; } |
| 92 | + public int X { get; set; } |
| 93 | + public int Y { get; set; } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments