-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Add some initial unit tests
Signed-off-by: Jonathan Mezach <[email protected]>
Showing
3 changed files
with
137 additions
and
0 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
91 changes: 91 additions & 0 deletions
91
test/MSBuild.Sdk.SqlProj.Aspire.Tests/AddSqlProjectTests.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,91 @@ | ||
using Aspire.Hosting; | ||
using Aspire.Hosting.Lifecycle; | ||
|
||
namespace MSBuild.Sdk.SqlProj.Aspire.Tests; | ||
|
||
public class AddSqlProjectTests | ||
{ | ||
[Fact] | ||
public void AddSqlProject_WithProjectMetadata() | ||
{ | ||
// Arrange | ||
var appBuilder = DistributedApplication.CreateBuilder(); | ||
appBuilder.AddSqlProject<TestProject>("MySqlProject"); | ||
|
||
// Act | ||
using var app = appBuilder.Build(); | ||
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>(); | ||
|
||
// Assert | ||
var sqlProjectResource = Assert.Single(appModel.Resources.OfType<SqlProjectResource>()); | ||
Assert.Equal("MySqlProject", sqlProjectResource.Name); | ||
|
||
var dacpacPath = sqlProjectResource.GetDacpacPath(); | ||
Assert.NotNull(dacpacPath); | ||
Assert.True(File.Exists(dacpacPath)); | ||
} | ||
|
||
[Fact] | ||
public void AddSqlProject_WithExplicitPath() | ||
{ | ||
// Arrange | ||
var appBuilder = DistributedApplication.CreateBuilder(); | ||
appBuilder.AddSqlProject("MySqlProject").FromDacpac("../../../../TestProject/TestProject.csproj"); | ||
|
||
// Act | ||
using var app = appBuilder.Build(); | ||
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>(); | ||
|
||
// Assert | ||
var sqlProjectResource = Assert.Single(appModel.Resources.OfType<SqlProjectResource>()); | ||
Assert.Equal("MySqlProject", sqlProjectResource.Name); | ||
|
||
var annotation = Assert.Single(sqlProjectResource.Annotations.OfType<DacpacMetadataAnnotation>()); | ||
Assert.Equal(Path.Combine(appBuilder.AppHostDirectory, "../../../../TestProject/TestProject.csproj"), annotation.DacpacPath); | ||
|
||
var dacpacPath = sqlProjectResource.GetDacpacPath(); | ||
Assert.NotNull(dacpacPath); | ||
Assert.True(File.Exists(dacpacPath)); | ||
} | ||
|
||
[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() | ||
{ | ||
// Arrange | ||
var appBuilder = DistributedApplication.CreateBuilder(); | ||
var targetDatabase = appBuilder.AddSqlServer("sql").AddDatabase("test"); | ||
appBuilder.AddSqlProject<TestProject>("MySqlProject") | ||
.PublishTo(targetDatabase); | ||
|
||
// Act | ||
using var app = appBuilder.Build(); | ||
|
||
// Assert | ||
var lifecycleHooks = app.Services.GetServices<IDistributedApplicationLifecycleHook>(); | ||
Assert.Single(lifecycleHooks.OfType<PublishSqlProjectLifecycleHook>()); | ||
} | ||
|
||
private class TestProject : IProjectMetadata | ||
{ | ||
public string ProjectPath { get; } = "../../../../TestProject/TestProject.csproj"; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
test/MSBuild.Sdk.SqlProj.Aspire.Tests/MSBuild.Sdk.SqlProj.Aspire.Tests.csproj
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,31 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Aspire.Hosting.Testing" Version="8.1.0" /> | ||
<PackageReference Include="coverlet.collector" Version="6.0.2" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" /> | ||
<PackageReference Include="xunit" Version="2.9.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="System.Net" /> | ||
<Using Include="Microsoft.Extensions.DependencyInjection" /> | ||
<Using Include="Aspire.Hosting.ApplicationModel" /> | ||
<Using Include="Aspire.Hosting.Testing" /> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\MSBuild.Sdk.SqlProj.Aspire\MSBuild.Sdk.SqlProj.Aspire.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |