Skip to content

Commit 414c530

Browse files
committed
tests
1 parent 7c0e21f commit 414c530

File tree

5 files changed

+258
-440
lines changed

5 files changed

+258
-440
lines changed

src/GoatQuery/tests/DatabaseTestFixture.cs

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,40 @@
77
public class DatabaseTestFixture : IAsyncLifetime
88
{
99
private PostgreSqlContainer? _postgresContainer;
10-
private TestDbContext? _dbContext;
11-
12-
public TestDbContext DbContext => _dbContext ?? throw new InvalidOperationException("Database not initialized");
10+
public TestDbContext DbContext { get; set; } = null!;
1311

1412
public async Task InitializeAsync()
1513
{
16-
// Create and start PostgreSQL container
1714
_postgresContainer = new PostgreSqlBuilder()
18-
.WithImage("postgres:16-alpine")
19-
.WithDatabase("testdb")
20-
.WithUsername("test")
21-
.WithPassword("test")
15+
.WithImage("postgres:18-alpine")
2216
.Build();
2317

2418
await _postgresContainer.StartAsync();
2519

26-
// Create DbContext with connection to container
2720
var connectionString = _postgresContainer.GetConnectionString();
2821
var optionsBuilder = new DbContextOptionsBuilder<TestDbContext>();
2922
optionsBuilder.UseNpgsql(connectionString);
3023

31-
// Enable EF Core logging
32-
optionsBuilder.LogTo(
33-
Console.WriteLine,
34-
new[] { DbLoggerCategory.Database.Command.Name, DbLoggerCategory.Query.Name },
35-
LogLevel.Information,
36-
DbContextLoggerOptions.DefaultWithLocalTime | DbContextLoggerOptions.SingleLine
37-
);
38-
39-
optionsBuilder.EnableSensitiveDataLogging();
40-
optionsBuilder.EnableDetailedErrors();
24+
DbContext = new TestDbContext(optionsBuilder.Options);
4125

42-
_dbContext = new TestDbContext(optionsBuilder.Options);
26+
await DbContext.Database.EnsureCreatedAsync();
4327

44-
// Create database schema
45-
await _dbContext.Database.EnsureCreatedAsync();
46-
47-
// Seed test data
4828
await SeedTestData();
4929
}
5030

5131
private async Task SeedTestData()
5232
{
5333
var users = TestData.Users.Values.ToList();
54-
await _dbContext.Users.AddRangeAsync(users);
55-
await _dbContext.SaveChangesAsync();
34+
35+
await DbContext.Users.AddRangeAsync(users);
36+
await DbContext.SaveChangesAsync();
5637
}
5738

5839
public async Task DisposeAsync()
5940
{
60-
if (_dbContext != null)
41+
if (DbContext != null)
6142
{
62-
await _dbContext.DisposeAsync();
43+
await DbContext.DisposeAsync();
6344
}
6445

6546
if (_postgresContainer != null)

0 commit comments

Comments
 (0)