|
| 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 Xunit; |
| 20 | + |
| 21 | +namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira |
| 22 | +{ |
| 23 | + public class CSharp4140Tests : Linq3IntegrationTest |
| 24 | + { |
| 25 | + [Theory] |
| 26 | + [InlineData("abc")] |
| 27 | + [InlineData("123")] |
| 28 | + [InlineData("12$")] |
| 29 | + [InlineData("!@#")] |
| 30 | + public void Should_not_use_getField(string fieldName) |
| 31 | + { |
| 32 | + var collection = GetCollection<BsonDocument>(); |
| 33 | + CreateCollection(collection, new BsonDocument(fieldName, 123)); |
| 34 | + var queryable = collection.AsQueryable() |
| 35 | + .Select(x => x[fieldName]); |
| 36 | + |
| 37 | + var stages = Translate(collection, queryable); |
| 38 | + AssertStages(stages, $"{{ $project : {{ _v : '${fieldName}', _id : 0 }} }}"); |
| 39 | + |
| 40 | + var results = queryable.ToList(); |
| 41 | + results.Should().Equal(123); |
| 42 | + } |
| 43 | + |
| 44 | + [Theory] |
| 45 | + [InlineData("")] |
| 46 | + [InlineData(".")] |
| 47 | + [InlineData(".a")] |
| 48 | + [InlineData("a.")] |
| 49 | + public void Should_use_getField(string fieldName) |
| 50 | + { |
| 51 | + var collection = GetCollection<BsonDocument>(); |
| 52 | + CreateCollection(collection, new BsonDocument(fieldName, 123)); |
| 53 | + var queryable = collection.AsQueryable() |
| 54 | + .Select(x => x[fieldName]); |
| 55 | + |
| 56 | + var stages = Translate(collection, queryable); |
| 57 | + AssertStages(stages, $"{{ $project : {{ _v : {{ $getField : {{ field : '{fieldName}', input : '$$ROOT' }} }}, _id : 0 }} }}"); |
| 58 | + |
| 59 | + var results = queryable.ToList(); |
| 60 | + results.Should().Equal(123); |
| 61 | + } |
| 62 | + |
| 63 | + [Theory] |
| 64 | + [InlineData("$")] |
| 65 | + [InlineData("$a")] |
| 66 | + [InlineData("$$a")] |
| 67 | + [InlineData("$a$")] |
| 68 | + public void Should_use_getField_with_literal(string fieldName) |
| 69 | + { |
| 70 | + var collection = GetCollection<BsonDocument>(); |
| 71 | + CreateCollection(collection, new BsonDocument(fieldName, 123)); |
| 72 | + var queryable = collection.AsQueryable() |
| 73 | + .Select(x => x[fieldName]); |
| 74 | + |
| 75 | + var stages = Translate(collection, queryable); |
| 76 | + AssertStages(stages, $"{{ $project : {{ _v : {{ $getField : {{ field : {{ $literal : '{fieldName}' }}, input : '$$ROOT' }} }}, _id : 0 }} }}"); |
| 77 | + |
| 78 | + var results = queryable.ToList(); |
| 79 | + results.Should().Equal(123); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments