|
| 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.Bson; |
| 19 | +using MongoDB.Bson.Serialization; |
| 20 | +using MongoDB.Driver.Linq; |
| 21 | +using Xunit; |
| 22 | + |
| 23 | +namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira |
| 24 | +{ |
| 25 | + public class CSharp2195Tests : Linq3IntegrationTest |
| 26 | + { |
| 27 | + [Fact] |
| 28 | + public void Filter_should_work() |
| 29 | + { |
| 30 | + var collection = CreateCollection(); |
| 31 | + var builder = Builders<RawBsonDocument>.Filter; |
| 32 | + var filter = builder.Eq(x => x["life"], 42); |
| 33 | + |
| 34 | + var serializerRegistry = BsonSerializer.SerializerRegistry; |
| 35 | + var documentSerializer = serializerRegistry.GetSerializer<RawBsonDocument>(); |
| 36 | + var renderedFilter = filter.Render(documentSerializer, serializerRegistry); |
| 37 | + renderedFilter.Should().Be("{ life : 42 }"); |
| 38 | + |
| 39 | + var results = collection.FindSync(filter).ToList(); |
| 40 | + results.Select(x => x["_id"].AsInt32).Should().Equal(2); |
| 41 | + } |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public void Where_should_work() |
| 45 | + { |
| 46 | + var collection = CreateCollection(); |
| 47 | + |
| 48 | + var queryable = collection |
| 49 | + .AsQueryable() |
| 50 | + .Where(x => x["life"] == 42); |
| 51 | + |
| 52 | + var stages = Translate(collection, queryable); |
| 53 | + AssertStages( |
| 54 | + stages, |
| 55 | + "{ $match : { life : 42 } }"); |
| 56 | + |
| 57 | + var results = queryable.ToList(); |
| 58 | + results.Select(x => x["_id"].AsInt32).Should().Equal(2); |
| 59 | + } |
| 60 | + |
| 61 | + private IMongoCollection<RawBsonDocument> CreateCollection() |
| 62 | + { |
| 63 | + var collection = GetCollection<BsonDocument>(); |
| 64 | + |
| 65 | + CreateCollection( |
| 66 | + collection, |
| 67 | + new BsonDocument { { "_id", 1 }, { "life", 41 } }, |
| 68 | + new BsonDocument { { "_id", 2 }, { "life", 42 } }); |
| 69 | + |
| 70 | + return GetCollection<RawBsonDocument>(); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments