Skip to content

Commit

Permalink
Replace MS package with a more acurate sample package
Browse files Browse the repository at this point in the history
Add integration test

Signed-off-by: Jonathan Mezach <[email protected]>
  • Loading branch information
jmezach committed Dec 17, 2024
1 parent 8e161df commit cf10b12
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<PackageVersion Include="EventStore.Client.Extensions.OpenTelemetry" Version="23.3.7" />
<PackageVersion Include="EventStore.Client.Grpc.Streams" Version="23.3.7" />
<PackageVersion Include="MassTransit.RabbitMQ" Version="8.3.2" />
<PackageVersion Include="Microsoft.SqlServer.Dacpacs.Master" Version="160.2.3" />
<PackageVersion Include="ErikEJ.Dacpac.Chinook" Version="1.0.0" />
<!-- Build dependencies -->
<PackageVersion Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.4" />
<!-- Testcontainers packages -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" />
<PackageReference Include="Microsoft.SqlServer.Dacpacs.Master" IsAspirePackageResource="true" />
<PackageReference Include="ErikEJ.Dacpac.Chinook" IsAspirePackageResource="true" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
builder.AddSqlProject<Projects.SdkProject>("sdk-project")
.WithReference(server);

builder.AddSqlPackage<Packages.Microsoft_SqlServer_Dacpacs>("dacpac-project")
.WithDacpac("tools/master.dacpac")
builder.AddSqlPackage<Packages.ErikEJ_Dacpac_Chinook>("chinook")
.WithReference(server);

builder.Build().Run();
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
DependsOnTargets="_CreateAspireProjectResources">
<ItemGroup>
<AspirePackageMetadataSource Include="@(_AspirePackageResource->WithMetadataValue('IsAspirePackageResource', 'true'))" Condition="'@(_AspirePackageResource)' != ''">
<ClassName Condition="%(_AspirePackageResource.AspirePackageMetadataTypeName) == ''">$([System.Text.RegularExpressions.Regex]::Replace($([System.IO.Path]::GetFileNameWithoutExtension(%(_AspirePackageResource.Identity))), $(_GeneratedClassNameFixupRegex), '_'))</ClassName>
<ClassName Condition="%(_AspirePackageResource.AspirePackageMetadataTypeName) == ''">$([System.Text.RegularExpressions.Regex]::Replace(%(_AspirePackageResource.Identity), $(_GeneratedClassNameFixupRegex), '_'))</ClassName>
<ClassName Condition="%(_AspirePackageResource.AspirePackageMetadataTypeName) != ''">$([System.Text.RegularExpressions.Regex]::Replace(%(_AspirePackageResource.AspirePackageMetadataTypeName), $(_GeneratedClassNameFixupRegex), '_'))</ClassName>
<PackageName>@(_AspirePackageResource->'%(Identity)'->Replace('.', '_'))</PackageName>
<PackagePath>$([System.String]::new('$(NuGetPackageRoot)%(_AspirePackageResource.Identity)/%(_AspirePackageResource.Version)').ToLower())</PackagePath>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,39 @@ public void AddSqlPackage_WithPackageMetadata()
{
// Arrange
var appBuilder = DistributedApplication.CreateBuilder();
appBuilder.AddSqlPackage<TestPackage>("master");
appBuilder.AddSqlPackage<TestPackage>("chinook");

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

// Assert
var sqlProjectResource = Assert.Single(appModel.Resources.OfType<SqlProjectResource>());
Assert.Equal("master", sqlProjectResource.Name);
Assert.Equal("chinook", sqlProjectResource.Name);

var dacpacPath = sqlProjectResource.GetDacpacPath();
Assert.NotNull(dacpacPath);
Assert.Equal(Path.Combine(TestPackage.NuGetPackageCache, "microsoft.sqlserver.dacpacs.master", "160.2.3", "tools", "Microsoft.SqlServer.Dacpacs.Master.dacpac"), dacpacPath);
Assert.Equal(Path.Combine(TestPackage.NuGetPackageCache, "erikej.dacpac.chinook", "1.0.0", "tools", "ErikEJ.Dacpac.Chinook.dacpac"), dacpacPath);
Assert.True(File.Exists(dacpacPath));
}

[Fact]
public void AddSqlPackage_WithExplicitRelativePath()
{
// Arrange
var appBuilder = DistributedApplication.CreateBuilder();
appBuilder.AddSqlPackage<TestPackage>("master").WithDacpac("tools/master.dacpac");
appBuilder.AddSqlPackage<TestPackage>("chinook").WithDacpac("tools/ErikEJ.Dacpac.Chinook2.dacpac");

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

// Assert
var sqlProjectResource = Assert.Single(appModel.Resources.OfType<SqlProjectResource>());
Assert.Equal("master", sqlProjectResource.Name);
Assert.Equal("chinook", sqlProjectResource.Name);

var dacpacPath = sqlProjectResource.GetDacpacPath();
Assert.NotNull(dacpacPath);
Assert.True(File.Exists(dacpacPath));
Assert.Equal(Path.Combine(TestPackage.NuGetPackageCache, "erikej.dacpac.chinook", "1.0.0", "tools", "ErikEJ.Dacpac.Chinook2.dacpac"), dacpacPath);

Check failure on line 45 in tests/CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.Tests/AddSqlPackageTests.cs

View workflow job for this annotation

GitHub Actions / .NET Test report windows-latest

CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.Tests.AddSqlPackageTests ► AddSqlPackage_WithExplicitRelativePath

Failed test found in: CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.Tests-net8.0.trx Error: Assert.Equal() Failure: Strings differ ↓ (pos 70) Expected: ···".chinook\\1.0.0\\tools\\ErikEJ.Dacpac.Chinoo"··· Actual: ···".chinook\\1.0.0\\tools/ErikEJ.Dacpac.Chinoo"··· ↑ (pos 70)
Raw output
Assert.Equal() Failure: Strings differ
                                    ↓ (pos 70)
Expected: ···".chinook\\1.0.0\\tools\\ErikEJ.Dacpac.Chinoo"···
Actual:   ···".chinook\\1.0.0\\tools/ErikEJ.Dacpac.Chinoo"···
                                    ↑ (pos 70)
   at CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.Tests.AddSqlPackageTests.AddSqlPackage_WithExplicitRelativePath() in /_/tests/CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.Tests/AddSqlPackageTests.cs:line 45
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.Tests;
public class AppHostTests(AspireIntegrationTestFixture<Projects.CommunityToolkit_Aspire_Hosting_SqlDatabaseProjects_AppHost> fixture) : IClassFixture<AspireIntegrationTestFixture<Projects.CommunityToolkit_Aspire_Hosting_SqlDatabaseProjects_AppHost>>
{
[Fact]
public async Task ResourceStartsAndRespondsOk()
public async Task ProjectBasedResourceStartsAndRespondsOk()
{
string resourceName = "sdk-project";
await fixture.ResourceNotificationService.WaitForResourceAsync(resourceName, KnownResourceStates.Finished).WaitAsync(TimeSpan.FromMinutes(5));
Expand All @@ -29,4 +29,27 @@ public async Task ResourceStartsAndRespondsOk()
var result = await command.ExecuteScalarAsync();
Assert.Equal(1, result);
}

[Fact]
public async Task PackageBasedResourceStartsAndRespondsOk()
{
string resourceName = "chinook";
await fixture.ResourceNotificationService.WaitForResourceAsync(resourceName, KnownResourceStates.Finished).WaitAsync(TimeSpan.FromMinutes(5));

string? connectionString = await fixture.GetConnectionString("TargetDatabase");
Assert.NotNull(connectionString);

using var connection = new SqlConnection(connectionString);
await connection.OpenAsync();

using var command = connection.CreateCommand();
command.CommandText =
"SELECT COUNT(1) " +
"FROM INFORMATION_SCHEMA.TABLES " +
"WHERE TABLE_SCHEMA = 'dbo' " +
"AND TABLE_NAME = 'InvoiceLine'";

var result = await command.ExecuteScalarAsync();
Assert.Equal(1, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ internal class TestPackage : IPackageMetadata
{
public static readonly string NuGetPackageCache = Path.Combine(Environment.GetEnvironmentVariable("HOME") ?? Environment.GetEnvironmentVariable("USERPROFILE") ?? string.Empty, ".nuget", "packages");

public string PackageId { get; } = "Microsoft.SqlServer.Dacpacs.Master";
public string PackageId { get; } = "ErikEJ.Dacpac.Chinook";

public Version PackageVersion { get; } = new Version(160, 2, 3);
public Version PackageVersion { get; } = new Version(1, 0, 0);

public string PackagePath { get; } = Path.Combine(NuGetPackageCache, "microsoft.sqlserver.dacpacs.master", "160.2.3");
public string PackagePath { get; } = Path.Combine(NuGetPackageCache, "erikej.dacpac.chinook", "1.0.0");
}

0 comments on commit cf10b12

Please sign in to comment.