Skip to content

Commit 6aeea38

Browse files
committed
test(backend/Infrastructure): add integration tests for TaskRepository and ConversationService
Implemented comprehensive integration tests for TaskRepository and ConversationService using SQL Server and PostgreSQL Testcontainers. These tests cover various functionalities including adding, retrieving, updating, and deleting tasks and conversation threads, ensuring data integrity and expected behavior. Modified files (3): - TaskRepositoryTests.cs: Added tests for task CRUD operations and search functionality - ConversationServiceTests.cs: Added tests for conversation thread management and message retrieval - TaskAgent.Infrastructure.IntegrationTests.csproj: Configured project dependencies for integration testing
1 parent 68277f3 commit 6aeea38

5 files changed

Lines changed: 933 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using DotNet.Testcontainers.Builders;
2+
using Microsoft.EntityFrameworkCore;
3+
using TaskAgent.Infrastructure.Data;
4+
using Testcontainers.PostgreSql;
5+
6+
namespace TaskAgent.Infrastructure.IntegrationTests.Fixtures;
7+
8+
/// <summary>
9+
/// Shared PostgreSQL container fixture for xUnit Collection.
10+
/// This fixture starts a single PostgreSQL container that is shared across all tests in the collection.
11+
/// </summary>
12+
public class PostgreSqlContainerFixture : IAsyncLifetime
13+
{
14+
private readonly PostgreSqlContainer _container;
15+
16+
public string ConnectionString { get; private set; } = string.Empty;
17+
18+
public PostgreSqlContainerFixture()
19+
{
20+
_container = new PostgreSqlBuilder()
21+
.WithImage("postgres:16-alpine")
22+
.WithDatabase("taskagent_integration_test")
23+
.WithUsername("test_user")
24+
.WithPassword("test_password")
25+
.WithWaitStrategy(Wait.ForUnixContainer().UntilCommandIsCompleted("pg_isready", "-U", "test_user"))
26+
.Build();
27+
}
28+
29+
public async Task InitializeAsync()
30+
{
31+
await _container.StartAsync();
32+
ConnectionString = _container.GetConnectionString();
33+
34+
// Create initial database schema
35+
await using ConversationDbContext context = CreateDbContext();
36+
await context.Database.EnsureCreatedAsync();
37+
}
38+
39+
public async Task DisposeAsync() => await _container.DisposeAsync();
40+
41+
/// <summary>
42+
/// Creates a new DbContext instance connected to the test container.
43+
/// </summary>
44+
public ConversationDbContext CreateDbContext()
45+
{
46+
DbContextOptions<ConversationDbContext> options = new DbContextOptionsBuilder<ConversationDbContext>()
47+
.UseNpgsql(ConnectionString)
48+
.Options;
49+
50+
return new ConversationDbContext(options);
51+
}
52+
}
53+
54+
/// <summary>
55+
/// xUnit Collection Definition for PostgreSQL tests.
56+
/// Tests marked with [Collection("PostgreSql")] will share the same container instance.
57+
/// </summary>
58+
[CollectionDefinition("PostgreSql")]
59+
#pragma warning disable CA1711 // Identifiers should not have incorrect suffix
60+
public class PostgreSqlCollection : ICollectionFixture<PostgreSqlContainerFixture>
61+
#pragma warning restore CA1711
62+
{
63+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using DotNet.Testcontainers.Builders;
2+
using Microsoft.EntityFrameworkCore;
3+
using TaskAgent.Infrastructure.Data;
4+
using Testcontainers.MsSql;
5+
6+
namespace TaskAgent.Infrastructure.IntegrationTests.Fixtures;
7+
8+
/// <summary>
9+
/// Shared SQL Server container fixture for xUnit Collection.
10+
/// This fixture starts a single SQL Server container that is shared across all tests in the collection.
11+
/// </summary>
12+
public class SqlServerContainerFixture : IAsyncLifetime
13+
{
14+
private readonly MsSqlContainer _container;
15+
16+
public string ConnectionString { get; private set; } = string.Empty;
17+
18+
public SqlServerContainerFixture()
19+
{
20+
_container = new MsSqlBuilder()
21+
.WithImage("mcr.microsoft.com/mssql/server:2022-latest")
22+
.WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(1433))
23+
.Build();
24+
}
25+
26+
public async Task InitializeAsync()
27+
{
28+
await _container.StartAsync();
29+
ConnectionString = _container.GetConnectionString();
30+
31+
// Create initial database schema
32+
await using TaskDbContext context = CreateDbContext();
33+
await context.Database.EnsureCreatedAsync();
34+
}
35+
36+
public async Task DisposeAsync() => await _container.DisposeAsync();
37+
38+
/// <summary>
39+
/// Creates a new DbContext instance connected to the test container.
40+
/// </summary>
41+
public TaskDbContext CreateDbContext()
42+
{
43+
DbContextOptions<TaskDbContext> options = new DbContextOptionsBuilder<TaskDbContext>()
44+
.UseSqlServer(ConnectionString)
45+
.Options;
46+
47+
return new TaskDbContext(options);
48+
}
49+
}
50+
51+
/// <summary>
52+
/// xUnit Collection Definition for SQL Server tests.
53+
/// Tests marked with [Collection("SqlServer")] will share the same container instance.
54+
/// </summary>
55+
[CollectionDefinition("SqlServer")]
56+
#pragma warning disable CA1711 // Identifiers should not have incorrect suffix
57+
public class SqlServerCollection : ICollectionFixture<SqlServerContainerFixture>
58+
#pragma warning restore CA1711
59+
{
60+
}

0 commit comments

Comments
 (0)