|
| 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.Collections.Generic; |
| 17 | +using System.Linq; |
| 18 | +using MongoDB.Driver.TestHelpers; |
| 19 | +using FluentAssertions; |
| 20 | +using MongoDB.Driver.Linq; |
| 21 | +using Xunit; |
| 22 | + |
| 23 | +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira; |
| 24 | + |
| 25 | +public class CSharp5463Tests : LinqIntegrationTest<CSharp5463Tests.ClassFixture> |
| 26 | +{ |
| 27 | + public CSharp5463Tests(ClassFixture fixture) |
| 28 | + : base(fixture) |
| 29 | + { |
| 30 | + } |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void Nested_AsQueryable_Contains_constant_should_be_partially_evaluated() |
| 34 | + { |
| 35 | + var collection = Fixture.Collection; |
| 36 | + var enumerable = new int[] { 1, 2, 3 }; |
| 37 | + |
| 38 | + var queryable = collection.AsQueryable() |
| 39 | + .Select(x => enumerable.AsQueryable().Contains(1)); |
| 40 | + |
| 41 | + var stages = Translate(collection, queryable); |
| 42 | + AssertStages(stages, "{ $project : { _v : { $in : [1, [1, 2, 3]] }, _id : 0 } }"); |
| 43 | + |
| 44 | + var result = queryable.Single(); |
| 45 | + result.Should().Be(true); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void NestedQueryable_Contains_constant_should_be_partially_evaluated() |
| 50 | + { |
| 51 | + var collection = Fixture.Collection; |
| 52 | + var nestedQueryable = new int[] { 1, 2, 3 }.AsQueryable(); |
| 53 | + |
| 54 | + var queryable = collection.AsQueryable() |
| 55 | + .Select(x => nestedQueryable.Contains(1)); // PartialEvaluator turns this into Select(x => true) |
| 56 | + |
| 57 | + var stages = Translate(collection, queryable); |
| 58 | + AssertStages(stages, "{ $project : { _v : { $literal : true }, _id : 0 } }"); |
| 59 | + |
| 60 | + var result = queryable.Single(); |
| 61 | + result.Should().Be(true); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void NestedQueryable_Contains_field_should_throw() |
| 66 | + { |
| 67 | + var collection = Fixture.Collection; |
| 68 | + var nestedQueryable = new int[] { 1, 2, 3 }.AsQueryable(); |
| 69 | + |
| 70 | + var queryable = collection.AsQueryable() |
| 71 | + .Select(x => nestedQueryable.Contains(x.Id)); |
| 72 | + |
| 73 | + var exception = Record.Exception(() => Translate(collection, queryable)); |
| 74 | + exception.Should().BeOfType<ExpressionNotSupportedException>(); |
| 75 | + exception.Message.Should().Contain("source argument is an unsupported IQueryable type"); |
| 76 | + } |
| 77 | + |
| 78 | + public class C |
| 79 | + { |
| 80 | + public int Id { get; set; } |
| 81 | + } |
| 82 | + |
| 83 | + public sealed class ClassFixture : MongoCollectionFixture<C> |
| 84 | + { |
| 85 | + protected override IEnumerable<C> InitialData => |
| 86 | + [ |
| 87 | + new C { Id = 1 } |
| 88 | + ]; |
| 89 | + } |
| 90 | +} |
0 commit comments