using System.Data.Common; using Dotnet.Samples.AspNetCore.WebApi.Data; using Dotnet.Samples.AspNetCore.WebApi.Migrations; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Migrations; namespace Dotnet.Samples.AspNetCore.WebApi.Tests.Utilities { /// /// A Fake is a working implementation that's simpler than the real one. /// It usually has some "real" logic but is not suitable for production /// (e.g., an in‑memory database instead of a full SQL Server). Fakes are /// useful when you need behavior that's closer to reality but still want /// to avoid external dependencies. /// public static class DatabaseFakes { /// /// Creates an in-memory SQLite connection and DbContext options for testing. /// The connection remains open for the lifetime of the test. /// /// A tuple containing the SQLite connection and DbContext options. public static (DbConnection, DbContextOptions) CreateSqliteConnection() { var dbConnection = new SqliteConnection("Filename=:memory:"); dbConnection.Open(); var dbContextOptions = new DbContextOptionsBuilder() .UseSqlite(dbConnection) .ReplaceService() .Options; return (dbConnection, dbContextOptions); } /// /// Creates a PlayerDbContext instance with the specified options. /// /// The DbContext options to use. /// A new PlayerDbContext instance. public static PlayerDbContext CreateDbContext( DbContextOptions dbContextOptions ) { return new PlayerDbContext(dbContextOptions); } /// /// Applies all pending EF Core migrations to the test database, including seed data. /// Extension method for PlayerDbContext. /// /// The PlayerDbContext instance. public static async Task MigrateAsync(this PlayerDbContext context) { await context.Database.MigrateAsync(); } } }