|
| 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.Translators.ExpressionToPipelineTranslators |
| 21 | +{ |
| 22 | + public class UnionMethodToPipelineTranslatorTests: Linq3IntegrationTest |
| 23 | + { |
| 24 | + private readonly IMongoCollection<Company> _firstCollection; |
| 25 | + private readonly IMongoCollection<Company> _secondCollection; |
| 26 | + |
| 27 | + public UnionMethodToPipelineTranslatorTests() |
| 28 | + { |
| 29 | + _firstCollection = CreateCollection("clients", |
| 30 | + new Company { Id = 1, Name = "first client" }, |
| 31 | + new Company { Id = 2, Name = "second client" }, |
| 32 | + new Company { Id = 3, Name = "third client" }); |
| 33 | + |
| 34 | + _secondCollection = CreateCollection("partners", |
| 35 | + new Company { Id = 4, Name = "partner" }, |
| 36 | + new Company { Id = 5, Name = "another partner" }); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public void Union_should_combine_collections() |
| 41 | + { |
| 42 | + var queryable = _firstCollection |
| 43 | + .AsQueryable() |
| 44 | + .Union(_secondCollection.AsQueryable()); |
| 45 | + |
| 46 | + var stages = Translate(_firstCollection, queryable); |
| 47 | + AssertStages(stages, "{ $unionWith : 'partners' }"); |
| 48 | + |
| 49 | + var results = queryable.ToList(); |
| 50 | + results.Select(x => x.Id).Should().BeEquivalentTo(new[] { 1, 2, 3, 4, 5 }); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public void Union_should_combine_collection_with_itself() |
| 55 | + { |
| 56 | + var queryable = _firstCollection |
| 57 | + .AsQueryable() |
| 58 | + .Union(_firstCollection.AsQueryable()); |
| 59 | + |
| 60 | + var stages = Translate(_firstCollection, queryable); |
| 61 | + AssertStages(stages, "{ $unionWith : 'clients' }"); |
| 62 | + |
| 63 | + var results = queryable.ToList(); |
| 64 | + results.Select(x => x.Id).Should().BeEquivalentTo(new[] { 1, 2, 3, 1, 2, 3 }); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public void Union_should_combine_filtered_collections() |
| 69 | + { |
| 70 | + var queryable = _firstCollection |
| 71 | + .AsQueryable() |
| 72 | + .Where(c => c.Name.StartsWith("second")) |
| 73 | + .Union(_secondCollection.AsQueryable().Where(c => c.Name.StartsWith("another"))); |
| 74 | + |
| 75 | + var stages = Translate(_firstCollection, queryable); |
| 76 | + AssertStages(stages, |
| 77 | + "{ $match : { Name : /^second/s } }", |
| 78 | + "{ $unionWith : { coll : 'partners', pipeline : [{ $match : { Name : /^another/s } }] } }"); |
| 79 | + |
| 80 | + var results = queryable.ToList(); |
| 81 | + results.Select(x => x.Id).Should().BeEquivalentTo(new[] { 2, 5 }); |
| 82 | + } |
| 83 | + |
| 84 | + [Fact] |
| 85 | + public void Union_should_support_projection() |
| 86 | + { |
| 87 | + var queryable = _firstCollection |
| 88 | + .AsQueryable() |
| 89 | + .Where(c => c.Name.StartsWith("second")) |
| 90 | + .Select(c => new ProjectedCompany { Number = c.Id }) |
| 91 | + .Union(_secondCollection.AsQueryable().Select(c => new ProjectedCompany { Number = c.Id })); |
| 92 | + |
| 93 | + var stages = Translate(_firstCollection, queryable); |
| 94 | + AssertStages(stages, |
| 95 | + "{ $match : { Name : /^second/s } }", |
| 96 | + "{ $project : { Number : '$_id', _id : 0 } }", |
| 97 | + "{ $unionWith : { coll : 'partners', pipeline : [{ $project : { Number : '$_id', _id : 0 } }] } }"); |
| 98 | + |
| 99 | + var results = queryable.ToList(); |
| 100 | + results.Select(x => x.Number).Should().BeEquivalentTo(new[] { 2, 4, 5 }); |
| 101 | + } |
| 102 | + |
| 103 | + [Fact] |
| 104 | + public void Union_should_support_projection_to_anonymous() |
| 105 | + { |
| 106 | + var queryable = _firstCollection |
| 107 | + .AsQueryable() |
| 108 | + .Where(c => c.Name.StartsWith("second")) |
| 109 | + .Select(c => new { Number = c.Id }) |
| 110 | + .Union(_secondCollection.AsQueryable().Select(c => new { Number = c.Id })); |
| 111 | + |
| 112 | + var stages = Translate(_firstCollection, queryable); |
| 113 | + AssertStages(stages, |
| 114 | + "{ $match : { Name : /^second/s } }", |
| 115 | + "{ $project : { Number : '$_id', _id : 0 } }", |
| 116 | + "{ $unionWith : { coll : 'partners', pipeline : [{ $project : { Number : '$_id', _id : 0 } }] } }"); |
| 117 | + |
| 118 | + var results = queryable.ToList(); |
| 119 | + results.Select(x => x.Number).Should().BeEquivalentTo(new[] { 2, 4, 5 }); |
| 120 | + } |
| 121 | + |
| 122 | + private IMongoCollection<Company> CreateCollection(string collectionName, params Company[] data) |
| 123 | + { |
| 124 | + var collection = GetCollection<Company>(collectionName); |
| 125 | + CreateCollection(collection, data); |
| 126 | + |
| 127 | + return collection; |
| 128 | + } |
| 129 | + |
| 130 | + public class Company |
| 131 | + { |
| 132 | + public int Id { get; set; } |
| 133 | + public string Name { get; set; } |
| 134 | + } |
| 135 | + |
| 136 | + public class ProjectedCompany |
| 137 | + { |
| 138 | + public int Number { get; set; } |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments