[CLAUDE] Tests Phase 2: Code generator format + sequence tests (SQLite in-memory)
All checks were successful
Deploy SOLUTION_ERP / build-deploy (push) Successful in 3m16s
All checks were successful
Deploy SOLUTION_ERP / build-deploy (push) Successful in 3m16s
Phase 2 — chống regression code generator. 17 test mới integration với
DB thật (SQLite in-memory) tổng cộng 71 test pass < 3 giây.
Test project:
- tests/SolutionErp.Infrastructure.Tests/ (xUnit + FluentAssertions + EF SQLite 10)
- ProjectReference SolutionErp.Infrastructure (transitively get Application + Domain)
- Added vào SolutionErp.slnx
Test fixtures:
- Common/SqliteDbFixture.cs:
- SQLite ":memory:" + shared connection + EnsureCreated() từ DbContext model
- TestApplicationDbContext subclass — override OnModelCreating replace
'nvarchar(max)' → 'TEXT' (SQLite không support max keyword)
- FixedDateTime stub IDateTime cho deterministic year boundary test
Test files:
- Services/ContractCodeGeneratorTests.cs (10 test):
- Format per ContractType (5 type × Project scope) — RG-001 spec
- Framework HĐ (NguyenTacNCC + NguyenTacDV) → year scope thay vì project
- Sequence increment per prefix (3 calls → /01, /02, /03)
- Different prefixes (project / supplier) → independent sequences
- Year change (2026 → 2027) → reset sequence vì prefix khác
- PersistsSequenceRow LastSeq verification
- Services/PurchaseEvaluationCodeGeneratorTests.cs (7 test):
- Format A/B (DuyetNcc → 'A', DuyetNccPhuongAn → 'B')
- Seq là 3-digit padded (001..012)
- Type A và B sequence độc lập trong cùng năm
- Year boundary reset cả A và B
CI gate update (.gitea/workflows/deploy.yml):
- Step "Run integration tests (Infrastructure)" thêm sau Domain tests
- TRX log saved riêng (infra-tests.trx)
- Cả 2 step đều exit non-zero → no deploy
Verify local:
- dotnet test SolutionErp.slnx → Total tests: 71 (54 Domain + 17 Infra) / Passed: 71 / 2.1s
- dotnet build SolutionErp.slnx → 0 error
Phase 3+ pending:
- Application handler tests (CQRS) với EF InMemory hoặc SQLite (~1 ngày)
- API smoke tests qua WebApplicationFactory (~0.5 ngày)
- FE Vitest cho lib utility (~0.5 ngày)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@ -0,0 +1,75 @@
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using SolutionErp.Application.Common.Interfaces;
|
||||
using SolutionErp.Infrastructure.Persistence;
|
||||
|
||||
namespace SolutionErp.Infrastructure.Tests.Common;
|
||||
|
||||
// Subclass cho test — override column types SQL Server-specific (`nvarchar(max)`)
|
||||
// về TEXT vì SQLite không hỗ trợ. Identity table inherit từ IdentityDbContext
|
||||
// dùng kiểu chuẩn nên không cần fix thêm.
|
||||
public class TestApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
|
||||
: ApplicationDbContext(options)
|
||||
{
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
// Replace `nvarchar(max)` / `varchar(max)` → `TEXT` cho SQLite compat.
|
||||
foreach (var entity in modelBuilder.Model.GetEntityTypes())
|
||||
{
|
||||
foreach (var prop in entity.GetProperties())
|
||||
{
|
||||
var colType = prop.GetColumnType();
|
||||
if (colType is not null && colType.Contains("max", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
prop.SetColumnType("TEXT");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fixture build SQLite in-memory ApplicationDbContext mỗi test isolated.
|
||||
// Pattern: shared connection (open in fixture) + EnsureCreated() từ model.
|
||||
//
|
||||
// Tại sao SQLite chứ không InMemory provider?
|
||||
// - InMemory không hỗ trợ transactions — code generator dùng IsolationLevel.Serializable
|
||||
// - SQLite hỗ trợ transactions thật (BEGIN/COMMIT/ROLLBACK), tuy nhiên IsolationLevel
|
||||
// enum value bị provider mapping gracefully (no exception, just default behavior)
|
||||
// - Đủ để test format + sequential increment + scope reset, KHÔNG đủ cho test
|
||||
// race condition thực (cần SQL Server thật)
|
||||
public sealed class SqliteDbFixture : IDisposable
|
||||
{
|
||||
private readonly SqliteConnection _connection;
|
||||
public TestApplicationDbContext Db { get; }
|
||||
|
||||
public SqliteDbFixture()
|
||||
{
|
||||
// ":memory:" + shared connection — DB tồn tại đến khi connection close.
|
||||
_connection = new SqliteConnection("DataSource=:memory:");
|
||||
_connection.Open();
|
||||
|
||||
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
|
||||
.UseSqlite(_connection)
|
||||
.EnableSensitiveDataLogging()
|
||||
.Options;
|
||||
|
||||
Db = new TestApplicationDbContext(options);
|
||||
Db.Database.EnsureCreated();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Db.Dispose();
|
||||
_connection.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
// Stub IDateTime cho tests deterministic — control thời điểm Year boundary.
|
||||
public sealed class FixedDateTime(DateTime utcNow) : IDateTime
|
||||
{
|
||||
public DateTime UtcNow { get; set; } = utcNow;
|
||||
public DateTime Now => UtcNow; // simplification — VPS chạy UTC, không bao giờ dùng local time trong tests
|
||||
}
|
||||
Reference in New Issue
Block a user