|
| 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 MongoDB.Driver.Linq; |
| 19 | +using Xunit; |
| 20 | + |
| 21 | +namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira |
| 22 | +{ |
| 23 | + public class CSharp2113Tests : Linq3IntegrationTest |
| 24 | + { |
| 25 | + [Fact] |
| 26 | + public void Query1_should_work() |
| 27 | + { |
| 28 | + var collection = CreateDocumentCollection(); |
| 29 | + |
| 30 | + var queryable = collection |
| 31 | + .AsQueryable() |
| 32 | + .Where(x => x.X == 1); |
| 33 | + |
| 34 | + var stages = Translate(collection, queryable); |
| 35 | + AssertStages(stages, "{ $match : { X : 1 } }"); |
| 36 | + |
| 37 | + var results = queryable.ToList(); |
| 38 | + results.Select(x => x.Id).Should().Equal(1); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void Query2_should_work() |
| 43 | + { |
| 44 | + var collection = CreateDocumentCollection(); |
| 45 | + |
| 46 | + var queryable = collection |
| 47 | + .AsQueryable() |
| 48 | + .Where(x => x.Inner.Y == 1); |
| 49 | + |
| 50 | + var stages = Translate(collection, queryable); |
| 51 | + AssertStages(stages, "{ $match : { 'Inner.Y' : 1 } }"); |
| 52 | + |
| 53 | + var results = queryable.ToList(); |
| 54 | + results.Select(x => x.Id).Should().Equal(1); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void Query3_should_work() |
| 59 | + { |
| 60 | + var collection = CreateIDocumentCollection(); |
| 61 | + |
| 62 | + var queryable = collection |
| 63 | + .AsQueryable() |
| 64 | + .Where(x => x.X == 1); |
| 65 | + |
| 66 | + var stages = Translate(collection, queryable); |
| 67 | + AssertStages(stages, "{ $match : { X : 1 } }"); |
| 68 | + |
| 69 | + var results = queryable.ToList(); |
| 70 | + results.Select(x => x.Id).Should().Equal(1); |
| 71 | + } |
| 72 | + |
| 73 | + [Fact] |
| 74 | + public void Query4_should_work() |
| 75 | + { |
| 76 | + var collection = CreateIDocumentCollection(); |
| 77 | + |
| 78 | + var queryable = collection |
| 79 | + .AsQueryable() |
| 80 | + .Where(x => x.Inner.Y == 1); |
| 81 | + |
| 82 | + var stages = Translate(collection, queryable); |
| 83 | + AssertStages(stages, "{ $match : { 'Inner.Y' : 1 } }"); |
| 84 | + |
| 85 | + var results = queryable.ToList(); |
| 86 | + results.Select(x => x.Id).Should().Equal(1); |
| 87 | + } |
| 88 | + |
| 89 | + private IMongoCollection<Document> CreateDocumentCollection() |
| 90 | + { |
| 91 | + var collection = GetCollection<Document>(); |
| 92 | + |
| 93 | + CreateCollection( |
| 94 | + collection, |
| 95 | + new Document { Id = 1, X = 1, Inner = new Inner { Y = 1 } }, |
| 96 | + new Document { Id = 2, X = 2, Inner = new Inner { Y = 2 } }); |
| 97 | + |
| 98 | + return collection; |
| 99 | + } |
| 100 | + |
| 101 | + private IMongoCollection<IDocument> CreateIDocumentCollection() |
| 102 | + { |
| 103 | + var collection = GetCollection<IDocument>(); |
| 104 | + |
| 105 | + CreateCollection( |
| 106 | + collection, |
| 107 | + new DocumentWithInterface { Id = 1, X = 1, Inner = new InnerWithInterface { Y = 1 } }, |
| 108 | + new DocumentWithInterface { Id = 2, X = 2, Inner = new InnerWithInterface { Y = 2 } }); |
| 109 | + |
| 110 | + return collection; |
| 111 | + } |
| 112 | + |
| 113 | + private class Document |
| 114 | + { |
| 115 | + public int Id { get; set; } |
| 116 | + public int X { get; set; } |
| 117 | + public Inner Inner { get; set; } |
| 118 | + } |
| 119 | + |
| 120 | + private class Inner |
| 121 | + { |
| 122 | + public int Y { get; set; } |
| 123 | + } |
| 124 | + |
| 125 | + private interface IDocument |
| 126 | + { |
| 127 | + int Id { get; set; } |
| 128 | + int X { get; set; } |
| 129 | + IInner Inner { get; set; } |
| 130 | + } |
| 131 | + |
| 132 | + private interface IInner |
| 133 | + { |
| 134 | + int Y { get; set; } |
| 135 | + } |
| 136 | + |
| 137 | + private class DocumentWithInterface : IDocument |
| 138 | + { |
| 139 | + public int Id { get; set; } |
| 140 | + public int X { get; set; } |
| 141 | + public IInner Inner { get; set; } |
| 142 | + } |
| 143 | + |
| 144 | + public class InnerWithInterface : IInner |
| 145 | + { |
| 146 | + public int Y { get; set; } |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments