|
| 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 FluentAssertions; |
| 19 | +using MongoDB.Driver.Linq; |
| 20 | +using MongoDB.TestHelpers.XunitExtensions; |
| 21 | +using Xunit; |
| 22 | + |
| 23 | +namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira |
| 24 | +{ |
| 25 | + public class CSharp4861Tests : Linq3IntegrationTest |
| 26 | + { |
| 27 | + [Theory] |
| 28 | + [ParameterAttributeData] |
| 29 | + public void One_less_than_Count_should_work( |
| 30 | + [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 31 | + { |
| 32 | + var collection = GetCollection(linqProvider); |
| 33 | + |
| 34 | + var queryable = collection.AsQueryable() |
| 35 | + .Where(x => 1 < x.Set.Count); |
| 36 | + |
| 37 | + var stages = Translate(collection, queryable); |
| 38 | + AssertStages(stages, "{ $match : { 'Set.1' : { $exists : true } } }"); |
| 39 | + |
| 40 | + var results = queryable.ToList(); |
| 41 | + results.Select(x => x.Id).Should().Equal(2, 3); |
| 42 | + } |
| 43 | + |
| 44 | + [Theory] |
| 45 | + [ParameterAttributeData] |
| 46 | + public void One_less_than_Length_should_work( |
| 47 | + [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 48 | + { |
| 49 | + var collection = GetCollection(linqProvider); |
| 50 | + |
| 51 | + var queryable = collection.AsQueryable() |
| 52 | + .Where(x => 1 < x.Array.Length); |
| 53 | + |
| 54 | + var stages = Translate(collection, queryable); |
| 55 | + AssertStages(stages, "{ $match : { 'Array.1' : { $exists : true } } }"); |
| 56 | + |
| 57 | + var results = queryable.ToList(); |
| 58 | + results.Select(x => x.Id).Should().Equal(2, 3); |
| 59 | + } |
| 60 | + |
| 61 | + [Theory] |
| 62 | + [ParameterAttributeData] |
| 63 | + public void Count_greater_than_one_should_work( |
| 64 | + [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 65 | + { |
| 66 | + var collection = GetCollection(linqProvider); |
| 67 | + |
| 68 | + var queryable = collection.AsQueryable() |
| 69 | + .Where(x => x.Set.Count > 1); |
| 70 | + |
| 71 | + var stages = Translate(collection, queryable); |
| 72 | + AssertStages(stages, "{ $match : { 'Set.1' : { $exists : true } } }"); |
| 73 | + |
| 74 | + var results = queryable.ToList(); |
| 75 | + results.Select(x => x.Id).Should().Equal(2, 3); |
| 76 | + } |
| 77 | + |
| 78 | + [Theory] |
| 79 | + [ParameterAttributeData] |
| 80 | + public void Length_greater_than_one_should_work( |
| 81 | + [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 82 | + { |
| 83 | + var collection = GetCollection(linqProvider); |
| 84 | + |
| 85 | + var queryable = collection.AsQueryable() |
| 86 | + .Where(x => x.Array.Length > 1); |
| 87 | + |
| 88 | + var stages = Translate(collection, queryable); |
| 89 | + AssertStages(stages, "{ $match : { 'Array.1' : { $exists : true } } }"); |
| 90 | + |
| 91 | + var results = queryable.ToList(); |
| 92 | + results.Select(x => x.Id).Should().Equal(2, 3); |
| 93 | + } |
| 94 | + |
| 95 | + private IMongoCollection<C> GetCollection(LinqProvider linqProvider) |
| 96 | + { |
| 97 | + var collection = GetCollection<C>("test", linqProvider); |
| 98 | + CreateCollection( |
| 99 | + collection, |
| 100 | + new C { Id = 1, Array = new[] { 1 }, Set = new HashSet<int> { 1 } }, |
| 101 | + new C { Id = 2, Array = new[] { 1, 2 }, Set = new HashSet<int> { 1, 2 } }, |
| 102 | + new C { Id = 3, Array = new[] { 1, 2, 3 }, Set = new HashSet<int> { 1, 2, 3 } }); |
| 103 | + return collection; |
| 104 | + } |
| 105 | + |
| 106 | + private class C |
| 107 | + { |
| 108 | + public int Id { get; set; } |
| 109 | + public int[] Array { get; set; } |
| 110 | + public HashSet<int> Set { get; set; } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments