Skip to content

Commit 47d0c2e

Browse files
VS-114: A lambda expression for sorting makes MongoDB.Analyzer not work as expected (#67)
* Fixed Error reported by VS-114 * Renamed Files and moved Tests Appropriately --------- Co-authored-by: Ravi Raghavan <ravi.raghavan@mongodb.com>
1 parent fcde6e7 commit 47d0c2e

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

src/MongoDB.Analyzer/Core/Builders/BuilderExpressionProcessor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ public static ExpressionsAnalysis ProcessSemanticModel(MongoAnalysisContext cont
5555
collectionNode = expressionNode
5656
.NestedInvocations()
5757
.FirstOrDefault(n => semanticModel.GetTypeInfo(n).Type.IsSupportedIMongoCollection());
58+
59+
if (collectionNode == null)
60+
{
61+
continue;
62+
}
63+
5864
break;
5965
}
6066
case NodeType.Invalid:
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2021-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+
using System;
16+
using MongoDB.Analyzer.Tests.Common.DataModel;
17+
using MongoDB.Driver;
18+
19+
namespace MongoDB.Analyzer.Tests.Common.TestCases.Builders
20+
{
21+
public sealed class BuildersComplexExpressions : TestCasesBase
22+
{
23+
[NoDiagnostics]
24+
public void SortByFluentFunction()
25+
{
26+
Func<IFindFluent<User, User>, IOrderedFindFluent<User, User>> fluentFunction = x => x.SortBy(x => x.Age);
27+
}
28+
29+
[NoDiagnostics]
30+
public void ThenByFluentFunction()
31+
{
32+
Func<IFindFluent<User, User>, IOrderedFindFluent<User, User>> fluentFunction = x => x.SortBy(x => x.Age).ThenBy(x => x.Height);
33+
}
34+
35+
[NoDiagnostics]
36+
public void SortByAndThenByFluentFunctions()
37+
{
38+
Func<IFindFluent<User, User>, IFindFluent<User, User>> fluentFunction = x => x.Skip(10).Limit(12).SortBy(x => x.Age).ThenBy(x => x.Height);
39+
}
40+
41+
[BuildersMQL("{ \"Age\" : -1, \"Name\" : 1, \"Address\" : 1 }")]
42+
public void SortFluentFunction()
43+
{
44+
Func<IFindFluent<User, User>, IFindFluent<User, User>> func = x => x.Sort(Builders<User>.Sort.Combine(
45+
Builders<User>.Sort.Descending(u => u.Age),
46+
Builders<User>.Sort.Ascending(u => u.Name),
47+
Builders<User>.Sort.Ascending(u => u.Address)));
48+
}
49+
50+
[BuildersMQL("{ \"Age\" : 1, \"Address\" : 0, \"Height\" : 1 }")]
51+
public void ProjectFluentFunction()
52+
{
53+
Func<IFindFluent<User, User>, IFindFluent<User, Bson.BsonDocument>> func = x => x.Project(Builders<User>.Projection.Include(u => u.Age).Exclude(u => u.Address).Include(u => u.Height));
54+
}
55+
56+
[BuildersMQL("{ \"Age\" : -1, \"Name\" : 1, \"Address\" : 1 }")]
57+
[BuildersMQL("{ \"Age\" : 1, \"Address\" : 0, \"Height\" : 1 }")]
58+
public void SortAndProjectFluentFunction()
59+
{
60+
Func<IFindFluent<User, User>, IFindFluent<User, Bson.BsonDocument>> func = x => x.Sort(Builders<User>.Sort.Combine(
61+
Builders<User>.Sort.Descending(u => u.Age),
62+
Builders<User>.Sort.Ascending(u => u.Name),
63+
Builders<User>.Sort.Ascending(u => u.Address)))
64+
.Project(Builders<User>.Projection.Include(u => u.Age)
65+
.Exclude(u => u.Address).Include(u => u.Height));
66+
}
67+
}
68+
}

tests/MongoDB.Analyzer.Tests.Common.TestCases/Builders/BuildersIgnoredExpressions.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ namespace MongoDB.Analyzer.Tests.Common.TestCases.Builders
2121
{
2222
public sealed class BuildersIgnoredExpressions : TestCasesBase
2323
{
24+
[BuildersMQL("{ \"Address.City\" : \"Vienna\" }")]
25+
public void Simple_valid_expression_as_baseline_1()
26+
{
27+
var person = new Person();
28+
_ = Builders<Person>.Filter.Eq(t => t.Address.City, "Vienna");
29+
}
30+
2431
[NoDiagnostics]
2532
public void Arrays_reference_should_be_ignored()
2633
{
@@ -62,7 +69,7 @@ public void Objects_comparison_expression_should_be_ignored()
6269
}
6370

6471
[BuildersMQL("{ \"Address.City\" : \"Vienna\" }")]
65-
public void Simple_valid_expression_as_baseline()
72+
public void Simple_valid_expression_as_baseline_2()
6673
{
6774
var person = new Person();
6875
_ = Builders<Person>.Filter.Eq(t => t.Address.City, "Vienna");

tests/MongoDB.Analyzer.Tests/Builders/BuildersTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public sealed class BuildersTests : DiagnosticsTestCasesRunner
5050
[CodeBasedTestCasesSource(typeof(BuildersComplexDataModel))]
5151
public Task ComplexDataModel(DiagnosticTestCase testCase) => VerifyTestCase(testCase);
5252

53+
[DataTestMethod]
54+
[CodeBasedTestCasesSource(typeof(BuildersComplexExpressions))]
55+
public Task ComplexExpressions(DiagnosticTestCase testCase) => VerifyTestCase(testCase);
56+
5357
[DataTestMethod]
5458
[CodeBasedTestCasesSource(typeof(BuildersConstantsReplacement))]
5559
public Task ConstantsReplacement(DiagnosticTestCase testCase) => VerifyTestCase(testCase);

0 commit comments

Comments
 (0)