Skip to content

Commit f27623b

Browse files
renemadsenclaude
andcommitted
docs: add plan for migrating InMemory tests to TestBaseSetup
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent fd04df3 commit f27623b

File tree

1 file changed

+313
-0
lines changed

1 file changed

+313
-0
lines changed
Lines changed: 313 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,313 @@
1+
# Migrate InMemory Tests to TestBaseSetup Implementation Plan
2+
3+
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
4+
5+
**Goal:** Migrate `DeviceTokenServiceTests.cs` and `PushNotificationServiceTests.cs` off the `Microsoft.EntityFrameworkCore.InMemory` provider onto the existing `TestBaseSetup` base class (Testcontainers + MariaDB), so the already-staged removal of the InMemory NuGet package does not break the test project build.
6+
7+
**Architecture:** The TimePlanning plugin test project already uses `Testcontainers.MariaDb` via `TestBaseSetup.cs` (inherited by ~20 other test classes like `GpsCoordinateServiceTests`). Two orphaned test files still spin up EF Core InMemory contexts directly; those contexts become uncompilable once `Microsoft.EntityFrameworkCore.InMemory` is dropped from the csproj. Migration is mechanical: inherit from `TestBaseSetup`, call `await base.Setup()` in `[SetUp]`, replace local `_dbContext` field usage with the inherited `TimePlanningPnDbContext` field. No production code changes.
8+
9+
**Tech Stack:** .NET, NUnit, EF Core, Testcontainers.MariaDb, NSubstitute.
10+
11+
**Working directory:** `/home/rene/Documents/workspace/microting/eform-angular-timeplanning-plugin/` (source plugin repo — not dev-mode, edits land directly here).
12+
13+
---
14+
15+
## File Structure
16+
17+
**Modified:**
18+
- `eFormAPI/Plugins/TimePlanning.Pn/TimePlanning.Pn.Test/DeviceTokenServiceTests.cs` — inherit `TestBaseSetup`, remove InMemory provider.
19+
- `eFormAPI/Plugins/TimePlanning.Pn/TimePlanning.Pn.Test/PushNotificationServiceTests.cs` — inherit `TestBaseSetup`, remove InMemory provider.
20+
- `eFormAPI/Plugins/TimePlanning.Pn/TimePlanning.Pn.Test/TimePlanning.Pn.Test.csproj` — already staged in working tree (InMemory package reference removed). Committed as part of the final task.
21+
22+
**Unchanged:**
23+
- `TestBaseSetup.cs` — already provides `TimePlanningPnDbContext` field initialized per test against a containerized MariaDB.
24+
- Production services (`DeviceTokenService`, `PushNotificationService`) — no behaviour change under test.
25+
26+
---
27+
28+
## Task 1: Migrate `DeviceTokenServiceTests.cs` to TestBaseSetup
29+
30+
**Files:**
31+
- Modify: `eFormAPI/Plugins/TimePlanning.Pn/TimePlanning.Pn.Test/DeviceTokenServiceTests.cs`
32+
33+
**Context the new engineer needs:**
34+
- `TestBaseSetup` exposes a protected `TimePlanningPnDbContext? TimePlanningPnDbContext` field, initialized in its `[SetUp] Setup()` against a fresh MariaDB (DB dropped + re-migrated per test). See lines 26 and 101 of `TestBaseSetup.cs` (`/home/rene/Documents/workspace/microting/eform-angular-timeplanning-plugin/eFormAPI/Plugins/TimePlanning.Pn/TimePlanning.Pn.Test/TestBaseSetup.cs`).
35+
- The canonical pattern for a derived test class is `GpsCoordinateServiceTests.cs` — inherit, override `SetUp`, call `await base.Setup()` first.
36+
- The current `DeviceTokenServiceTests` uses a per-test InMemory database name keyed on `TestContext.CurrentContext.Test.Name`. That isolation is automatic under `TestBaseSetup` because the base class drops & re-migrates the DB on each `[SetUp]`.
37+
- A real MariaDB enforces the unique index on `DeviceToken.Token` (defined in `TimePlanningPnDbContext` with `HasIndex(x => x.Token).IsUnique()`). The existing test `RegisterAsync_SameTokenTwice_UpsertsWithoutDuplicate` now becomes a genuine validation of that constraint rather than a no-op.
38+
39+
- [ ] **Step 1: Establish baseline — attempt the build and confirm the InMemory removal currently breaks it**
40+
41+
Run:
42+
```bash
43+
cd /home/rene/Documents/workspace/microting/eform-angular-timeplanning-plugin/eFormAPI/Plugins/TimePlanning.Pn/TimePlanning.Pn.Test && dotnet build
44+
```
45+
46+
Expected: build fails with errors like `The type or namespace 'UseInMemoryDatabase' could not be found` (or equivalent) pointing at `DeviceTokenServiceTests.cs` line 22 and `PushNotificationServiceTests.cs` lines 17 and 34. This confirms the InMemory package has been removed from the csproj and the two orphaned files need migrating.
47+
48+
- [ ] **Step 2: Rewrite `DeviceTokenServiceTests.cs` to inherit `TestBaseSetup`**
49+
50+
Replace the entire file `eFormAPI/Plugins/TimePlanning.Pn/TimePlanning.Pn.Test/DeviceTokenServiceTests.cs` with:
51+
52+
```csharp
53+
using System.Linq;
54+
using System.Threading.Tasks;
55+
using Microsoft.EntityFrameworkCore;
56+
using Microsoft.Extensions.Logging;
57+
using NSubstitute;
58+
using NUnit.Framework;
59+
using TimePlanning.Pn.Services.DeviceTokenService;
60+
61+
namespace TimePlanning.Pn.Test;
62+
63+
[TestFixture]
64+
public class DeviceTokenServiceTests : TestBaseSetup
65+
{
66+
private DeviceTokenService _service = null!;
67+
68+
[SetUp]
69+
public async Task SetUp()
70+
{
71+
await base.Setup();
72+
73+
_service = new DeviceTokenService(
74+
TimePlanningPnDbContext!,
75+
Substitute.For<ILogger<DeviceTokenService>>());
76+
}
77+
78+
[Test]
79+
public async Task RegisterAsync_NewToken_IsStored()
80+
{
81+
var result = await _service.RegisterAsync(42, "fcm-token-abc", "android");
82+
83+
Assert.That(result.Success, Is.True);
84+
85+
var stored = await TimePlanningPnDbContext!.DeviceTokens.SingleAsync();
86+
Assert.That(stored.SdkSiteId, Is.EqualTo(42));
87+
Assert.That(stored.Token, Is.EqualTo("fcm-token-abc"));
88+
Assert.That(stored.Platform, Is.EqualTo("android"));
89+
}
90+
91+
[Test]
92+
public async Task RegisterAsync_SameTokenTwice_UpsertsWithoutDuplicate()
93+
{
94+
await _service.RegisterAsync(1, "dup-token", "android");
95+
96+
var result = await _service.RegisterAsync(2, "dup-token", "ios");
97+
98+
Assert.That(result.Success, Is.True);
99+
Assert.That(await TimePlanningPnDbContext!.DeviceTokens.CountAsync(), Is.EqualTo(1));
100+
101+
var stored = await TimePlanningPnDbContext.DeviceTokens.SingleAsync();
102+
Assert.That(stored.SdkSiteId, Is.EqualTo(2));
103+
Assert.That(stored.Platform, Is.EqualTo("ios"));
104+
}
105+
106+
[Test]
107+
public async Task UnregisterAsync_ExistingToken_IsRemoved()
108+
{
109+
await _service.RegisterAsync(1, "remove-me", "android");
110+
Assert.That(await TimePlanningPnDbContext!.DeviceTokens.CountAsync(), Is.EqualTo(1));
111+
112+
var result = await _service.UnregisterAsync("remove-me");
113+
114+
Assert.That(result.Success, Is.True);
115+
}
116+
117+
[Test]
118+
public async Task UnregisterAsync_NonExistentToken_SucceedsWithoutError()
119+
{
120+
var result = await _service.UnregisterAsync("does-not-exist");
121+
122+
Assert.That(result.Success, Is.True);
123+
}
124+
}
125+
```
126+
127+
Notes on the diff:
128+
- Drops `Microting.TimePlanningBase.Infrastructure.Data` import (the DbContext is inherited as a field).
129+
- Removes the private `_dbContext` field; uses the inherited `TimePlanningPnDbContext` member (nullable, so dereference with `!`).
130+
- Removes `[TearDown]` entirely; `TestBaseSetup.TearDown()` already disposes the context.
131+
- Local `databaseName:` per-test key is no longer needed — `TestBaseSetup.Setup()` drops and re-migrates per `[SetUp]`.
132+
133+
- [ ] **Step 3: Build just the test project to verify `DeviceTokenServiceTests` compiles**
134+
135+
Run:
136+
```bash
137+
cd /home/rene/Documents/workspace/microting/eform-angular-timeplanning-plugin/eFormAPI/Plugins/TimePlanning.Pn/TimePlanning.Pn.Test && dotnet build
138+
```
139+
140+
Expected: compile errors for `DeviceTokenServiceTests.cs` are gone. Remaining errors will be limited to `PushNotificationServiceTests.cs` (handled in Task 2). If you see any other compile error, stop and investigate — it indicates a pre-existing issue unrelated to this migration.
141+
142+
- [ ] **Step 4: Run the migrated DeviceTokenServiceTests and verify green**
143+
144+
Run:
145+
```bash
146+
cd /home/rene/Documents/workspace/microting/eform-angular-timeplanning-plugin/eFormAPI/Plugins/TimePlanning.Pn/TimePlanning.Pn.Test && dotnet test --filter "FullyQualifiedName~DeviceTokenServiceTests"
147+
```
148+
149+
Expected: 4 tests pass. First run will take longer (~20–40s) because Testcontainers boots a MariaDB image. If `RegisterAsync_SameTokenTwice_UpsertsWithoutDuplicate` fails with a unique-constraint violation, that is a genuine finding — the service upsert logic races or uses the wrong index — and should be reported rather than worked around. Do not modify the production service here.
150+
151+
If the command cannot run PushNotificationServiceTests yet because the test project still has compile errors, use `dotnet build -p:CompilerErrorsAsWarnings=false` as normal — the filter will only run matching tests, but compilation still has to succeed project-wide. In that case, skip the run to after Task 2 and run both together.
152+
153+
- [ ] **Step 5: Commit the DeviceTokenServiceTests migration**
154+
155+
Run:
156+
```bash
157+
cd /home/rene/Documents/workspace/microting/eform-angular-timeplanning-plugin && git add eFormAPI/Plugins/TimePlanning.Pn/TimePlanning.Pn.Test/DeviceTokenServiceTests.cs && git commit -m "test: migrate DeviceTokenServiceTests off EF InMemory onto TestBaseSetup
158+
159+
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>"
160+
```
161+
162+
Expected: a single-file commit on the current `stable` branch.
163+
164+
---
165+
166+
## Task 2: Migrate `PushNotificationServiceTests.cs` to TestBaseSetup
167+
168+
**Files:**
169+
- Modify: `eFormAPI/Plugins/TimePlanning.Pn/TimePlanning.Pn.Test/PushNotificationServiceTests.cs`
170+
171+
**Context the new engineer needs:**
172+
- `PushNotificationService`'s constructor queries `PluginConfigurationValues` during construction (see `PushNotificationService.cs:28-29`). Under the real MariaDB provided by `TestBaseSetup`, that table exists (migrations create it) and the `FirstOrDefault` returns `null` — so the `_isEnabled` path stays `false`, which is exactly what the two tests validate ("without Firebase config").
173+
- Both tests assert fire-and-forget / constructor behaviour: no DB rows read or written. The migration simply swaps the DbContext source; the assertions stay identical.
174+
175+
- [ ] **Step 1: Rewrite `PushNotificationServiceTests.cs` to inherit `TestBaseSetup`**
176+
177+
Replace the entire file `eFormAPI/Plugins/TimePlanning.Pn/TimePlanning.Pn.Test/PushNotificationServiceTests.cs` with:
178+
179+
```csharp
180+
using System.Threading.Tasks;
181+
using Microsoft.Extensions.Logging;
182+
using NSubstitute;
183+
using NUnit.Framework;
184+
using TimePlanning.Pn.Services.PushNotificationService;
185+
186+
namespace TimePlanning.Pn.Test;
187+
188+
[TestFixture]
189+
public class PushNotificationServiceTests : TestBaseSetup
190+
{
191+
[SetUp]
192+
public async Task SetUp()
193+
{
194+
await base.Setup();
195+
}
196+
197+
[Test]
198+
public void Constructor_WithoutFirebaseConfig_DoesNotThrow()
199+
{
200+
Assert.DoesNotThrow(() =>
201+
{
202+
_ = new PushNotificationService(
203+
TimePlanningPnDbContext!,
204+
Substitute.For<ILogger<PushNotificationService>>());
205+
});
206+
}
207+
208+
[Test]
209+
public async Task SendToSiteAsync_WhenFirebaseNotConfigured_IsNoOp()
210+
{
211+
var service = new PushNotificationService(
212+
TimePlanningPnDbContext!,
213+
Substitute.For<ILogger<PushNotificationService>>());
214+
215+
await service.SendToSiteAsync(1, "Title", "Body");
216+
}
217+
}
218+
```
219+
220+
Notes on the diff:
221+
- Drops `Microsoft.EntityFrameworkCore` and `Microting.TimePlanningBase.Infrastructure.Data` imports — no longer needed at call site.
222+
- Removes both local `DbContextOptionsBuilder` blocks; uses inherited `TimePlanningPnDbContext`.
223+
- Removes per-test `dbContext.Dispose()``TestBaseSetup.TearDown()` handles it.
224+
- `SendToSiteAsync_WhenFirebaseNotConfigured_IsNoOp` keeps its `async Task` signature because `SendToSiteAsync` is awaited; no assertion needed — passing = didn't throw.
225+
226+
- [ ] **Step 2: Build the test project**
227+
228+
Run:
229+
```bash
230+
cd /home/rene/Documents/workspace/microting/eform-angular-timeplanning-plugin/eFormAPI/Plugins/TimePlanning.Pn/TimePlanning.Pn.Test && dotnet build
231+
```
232+
233+
Expected: build succeeds with no errors. All references to `UseInMemoryDatabase` are gone.
234+
235+
- [ ] **Step 3: Run the migrated PushNotificationServiceTests and verify green**
236+
237+
Run:
238+
```bash
239+
cd /home/rene/Documents/workspace/microting/eform-angular-timeplanning-plugin/eFormAPI/Plugins/TimePlanning.Pn/TimePlanning.Pn.Test && dotnet test --filter "FullyQualifiedName~PushNotificationServiceTests&FullyQualifiedName!~Integration"
240+
```
241+
242+
The `!~Integration` exclusion avoids running `PushNotificationIntegrationTests`, which is a larger test and orthogonal to this migration.
243+
244+
Expected: 2 tests pass.
245+
246+
- [ ] **Step 4: Commit the PushNotificationServiceTests migration**
247+
248+
Run:
249+
```bash
250+
cd /home/rene/Documents/workspace/microting/eform-angular-timeplanning-plugin && git add eFormAPI/Plugins/TimePlanning.Pn/TimePlanning.Pn.Test/PushNotificationServiceTests.cs && git commit -m "test: migrate PushNotificationServiceTests off EF InMemory onto TestBaseSetup
251+
252+
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>"
253+
```
254+
255+
Expected: a single-file commit on the current `stable` branch.
256+
257+
---
258+
259+
## Task 3: Full-suite verification and csproj commit
260+
261+
**Files:**
262+
- Modify: `eFormAPI/Plugins/TimePlanning.Pn/TimePlanning.Pn.Test/TimePlanning.Pn.Test.csproj` (already modified in the working tree — InMemory package reference removed).
263+
264+
- [ ] **Step 1: Run the full test suite**
265+
266+
Run:
267+
```bash
268+
cd /home/rene/Documents/workspace/microting/eform-angular-timeplanning-plugin/eFormAPI/Plugins/TimePlanning.Pn/TimePlanning.Pn.Test && dotnet test
269+
```
270+
271+
Expected: all tests pass. First invocation is slow because the whole suite shares the Testcontainers MariaDB lifecycle. If any test other than the two migrated ones fails, it is a pre-existing issue unrelated to this plan — stop and report rather than attempting to fix, as unrelated fixes would pollute the migration commits.
272+
273+
- [ ] **Step 2: Inspect working tree and confirm only the intended csproj change remains**
274+
275+
Run:
276+
```bash
277+
cd /home/rene/Documents/workspace/microting/eform-angular-timeplanning-plugin && git status && git diff eFormAPI/Plugins/TimePlanning.Pn/TimePlanning.Pn.Test/TimePlanning.Pn.Test.csproj
278+
```
279+
280+
Expected: the only unstaged change is the removal of the `<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" ... />` line from the csproj. No other modifications should be present. If anything else shows up, `git checkout` it back before the next step.
281+
282+
- [ ] **Step 3: Commit the csproj change**
283+
284+
Run:
285+
```bash
286+
cd /home/rene/Documents/workspace/microting/eform-angular-timeplanning-plugin && git add eFormAPI/Plugins/TimePlanning.Pn/TimePlanning.Pn.Test/TimePlanning.Pn.Test.csproj && git commit -m "test: drop Microsoft.EntityFrameworkCore.InMemory dependency
287+
288+
All tests now run against a real MariaDB via Testcontainers (TestBaseSetup),
289+
matching production behaviour for unique constraints, transactions, and
290+
referential integrity.
291+
292+
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>"
293+
```
294+
295+
Expected: a single-file commit. Three commits total on the branch after this plan completes.
296+
297+
- [ ] **Step 4: Final verification that the branch is clean and green**
298+
299+
Run:
300+
```bash
301+
cd /home/rene/Documents/workspace/microting/eform-angular-timeplanning-plugin && git status && git log --oneline -4
302+
```
303+
304+
Expected: clean working tree; the three new commits at the top of the log (csproj drop, PushNotification migration, DeviceToken migration) in addition to prior history.
305+
306+
---
307+
308+
## Out of Scope
309+
310+
- No production code changes (`DeviceTokenService`, `PushNotificationService`).
311+
- No changes to `TestBaseSetup` or any other test file.
312+
- No addition of new test cases — migration preserves the existing 4 + 2 cases.
313+
- Pushing or opening a PR — the user will handle that separately.

0 commit comments

Comments
 (0)