|
| 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; |
| 17 | +using System.Linq; |
| 18 | +using System.Linq.Expressions; |
| 19 | +using FluentAssertions; |
| 20 | +using MongoDB.Driver.Linq; |
| 21 | +using MongoDB.TestHelpers.XunitExtensions; |
| 22 | +using Xunit; |
| 23 | + |
| 24 | +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira |
| 25 | +{ |
| 26 | + public class CSharp4937Tests : Linq3IntegrationTest |
| 27 | + { |
| 28 | + [Theory] |
| 29 | + [InlineData(2, "<" , 0, LinqProvider.V2, "{ _id : { $lt : 2 } }", new int[] { 1 })] |
| 30 | + [InlineData(2, "<=", 0, LinqProvider.V2, "{ _id : { $lte : 2 } }", new int[] { 1, 2 })] |
| 31 | + [InlineData(2, "==", 0, LinqProvider.V2, "{ _id : 2 }", new int[] { 2 })] |
| 32 | + [InlineData(2, "!=", 0, LinqProvider.V2, "{ _id : { $ne : 2 } }", new int[] { 1, 3 })] |
| 33 | + [InlineData(2, ">=", 0, LinqProvider.V2, "{ _id : { $gte : 2 } }", new int[] { 2, 3 })] |
| 34 | + [InlineData(2, ">" , 0, LinqProvider.V2, "{ _id : { $gt : 2 } }", new int[] { 3 })] |
| 35 | + [InlineData(2, "<" , 0, LinqProvider.V3, "{ _id : { $lt : 2 } }", new int[] { 1 })] |
| 36 | + [InlineData(2, "<=", 0, LinqProvider.V3, "{ _id : { $lte : 2 } }", new int[] { 1, 2 })] |
| 37 | + [InlineData(2, "==", 0, LinqProvider.V3, "{ _id : 2 }", new int[] { 2 })] |
| 38 | + [InlineData(2, "!=", 0, LinqProvider.V3, "{ _id : { $ne : 2 } }", new int[] { 1, 3 })] |
| 39 | + [InlineData(2, ">=", 0, LinqProvider.V3, "{ _id : { $gte : 2 } }", new int[] { 2, 3 })] |
| 40 | + [InlineData(2, ">" , 0, LinqProvider.V3, "{ _id : { $gt : 2 } }", new int[] { 3 })] |
| 41 | + public void CompareTo_filter_should_work( |
| 42 | + int comparand, |
| 43 | + string comparisonOperator, |
| 44 | + int compareToResult, |
| 45 | + LinqProvider linqProvider, |
| 46 | + string expectedFilter, |
| 47 | + int[] expectedIds) |
| 48 | + { |
| 49 | + var collection = GetCollection(linqProvider); |
| 50 | + |
| 51 | + Expression<Func<C, bool>> predicate = comparisonOperator switch |
| 52 | + { |
| 53 | + "<" => x => x.Id.CompareTo(comparand) < compareToResult, |
| 54 | + "<=" => x => x.Id.CompareTo(comparand) <= compareToResult, |
| 55 | + "==" => x => x.Id.CompareTo(comparand) == compareToResult, |
| 56 | + "!=" => x => x.Id.CompareTo(comparand) != compareToResult, |
| 57 | + ">=" => x => x.Id.CompareTo(comparand) >= compareToResult, |
| 58 | + ">" => x => x.Id.CompareTo(comparand) > compareToResult, |
| 59 | + _ => throw new InvalidOperationException() |
| 60 | + }; |
| 61 | + |
| 62 | + Implementation(collection, comparand, predicate); |
| 63 | + |
| 64 | + void Implementation<TModel, TId>(IMongoCollection<TModel> collection, TId _, Expression<Func<TModel, bool>> predicate) |
| 65 | + where TModel : IIdentity<TId> |
| 66 | + where TId : IComparable<TId> |
| 67 | + { |
| 68 | + var queryable = collection.AsQueryable() |
| 69 | + .Where(predicate); |
| 70 | + |
| 71 | + var stages = Translate(collection, queryable); |
| 72 | + var expectedStage = "{ $match : <filter> }".Replace("<filter>", expectedFilter); |
| 73 | + AssertStages(stages, expectedStage); |
| 74 | + |
| 75 | + var results = queryable.ToList(); |
| 76 | + results.Select(x => x.Id).Should().Equal(expectedIds); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + [Theory] |
| 81 | + [InlineData(2, "<", 0, LinqProvider.V2, "__fld0 : { $lt : [{ $cmp : ['$_id', 2] }, 0] }", new bool[] { true, false, false })] |
| 82 | + [InlineData(2, "<=", 0, LinqProvider.V2, "__fld0 : { $lte : [{ $cmp : ['$_id', 2] }, 0] }", new bool[] { true, true, false })] |
| 83 | + [InlineData(2, "==", 0, LinqProvider.V2, "__fld0 : { $eq : [{ $cmp : ['$_id', 2] }, 0] }", new bool[] { false, true, false })] |
| 84 | + [InlineData(2, "!=", 0, LinqProvider.V2, "__fld0 : { $ne : [{ $cmp : ['$_id', 2] }, 0] }", new bool[] { true, false, true })] |
| 85 | + [InlineData(2, ">=", 0, LinqProvider.V2, "__fld0 : { $gte : [{ $cmp : ['$_id', 2] }, 0] }", new bool[] { false, true, true})] |
| 86 | + [InlineData(2, ">", 0, LinqProvider.V2, "__fld0 : { $gt : [{ $cmp : ['$_id', 2] }, 0] }", new bool[] { false, false, true })] |
| 87 | + [InlineData(2, "<", 0, LinqProvider.V3, "_v : { $lt : [{ $cmp : ['$_id', 2] }, 0] }", new bool[] { true, false, false })] |
| 88 | + [InlineData(2, "<=", 0, LinqProvider.V3, "_v : { $lte : [{ $cmp : ['$_id', 2] }, 0] }", new bool[] { true, true, false })] |
| 89 | + [InlineData(2, "==", 0, LinqProvider.V3, "_v : { $eq : [{ $cmp : ['$_id', 2] }, 0] }", new bool[] { false, true, false })] |
| 90 | + [InlineData(2, "!=", 0, LinqProvider.V3, "_v : { $ne : [{ $cmp : ['$_id', 2] }, 0] }", new bool[] { true, false, true })] |
| 91 | + [InlineData(2, ">=", 0, LinqProvider.V3, "_v : { $gte : [{ $cmp : ['$_id', 2] }, 0] }", new bool[] { false, true, true })] |
| 92 | + [InlineData(2, ">", 0, LinqProvider.V3, "_v : { $gt : [{ $cmp : ['$_id', 2] }, 0] }", new bool[] { false, false, true })] |
| 93 | + public void CompareTo_expression_should_work( |
| 94 | + int comparand, |
| 95 | + string comparisonOperator, |
| 96 | + int compareToResult, |
| 97 | + LinqProvider linqProvider, |
| 98 | + string expectedProjection, |
| 99 | + bool[] expectedResults) |
| 100 | + { |
| 101 | + var collection = GetCollection(linqProvider); |
| 102 | + |
| 103 | + Expression<Func<C, bool>> selector = comparisonOperator switch |
| 104 | + { |
| 105 | + "<" => x => x.Id.CompareTo(comparand) < compareToResult, |
| 106 | + "<=" => x => x.Id.CompareTo(comparand) <= compareToResult, |
| 107 | + "==" => x => x.Id.CompareTo(comparand) == compareToResult, |
| 108 | + "!=" => x => x.Id.CompareTo(comparand) != compareToResult, |
| 109 | + ">=" => x => x.Id.CompareTo(comparand) >= compareToResult, |
| 110 | + ">" => x => x.Id.CompareTo(comparand) > compareToResult, |
| 111 | + _ => throw new InvalidOperationException() |
| 112 | + }; |
| 113 | + |
| 114 | + Implementation(collection, comparand, selector); |
| 115 | + |
| 116 | + void Implementation<TModel, TId>(IMongoCollection<TModel> collection, TId _, Expression<Func<TModel, bool>> selector) |
| 117 | + where TModel : IIdentity<TId> |
| 118 | + where TId : IComparable<TId> |
| 119 | + { |
| 120 | + var queryable = collection.AsQueryable() |
| 121 | + .Select(selector); |
| 122 | + |
| 123 | + var stages = Translate(collection, queryable); |
| 124 | + var fieldName = linqProvider == LinqProvider.V2 ? "__fld0" : "_v"; |
| 125 | + var expectedStage = "{ $project : { <projection>, _id : 0 } }".Replace("<projection>", expectedProjection); |
| 126 | + AssertStages(stages, expectedStage); |
| 127 | + |
| 128 | + var results = queryable.ToList(); |
| 129 | + results.Should().Equal(expectedResults); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private IMongoCollection<C> GetCollection(LinqProvider linqProvider) |
| 134 | + { |
| 135 | + var collection = GetCollection<C>("test", linqProvider); |
| 136 | + CreateCollection( |
| 137 | + collection, |
| 138 | + new C { Id = 1 }, |
| 139 | + new C { Id = 2 }, |
| 140 | + new C { Id = 3 }); |
| 141 | + return collection; |
| 142 | + } |
| 143 | + |
| 144 | + public interface IIdentity<TId> |
| 145 | + { |
| 146 | + public TId Id { get; set; } |
| 147 | + } |
| 148 | + |
| 149 | + private class C : IIdentity<int> |
| 150 | + { |
| 151 | + public int Id { get; set; } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments