Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 186 additions & 0 deletions tests/MongoDB.Analyzer.Tests.Common.TestCases/Poco/PocoLimits.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
// Copyright 2021-present MongoDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License")
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;

namespace MongoDB.Analyzer.Tests.Common.TestCases.Poco
{
public sealed class PocoLimits : TestCasesBase
{
[PocoJson("{ \"AirlineName\" : \"Radiant Skies Aviation\" }", pocoLimit: 10, pocoAnalysisVerbosity: PocoAnalysisVerbosity.All)]
public void Airline_not_used_in_expression1()
{
}

[PocoJson("{ \"AirlineName\" : \"Radiant Skies Aviation\" }", pocoLimit: 10, pocoAnalysisVerbosity: PocoAnalysisVerbosity.All)]
public void Airline_not_used_in_expression2()
{
}

[PocoJson("{ \"AirlineName\" : \"Radiant Skies Aviation\" }", pocoLimit: 10, pocoAnalysisVerbosity: PocoAnalysisVerbosity.All)]
public void Airline_not_used_in_expression3()
{
}

[BuildersMQL("{ \"AirlineName\" : \"Lufthansa\" }", pocoLimit: 10, pocoAnalysisVerbosity: PocoAnalysisVerbosity.All)]
[PocoJson("{ \"AirlineName\" : \"Radiant Skies Aviation\" }", pocoLimit: 10, pocoAnalysisVerbosity: PocoAnalysisVerbosity.All)]
public void Airline_used_in_expression1()
{
_ = Builders<TestClasses.Airline_used_in_expression1>.Filter.Eq(u => u.AirlineName, "Lufthansa");
}

[BuildersMQL("{ \"AirlineName\" : \"Lufthansa\" }", pocoLimit: 10, pocoAnalysisVerbosity: PocoAnalysisVerbosity.All)]
[PocoJson("{ \"AirlineName\" : \"Radiant Skies Aviation\" }", pocoLimit: 10, pocoAnalysisVerbosity: PocoAnalysisVerbosity.All)]
public void Airline_used_in_expression2()
{
_ = Builders<TestClasses.Airline_used_in_expression2>.Filter.Eq(u => u.AirlineName, "Lufthansa");
}

[BuildersMQL("{ \"AirlineName\" : \"Lufthansa\" }", pocoLimit: 10, pocoAnalysisVerbosity: PocoAnalysisVerbosity.All)]
[PocoJson("{ \"AirlineName\" : \"Radiant Skies Aviation\" }", pocoLimit: 10, pocoAnalysisVerbosity: PocoAnalysisVerbosity.All)]
public void Airline_used_in_expression3()
{
_ = Builders<TestClasses.Airline_used_in_expression3>.Filter.Eq(u => u.AirlineName, "Lufthansa");
}

[PocoJson("{ \"StringProperty\" : \"StringProperty_val\" }", pocoLimit: 10, pocoAnalysisVerbosity: PocoAnalysisVerbosity.All)]
public void ClassWithBsonAttributes()
{
}

[PocoJson("{ \"StringProperty\" : \"StringProperty_val\" }", pocoLimit: 10, pocoAnalysisVerbosity: PocoAnalysisVerbosity.All)]
public void ClassWithBsonAttributes2()
{
}

[NoDiagnostics(pocoLimit: 10, pocoAnalysisVerbosity: PocoAnalysisVerbosity.All)]
public void ClassWithFieldBsonAttributes()
{
}

[NoDiagnostics(pocoLimit: 10, pocoAnalysisVerbosity: PocoAnalysisVerbosity.All)]
public void ClassWithFieldBsonAttributes2()
{
}

[NoDiagnostics(pocoLimit: 10, pocoAnalysisVerbosity: PocoAnalysisVerbosity.All)]
public void ClassWithPropertyAndFieldAttributes()
{
}

[NoDiagnostics(pocoLimit: 10, pocoAnalysisVerbosity: PocoAnalysisVerbosity.All)]
public void ClassWithPropertyAndFieldAttributes2()
{
}

[NoDiagnostics(pocoLimit: 10, pocoAnalysisVerbosity: PocoAnalysisVerbosity.All)]
public void ClassWithPropertyBsonAttributes()
{
}

[NoDiagnostics(pocoLimit: 10, pocoAnalysisVerbosity: PocoAnalysisVerbosity.All)]
public void ClassWithPropertyBsonAttributes2()
{
}

public class TestClasses
{
public class Airline_not_used_in_expression1
{
public string AirlineName { get; set; }
}

public class Airline_not_used_in_expression2
{
public string AirlineName { get; set; }
}

public class Airline_not_used_in_expression3
{
public string AirlineName { get; set; }
}

public class Airline_used_in_expression1
{
public string AirlineName { get; set; }
}

public class Airline_used_in_expression2
{
public string AirlineName { get; set; }
}

public class Airline_used_in_expression3
{
public string AirlineName { get; set; }
}

[BsonIgnoreExtraElements]
public class ClassWithBsonAttributes
{
public string StringProperty { get; set; }
}

[BsonIgnoreExtraElements]
public class ClassWithBsonAttributes2
{
public string StringProperty { get; set; }
}

public class ClassWithFieldBsonAttributes
{
[BsonElement("string_field", Order = 2)]
public string StringField;
}

public class ClassWithFieldBsonAttributes2
{
[BsonElement("string_field", Order = 2)]
public string StringField;
}

public class ClassWithPropertyAndFieldAttributes
{
[BsonElement("string_property", Order = 2)]
public string StringProperty { get; set; }

[BsonElement("string_field", Order = 1)]
public string StringField;
}

public class ClassWithPropertyAndFieldAttributes2
{
[BsonElement("string_property", Order = 2)]
public string StringProperty { get; set; }

[BsonElement("string_field", Order = 1)]
public string StringField;
}

public class ClassWithPropertyBsonAttributes
{
[BsonElement("string_property", Order = 2)]
public string StringProperty { get; set; }
}

public class ClassWithPropertyBsonAttributes2
{
[BsonElement("string_property", Order = 2)]
public string StringProperty { get; set; }
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@ public void Airline_not_used_in_expression2()
{
}

[PocoJson("{ \"AirlineName\" : \"Radiant Skies Aviation\" }", PocoAnalysisVerbosity.All)]
[PocoJson("{ \"AirlineName\" : \"Radiant Skies Aviation\" }", pocoAnalysisVerbosity: PocoAnalysisVerbosity.All)]
public void Airline_not_used_in_expression3()
{
}

[BuildersMQL("{ \"AirlineName\" : \"Lufthansa\" }", PocoAnalysisVerbosity.None)]
[BuildersMQL("{ \"AirlineName\" : \"Lufthansa\" }", pocoLimit: 500, pocoAnalysisVerbosity: PocoAnalysisVerbosity.None)]
public void Airline_used_in_expression1()
{
_ = Builders<TestClasses.Airline_used_in_expression1>.Filter.Eq(u => u.AirlineName, "Lufthansa");
}

[BuildersMQL("{ \"AirlineName\" : \"Lufthansa\" }", PocoAnalysisVerbosity.Medium)]
[PocoJson("{ \"AirlineName\" : \"Radiant Skies Aviation\" }", PocoAnalysisVerbosity.Medium)]
[BuildersMQL("{ \"AirlineName\" : \"Lufthansa\" }", pocoLimit: 500, pocoAnalysisVerbosity: PocoAnalysisVerbosity.Medium)]
[PocoJson("{ \"AirlineName\" : \"Radiant Skies Aviation\" }", pocoAnalysisVerbosity: PocoAnalysisVerbosity.Medium)]
public void Airline_used_in_expression2()
{
_ = Builders<TestClasses.Airline_used_in_expression2>.Filter.Eq(u => u.AirlineName, "Lufthansa");
}

[BuildersMQL("{ \"AirlineName\" : \"Lufthansa\" }")]
[PocoJson("{ \"AirlineName\" : \"Radiant Skies Aviation\" }", PocoAnalysisVerbosity.All)]
[PocoJson("{ \"AirlineName\" : \"Radiant Skies Aviation\" }", pocoAnalysisVerbosity: PocoAnalysisVerbosity.All)]
public void Airline_used_in_expression3()
{
_ = Builders<TestClasses.Airline_used_in_expression3>.Filter.Eq(u => u.AirlineName, "Lufthansa");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class DiagnosticRuleTestCaseAttribute : Attribute
public Location[] Locations { get; }
public DriverTargetFramework TargetFramework { get; }
public LinqVersion LinqProvider { get; }
public int PocoLimit { get; }
public PocoAnalysisVerbosity PocoAnalysisVerbosity { get; }

public DiagnosticRuleTestCaseAttribute(
Expand All @@ -34,6 +35,7 @@ public DiagnosticRuleTestCaseAttribute(
string version = null,
LinqVersion linqProvider = LinqVersion.V2,
DriverTargetFramework targetFramework = DriverTargetFramework.All,
int pocoLimit = 500,
PocoAnalysisVerbosity pocoAnalysisVerbosity = PocoAnalysisVerbosity.All,
int[] codeLines = null)
{
Expand All @@ -43,14 +45,15 @@ public DiagnosticRuleTestCaseAttribute(
LinqProvider = linqProvider;
TargetFramework = targetFramework;
Locations = codeLines?.Any() == true ? codeLines.Select(l => new Location(l, -1)).ToArray() : new[] { Location.Empty };
PocoLimit = pocoLimit;
PocoAnalysisVerbosity = pocoAnalysisVerbosity;
}
}

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class NoDiagnosticsAttribute : DiagnosticRuleTestCaseAttribute
{
public NoDiagnosticsAttribute(string version = null, PocoAnalysisVerbosity pocoAnalysisVerbosity = PocoAnalysisVerbosity.All) : base(DiagnosticRulesConstants.NoRule, null, version, pocoAnalysisVerbosity: pocoAnalysisVerbosity) { }
public NoDiagnosticsAttribute(string version = null, int pocoLimit = 500, PocoAnalysisVerbosity pocoAnalysisVerbosity = PocoAnalysisVerbosity.All) : base(DiagnosticRulesConstants.NoRule, null, version, pocoLimit: pocoLimit, pocoAnalysisVerbosity: pocoAnalysisVerbosity) { }
}

public class MQLAttribute : DiagnosticRuleTestCaseAttribute
Expand Down Expand Up @@ -173,9 +176,10 @@ public BuildersMQLAttribute(string message, string version, params int[] codeLin
{
}

public BuildersMQLAttribute(string message, PocoAnalysisVerbosity pocoAnalysisVerbosity) :
public BuildersMQLAttribute(string message, int pocoLimit, PocoAnalysisVerbosity pocoAnalysisVerbosity) :
base(DiagnosticRulesConstants.Builders2MQL,
message,
pocoLimit: pocoLimit,
pocoAnalysisVerbosity: pocoAnalysisVerbosity)
{
}
Expand All @@ -193,17 +197,17 @@ public NotSupportedBuildersAttribute(string message, string version = null) :
public sealed class PocoJsonAttribute : DiagnosticRuleTestCaseAttribute
{
public PocoJsonAttribute(
string message, PocoAnalysisVerbosity pocoAnalysisVerbosity = PocoAnalysisVerbosity.All) :
base(DiagnosticRulesConstants.Poco2Json, message, null, targetFramework: DriverTargetFramework.All, pocoAnalysisVerbosity: pocoAnalysisVerbosity, codeLines: null)
string message, int pocoLimit = 500, PocoAnalysisVerbosity pocoAnalysisVerbosity = PocoAnalysisVerbosity.All) :
base(DiagnosticRulesConstants.Poco2Json, message, null, targetFramework: DriverTargetFramework.All, pocoLimit: pocoLimit, pocoAnalysisVerbosity: pocoAnalysisVerbosity, codeLines: null)
{
}
}

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public sealed class NotSupportedPocoAttribute : DiagnosticRuleTestCaseAttribute
{
public NotSupportedPocoAttribute(string message, string version = null, PocoAnalysisVerbosity pocoAnalysisVerbosity = PocoAnalysisVerbosity.All) :
base(DiagnosticRulesConstants.NotSupportedPoco, message, version, pocoAnalysisVerbosity: pocoAnalysisVerbosity)
public NotSupportedPocoAttribute(string message, string version = null, int pocoLimit = 500, PocoAnalysisVerbosity pocoAnalysisVerbosity = PocoAnalysisVerbosity.All) :
base(DiagnosticRulesConstants.NotSupportedPoco, message, version, pocoLimit: pocoLimit, pocoAnalysisVerbosity: pocoAnalysisVerbosity)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ from location in attribute.Locations
location.StartLine >= 0 ? location.StartLine : 0,
location.StartLine >= 0 ? attribute.Message : null
group new DiagnosticRule(attribute.RuleId, $"{attribute.Message}_v{version.ToString("V", new VersionFormatter())}", location)
by new { version, attribute.LinqProvider, attribute.PocoAnalysisVerbosity } into g
select new DiagnosticTestCase(fileName, memberInfo.Name, g.Key.version.ToString(), g.Key.LinqProvider, g.Key.PocoAnalysisVerbosity, g.ToArray());
by new { version, attribute.LinqProvider, attribute.PocoLimit, attribute.PocoAnalysisVerbosity } into g
select new DiagnosticTestCase(fileName, memberInfo.Name, g.Key.version.ToString(), g.Key.LinqProvider, g.Key.PocoLimit, g.Key.PocoAnalysisVerbosity, g.ToArray());

return diagnosticsTestCases.ToArray();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public record DiagnosticTestCase(
string MethodName,
string Version,
LinqVersion LinqVersion,
int PocoLimit,
PocoAnalysisVerbosity JsonAnalyzerVerbosity,
DiagnosticRule[] DiagnosticRules)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static async Task<ImmutableArray<Diagnostic>> Analyze(
string testCodeFilename,
string driverVersion,
Common.LinqVersion linqVersion,
int pocoLimit,
Common.PocoAnalysisVerbosity jsonAnalyzerVerbosity)
{
var testDataModelAssembly = PathUtilities.GetTestDataModelAssemblyPath(driverVersion);
Expand Down Expand Up @@ -69,6 +70,7 @@ public static async Task<ImmutableArray<Diagnostic>> Analyze(
OutputDriverVersion: true,
DefaultLinqVersion: linqDefaultVersion,
SendTelemetry: false,
PocoLimit: pocoLimit,
PocoAnalysisVerbosity: (PocoAnalysisVerbosity)jsonAnalyzerVerbosity);
var analyzerOptions = new AnalyzerOptions(ImmutableArray.Create<AdditionalText>(new AdditionalTextAnalyzerSettings(settings)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ namespace MongoDB.Analyzer.Tests.Infrastructure;

internal static class TestCasesRunner
{
private record TestsBundleKey(string TestFileName, string DriverVersion, LinqVersion LinqVersion, PocoAnalysisVerbosity PocoAnalysisVerbosity);
private record TestsBundleKey(string TestFileName, string DriverVersion, LinqVersion LinqVersion, int PocoLimit, PocoAnalysisVerbosity PocoAnalysisVerbosity);

private static readonly IDictionary<TestsBundleKey, IDictionary<string, DiagnosticTestCaseResult>>
s_testResults = new Dictionary<TestsBundleKey, IDictionary<string, DiagnosticTestCaseResult>>();

public static async Task<DiagnosticTestCaseResult> RunTestCase(DiagnosticTestCase diagnosticTestCase)
{
var testCollectionKey = new TestsBundleKey(diagnosticTestCase.FileName, diagnosticTestCase.Version, diagnosticTestCase.LinqVersion, diagnosticTestCase.JsonAnalyzerVerbosity);
var testCollectionKey = new TestsBundleKey(diagnosticTestCase.FileName, diagnosticTestCase.Version, diagnosticTestCase.LinqVersion, diagnosticTestCase.PocoLimit, diagnosticTestCase.JsonAnalyzerVerbosity);

if (!s_testResults.TryGetValue(testCollectionKey, out var testCasesResults))
{
Expand All @@ -50,6 +50,7 @@ private static async Task<IDictionary<string, DiagnosticTestCaseResult>> Execute
testsBundleKey.TestFileName,
testsBundleKey.DriverVersion,
testsBundleKey.LinqVersion,
testsBundleKey.PocoLimit,
testsBundleKey.PocoAnalysisVerbosity);

var diagnosticsAndMethodNodes = diagnostics
Expand Down
4 changes: 4 additions & 0 deletions tests/MongoDB.Analyzer.Tests/Poco/PocoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public sealed class PocoTests : DiagnosticsTestCasesRunner
[CodeBasedTestCasesSource(typeof(PocoIgnoredBsonAttributes))]
public Task IgnoredBsonAttributes(DiagnosticTestCase testCase) => VerifyTestCase(testCase);

[DataTestMethod]
[CodeBasedTestCasesSource(typeof(PocoLimits))]
public Task Limits(DiagnosticTestCase testCase) => VerifyTestCase(testCase);

[DataTestMethod]
[CodeBasedTestCasesSource(typeof(PocoNestedTypes))]
public Task NestedTypes(DiagnosticTestCase testCase) => VerifyTestCase(testCase);
Expand Down