Skip to content

Commit f31a69f

Browse files
committed
Added unit tests for PropertiesColumnDataGenerator.
1 parent 30bb491 commit f31a69f

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.Data;
5+
using System.Linq;
6+
using Serilog.Events;
7+
using Serilog.Sinks.MSSqlServer.Output;
8+
using Serilog.Sinks.MSSqlServer.Tests.TestUtils;
9+
using Xunit;
10+
11+
namespace Serilog.Sinks.MSSqlServer.Tests.Sinks.MSSqlServer.Output
12+
{
13+
[Trait(TestCategory.TraitName, TestCategory.Unit)]
14+
public class PropertiesColumnDataGeneratorTests
15+
{
16+
private readonly Serilog.Sinks.MSSqlServer.ColumnOptions _columnOptions;
17+
private PropertiesColumnDataGenerator _sut;
18+
19+
public PropertiesColumnDataGeneratorTests()
20+
{
21+
_columnOptions = new Serilog.Sinks.MSSqlServer.ColumnOptions
22+
{
23+
AdditionalColumns = new List<SqlColumn>()
24+
};
25+
}
26+
27+
[Fact]
28+
public void ConvertPropertiesToColumnReturnsKeyValueForAdditionalColumnProperty()
29+
{
30+
// Arrange
31+
const string propertyKey = "AdditionalProperty1";
32+
const string propertyValue = "Additonal Property Value";
33+
var properties = new ReadOnlyDictionary<string, LogEventPropertyValue>(
34+
new Dictionary<string, LogEventPropertyValue>
35+
{
36+
{ propertyKey, new ScalarValue(propertyValue) }
37+
});
38+
_columnOptions.AdditionalColumns.Add(new SqlColumn(propertyKey, SqlDbType.NVarChar));
39+
CreateSut();
40+
41+
// Act
42+
var result = _sut.ConvertPropertiesToColumn(properties).ToArray();
43+
44+
// Assert
45+
Assert.Single(result);
46+
Assert.Equal(propertyKey, result[0].Key);
47+
Assert.Equal(propertyValue, result[0].Value);
48+
}
49+
50+
[Fact]
51+
public void ConvertPropertiesToColumnIgnoresPropertiesNamedLikeStandardColumns()
52+
{
53+
// Arrange
54+
var properties = new ReadOnlyDictionary<string, LogEventPropertyValue>(
55+
new Dictionary<string, LogEventPropertyValue>
56+
{
57+
{ StandardColumn.Id.ToString(), new ScalarValue(1) },
58+
{ StandardColumn.Message.ToString(), new ScalarValue("Message") },
59+
{ StandardColumn.MessageTemplate.ToString(), new ScalarValue("MessageTemplate") },
60+
{ StandardColumn.Level.ToString(), new ScalarValue(1) },
61+
{ StandardColumn.TimeStamp.ToString(), new ScalarValue(new DateTimeOffset(2020, 1, 1, 0, 0, 0, TimeSpan.Zero)) },
62+
{ StandardColumn.Exception.ToString(), new ScalarValue("Exception") },
63+
{ StandardColumn.Properties.ToString(), new ScalarValue("Properties") },
64+
{ StandardColumn.LogEvent.ToString(), new ScalarValue("LogEvent") },
65+
});
66+
CreateSut();
67+
68+
// Act
69+
var result = _sut.ConvertPropertiesToColumn(properties).ToArray();
70+
71+
// Assert
72+
Assert.Empty(result);
73+
}
74+
75+
[Fact]
76+
public void ConvertPropertiesToColumnReturnsDbNullValueForAllowNullColumnProperty()
77+
{
78+
// Arrange
79+
const string propertyKey = "AdditionalProperty1";
80+
var properties = new ReadOnlyDictionary<string, LogEventPropertyValue>(
81+
new Dictionary<string, LogEventPropertyValue>
82+
{
83+
{ propertyKey, new ScalarValue(null) }
84+
});
85+
_columnOptions.AdditionalColumns.Add(new SqlColumn(propertyKey, SqlDbType.NVarChar, allowNull: true));
86+
CreateSut();
87+
88+
// Act
89+
var result = _sut.ConvertPropertiesToColumn(properties).ToArray();
90+
91+
// Assert
92+
Assert.Equal(propertyKey, result[0].Key);
93+
Assert.Equal(DBNull.Value, result[0].Value);
94+
}
95+
96+
[Fact]
97+
public void ConvertPropertiesToColumnConvertsValueType()
98+
{
99+
// Arrange
100+
const string propertyKey = "AdditionalProperty1";
101+
const string propertyValue = "Additonal Property Value";
102+
var properties = new ReadOnlyDictionary<string, LogEventPropertyValue>(
103+
new Dictionary<string, LogEventPropertyValue>
104+
{
105+
{ propertyKey, new ScalarValue(propertyValue) }
106+
});
107+
_columnOptions.AdditionalColumns.Add(new SqlColumn(propertyKey, SqlDbType.NVarChar));
108+
CreateSut();
109+
110+
// Act
111+
var result = _sut.ConvertPropertiesToColumn(properties).ToArray();
112+
113+
// Assert
114+
Assert.Equal(propertyKey, result[0].Key);
115+
Assert.Equal(propertyValue, result[0].Value);
116+
}
117+
118+
[Fact]
119+
public void ConvertPropertiesToColumnConvertsValueTypeToStringIfConversionToColumnTypeFails()
120+
{
121+
// Arrange
122+
const string propertyKey = "AdditionalProperty1";
123+
var properties = new ReadOnlyDictionary<string, LogEventPropertyValue>(
124+
new Dictionary<string, LogEventPropertyValue>
125+
{
126+
{ propertyKey, new ScalarValue(1) }
127+
});
128+
_columnOptions.AdditionalColumns.Add(new SqlColumn(propertyKey, SqlDbType.DateTimeOffset));
129+
CreateSut();
130+
131+
// Act
132+
var result = _sut.ConvertPropertiesToColumn(properties).ToArray();
133+
134+
// Assert
135+
Assert.Equal(propertyKey, result[0].Key);
136+
Assert.IsType<string>(result[0].Value);
137+
Assert.Equal("1", result[0].Value);
138+
}
139+
140+
private void CreateSut()
141+
{
142+
_sut = new PropertiesColumnDataGenerator(_columnOptions);
143+
}
144+
}
145+
}

0 commit comments

Comments
 (0)