Skip to content

Commit 501ddc5

Browse files
committed
Fixed some query result output formatting issues
1 parent 9b3e2fc commit 501ddc5

5 files changed

Lines changed: 62 additions & 5 deletions

File tree

src/DatabaseBenchmark/Common/LightweightDataRow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class LightweightDataRow
66

77
public object this[string columnName]
88
{
9-
get => _columnValues[columnName];
9+
get => _columnValues.GetValueOrDefault(columnName);
1010
set => _columnValues[columnName] = value;
1111
}
1212

src/DatabaseBenchmark/Core/ValueFormatter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ public class ValueFormatter : IValueFormatter
99
public string Format(object value) =>
1010
value switch
1111
{
12+
null => "NULL",
1213
string => $"\"{value}\"",
1314
IEnumerable arrayValue => $"[{string.Join(", ", arrayValue.Cast<object>().Select(Format))}]",
1415
DateTime dateTimeValue => dateTimeValue.ToSortableString(), // TODO: Make output date/time format configurable
1516
DateTimeOffset dateTimeOffsetValue => dateTimeOffsetValue.ToSortableString(), // TODO: Make output date/time format configurable
16-
double or float => string.Format($"{value:0.##}"), // TODO: Make precision configurable
17+
double or float or decimal => string.Format($"{value:0.##}"), // TODO: Make precision configurable
1718
bool boolValue => boolValue.ToString(),
1819
_ when value.IsNumber() => value.ToString(),
1920
_ => $"\"{value}\"" // TODO: Make quotemarks configurable

src/DatabaseBenchmark/Reporting/TextTableReportFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public TextTableReportFormatter(IValueFormatter valueFormatter)
1515

1616
public void Print(Stream stream, LightweightDataTable results)
1717
{
18-
using var writer = new StreamWriter(stream);
18+
using var writer = new StreamWriter(stream, leaveOpen: true);
1919

2020
foreach (var column in results.Columns)
2121
{

tests/DatabaseBenchmark.Tests/Core/ValueFormatterTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ public class ValueFormatterTests
1919
[(long)123, "123"],
2020
[(float)1.2345, "1.23"],
2121
[(double)1.2345, "1.23"],
22-
[(decimal)1.2345, "1.2345"],
22+
[(decimal)1.2345, "1.23"],
2323
[true, "True"],
2424
["abc", "\"abc\""],
2525
[new Guid("6971bc34-4117-4dba-a405-d2ffa82bf6d2"), "\"6971bc34-4117-4dba-a405-d2ffa82bf6d2\""],
2626
[new string[] {"one", "two", "three"}, "[\"one\", \"two\", \"three\"]"],
2727
[new double[] {1.765, 12.351}, "[1.77, 12.35]"],
2828
[new DateTime(2023, 12, 11), "2023-12-11T00:00:00.0000000"],
29-
[new DateTime(2023, 12, 11, 1, 2, 3), "2023-12-11T01:02:03.0000000"]
29+
[new DateTime(2023, 12, 11, 1, 2, 3), "2023-12-11T01:02:03.0000000"],
30+
[null, "NULL"]
3031
];
3132

3233
[Theory]
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using DatabaseBenchmark.Common;
2+
using DatabaseBenchmark.Core;
3+
using DatabaseBenchmark.Reporting;
4+
using System;
5+
using System.IO;
6+
using Xunit;
7+
8+
namespace DatabaseBenchmark.Tests.Reporting
9+
{
10+
public class TextTableReportFormatterTests
11+
{
12+
private const string NameColumn = "Name";
13+
private const string CountColumn = "Count";
14+
private const string OptionalColumn = "Optional";
15+
16+
private readonly LightweightDataTable _sampleResults;
17+
18+
public TextTableReportFormatterTests()
19+
{
20+
_sampleResults = new LightweightDataTable();
21+
_sampleResults.Columns.Add(new LightweightDataColumn(NameColumn, _sampleResults));
22+
_sampleResults.Columns.Add(new LightweightDataColumn(CountColumn, _sampleResults));
23+
_sampleResults.Columns.Add(new LightweightDataColumn(OptionalColumn, _sampleResults));
24+
var row = _sampleResults.AddRow();
25+
row[NameColumn] = "Regular row";
26+
row[CountColumn] = 1;
27+
row = _sampleResults.AddRow();
28+
row[NameColumn] = "Optional attribute row";
29+
row[CountColumn] = 12;
30+
row[OptionalColumn] = 123.4567;
31+
}
32+
33+
[Fact]
34+
public void PrintTable()
35+
{
36+
var tableFormatter = new TextTableReportFormatter(new ValueFormatter());
37+
using var stream = new MemoryStream();
38+
39+
tableFormatter.Print(stream, _sampleResults);
40+
41+
stream.Seek(0, SeekOrigin.Begin);
42+
using var reader = new StreamReader(stream);
43+
var result = reader.ReadToEnd();
44+
45+
var expectedText = "+------------------------+-----+--------+" + Environment.NewLine +
46+
"| Name|Count|Optional|" + Environment.NewLine +
47+
"+------------------------+-----+--------+" + Environment.NewLine +
48+
"|\"Regular row\" | 1|NULL |" + Environment.NewLine +
49+
"|\"Optional attribute row\"| 12| 123.46|" + Environment.NewLine +
50+
"+------------------------+-----+--------+" + Environment.NewLine + Environment.NewLine;
51+
52+
Assert.Equal(expectedText, result);
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)