From 9b094cb6a141d5d22a2cb9e680a6eb9157c8bbc7 Mon Sep 17 00:00:00 2001 From: Ravi Raghavan Date: Tue, 23 Jul 2024 16:15:33 -0400 Subject: [PATCH] VS-107: Add Testing for poco_limit --- .../Poco/PocoLimits.cs | 186 ++++++++++++++++++ .../Poco/PocoVerbosity.cs | 10 +- .../DiagnosticTestCaseAttribute.cs | 16 +- .../CodeBasedTestCasesSourceAttribute.cs | 4 +- .../Infrastructure/DiagnosticTestCase.cs | 1 + .../Infrastructure/DiagnosticsAnalyzer.cs | 2 + .../Infrastructure/TestCasesRunner.cs | 5 +- .../MongoDB.Analyzer.Tests/Poco/PocoTests.cs | 4 + 8 files changed, 213 insertions(+), 15 deletions(-) create mode 100644 tests/MongoDB.Analyzer.Tests.Common.TestCases/Poco/PocoLimits.cs diff --git a/tests/MongoDB.Analyzer.Tests.Common.TestCases/Poco/PocoLimits.cs b/tests/MongoDB.Analyzer.Tests.Common.TestCases/Poco/PocoLimits.cs new file mode 100644 index 00000000..cc4d39d4 --- /dev/null +++ b/tests/MongoDB.Analyzer.Tests.Common.TestCases/Poco/PocoLimits.cs @@ -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.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.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.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; } + } + } + } +} + diff --git a/tests/MongoDB.Analyzer.Tests.Common.TestCases/Poco/PocoVerbosity.cs b/tests/MongoDB.Analyzer.Tests.Common.TestCases/Poco/PocoVerbosity.cs index 9da052b3..134fe4a0 100644 --- a/tests/MongoDB.Analyzer.Tests.Common.TestCases/Poco/PocoVerbosity.cs +++ b/tests/MongoDB.Analyzer.Tests.Common.TestCases/Poco/PocoVerbosity.cs @@ -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.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.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.Filter.Eq(u => u.AirlineName, "Lufthansa"); diff --git a/tests/MongoDB.Analyzer.Tests.Common/DiagnosticTestCaseAttribute.cs b/tests/MongoDB.Analyzer.Tests.Common/DiagnosticTestCaseAttribute.cs index 607f8ded..8f1038b3 100644 --- a/tests/MongoDB.Analyzer.Tests.Common/DiagnosticTestCaseAttribute.cs +++ b/tests/MongoDB.Analyzer.Tests.Common/DiagnosticTestCaseAttribute.cs @@ -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( @@ -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) { @@ -43,6 +45,7 @@ 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; } } @@ -50,7 +53,7 @@ public DiagnosticRuleTestCaseAttribute( [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 @@ -165,9 +168,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) { } @@ -185,8 +189,8 @@ 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) { } } @@ -194,8 +198,8 @@ public PocoJsonAttribute( [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) { } } diff --git a/tests/MongoDB.Analyzer.Tests/Infrastructure/CodeBasedTestCasesSourceAttribute.cs b/tests/MongoDB.Analyzer.Tests/Infrastructure/CodeBasedTestCasesSourceAttribute.cs index 37196fc3..9f54d0fa 100644 --- a/tests/MongoDB.Analyzer.Tests/Infrastructure/CodeBasedTestCasesSourceAttribute.cs +++ b/tests/MongoDB.Analyzer.Tests/Infrastructure/CodeBasedTestCasesSourceAttribute.cs @@ -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(); } diff --git a/tests/MongoDB.Analyzer.Tests/Infrastructure/DiagnosticTestCase.cs b/tests/MongoDB.Analyzer.Tests/Infrastructure/DiagnosticTestCase.cs index 679d8c44..7bdcd3e5 100644 --- a/tests/MongoDB.Analyzer.Tests/Infrastructure/DiagnosticTestCase.cs +++ b/tests/MongoDB.Analyzer.Tests/Infrastructure/DiagnosticTestCase.cs @@ -29,6 +29,7 @@ public record DiagnosticTestCase( string MethodName, string Version, LinqVersion LinqVersion, + int PocoLimit, PocoAnalysisVerbosity JsonAnalyzerVerbosity, DiagnosticRule[] DiagnosticRules) { diff --git a/tests/MongoDB.Analyzer.Tests/Infrastructure/DiagnosticsAnalyzer.cs b/tests/MongoDB.Analyzer.Tests/Infrastructure/DiagnosticsAnalyzer.cs index b8e90638..76a456c2 100644 --- a/tests/MongoDB.Analyzer.Tests/Infrastructure/DiagnosticsAnalyzer.cs +++ b/tests/MongoDB.Analyzer.Tests/Infrastructure/DiagnosticsAnalyzer.cs @@ -29,6 +29,7 @@ public static async Task> Analyze( string testCodeFilename, string driverVersion, Common.LinqVersion linqVersion, + int pocoLimit, Common.PocoAnalysisVerbosity jsonAnalyzerVerbosity) { PathUtilities.VerifyTestDataModelAssembly(); @@ -69,6 +70,7 @@ public static async Task> Analyze( OutputDriverVersion: true, DefaultLinqVersion: linqDefaultVersion, SendTelemetry: false, + PocoLimit: pocoLimit, PocoAnalysisVerbosity: (PocoAnalysisVerbosity)jsonAnalyzerVerbosity); var analyzerOptions = new AnalyzerOptions(ImmutableArray.Create(new AdditionalTextAnalyzerSettings(settings))); diff --git a/tests/MongoDB.Analyzer.Tests/Infrastructure/TestCasesRunner.cs b/tests/MongoDB.Analyzer.Tests/Infrastructure/TestCasesRunner.cs index c2f8c516..7f9e7b2e 100644 --- a/tests/MongoDB.Analyzer.Tests/Infrastructure/TestCasesRunner.cs +++ b/tests/MongoDB.Analyzer.Tests/Infrastructure/TestCasesRunner.cs @@ -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> s_testResults = new Dictionary>(); public static async Task 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)) { @@ -50,6 +50,7 @@ private static async Task> Execute testsBundleKey.TestFileName, testsBundleKey.DriverVersion, testsBundleKey.LinqVersion, + testsBundleKey.PocoLimit, testsBundleKey.PocoAnalysisVerbosity); var diagnosticsAndMethodNodes = diagnostics diff --git a/tests/MongoDB.Analyzer.Tests/Poco/PocoTests.cs b/tests/MongoDB.Analyzer.Tests/Poco/PocoTests.cs index 821f3c0d..35c491e0 100644 --- a/tests/MongoDB.Analyzer.Tests/Poco/PocoTests.cs +++ b/tests/MongoDB.Analyzer.Tests/Poco/PocoTests.cs @@ -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);