|
| 1 | +using FluentAssertions; |
| 2 | +using Microsoft.Extensions.Configuration; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
| 4 | +using Microsoft.Extensions.Logging; |
| 5 | +using QueryPush.Configuration; |
| 6 | +using QueryPush.Services; |
| 7 | +using Xunit; |
| 8 | +using Xunit.Categories; |
| 9 | + |
| 10 | +namespace QueryPush.Tests; |
| 11 | + |
| 12 | +public class DatabaseIntegrationTests : IDisposable |
| 13 | +{ |
| 14 | + private readonly IServiceProvider _services; |
| 15 | + private readonly string _testDbPath = "integration_test.db"; |
| 16 | + |
| 17 | + public DatabaseIntegrationTests() |
| 18 | + { |
| 19 | + var configuration = new ConfigurationBuilder() |
| 20 | + .AddInMemoryCollection(new Dictionary<string, string?> |
| 21 | + { |
| 22 | + ["databases:0:name"] = "TestDb", |
| 23 | + ["databases:0:connectionString"] = $"Driver={{SQLite3 ODBC Driver}};Database={_testDbPath};", |
| 24 | + ["endpoints:0:name"] = "TestEndpoint", |
| 25 | + ["endpoints:0:url"] = "https://webhook.site/test" |
| 26 | + }) |
| 27 | + .Build(); |
| 28 | + |
| 29 | + var services = new ServiceCollection(); |
| 30 | + services.Configure<QueryPushSettings>(configuration); |
| 31 | + services.AddLogging(builder => builder.ClearProviders()); |
| 32 | + services.AddScoped<IDatabaseService, DatabaseService>(); |
| 33 | + |
| 34 | + _services = services.BuildServiceProvider(); |
| 35 | + } |
| 36 | + |
| 37 | + [Fact, IntegrationTest] |
| 38 | + public async Task DatabaseService_WithSQLiteODBC_ShouldCreateTableAndQuery() |
| 39 | + { |
| 40 | + var dbService = _services.GetRequiredService<IDatabaseService>(); |
| 41 | + |
| 42 | + await dbService.ExecuteQueryAsync("TestDb", |
| 43 | + "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)", 30, 1000); |
| 44 | + |
| 45 | + await dbService.ExecuteQueryAsync("TestDb", |
| 46 | + "INSERT OR REPLACE INTO users (id, name, email) VALUES (1, 'John Doe', 'john@example.com'), (2, 'Jane Smith', 'jane@example.com')", 30, 1000); |
| 47 | + |
| 48 | + var results = await dbService.ExecuteQueryAsync("TestDb", |
| 49 | + "SELECT id, name, email FROM users ORDER BY id", 30, 1000); |
| 50 | + |
| 51 | + results.Should().HaveCount(2); |
| 52 | + results.First()["name"].Should().Be("John Doe"); |
| 53 | + results.Last()["email"].Should().Be("jane@example.com"); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact, IntegrationTest] |
| 57 | + public async Task DatabaseService_WithInvalidQuery_ShouldThrow() |
| 58 | + { |
| 59 | + var dbService = _services.GetRequiredService<IDatabaseService>(); |
| 60 | + |
| 61 | + var act = async () => await dbService.ExecuteQueryAsync("TestDb", "SELECT * FROM nonexistent_table", 30, 100); |
| 62 | + |
| 63 | + await act.Should().ThrowAsync<Exception>(); |
| 64 | + } |
| 65 | + |
| 66 | + public void Dispose() |
| 67 | + { |
| 68 | + if (File.Exists(_testDbPath)) |
| 69 | + File.Delete(_testDbPath); |
| 70 | + } |
| 71 | +} |
0 commit comments