|
| 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.Linq3ImplementationTests.Jira |
| 21 | +{ |
| 22 | + public class CSharp3713Tests |
| 23 | + { |
| 24 | + [Fact] |
| 25 | + public void DefaultIfEmpty_should_work() |
| 26 | + { |
| 27 | + var client = DriverTestConfiguration.Linq3Client; |
| 28 | + var database = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName); |
| 29 | + var collection = database.GetCollection<C>(DriverTestConfiguration.CollectionNamespace.CollectionName); |
| 30 | + var subject = collection.AsQueryable(); |
| 31 | + |
| 32 | + database.DropCollection(collection.CollectionNamespace.CollectionName); |
| 33 | + collection.InsertMany(new[] { |
| 34 | + new C { Id = 1, InnerArray = new A[0] }, |
| 35 | + new C { Id = 2, InnerArray = new[] { new A { S = "abc" } } } |
| 36 | + }); |
| 37 | + |
| 38 | + var queryable = subject.SelectMany(outerObject => outerObject.InnerArray.DefaultIfEmpty(), (o, a) => new { o, a }); |
| 39 | + |
| 40 | + var stages = Linq3TestHelpers.Translate(collection, queryable); |
| 41 | + var expectedStages = new[] |
| 42 | + { |
| 43 | + "{ $project : { _v : { $map : { input : { $let : { vars : { source : '$InnerArray' }, in : { $cond : { if : { $eq : [{ $size : '$$source' }, 0] }, then : [{ S : null }], else : '$$source' } } } }, as : 'a', in : { o : '$$ROOT', a : '$$a' } } }, _id : 0 } }", |
| 44 | + "{ $unwind : '$_v' }" |
| 45 | + }; |
| 46 | + Linq3TestHelpers.AssertStages(stages, expectedStages); |
| 47 | + |
| 48 | + var result = queryable.ToList(); |
| 49 | + result.Count.Should().Be(2); |
| 50 | + result[0].o.Id.Should().Be(1); |
| 51 | + result[0].a.S.Should().Be(null); |
| 52 | + result[1].o.Id.Should().Be(2); |
| 53 | + result[1].a.S.Should().Be("abc"); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void DefaultIfEmpty_with_explicit_default_should_work() |
| 58 | + { |
| 59 | + var client = DriverTestConfiguration.Linq3Client; |
| 60 | + var database = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName); |
| 61 | + var collection = database.GetCollection<C>(DriverTestConfiguration.CollectionNamespace.CollectionName); |
| 62 | + var subject = collection.AsQueryable(); |
| 63 | + |
| 64 | + database.DropCollection(collection.CollectionNamespace.CollectionName); |
| 65 | + collection.InsertMany(new[] { |
| 66 | + new C { Id = 1, InnerArray = new A[0] }, |
| 67 | + new C { Id = 2, InnerArray = new[] { new A { S = "abc" } } } |
| 68 | + }); |
| 69 | + |
| 70 | + var defaultValue = new A { S = "default" }; |
| 71 | + var queryable = subject.SelectMany(outerObject => outerObject.InnerArray.DefaultIfEmpty(defaultValue), (o, a) => new { o, a }); |
| 72 | + |
| 73 | + var stages = Linq3TestHelpers.Translate(collection, queryable); |
| 74 | + var expectedStages = new[] |
| 75 | + { |
| 76 | + "{ $project : { _v : { $map : { input : { $let : { vars : { source : '$InnerArray' }, in : { $cond : { if : { $eq : [{ $size : '$$source' }, 0] }, then : [{ S : 'default' }], else : '$$source' } } } }, as : 'a', in : { o : '$$ROOT', a : '$$a' } } }, _id : 0 } }", |
| 77 | + "{ $unwind : '$_v' }" |
| 78 | + }; |
| 79 | + Linq3TestHelpers.AssertStages(stages, expectedStages); |
| 80 | + |
| 81 | + var result = queryable.ToList(); |
| 82 | + result.Count.Should().Be(2); |
| 83 | + result[0].o.Id.Should().Be(1); |
| 84 | + result[0].a.S.Should().Be("default"); |
| 85 | + result[1].o.Id.Should().Be(2); |
| 86 | + result[1].a.S.Should().Be("abc"); |
| 87 | + } |
| 88 | + |
| 89 | + private class C |
| 90 | + { |
| 91 | + public int Id { get; set; } |
| 92 | + public A[] InnerArray { get; set; } |
| 93 | + } |
| 94 | + |
| 95 | + private class A |
| 96 | + { |
| 97 | + public string S { get; set; } |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments