-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathStatsTestFixture.cs
More file actions
121 lines (96 loc) · 4.5 KB
/
Copy pathStatsTestFixture.cs
File metadata and controls
121 lines (96 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using NpgsqlRestClient;
using Npgsql;
namespace NpgsqlRestTests.Setup;
[CollectionDefinition("StatsTestFixture")]
public class StatsTestFixtureCollection : ICollectionFixture<StatsTestFixture> { }
/// <summary>
/// Test fixture for PostgreSQL Stats endpoint tests.
/// Creates a web application with stats endpoints enabled to verify:
/// - /stats/routines endpoint returns function/procedure statistics
/// - /stats/tables endpoint returns table statistics
/// - /stats/indexes endpoint returns index statistics
/// - /stats/activity endpoint returns current session activity
/// </summary>
public class StatsTestFixture : IDisposable
{
private readonly WebApplication _app;
private readonly HttpClient _client;
private readonly string _connectionString;
public HttpClient Client => _client;
public string ServerAddress { get; }
/// <summary>
/// Stats endpoint paths
/// </summary>
public const string RoutinesPath = "/stats/routines";
public const string TablesPath = "/stats/tables";
public const string IndexesPath = "/stats/indexes";
public const string ActivityPath = "/stats/activity";
public StatsTestFixture()
{
_connectionString = Database.Create();
// Create some test data for stats
CreateTestData();
var builder = WebApplication.CreateBuilder();
builder.WebHost.UseUrls("http://127.0.0.1:0");
_app = builder.Build();
// Map stats endpoints manually (simulating what Program.cs does)
_app.MapGet(RoutinesPath, async (Microsoft.AspNetCore.Http.HttpContext context) =>
await StatsEndpoints.HandleRoutinesStats(context, _connectionString, "json", null, false, null, null, null));
_app.MapGet(TablesPath, async (Microsoft.AspNetCore.Http.HttpContext context) =>
await StatsEndpoints.HandleTablesStats(context, _connectionString, "json", null, false, null, null, null));
_app.MapGet(IndexesPath, async (Microsoft.AspNetCore.Http.HttpContext context) =>
await StatsEndpoints.HandleIndexesStats(context, _connectionString, "json", null, false, null, null, null));
_app.MapGet(ActivityPath, async (Microsoft.AspNetCore.Http.HttpContext context) =>
await StatsEndpoints.HandleActivityStats(context, _connectionString, "json", false, null, null, null));
// Add HTML endpoints for testing HTML format
_app.MapGet("/stats/tables-html", async (Microsoft.AspNetCore.Http.HttpContext context) =>
await StatsEndpoints.HandleTablesStats(context, _connectionString, "html", null, false, null, null, null));
// Add NpgsqlRest for completeness
_app.UseNpgsqlRest(new NpgsqlRestOptions(_connectionString)
{
IncludeSchemas = ["public"],
NameNotSimilarTo = "(cp[_x]|rlpt[_]|ast[_])%", // exclude functions owned by CacheProfilesTestFixture, RateLimiterPartitionTestFixture, and AuthSchemeTestFixture
CommentsMode = CommentsMode.ParseAll,
RequiresAuthorization = false
});
_app.StartAsync().GetAwaiter().GetResult();
ServerAddress = _app.Urls.First();
_client = new HttpClient { BaseAddress = new Uri(ServerAddress) };
_client.Timeout = TimeSpan.FromMinutes(5);
}
private void CreateTestData()
{
using var connection = new NpgsqlConnection(_connectionString);
connection.Open();
using var command = connection.CreateCommand();
// Create a test table for table stats
command.CommandText = """
CREATE TABLE IF NOT EXISTS stats_test_table (
id serial PRIMARY KEY,
name text NOT NULL,
created_at timestamptz DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_stats_test_name ON stats_test_table(name);
INSERT INTO stats_test_table (name)
SELECT 'test_' || i FROM generate_series(1, 10) AS i
ON CONFLICT DO NOTHING;
CREATE OR REPLACE FUNCTION stats_test_function() RETURNS integer AS $$
BEGIN
RETURN 42;
END;
$$ LANGUAGE plpgsql;
SELECT stats_test_function();
""";
command.ExecuteNonQuery();
}
#pragma warning disable CA1816
public void Dispose()
#pragma warning restore CA1816
{
_client.Dispose();
_app.StopAsync().GetAwaiter().GetResult();
_app.DisposeAsync().GetAwaiter().GetResult();
}
}