Skip to content

Commit

Permalink
cleanups in TestDbBase.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
adelikat committed Nov 7, 2024
1 parent fe25813 commit 50dcbc0
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions tests/TASVideos.Tests.Base/TestDbBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class TestDbBase
protected TestDbContext _db;

private static IDbContextTransaction? _transaction;
private static bool isInitialized = false;
private static string? connectionString;
private static bool _isInitialized = false;
private static string? _connectionString;

public TestDbBase()
{
Expand All @@ -27,31 +27,31 @@ public static void AssemblyInit(TestContext context)
var contextConnectionString = context.Properties["PostgresTestsConnection"]?.ToString();
var builder = new NpgsqlConnectionStringBuilder(contextConnectionString);
builder.Database += "-" + Assembly.GetCallingAssembly().GetName().Name;
connectionString = builder.ToString();
_connectionString = builder.ToString();
}

public static TestDbContext Create()
{
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseNpgsql(connectionString)
.UseNpgsql(_connectionString)
.UseSnakeCaseNamingConvention()
.EnableSensitiveDataLogging()
.Options;

var testHttpContext = new TestDbContext.TestHttpContextAccessor();
var db = new TestDbContext(options, testHttpContext);

if (!isInitialized)
if (!_isInitialized)
{
db.Database.EnsureDeleted();
db.Database.EnsureCreated();

// We have constant Forum IDs required by parts of our code, but the Test Database doesn't know about this and starts its IDs at 0.
// This causes us to eventually run into duplicate IDs. As a workaround, we increase the starting ID to 100.
db.Database.ExecuteSqlRaw($"ALTER SEQUENCE forums_id_seq RESTART WITH 100;");
db.Database.ExecuteSqlRaw("ALTER SEQUENCE forums_id_seq RESTART WITH 100;");

isInitialized = true;
_isInitialized = true;
}

return db;
Expand All @@ -65,7 +65,7 @@ public void Cleanup()

public static void AssemblyCleanup()
{
if (isInitialized)
if (_isInitialized)
{
var db = Create();
db.Database.EnsureDeleted();
Expand Down

0 comments on commit 50dcbc0

Please sign in to comment.