Skip to content

Commit

Permalink
Convert multiple scope levels to theory
Browse files Browse the repository at this point in the history
  • Loading branch information
mfogliatto committed Jun 17, 2023
1 parent 45cba37 commit 3df1a8b
Showing 1 changed file with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<LogRecord>(1);
Expand All @@ -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<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>(scopeKey, scopeValue1),
new KeyValuePair<string, object>(scopeKey, scopeValue2),
new KeyValuePair<string, object>(scopeKey1, scopeValue1),
new KeyValuePair<string, object>(scopeKey2, scopeValue2),
}))
{
logger.LogInformation("Some log information message.");
Expand All @@ -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<LogRecord>(1);
Expand All @@ -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<KeyValuePair<string, object>> { new KeyValuePair<string, object>(scopeKey, scopeValue1) }))
using (logger.BeginScope(new List<KeyValuePair<string, object>> { new KeyValuePair<string, object>(scopeKey1, scopeValue1) }))
{
using (logger.BeginScope(new List<KeyValuePair<string, object>> { new KeyValuePair<string, object>(scopeKey, scopeValue2) }))
using (logger.BeginScope(new List<KeyValuePair<string, object>> { new KeyValuePair<string, object>(scopeKey2, scopeValue2) }))
{
logger.LogInformation("Some log information message.");
}
Expand All @@ -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<LogRecord>(1);
Expand All @@ -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<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>(scopeKey, scopeValue1),
new KeyValuePair<string, object>(scopeKey1, scopeValue1),
}))
{
logger.Log(
LogLevel.Error,
new EventId(1),
new List<KeyValuePair<string, object>> { new KeyValuePair<string, object>(scopeKey, scopeValue2) },
new List<KeyValuePair<string, object>> { new KeyValuePair<string, object>(scopeKey2, scopeValue2) },
exception: new Exception("Some exception message"),
formatter: (s, e) => string.Empty);
}
Expand All @@ -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);
Expand Down

0 comments on commit 3df1a8b

Please sign in to comment.