Files
mic-check/src/api/MicCheck.Api/Environments/EnvironmentDocumentService.cs
James Wampler 20188c61a2
All checks were successful
CI / build-and-push (push) Successful in 48s
CI / deploy-qa (push) Successful in 12s
CI / smoke-qa (push) Successful in 20s
Refactor nunit tests off InMemory/WebApplicationFactory, remove tuple returns (#4)
## Summary
- Extract `IMicCheckDbContext` and mock DB access with Moq instead of the EF InMemory provider, per CLAUDE.md testing guidelines.
- Rewrite HTTP-pipeline tests to exercise controllers/auth handlers directly instead of `WebApplicationFactory`.
- Remove tuple return types across the API in favor of named records (`PagedResult<T>`, `ProfileUpdateResult`, `ApiKeyCreationResult`).

## Test plan
- [x] `dotnet build` (full solution)
- [x] `dotnet test tests/api/MicCheck.Api.Tests.Unit` — 187/187 pass
- [x] `dotnet test tests/api/MicCheck.Api.Tests.Integration` — 8/8 pass (live API + Postgres via Aspire)
- [x] Verified Aspire AppHost/dashboard starts and API responds

Reviewed-on: #4
2026-07-04 21:40:47 -07:00

55 lines
1.8 KiB
C#
Executable File

using MicCheck.Api.Data;
using MicCheck.Api.Features;
using Microsoft.EntityFrameworkCore;
using AppEnvironment = MicCheck.Api.Environments.Environment;
namespace MicCheck.Api.Environments;
public class EnvironmentDocumentService(IMicCheckDbContext db)
{
public async Task<EnvironmentDocumentResponse?> GetAsync(int environmentId, CancellationToken ct = default)
{
var environment = await db.Environments
.FindAsync([environmentId], ct);
if (environment is null)
return null;
var featureStateResults = await (
from fs in db.FeatureStates
join f in db.Features on fs.FeatureId equals f.Id
where fs.EnvironmentId == environmentId
&& fs.IdentityId == null
&& fs.FeatureSegmentId == null
select new FeatureStateResult
{
Feature = f,
Enabled = fs.Enabled,
Value = fs.Value
}
).ToListAsync(ct);
var project = await db.Projects
.Include(p => p.Segments)
.ThenInclude(s => s.Rules)
.ThenInclude(r => r.Conditions)
.Include(p => p.Segments)
.ThenInclude(s => s.Rules)
.ThenInclude(r => r.ChildRules)
.ThenInclude(cr => cr.Conditions)
.FirstOrDefaultAsync(p => p.Id == environment.ProjectId, ct);
if (project is null)
return null;
return new EnvironmentDocumentResponse(
environment.Id,
environment.ApiKey,
featureStateResults.Select(FlagResponse.From).ToList(),
new EnvironmentProjectResponse(
project.Id,
project.Name,
project.Segments.Select(SegmentDocumentResponse.From).ToList()));
}
}