generated from PDMLab/aspnet-core-htmx-vsa-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add marten and (test) databases and test setup
- Loading branch information
1 parent
3ee5d06
commit c657454
Showing
24 changed files
with
382 additions
and
574 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
version: "3" | ||
|
||
services: | ||
database: | ||
image: library/postgres:14 | ||
container_name: eventstoredb | ||
environment: | ||
POSTGRES_USER: 'marten' | ||
POSTGRES_PASSWORD: '123456' | ||
POSTGRES_DB: 'marten' | ||
ports: | ||
- "5400:5432" | ||
user: "1000:1000" | ||
volumes: | ||
- ./data:/var/lib/postgresql/data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/bin/bash | ||
|
||
yarn | ||
cd src/AspNetMartenHtmxVsa | ||
yarn | ||
libman restore | ||
cd ../../ | ||
mkdir -p database/data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/AspNetMartenHtmxVsa.IntegrationTests/IntegrationTestHost/IntegrationTestHost.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using Alba; | ||
using AspNetMartenHtmxVsa.Tests.TestSetup; | ||
using Microsoft.Extensions.Configuration; | ||
using Xunit.Abstractions; | ||
|
||
namespace AspNetMartenHtmxVsa.Tests.IntegrationTestHost; | ||
|
||
public class TestConfiguration : Dictionary<string, string?> | ||
{ | ||
public IConfigurationRoot AsConfigurationRoot() | ||
{ | ||
return new ConfigurationBuilder() | ||
.AddInMemoryCollection(this) | ||
.Build(); | ||
} | ||
} | ||
|
||
public class IntegrationTestHost : IDisposable | ||
{ | ||
private IAlbaHost Host { get; init; } | ||
private TestEventStore EventStore { get; init; } | ||
|
||
private IntegrationTestHost(IAlbaHost host, TestEventStore eventStore) | ||
{ | ||
Host = host; | ||
EventStore = eventStore; | ||
} | ||
|
||
public static async Task<IAlbaHost> InitializeAsync( | ||
ITestOutputHelper? testOutputHelper = null | ||
) | ||
{ | ||
var testEventStore = await TestEventStore.InitializeAsync(); | ||
var configuration = new TestConfiguration | ||
{ | ||
["ConnectionStrings:EventStore"] = testEventStore.MasterDbConnectionString | ||
}.AsConfigurationRoot(); | ||
|
||
var builder = ConfigureHost.GetHostBuilder(configuration, services => {}); | ||
|
||
return await builder.StartAlbaAsync(); | ||
} | ||
|
||
public async Task DisposeAsync() | ||
{ | ||
await Host.DisposeAsync(); | ||
await EventStore.DisposeAsync(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
DisposeAsync().GetAwaiter().GetResult(); | ||
} | ||
} |
168 changes: 168 additions & 0 deletions
168
src/AspNetMartenHtmxVsa.IntegrationTests/TestSetup/TestEventStore.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
using AspNetMartenHtmxVsa.EventSourcing; | ||
using Marten; | ||
using Npgsql; | ||
|
||
namespace AspNetMartenHtmxVsa.Tests.TestSetup; | ||
|
||
public class PostgresAdministration | ||
{ | ||
private readonly string _connectionString; | ||
|
||
public PostgresAdministration( | ||
string connectionString | ||
) | ||
{ | ||
_connectionString = connectionString; | ||
} | ||
|
||
public async Task CreateDatabaseAsync( | ||
string? databaseName | ||
) | ||
{ | ||
await using var connection = new NpgsqlConnection(); | ||
connection.ConnectionString = _connectionString; | ||
await connection.OpenAsync(); | ||
var command = new NpgsqlCommand( | ||
$"CREATE DATABASE {databaseName}", | ||
connection | ||
); | ||
await command.ExecuteNonQueryAsync(); | ||
await connection.CloseAsync(); | ||
} | ||
|
||
public async Task DropDatabase( | ||
string? databaseName | ||
) | ||
{ | ||
await using var connection = new NpgsqlConnection(); | ||
connection.ConnectionString = _connectionString; | ||
await connection.OpenAsync(); | ||
var command = new NpgsqlCommand( | ||
$"DROP DATABASE IF EXISTS {databaseName} WITH (FORCE);", | ||
connection | ||
); | ||
await command.ExecuteNonQueryAsync(); | ||
await connection.CloseAsync(); | ||
} | ||
|
||
public async Task<bool> EnsureDatabaseExists( | ||
string databaseName | ||
) | ||
{ | ||
await using var connection = new NpgsqlConnection(); | ||
connection.ConnectionString = _connectionString; | ||
|
||
await connection.OpenAsync(); | ||
var command = new NpgsqlCommand( | ||
$"SELECT 1 FROM pg_database WHERE datname LIKE '{databaseName}'", | ||
connection | ||
); | ||
|
||
var result = await command.ExecuteScalarAsync(); | ||
await connection.CloseAsync(); | ||
|
||
return result != null; | ||
} | ||
} | ||
|
||
public class TestDatabase | ||
{ | ||
private readonly PostgresAdministration _postgresAdministration; | ||
private string? _testDbName; | ||
public string MasterDbConnectionString { get; } | ||
|
||
public TestDatabase( | ||
string masterDbConnectionString | ||
) | ||
{ | ||
MasterDbConnectionString = masterDbConnectionString; | ||
_postgresAdministration = new PostgresAdministration( | ||
masterDbConnectionString | ||
); | ||
} | ||
|
||
public async Task<string> InitializeAsync( | ||
string masterDbConnection | ||
) | ||
{ | ||
Task | ||
.Delay(new Random().Next(50, 100)) | ||
.Wait(); | ||
|
||
var dbId = DateTime.UtcNow.ToString("yyyy_MM_dd_HH_mm_ss_fff"); | ||
_testDbName = $"marten_test_{dbId}_{Guid.NewGuid().ToString()[..4]}"; | ||
await _postgresAdministration.CreateDatabaseAsync( | ||
_testDbName | ||
); | ||
|
||
var connectionString = new NpgsqlConnectionStringBuilder | ||
{ | ||
Pooling = false, | ||
Port = 5435, | ||
Host = "localhost", | ||
CommandTimeout = 20, | ||
Database = _testDbName, | ||
Password = "123456", | ||
Username = "postgres" | ||
}.ToString(); | ||
|
||
return connectionString; | ||
} | ||
|
||
public async Task DropAsync() => await _postgresAdministration.DropDatabase(_testDbName); | ||
} | ||
|
||
public class TestEventStore | ||
{ | ||
public string MasterDbConnectionString { get; } | ||
public IDocumentStore Store { get; } | ||
private TestDatabase TestDatabase { get; } | ||
|
||
private TestEventStore( | ||
IDocumentStore store, | ||
TestDatabase testDatabase, | ||
string masterDbConnectionString | ||
) | ||
{ | ||
Store = store; | ||
TestDatabase = testDatabase; | ||
MasterDbConnectionString = masterDbConnectionString; | ||
} | ||
|
||
public static async Task<TestEventStore> InitializeAsync( | ||
Action<StoreOptions>? configureStoreOptions = null | ||
) | ||
{ | ||
var masterDbConnection = new NpgsqlConnectionStringBuilder | ||
{ | ||
Pooling = false, | ||
Port = 5435, | ||
Host = "localhost", | ||
CommandTimeout = 20, | ||
Database = "postgres", | ||
Password = "123456", | ||
Username = "postgres" | ||
}.ToString(); | ||
var testDatabase = new TestDatabase(masterDbConnection); | ||
|
||
var connectionString = await testDatabase.InitializeAsync(masterDbConnection); | ||
|
||
|
||
var store = DocumentStore.For( | ||
options => | ||
{ | ||
options.Connection(connectionString); | ||
StoreConfiguration.Configure(options); | ||
configureStoreOptions?.Invoke(options); | ||
} | ||
); | ||
|
||
return new TestEventStore(store, testDatabase, connectionString); | ||
} | ||
|
||
public async Task DisposeAsync() | ||
{ | ||
await TestDatabase.DropAsync(); | ||
Store.Dispose(); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions
1
src/AspNetMartenHtmxVsa.IntegrationTests/test-database/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
postgres_data |
12 changes: 12 additions & 0 deletions
12
src/AspNetMartenHtmxVsa.IntegrationTests/test-database/docker-compose.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
version: "3" | ||
|
||
services: | ||
database: | ||
image: library/postgres:14 | ||
container_name: eventstoretestdb | ||
environment: | ||
POSTGRES_USER: 'postgres' | ||
POSTGRES_PASSWORD: '123456' | ||
POSTGRES_DB: 'postgres' | ||
ports: | ||
- "5435:5432" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.