Skip to content

Commit 1b3f1f2

Browse files
Copilotmo-esmp
andcommitted
Add comprehensive dashboard tests for PostgresDataProvider
Co-authored-by: mo-esmp <[email protected]>
1 parent d244f47 commit 1b3f1f2

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using FluentAssertions;
4+
using Postgres.Tests.Util;
5+
using Serilog.Ui.Core;
6+
using Serilog.Ui.Core.Models;
7+
using Xunit;
8+
9+
namespace Postgres.Tests.DataProvider;
10+
11+
[Collection(nameof(PostgresTestProvider))]
12+
[Trait("Integration-Dashboard", "Postgres")]
13+
public class DataProviderDashboardTests(PostgresTestProvider instance)
14+
{
15+
private readonly IDataProvider _provider = instance.GetDataProvider();
16+
17+
[Fact]
18+
public async Task FetchDashboardAsync_Should_Return_Dashboard_Model_With_Correct_Structure()
19+
{
20+
// Act
21+
var dashboard = await _provider.FetchDashboardAsync();
22+
23+
// Assert
24+
dashboard.Should().NotBeNull();
25+
dashboard.TotalLogs.Should().BeGreaterThanOrEqualTo(0);
26+
dashboard.LogsByLevel.Should().NotBeNull();
27+
dashboard.TodayLogs.Should().BeGreaterThanOrEqualTo(0);
28+
dashboard.TodayErrorLogs.Should().BeGreaterThanOrEqualTo(0);
29+
}
30+
31+
[Fact]
32+
public async Task FetchDashboardAsync_Should_Return_Consistent_Log_Counts()
33+
{
34+
// Act
35+
var dashboard = await _provider.FetchDashboardAsync();
36+
37+
// Assert
38+
dashboard.Should().NotBeNull();
39+
40+
// Today's logs should not exceed total logs
41+
dashboard.TodayLogs.Should().BeLessThanOrEqualTo(dashboard.TotalLogs);
42+
43+
// Today's error logs should not exceed today's total logs
44+
dashboard.TodayErrorLogs.Should().BeLessThanOrEqualTo(dashboard.TodayLogs);
45+
}
46+
47+
[Fact]
48+
public async Task FetchDashboardAsync_Should_Return_Valid_Logs_By_Level_Dictionary()
49+
{
50+
// Act
51+
var dashboard = await _provider.FetchDashboardAsync();
52+
53+
// Assert
54+
dashboard.Should().NotBeNull();
55+
dashboard.LogsByLevel.Should().NotBeNull();
56+
57+
// Sum of all level counts should not exceed total logs (could be less if data exists from before)
58+
var sumByLevel = 0;
59+
foreach (var kvp in dashboard.LogsByLevel)
60+
{
61+
kvp.Key.Should().NotBeNullOrEmpty();
62+
kvp.Value.Should().BeGreaterThanOrEqualTo(0);
63+
sumByLevel += kvp.Value;
64+
}
65+
66+
sumByLevel.Should().BeLessThanOrEqualTo(dashboard.TotalLogs);
67+
}
68+
69+
[Fact]
70+
public async Task FetchDashboardAsync_Should_Handle_Empty_Database()
71+
{
72+
// Note: This test assumes the database might be empty or the provider handles empty results gracefully
73+
74+
// Act
75+
var dashboard = await _provider.FetchDashboardAsync();
76+
77+
// Assert
78+
dashboard.Should().NotBeNull();
79+
dashboard.TotalLogs.Should().BeGreaterThanOrEqualTo(0);
80+
dashboard.LogsByLevel.Should().NotBeNull();
81+
dashboard.TodayLogs.Should().BeGreaterThanOrEqualTo(0);
82+
dashboard.TodayErrorLogs.Should().BeGreaterThanOrEqualTo(0);
83+
84+
// If total logs is 0, all other counts should also be 0
85+
if (dashboard.TotalLogs == 0)
86+
{
87+
dashboard.TodayLogs.Should().Be(0);
88+
dashboard.TodayErrorLogs.Should().Be(0);
89+
dashboard.LogsByLevel.Should().BeEmpty();
90+
}
91+
}
92+
93+
[Fact]
94+
public async Task FetchDashboardAsync_Should_Return_Dashboard_Model_With_Today_Date_Filter()
95+
{
96+
// Arrange
97+
var today = DateTime.Today;
98+
99+
// Act
100+
var dashboard = await _provider.FetchDashboardAsync();
101+
102+
// Assert
103+
dashboard.Should().NotBeNull();
104+
105+
// Today's error logs should be a subset of today's total logs
106+
dashboard.TodayErrorLogs.Should().BeLessThanOrEqualTo(dashboard.TodayLogs);
107+
108+
// Today's logs should be a subset of total logs
109+
dashboard.TodayLogs.Should().BeLessThanOrEqualTo(dashboard.TotalLogs);
110+
}
111+
}
112+
113+
[Collection(nameof(PostgresAdditionalColsTestProvider))]
114+
[Trait("Integration-Dashboard-AdditionalColumns", "Postgres")]
115+
public class DataProviderDashboardWithColsTests(PostgresAdditionalColsTestProvider instance)
116+
{
117+
private readonly IDataProvider _provider = instance.GetDataProvider();
118+
119+
[Fact]
120+
public async Task FetchDashboardAsync_Should_Work_With_Additional_Columns()
121+
{
122+
// Act
123+
var dashboard = await _provider.FetchDashboardAsync();
124+
125+
// Assert
126+
dashboard.Should().NotBeNull();
127+
dashboard.TotalLogs.Should().BeGreaterThanOrEqualTo(0);
128+
dashboard.LogsByLevel.Should().NotBeNull();
129+
dashboard.TodayLogs.Should().BeGreaterThanOrEqualTo(0);
130+
dashboard.TodayErrorLogs.Should().BeGreaterThanOrEqualTo(0);
131+
}
132+
133+
[Fact]
134+
public async Task FetchDashboardAsync_Should_Return_Consistent_Counts_With_Additional_Columns()
135+
{
136+
// Act
137+
var dashboard = await _provider.FetchDashboardAsync();
138+
139+
// Assert
140+
dashboard.Should().NotBeNull();
141+
142+
// Validate logical relationships between counts
143+
dashboard.TodayLogs.Should().BeLessThanOrEqualTo(dashboard.TotalLogs);
144+
dashboard.TodayErrorLogs.Should().BeLessThanOrEqualTo(dashboard.TodayLogs);
145+
146+
// Validate LogsByLevel structure
147+
foreach (var kvp in dashboard.LogsByLevel)
148+
{
149+
kvp.Key.Should().NotBeNullOrEmpty();
150+
kvp.Value.Should().BeGreaterThanOrEqualTo(0);
151+
}
152+
}
153+
}

0 commit comments

Comments
 (0)