Skip to content

Commit

Permalink
Adding public API test coverage (#5156)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alirexaa authored Aug 6, 2024
1 parent e29edf8 commit 5373547
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public static class MySqlBuilderExtensions
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<MySqlServerResource> AddMySql(this IDistributedApplicationBuilder builder, string name, IResourceBuilder<ParameterResource>? password = null, int? port = null)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(name);

var passwordParameter = password?.Resource ?? ParameterResourceBuilderExtensions.CreateDefaultPasswordParameter(builder, $"{name}-password");

var resource = new MySqlServerResource(name, passwordParameter);
Expand All @@ -47,6 +50,9 @@ public static IResourceBuilder<MySqlServerResource> AddMySql(this IDistributedAp
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<MySqlDatabaseResource> AddDatabase(this IResourceBuilder<MySqlServerResource> builder, string name, string? databaseName = null)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(name);

// Use the resource name as the database name if it's not provided
databaseName ??= name;

Expand All @@ -64,6 +70,8 @@ public static IResourceBuilder<MySqlDatabaseResource> AddDatabase(this IResource
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<T> WithPhpMyAdmin<T>(this IResourceBuilder<T> builder, Action<IResourceBuilder<PhpMyAdminContainerResource>>? configureContainer = null, string? containerName = null) where T : MySqlServerResource
{
ArgumentNullException.ThrowIfNull(builder);

if (builder.ApplicationBuilder.Resources.OfType<PhpMyAdminContainerResource>().Any())
{
return builder;
Expand Down Expand Up @@ -94,6 +102,8 @@ public static IResourceBuilder<T> WithPhpMyAdmin<T>(this IResourceBuilder<T> bui
/// <returns>The resource builder for PGAdmin.</returns>
public static IResourceBuilder<PhpMyAdminContainerResource> WithHostPort(this IResourceBuilder<PhpMyAdminContainerResource> builder, int? port)
{
ArgumentNullException.ThrowIfNull(builder);

return builder.WithEndpoint("http", endpoint =>
{
endpoint.Port = port;
Expand All @@ -108,7 +118,11 @@ public static IResourceBuilder<PhpMyAdminContainerResource> WithHostPort(this IR
/// <param name="isReadOnly">A flag that indicates if this is a read-only volume.</param>
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<MySqlServerResource> WithDataVolume(this IResourceBuilder<MySqlServerResource> builder, string? name = null, bool isReadOnly = false)
=> builder.WithVolume(name ?? VolumeNameGenerator.CreateVolumeName(builder, "data"), "/var/lib/mysql", isReadOnly);
{
ArgumentNullException.ThrowIfNull(builder);

return builder.WithVolume(name ?? VolumeNameGenerator.CreateVolumeName(builder, "data"), "/var/lib/mysql", isReadOnly);
}

/// <summary>
/// Adds a bind mount for the data folder to a MySql container resource.
Expand All @@ -118,7 +132,12 @@ public static IResourceBuilder<MySqlServerResource> WithDataVolume(this IResourc
/// <param name="isReadOnly">A flag that indicates if this is a read-only mount.</param>
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<MySqlServerResource> WithDataBindMount(this IResourceBuilder<MySqlServerResource> builder, string source, bool isReadOnly = false)
=> builder.WithBindMount(source, "/var/lib/mysql", isReadOnly);
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(source);

return builder.WithBindMount(source, "/var/lib/mysql", isReadOnly);
}

/// <summary>
/// Adds a bind mount for the init folder to a MySql container resource.
Expand All @@ -128,5 +147,10 @@ public static IResourceBuilder<MySqlServerResource> WithDataBindMount(this IReso
/// <param name="isReadOnly">A flag that indicates if this is a read-only mount.</param>
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<MySqlServerResource> WithInitBindMount(this IResourceBuilder<MySqlServerResource> builder, string source, bool isReadOnly = true)
=> builder.WithBindMount(source, "/docker-entrypoint-initdb.d", isReadOnly);
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(source);

return builder.WithBindMount(source, "/docker-entrypoint-initdb.d", isReadOnly);
}
}
168 changes: 168 additions & 0 deletions tests/Aspire.Hosting.MySql.Tests/MySqlPublicApiTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Utils;
using Xunit;

namespace Aspire.Hosting.MySql.Tests;

public class MySqlPublicApiTests
{
[Fact]
public void AddMySqlContainerShouldThrowWhenBuilderIsNull()
{
IDistributedApplicationBuilder builder = null!;
string name = "MySql";

var action = () => builder.AddMySql(name);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
}

[Fact]
public void AddMySqlContainerShouldThrowWhenNameIsNull()
{
var builder = DistributedApplication.CreateBuilder([]);
string name = null!;

var action = () => builder.AddMySql(name);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(name), exception.ParamName);
}

[Fact]
public void WithDataVolumeShouldThrowWhenBuilderIsNull()
{
IResourceBuilder<MySqlServerResource> builder = null!;

var action = () => builder.WithDataVolume();

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
}

[Fact]
public void WithDataBindMountShouldThrowWhenBuilderIsNull()
{
IResourceBuilder<MySqlServerResource> builder = null!;
string source = "/MySql/data";

var action = () => builder.WithDataBindMount(source);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
}

[Fact]
public void WithDataBindMountShouldThrowWhenSourceIsNull()
{
var builder = TestDistributedApplicationBuilder.Create();
var mySql = builder.AddMySql("MySql");
string source = null!;

var action = () => mySql.WithDataBindMount(source);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(source), exception.ParamName);
}

[Fact]
public void WithInitBindMountShouldThrowWhenBuilderIsNull()
{
IResourceBuilder<MySqlServerResource> builder = null!;
string source = "/MySql/init.sql";

var action = () => builder.WithInitBindMount(source);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
}

[Fact]
public void WithInitBindMountShouldThrowWhenSourceIsNull()
{
var builder = TestDistributedApplicationBuilder.Create();
var mySql = builder.AddMySql("MySql");
string source = null!;

var action = () => mySql.WithInitBindMount(source);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(source), exception.ParamName);
}

[Fact]
public void AddDatabaseShouldThrowWhenBuilderIsNull()
{
IResourceBuilder<MySqlServerResource> builder = null!;
var name = "db";

var action = () => builder.AddDatabase(name);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
}

[Fact]
public void AddDatabaseShouldThrowWhenNameIsNull()
{
var builder = TestDistributedApplicationBuilder.Create();
var mySql = builder.AddMySql("MySql");
string name = null!;

var action = () => mySql.AddDatabase(name);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(name), exception.ParamName);
}

[Fact]
public void WithPhpMyAdminShouldThrowWhenBuilderIsNull()
{
IResourceBuilder<MySqlServerResource> builder = null!;

var action = () => builder.WithPhpMyAdmin();

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
}
[Fact]
public void WithHostPortShouldThrowWhenBuilderIsNull()
{
IResourceBuilder<PhpMyAdminContainerResource> builder = null!;

var action = () => builder.WithHostPort(6033);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(builder), exception.ParamName);
}

[Fact]
public void CtorMySqlServerResourceShouldThrowWhenNameIsNull()
{
var distributedApplicationBuilder = DistributedApplication.CreateBuilder([]);
string name = null!;

var password = ParameterResourceBuilderExtensions.CreateDefaultPasswordParameter(distributedApplicationBuilder, "password", special: false);

var action = () => new MySqlServerResource(name, password);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(name), exception.ParamName);
}

[Fact]
public void CtorMySqlServerResourceShouldThrowWhenPasswordIsNull()
{
const string name = "MySql";
ParameterResource password = null!;

var action = () => new MySqlServerResource(name, password);

var exception = Assert.Throws<ArgumentNullException>(action);
Assert.Equal(nameof(password), exception.ParamName);
}
}

0 comments on commit 5373547

Please sign in to comment.