|
| 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.Bson; |
| 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 CSharp702Tests : Linq3IntegrationTest |
| 27 | + { |
| 28 | + [Theory] |
| 29 | + [ParameterAttributeData] |
| 30 | + public void Query1_using_list_should_work( |
| 31 | + [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 32 | + { |
| 33 | + var collection = GetCollection(linqProvider); |
| 34 | + var lookingFor = new List<string> { "value1", "value2" }; |
| 35 | + |
| 36 | + var queryable = collection.AsQueryable() |
| 37 | + .Where(model => lookingFor.Any(value => model.List.Contains(value))); |
| 38 | + |
| 39 | + var stages = Translate(collection, queryable); |
| 40 | + AssertStages(stages, "{ $match : { List : { $in : ['value1', 'value2'] } } }"); |
| 41 | + |
| 42 | + var results = queryable.ToList(); |
| 43 | + results.Select(model => model.Id).Should().BeEquivalentTo(4, 5); |
| 44 | + } |
| 45 | + |
| 46 | + [Theory] |
| 47 | + [ParameterAttributeData] |
| 48 | + public void Query2_using_list_should_work( |
| 49 | + [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 50 | + { |
| 51 | + var collection = GetCollection(linqProvider); |
| 52 | + var lookingFor = new List<string> { "value1", "value2" }; |
| 53 | + |
| 54 | + var queryable = collection.AsQueryable() |
| 55 | + .Where(model => model.List.Any(value => lookingFor.Contains(value))); |
| 56 | + |
| 57 | + var stages = Translate(collection, queryable); |
| 58 | + if (linqProvider == LinqProvider.V2) |
| 59 | + { |
| 60 | + AssertStages(stages, "{ $match : { List : { $elemMatch : { $in : ['value1', 'value2'] } } } }"); |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + AssertStages(stages, "{ $match : { List : { $in : ['value1', 'value2'] } } }"); |
| 65 | + } |
| 66 | + |
| 67 | + var results = queryable.ToList(); |
| 68 | + results.Select(model => model.Id).Should().BeEquivalentTo(4, 5); |
| 69 | + } |
| 70 | + |
| 71 | + [Theory] |
| 72 | + [ParameterAttributeData] |
| 73 | + public void Query1_using_hashset_should_work( |
| 74 | + [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 75 | + { |
| 76 | + var collection = GetCollection(linqProvider); |
| 77 | + var lookingFor = new List<string> { "value1", "value2" }; |
| 78 | + |
| 79 | + var queryable = collection.AsQueryable() |
| 80 | + .Where(model => lookingFor.Any(value => model.HashSet.Contains(value))); |
| 81 | + |
| 82 | + var stages = Translate(collection, queryable); |
| 83 | + AssertStages(stages, "{ $match : { HashSet : { $in : ['value1', 'value2'] } } }"); |
| 84 | + |
| 85 | + var results = queryable.ToList(); |
| 86 | + results.Select(model => model.Id).Should().BeEquivalentTo(4, 5); |
| 87 | + } |
| 88 | + |
| 89 | + [Theory] |
| 90 | + [ParameterAttributeData] |
| 91 | + public void Query2_using_hashset_should_work( |
| 92 | + [Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) |
| 93 | + { |
| 94 | + var collection = GetCollection(linqProvider); |
| 95 | + var lookingFor = new List<string> { "value1", "value2" }; |
| 96 | + |
| 97 | + var queryable = collection.AsQueryable() |
| 98 | + .Where(model => model.HashSet.Any(value => lookingFor.Contains(value))); |
| 99 | + |
| 100 | + var stages = Translate(collection, queryable); |
| 101 | + if (linqProvider == LinqProvider.V2) |
| 102 | + { |
| 103 | + AssertStages(stages, "{ $match : { HashSet : { $elemMatch : { $in : ['value1', 'value2'] } } } }"); |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + AssertStages(stages, "{ $match : { HashSet : { $in : ['value1', 'value2'] } } }"); |
| 108 | + } |
| 109 | + |
| 110 | + var results = queryable.ToList(); |
| 111 | + results.Select(model => model.Id).Should().BeEquivalentTo(4, 5); |
| 112 | + } |
| 113 | + |
| 114 | + private IMongoCollection<Model> GetCollection(LinqProvider linqProvider) |
| 115 | + { |
| 116 | + var collection = GetCollection<Model>("test", linqProvider); |
| 117 | + var documentsCollection = GetCollection<BsonDocument>("test"); |
| 118 | + CreateCollection( |
| 119 | + documentsCollection, |
| 120 | + BsonDocument.Parse("{ _id : 1 }"), |
| 121 | + BsonDocument.Parse("{ _id : 2, List : null, HashSet : null }"), |
| 122 | + BsonDocument.Parse("{ _id : 3, List : [], HashSet : [] }"), |
| 123 | + BsonDocument.Parse("{ _id : 4, List : ['value1'], HashSet : ['value1'] }"), |
| 124 | + BsonDocument.Parse("{ _id : 5, List : ['value1', 'value2'], HashSet : ['value1', 'value2'] }"), |
| 125 | + BsonDocument.Parse("{ _id : 6, List : ['value3'], HashSet : ['value3'] }"), |
| 126 | + BsonDocument.Parse("{ _id : 7, List : ['value3', 'value4'], HashSet : ['value3', 'value4'] }")); |
| 127 | + return collection; |
| 128 | + } |
| 129 | + |
| 130 | + private class Model |
| 131 | + { |
| 132 | + public int Id { get; set; } |
| 133 | + public List<string> List { get; set; } |
| 134 | + public HashSet<string> HashSet { get; set; } |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments