-
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.
- Loading branch information
1 parent
42078a9
commit 5af5ce7
Showing
27 changed files
with
785 additions
and
412 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
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
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
2 changes: 1 addition & 1 deletion
2
...iveDocsTestModelGenerator/NamingHelper.cs → ...DotNet.TestModelGenerator/NamingHelper.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
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
File renamed without changes.
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 |
---|---|---|
@@ -1,52 +1,68 @@ | ||
using LiveDocs.GraphQLApi.Infrastructure; | ||
using Docker.DotNet.Models; | ||
using Docker.DotNet; | ||
using LiveDocs.GraphQLApi.Infrastructure; | ||
using Testcontainers.MsSql; | ||
using Xunit.Abstractions; | ||
using LiveDocs.GraphQLApi.Data; | ||
using LiveDocs.GraphQLApi.Models.Entities; | ||
using LiveDocs.GraphQLApi.Models.Replication; | ||
using LiveDocs.GraphQLApi.Models.Shared; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace RxDBDotNet.Tests.Setup; | ||
|
||
public static class DbSetupUtil | ||
{ | ||
private const string DbConnectionString = "Server=127.0.0.1,1445;Database=LiveDocsTestDb;User Id=sa;Password=Password123!;TrustServerCertificate=True"; | ||
|
||
private static readonly SemaphoreSlim Semaphore = new(1, 1); | ||
|
||
private static volatile bool _isInitialized; | ||
|
||
public static async Task SetupAsync( | ||
IServiceProvider serviceProvider, | ||
ITestOutputHelper output, | ||
CancellationToken cancellationToken) | ||
public static async Task SetupAsync() | ||
{ | ||
ArgumentNullException.ThrowIfNull(output); | ||
|
||
await Semaphore.WaitAsync(cancellationToken).ConfigureAwait(false); | ||
|
||
await Semaphore.WaitAsync(); | ||
try | ||
{ | ||
if (!_isInitialized) | ||
{ | ||
output.WriteLine("Initializing the unit test db"); | ||
const string containerName = "rxdbdotnet_test_db"; | ||
const string saPassword = "Password123!"; | ||
|
||
// Specify a host port that will be consistent across runs | ||
// const int hostPort = 58000; | ||
var sqlServerDockerContainer = new MsSqlBuilder() | ||
.WithName("livedocs_test_db") | ||
.WithPassword("Password123!") | ||
// Bind the container's SQL Server port to the host port | ||
// .WithPortBinding(hostPort, containerPort: 1433) | ||
.Build(); | ||
// Check if the container already exists | ||
using var dockerClientConfiguration = new DockerClientConfiguration(); | ||
using var client = dockerClientConfiguration.CreateClient(); | ||
var existingContainers = await client.Containers.ListContainersAsync(new ContainersListParameters | ||
{ | ||
All = true, | ||
}); | ||
var existingContainer = | ||
existingContainers.FirstOrDefault(c => c.Names.Contains($"/{containerName}", StringComparer.OrdinalIgnoreCase)); | ||
|
||
await sqlServerDockerContainer.StartAsync(cancellationToken).ConfigureAwait(false); | ||
if (existingContainer != null) | ||
{ | ||
if (!string.Equals(existingContainer.State, "running", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
// Start the existing container | ||
await client.Containers.StartContainerAsync(existingContainer.ID, new ContainerStartParameters()); | ||
} | ||
} | ||
else | ||
{ | ||
var sqlServerDockerContainer = new MsSqlBuilder().WithName(containerName) | ||
.WithPassword(saPassword) | ||
.WithPortBinding(1445, 1433) | ||
.WithCleanUp(false) | ||
.Build(); | ||
await sqlServerDockerContainer.StartAsync(); | ||
} | ||
|
||
var connectionStringToMaster = sqlServerDockerContainer.GetConnectionString(); | ||
Environment.SetEnvironmentVariable(ConfigKeys.DbConnectionString, DbConnectionString); | ||
|
||
// Modify the connection string to use the consistent port | ||
var connectionStringToTestDb = connectionStringToMaster | ||
.Replace("master", "LiveDocsTestDb", StringComparison.OrdinalIgnoreCase); | ||
await using var dbContext = new LiveDocsDbContext(); | ||
|
||
Environment.SetEnvironmentVariable( | ||
ConfigKeys.DbConnectionString, | ||
connectionStringToTestDb); | ||
await dbContext.Database.EnsureCreatedAsync(); | ||
|
||
await LiveDocsDbInitializer.InitializeAsync(serviceProvider, cancellationToken).ConfigureAwait(false); | ||
await SeedDataAsync(dbContext); | ||
|
||
_isInitialized = true; | ||
} | ||
|
@@ -55,5 +71,59 @@ public static async Task SetupAsync( | |
{ | ||
Semaphore.Release(); | ||
} | ||
|
||
Environment.SetEnvironmentVariable(ConfigKeys.DbConnectionString, DbConnectionString); | ||
} | ||
|
||
private static async Task SeedDataAsync(LiveDocsDbContext dbContext) | ||
{ | ||
if (await dbContext.Workspaces.AnyAsync()) | ||
{ | ||
return; // Data has already been seeded | ||
} | ||
|
||
var workspacePk = RT.Comb.Provider.Sql.Create(); | ||
var liveDocsWorkspace = new Workspace | ||
{ | ||
Id = workspacePk, | ||
Name = "LiveDocs Org Workspace", | ||
UpdatedAt = DateTimeOffset.UtcNow, | ||
IsDeleted = false, | ||
ReplicatedDocumentId = workspacePk, | ||
}; | ||
|
||
await dbContext.Workspaces.AddAsync(liveDocsWorkspace); | ||
|
||
var userPk = RT.Comb.Provider.Sql.Create(); | ||
var systemAdminReplicatedUser = new ReplicatedUser | ||
{ | ||
Id = userPk, | ||
FirstName = "System", | ||
LastName = "Admin", | ||
Email = "[email protected]", | ||
JwtAccessToken = null, | ||
WorkspaceId = liveDocsWorkspace.Id, | ||
UpdatedAt = DateTimeOffset.UtcNow, | ||
IsDeleted = false, | ||
}; | ||
|
||
var jwtAccessToken = JwtUtil.GenerateJwtToken(systemAdminReplicatedUser, UserRole.SystemAdmin); | ||
|
||
var superAdminUser = new User | ||
{ | ||
Id = userPk, | ||
FirstName = "System", | ||
LastName = "Admin", | ||
Email = "[email protected]", | ||
JwtAccessToken = jwtAccessToken, | ||
WorkspaceId = liveDocsWorkspace.Id, | ||
UpdatedAt = DateTimeOffset.UtcNow, | ||
IsDeleted = false, | ||
ReplicatedDocumentId = userPk, | ||
}; | ||
|
||
await dbContext.Users.AddAsync(superAdminUser); | ||
|
||
await dbContext.SaveChangesAsync(); | ||
} | ||
} |
Oops, something went wrong.