Skip to content

Commit

Permalink
Fix broken tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan Mezach <[email protected]>
  • Loading branch information
jmezach committed Oct 11, 2024
1 parent 1d9922e commit 8da2aaf
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/MSBuild.Sdk.SqlProj.Aspire/SqlProjectBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ public static IResourceBuilder<SqlProjectResource> PublishTo(
this IResourceBuilder<SqlProjectResource> builder, IResourceBuilder<SqlServerDatabaseResource> target)
{
builder.ApplicationBuilder.Services.TryAddSingleton<IDacpacDeployer, DacpacDeployer>();
builder.ApplicationBuilder.Services.TryAddSingleton<SqlProjectPublisher>();
builder.ApplicationBuilder.Services.TryAddSingleton<SqlProjectPublishService>();

builder.ApplicationBuilder.Eventing.Subscribe<ResourceReadyEvent>(target.Resource, (resourceReady, ct) =>
{
var service = resourceReady.Services.GetRequiredService<SqlProjectPublisher>();
var service = resourceReady.Services.GetRequiredService<SqlProjectPublishService>();
return service.PublishSqlProject(builder.Resource, target.Resource, ct);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

namespace MSBuild.Sdk.SqlProj.Aspire;

public class SqlProjectPublisher
public class SqlProjectPublishService
{
private readonly IDacpacDeployer _deployer;
private readonly ResourceLoggerService _resourceLoggerService;
private readonly ResourceNotificationService _resourceNotificationService;

public SqlProjectPublisher(IDacpacDeployer deployer, ResourceLoggerService resourceLoggerService,
public SqlProjectPublishService(IDacpacDeployer deployer, ResourceLoggerService resourceLoggerService,
ResourceNotificationService resourceNotificationService)
{
_deployer = deployer ?? throw new ArgumentNullException(nameof(deployer));
Expand Down
25 changes: 3 additions & 22 deletions test/MSBuild.Sdk.SqlProj.Aspire.Tests/AddSqlProjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,7 @@ public void AddSqlProject_WithExplicitPath()
}

[Fact]
public void PublishTo_AddsAnnotation()
{
// Arrange
var appBuilder = DistributedApplication.CreateBuilder();
var targetDatabase = appBuilder.AddSqlServer("sql").AddDatabase("test");
appBuilder.AddSqlProject<TestProject>("MySqlProject")
.PublishTo(targetDatabase);

// Act
using var app = appBuilder.Build();
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();

// Assert
var sqlProjectResource = Assert.Single(appModel.Resources.OfType<SqlProjectResource>());
var annotation = Assert.Single(sqlProjectResource.Annotations.OfType<TargetDatabaseResourceAnnotation>());
Assert.Equal("test", annotation.TargetDatabaseResourceName);
}

[Fact]
public void PublishTo_AddsLifecycleHook()
public void PublishTo_AddsRequiredServices()
{
// Arrange
var appBuilder = DistributedApplication.CreateBuilder();
Expand All @@ -80,7 +61,7 @@ public void PublishTo_AddsLifecycleHook()
using var app = appBuilder.Build();

// Assert
var lifecycleHooks = app.Services.GetServices<IDistributedApplicationLifecycleHook>();
Assert.Single(lifecycleHooks.OfType<SqlProjectPublisher>());
Assert.Single(app.Services.GetServices<SqlProjectPublishService>());
Assert.Single(app.Services.GetServices<IDacpacDeployer>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

namespace MSBuild.Sdk.SqlProj.Aspire.Tests;

public class PublishSqlProjectLifecycleHookTests
public class SqlProjectPublisherServiceTests
{
[Fact]
public async Task AfterResourcesCreatedAsync_PublishesDacpacToTargetDatabase()
public async Task PublishesDacpacToTargetDatabase()
{
// Arrange
var dacDeployerMock = Substitute.For<IDacpacDeployer>();
Expand All @@ -18,14 +18,14 @@ public async Task AfterResourcesCreatedAsync_PublishesDacpacToTargetDatabase()
var targetDatabase = appBuilder.AddSqlServer("sql")
.WithEndpoint("tcp", e => e.AllocatedEndpoint = new AllocatedEndpoint(e, "localhost", 1433)) // Simulated endpoint
.AddDatabase("test");
appBuilder.AddSqlProject<TestProject>("MySqlProject")
var project = appBuilder.AddSqlProject<TestProject>("MySqlProject")
.PublishTo(targetDatabase);

// Act
using var app = appBuilder.Build();
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();
var lifecycleHook = Assert.Single(app.Services.GetServices<IDistributedApplicationLifecycleHook>().OfType<SqlProjectPublisher>());
await lifecycleHook.AfterResourcesCreatedAsync(appModel, CancellationToken.None);
var service = Assert.Single(app.Services.GetServices<SqlProjectPublishService>());
await service.PublishSqlProject(project.Resource, targetDatabase.Resource, CancellationToken.None);

// Assert
var expectedPath = Path.GetFullPath(Path.Combine(appBuilder.AppHostDirectory, "../../../../TestProject/bin/Debug/netstandard2.0/TestProject.dacpac"));
Expand Down

0 comments on commit 8da2aaf

Please sign in to comment.