-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlServerLocalDbFixture.cs
More file actions
60 lines (54 loc) · 2.16 KB
/
Copy pathSqlServerLocalDbFixture.cs
File metadata and controls
60 lines (54 loc) · 2.16 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
// Copyright (c) 2026 Marco Parenzan
//
// Licensed under the MIT License. See the LICENSE file in the project
// root for full license information.
using Microsoft.Data.SqlClient;
namespace PySharp.Tests.M6_Stdlib;
/// <summary>Real SQL Server LocalDB (MSSQLLocalDB) integration fixture for
/// <see cref="PyodbcTests"/> — unlike sqlite3's ":memory:" tests, this needs an actual running SQL
/// Server instance. Probes reachability once per test class run and creates one throwaway database
/// shared by every test (dropped again on dispose); tests use <see cref="Available"/> with
/// `Skip.IfNot` (via Xunit.SkippableFact) so the suite stays green on a machine/CI agent that has no
/// LocalDB installed, instead of hard-failing. In this dev environment LocalDB is genuinely
/// available (confirmed via `sqllocaldb info` — see SQL_PLAN.md Phase 3), so these tests actually
/// run for real here.</summary>
public sealed class SqlServerLocalDbFixture : IDisposable
{
private const string ServerConnStr =
@"Server=(localdb)\MSSQLLocalDB;Database=master;Trusted_Connection=True;TrustServerCertificate=True;Pooling=False;Connect Timeout=5;";
public bool Available { get; }
public string DatabaseName { get; } = "pysharp_tests_" + Guid.NewGuid().ToString("N");
public SqlServerLocalDbFixture()
{
try
{
using var conn = new SqlConnection(ServerConnStr);
conn.Open();
using var cmd = conn.CreateCommand();
cmd.CommandText = $"CREATE DATABASE [{DatabaseName}]";
cmd.ExecuteNonQuery();
Available = true;
}
catch
{
Available = false;
}
}
public void Dispose()
{
if (!Available)
return;
try
{
using var conn = new SqlConnection(ServerConnStr);
conn.Open();
using var cmd = conn.CreateCommand();
cmd.CommandText = $"ALTER DATABASE [{DatabaseName}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE [{DatabaseName}]";
cmd.ExecuteNonQuery();
}
catch
{
// best-effort cleanup only
}
}
}