From 3df1a8bc383443c844e25d6fe6b8b27f8c13a569 Mon Sep 17 00:00:00 2001 From: Marco Fogliatto <2962955+mfogliatto@users.noreply.github.com> Date: Sat, 17 Jun 2023 10:04:13 +0200 Subject: [PATCH] Convert multiple scope levels to theory --- .../OtlpLogExporterTests.cs | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs index e335f374eee..cdd73675b9c 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OtlpLogExporterTests.cs @@ -987,8 +987,10 @@ public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_AndScopeStateIsOfEnumerable Assert.Equal(scopeKey, actualScope.Key); } - [Fact] - public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_AndMultipleScopesWithSameKeyAreAdded_ContainsAllAddedScopeValues() + [Theory] + [InlineData("Same scope key", "Same scope key")] + [InlineData("Scope key 1", "Scope key 2")] + public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_AndMultipleScopesAreAdded_ContainsAllAddedScopeValues(string scopeKey1, string scopeKey2) { // Arrange. var logRecords = new List(1); @@ -1002,15 +1004,14 @@ public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_AndMultipleScopesWithSameKe }); var logger = loggerFactory.CreateLogger(nameof(OtlpLogExporterTests)); - const string scopeKey = "Some scope key"; const string scopeValue1 = "Some scope value"; const string scopeValue2 = "Some other scope value"; // Act. using (logger.BeginScope(new List> { - new KeyValuePair(scopeKey, scopeValue1), - new KeyValuePair(scopeKey, scopeValue2), + new KeyValuePair(scopeKey1, scopeValue1), + new KeyValuePair(scopeKey2, scopeValue2), })) { logger.LogInformation("Some log information message."); @@ -1020,15 +1021,17 @@ public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_AndMultipleScopesWithSameKe var logRecord = logRecords.Single(); var otlpLogRecord = logRecord.ToOtlpLog(DefaultSdkLimitOptions); var allScopeValues = otlpLogRecord.Attributes - .Where(_ => _.Key == scopeKey) + .Where(_ => _.Key == scopeKey1 || _.Key == scopeKey2) .Select(_ => _.Value.StringValue); Assert.Equal(2, allScopeValues.Count()); Assert.Contains(scopeValue1, allScopeValues); Assert.Contains(scopeValue2, allScopeValues); } - [Fact] - public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_AndMultipleScopeLevelsWithSameKeyAreAdded_ContainsAllAddedScopeValues() + [Theory] + [InlineData("Same scope key", "Same scope key")] + [InlineData("Scope key 1", "Scope key 2")] + public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_AndMultipleScopeLevelsAreAdded_ContainsAllAddedScopeValues(string scopeKey1, string scopeKey2) { // Arrange. var logRecords = new List(1); @@ -1042,14 +1045,13 @@ public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_AndMultipleScopeLevelsWithS }); var logger = loggerFactory.CreateLogger(nameof(OtlpLogExporterTests)); - const string scopeKey = "Some scope key"; const string scopeValue1 = "Some scope value"; const string scopeValue2 = "Some other scope value"; // Act. - using (logger.BeginScope(new List> { new KeyValuePair(scopeKey, scopeValue1) })) + using (logger.BeginScope(new List> { new KeyValuePair(scopeKey1, scopeValue1) })) { - using (logger.BeginScope(new List> { new KeyValuePair(scopeKey, scopeValue2) })) + using (logger.BeginScope(new List> { new KeyValuePair(scopeKey2, scopeValue2) })) { logger.LogInformation("Some log information message."); } @@ -1059,15 +1061,17 @@ public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_AndMultipleScopeLevelsWithS var logRecord = logRecords.Single(); var otlpLogRecord = logRecord.ToOtlpLog(DefaultSdkLimitOptions); var allScopeValues = otlpLogRecord.Attributes - .Where(_ => _.Key == scopeKey) + .Where(_ => _.Key == scopeKey1 || _.Key == scopeKey2) .Select(_ => _.Value.StringValue); Assert.Equal(2, allScopeValues.Count()); Assert.Contains(scopeValue1, allScopeValues); Assert.Contains(scopeValue2, allScopeValues); } - [Fact] - public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_AndScopeWithExistingKeyIsUsedInLogMethod_ContainsAllAddedScopeValues() + [Theory] + [InlineData("Same scope key", "Same scope key")] + [InlineData("Scope key 1", "Scope key 2")] + public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_AndScopeIsUsedInLogMethod_ContainsAllAddedScopeValues(string scopeKey1, string scopeKey2) { // Arrange. var logRecords = new List(1); @@ -1081,20 +1085,19 @@ public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_AndScopeWithExistingKeyIsUs }); var logger = loggerFactory.CreateLogger(nameof(OtlpLogExporterTests)); - const string scopeKey = "Some scope key"; const string scopeValue1 = "Some scope value"; const string scopeValue2 = "Some other scope value"; // Act. using (logger.BeginScope(new List> { - new KeyValuePair(scopeKey, scopeValue1), + new KeyValuePair(scopeKey1, scopeValue1), })) { logger.Log( LogLevel.Error, new EventId(1), - new List> { new KeyValuePair(scopeKey, scopeValue2) }, + new List> { new KeyValuePair(scopeKey2, scopeValue2) }, exception: new Exception("Some exception message"), formatter: (s, e) => string.Empty); } @@ -1103,7 +1106,7 @@ public void ToOtlpLog_WhenOptionsIncludeScopesIsTrue_AndScopeWithExistingKeyIsUs var logRecord = logRecords.Single(); var otlpLogRecord = logRecord.ToOtlpLog(DefaultSdkLimitOptions); var allScopeValues = otlpLogRecord.Attributes - .Where(_ => _.Key == scopeKey) + .Where(_ => _.Key == scopeKey1 || _.Key == scopeKey2) .Select(_ => _.Value.StringValue); Assert.Equal(2, allScopeValues.Count()); Assert.Contains(scopeValue1, allScopeValues);