|
| 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 | +#if NET10_0_OR_GREATER |
| 17 | + |
| 18 | +using System; |
| 19 | +using System.Linq; |
| 20 | +using FluentAssertions; |
| 21 | +using MongoDB.Driver.Core.Configuration; |
| 22 | +using MongoDB.Driver.Linq; |
| 23 | +using Xunit; |
| 24 | + |
| 25 | +namespace MongoDB.Driver.SmokeTests.Sdk |
| 26 | +{ |
| 27 | + [Trait("Category", "Integration")] |
| 28 | + public class LeftJoinTests |
| 29 | + { |
| 30 | + [Fact] |
| 31 | + public void LeftJoin_returns_matched_and_unmatched_outer_elements() |
| 32 | + { |
| 33 | + var client = new MongoClient(InfrastructureUtilities.MongoUri); |
| 34 | + var database = client.GetDatabase("leftjoin_smoke_" + Guid.NewGuid().ToString("N")); |
| 35 | + |
| 36 | + try |
| 37 | + { |
| 38 | + var orders = database.GetCollection<Order>("orders"); |
| 39 | + var customers = database.GetCollection<Customer>("customers"); |
| 40 | + |
| 41 | + orders.InsertMany(new[] |
| 42 | + { |
| 43 | + new Order { Id = 1, CustomerId = 10 }, |
| 44 | + new Order { Id = 2, CustomerId = 20 }, |
| 45 | + new Order { Id = 3, CustomerId = 99 } |
| 46 | + }); |
| 47 | + |
| 48 | + customers.InsertMany(new[] |
| 49 | + { |
| 50 | + new Customer { Id = 10, Name = "Alice" }, |
| 51 | + new Customer { Id = 20, Name = "Bob" } |
| 52 | + }); |
| 53 | + |
| 54 | + var results = orders.AsQueryable() |
| 55 | + .LeftJoin( |
| 56 | + customers, |
| 57 | + o => o.CustomerId, |
| 58 | + c => c.Id, |
| 59 | + (o, c) => new LeftJoinResult<Order, Customer> { Outer = o, Inner = c }) |
| 60 | + .OrderBy(r => r.Outer.Id) |
| 61 | + .ToList(); |
| 62 | + |
| 63 | + results.Should().HaveCount(3); |
| 64 | + results[0].Inner.Name.Should().Be("Alice"); |
| 65 | + results[1].Inner.Name.Should().Be("Bob"); |
| 66 | + results[2].Inner.Should().BeNull(); |
| 67 | + } |
| 68 | + finally |
| 69 | + { |
| 70 | + client.DropDatabase(database.DatabaseNamespace.DatabaseName); |
| 71 | + ClusterRegistry.Instance.UnregisterAndDisposeCluster(client.Cluster); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private class Order |
| 76 | + { |
| 77 | + public int Id { get; set; } |
| 78 | + public int CustomerId { get; set; } |
| 79 | + } |
| 80 | + |
| 81 | + private class Customer |
| 82 | + { |
| 83 | + public int Id { get; set; } |
| 84 | + public string Name { get; set; } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +#endif |
0 commit comments