Skip to content

Commit 0a855b4

Browse files
committed
Refactor tests
1 parent 755b58d commit 0a855b4

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

test/Serilog.Sinks.OpenTelemetry.Tests/LogRecordBuilderTests.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void TestProcessMessage()
4545
public void TestProcessLevel()
4646
{
4747
var logRecord = new LogRecord();
48-
var logEvent = Some.SerilogEvent();
48+
var logEvent = Some.DefaultSerilogEvent();
4949

5050
LogRecordBuilder.ProcessLevel(logRecord, logEvent);
5151

@@ -57,7 +57,7 @@ public void TestProcessLevel()
5757
public void TestProcessProperties()
5858
{
5959
var logRecord = new LogRecord();
60-
var logEvent = Some.SerilogEvent();
60+
var logEvent = Some.DefaultSerilogEvent();
6161

6262
var prop = new LogEventProperty("property_name", new ScalarValue("ok"));
6363
var propertyKeyValue = PrimitiveConversions.NewStringAttribute("property_name", "ok");
@@ -76,7 +76,7 @@ public void TestTimestamp()
7676
var nowNano = PrimitiveConversions.ToUnixNano(now);
7777

7878
var logRecord = new LogRecord();
79-
var logEvent = Some.SerilogEvent(timestamp: now);
79+
var logEvent = Some.SerilogEvent(Some.TestMessageTemplate, timestamp: now);
8080

8181
LogRecordBuilder.ProcessTimestamp(logRecord, logEvent);
8282

@@ -96,7 +96,7 @@ public void TestException()
9696
catch (Exception ex)
9797
{
9898
var logRecord = new LogRecord();
99-
var logEvent = Some.SerilogEvent(ex: ex);
99+
var logEvent = Some.SerilogEvent(Some.TestMessageTemplate, ex: ex);
100100

101101
LogRecordBuilder.ProcessException(logRecord, logEvent);
102102

@@ -131,11 +131,14 @@ public void IncludeMessageTemplateMD5Hash()
131131
[Fact]
132132
public void IncludeMessageTemplateText()
133133
{
134-
var logEvent = Some.SerilogEvent(messageTemplate: Some.TestMessageTemplate);
134+
var messageTemplate = "Hello, {Name}";
135+
var properties = new List<LogEventProperty> { new("Name", new ScalarValue("World")) };
136+
137+
var logEvent = Some.SerilogEvent(messageTemplate, properties);
135138

136139
var logRecord = LogRecordBuilder.ToLogRecord(logEvent, null, IncludedData.MessageTemplateTextAttribute, new());
137140

138-
var expectedAttribute = new KeyValue { Key = SemanticConventions.AttributeMessageTemplateText, Value = new() { StringValue = Some.TestMessageTemplate }};
141+
var expectedAttribute = new KeyValue { Key = SemanticConventions.AttributeMessageTemplateText, Value = new() { StringValue = messageTemplate } };
139142
Assert.Contains(expectedAttribute, logRecord.Attributes);
140143
}
141144

@@ -146,7 +149,7 @@ public void IncludeTraceIdWhenActivityIsNull()
146149

147150
var collector = new ActivityContextCollector();
148151

149-
var logEvent = Some.SerilogEvent();
152+
var logEvent = Some.DefaultSerilogEvent();
150153
collector.CollectFor(logEvent);
151154

152155
var logRecord = LogRecordBuilder.ToLogRecord(logEvent, null, IncludedData.TraceIdField | IncludedData.SpanIdField, collector);
@@ -172,7 +175,7 @@ public void IncludeTraceIdAndSpanId()
172175

173176
var collector = new ActivityContextCollector();
174177

175-
var logEvent = Some.SerilogEvent();
178+
var logEvent = Some.DefaultSerilogEvent();
176179
collector.CollectFor(logEvent);
177180

178181
var logRecord = LogRecordBuilder.ToLogRecord(logEvent, null, IncludedData.TraceIdField | IncludedData.SpanIdField, collector);
@@ -182,10 +185,13 @@ public void IncludeTraceIdAndSpanId()
182185
}
183186

184187
[Fact]
185-
public void IncludeTemplateBody()
188+
public void TemplateBodyIncludesMessageTemplateInBody()
186189
{
187-
var logRecord = LogRecordBuilder.ToLogRecord(Some.SerilogEvent(), null, IncludedData.TemplateBody, new());
190+
var messageTemplate = "Hello, {Name}";
191+
var properties = new List<LogEventProperty> { new("Name", new ScalarValue("World")) };
192+
193+
var logRecord = LogRecordBuilder.ToLogRecord(Some.SerilogEvent(messageTemplate, properties), null, IncludedData.TemplateBody, new());
188194
Assert.NotNull(logRecord.Body);
189-
Assert.Equal(Some.TestMessageTemplate, logRecord.Body.StringValue);
195+
Assert.Equal(messageTemplate, logRecord.Body.StringValue);
190196
}
191197
}

test/Serilog.Sinks.OpenTelemetry.Tests/OpenTelemetryUtilsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class RequestTemplateFactoryTests
2424
// request template to another.
2525
public void TestNoDuplicateLogs()
2626
{
27-
var logEvent = Some.SerilogEvent();
27+
var logEvent = Some.DefaultSerilogEvent();
2828
var logRecord = LogRecordBuilder.ToLogRecord(logEvent, null, IncludedData.None, new());
2929

3030
var requestTemplate = RequestTemplateFactory.CreateRequestTemplate(new Dictionary<string, object>());

test/Serilog.Sinks.OpenTelemetry.Tests/Some.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,21 @@ static class Some
2323

2424
public const string TestMessageTemplate = "Message template {Variable}";
2525

26-
internal static LogEvent SerilogEvent(DateTimeOffset? timestamp = null, Exception? ex = null, string messageTemplate = TestMessageTemplate)
26+
internal static LogEvent DefaultSerilogEvent()
27+
{
28+
return SerilogEvent(
29+
TestMessageTemplate,
30+
new List<LogEventProperty> { new("Variable", new ScalarValue(42)) },
31+
DateTimeOffset.UtcNow,
32+
null);
33+
}
34+
35+
internal static LogEvent SerilogEvent(string messageTemplate, DateTimeOffset? timestamp = null, Exception? ex = null)
36+
{
37+
return SerilogEvent(messageTemplate, new List<LogEventProperty>(), timestamp, ex);
38+
}
39+
40+
internal static LogEvent SerilogEvent(string messageTemplate, IEnumerable<LogEventProperty> properties, DateTimeOffset? timestamp = null, Exception? ex = null)
2741
{
2842
var ts = timestamp ?? DateTimeOffset.UtcNow;
2943
var parser = new MessageTemplateParser();
@@ -33,7 +47,7 @@ internal static LogEvent SerilogEvent(DateTimeOffset? timestamp = null, Exceptio
3347
LogEventLevel.Warning,
3448
ex,
3549
template,
36-
new List<LogEventProperty>{ new("Variable", new ScalarValue(42)) });
50+
properties);
3751

3852
return logEvent;
3953
}

0 commit comments

Comments
 (0)