Skip to content

Commit 00fdd94

Browse files
committed
- fix tests for SQL to run using container
1 parent 88d8005 commit 00fdd94

3 files changed

Lines changed: 57 additions & 28 deletions

File tree

.github/workflows/master-build.yml

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,26 @@ jobs:
6262
--no-build --configuration Release --verbosity normal
6363
--logger "console;verbosity=normal"
6464
65-
# ── 3. SQL Server unit tests (no live DB — integration tests filtered out) ────
66-
test-sqlserver-unit:
67-
name: "Tests — SQL Server (unit only)"
65+
# ── 3. SQL Server tests (full suite against SQL Server 2022 in Docker) ─────────
66+
test-sqlserver:
67+
name: "Tests — SQL Server"
6868
needs: build
6969
runs-on: ubuntu-latest
70+
71+
services:
72+
sqlserver:
73+
image: mcr.microsoft.com/mssql/server:2022-latest
74+
env:
75+
ACCEPT_EULA: "Y"
76+
MSSQL_SA_PASSWORD: "Pa55w0rd!"
77+
ports:
78+
- 1433:1433
79+
options: >-
80+
--health-cmd "/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P 'Pa55w0rd!' -No -Q 'SELECT 1'"
81+
--health-interval 10s
82+
--health-timeout 5s
83+
--health-retries 10
84+
7085
steps:
7186
- uses: actions/checkout@v4
7287

@@ -81,11 +96,12 @@ jobs:
8196
- name: Build (Release)
8297
run: dotnet build --no-restore --configuration Release
8398

84-
- name: "Test: ActiveForge.SqlServer.Tests (unit only)"
99+
- name: "Test: ActiveForge.SqlServer.Tests"
100+
env:
101+
SS_ADMIN_CONNSTR: "Server=localhost,1433;Database=master;User Id=sa;Password=Pa55w0rd!;TrustServerCertificate=True"
85102
run: >
86103
dotnet test tests/ActiveForge.SqlServer.Tests/ActiveForge.SqlServer.Tests.csproj
87104
--no-build --configuration Release --verbosity normal
88-
--filter "Category!=Integration"
89105
--logger "console;verbosity=normal"
90106
91107
# ── 4. PostgreSQL integration tests ──────────────────────────────────────────
@@ -132,25 +148,34 @@ jobs:
132148
--logger "console;verbosity=normal"
133149
134150
# ── 5. MongoDB integration tests ─────────────────────────────────────────────
151+
# Transactions require a replica set; service containers can't be started with
152+
# --replSet, so we start MongoDB manually and initialise the replica set.
135153
test-mongodb:
136154
name: "Tests — MongoDB"
137155
needs: build
138156
runs-on: ubuntu-latest
139157

140-
services:
141-
mongodb:
142-
image: mongo:7
143-
ports:
144-
- 27017:27017
145-
options: >-
146-
--health-cmd "mongosh --eval 'db.adminCommand(\"ping\")' --quiet"
147-
--health-interval 10s
148-
--health-timeout 10s
149-
--health-retries 5
150-
151158
steps:
152159
- uses: actions/checkout@v4
153160

161+
- name: Start MongoDB replica set
162+
run: |
163+
docker run -d --name mongo \
164+
-p 27017:27017 \
165+
mongo:7 --replSet rs0 --bind_ip_all
166+
# Wait for mongod to accept connections
167+
for i in $(seq 1 30); do
168+
docker exec mongo mongosh --quiet --eval "db.adminCommand('ping')" \
169+
&& break || sleep 2
170+
done
171+
# Initialise the single-node replica set
172+
docker exec mongo mongosh --quiet --eval "rs.initiate({_id:'rs0',members:[{_id:0,host:'127.0.0.1:27017'}]})"
173+
# Wait until the node becomes PRIMARY
174+
for i in $(seq 1 20); do
175+
STATUS=$(docker exec mongo mongosh --quiet --eval "rs.status().myState")
176+
[ "$STATUS" = "1" ] && break || sleep 2
177+
done
178+
154179
- name: Setup .NET 8
155180
uses: actions/setup-dotnet@v4
156181
with:

tests/ActiveForge.SqlServer.Tests/SqlServerConnectionCrudTests.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,16 @@ public sealed class SqlServerConnectionCrudTests : IDisposable
3737
private readonly string _dbName =
3838
$"af_crud_{System.Threading.Interlocked.Increment(ref _counter)}";
3939

40-
private const string MasterConnStr =
41-
@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=master;" +
42-
"Integrated Security=True;TrustServerCertificate=True";
40+
private static string MasterConnStr =>
41+
Environment.GetEnvironmentVariable("SS_ADMIN_CONNSTR")
42+
?? @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;TrustServerCertificate=True";
4343

4444
private string TestConnStr =>
45-
$@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog={_dbName};" +
46-
"Integrated Security=True;TrustServerCertificate=True;" +
47-
"MultipleActiveResultSets=True";
45+
new Microsoft.Data.SqlClient.SqlConnectionStringBuilder(MasterConnStr)
46+
{
47+
InitialCatalog = _dbName,
48+
MultipleActiveResultSets = true,
49+
}.ConnectionString;
4850

4951
private readonly SqlServerConnection _conn;
5052

tests/ActiveForge.SqlServer.Tests/SqlServerJoinIntegrationTests.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,16 @@ public sealed class SqlServerJoinIntegrationTests : IDisposable
6363
private readonly string _dbName =
6464
$"af_join_{System.Threading.Interlocked.Increment(ref _counter)}";
6565

66-
private const string MasterConnStr =
67-
@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=master;" +
68-
"Integrated Security=True;TrustServerCertificate=True";
66+
private static string MasterConnStr =>
67+
Environment.GetEnvironmentVariable("SS_ADMIN_CONNSTR")
68+
?? @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;TrustServerCertificate=True";
6969

7070
private string TestConnStr =>
71-
$@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog={_dbName};" +
72-
"Integrated Security=True;TrustServerCertificate=True;" +
73-
"MultipleActiveResultSets=True";
71+
new Microsoft.Data.SqlClient.SqlConnectionStringBuilder(MasterConnStr)
72+
{
73+
InitialCatalog = _dbName,
74+
MultipleActiveResultSets = true,
75+
}.ConnectionString;
7476

7577
private readonly SqlServerConnection _conn;
7678
private int _electronicsId;

0 commit comments

Comments
 (0)