Skip to content

Commit

Permalink
Adding public API test coverage (#5149)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zombach authored Aug 14, 2024
1 parent 19fe787 commit 4a71736
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/Aspire.Hosting.PostgreSQL/PgAdminConfigWriterHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ internal sealed class PgAdminConfigWriterHook : IDistributedApplicationLifecycle
{
public Task AfterEndpointsAllocatedAsync(DistributedApplicationModel appModel, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(appModel);

var adminResource = appModel.Resources.OfType<PgAdminContainerResource>().Single();
var serverFileMount = adminResource.Annotations.OfType<ContainerMountAnnotation>().Single(v => v.Target == "/pgadmin4/servers.json");
var postgresInstances = appModel.Resources.OfType<PostgresServerResource>();
Expand Down
6 changes: 5 additions & 1 deletion src/Aspire.Hosting.PostgreSQL/PgAdminContainerResource.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using Aspire.Hosting.ApplicationModel;

namespace Aspire.Hosting.Postgres;
Expand All @@ -9,6 +11,8 @@ namespace Aspire.Hosting.Postgres;
/// Represents a container resource for PGAdmin.
/// </summary>
/// <param name="name">The name of the container resource.</param>
public sealed class PgAdminContainerResource(string name) : ContainerResource(name)
public sealed class PgAdminContainerResource(string name) : ContainerResource(ThrowIfNull(name))
{
private static string ThrowIfNull([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
=> argument ?? throw new ArgumentNullException(paramName);
}
31 changes: 28 additions & 3 deletions src/Aspire.Hosting.PostgreSQL/PostgresBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public static IResourceBuilder<PostgresServerResource> AddPostgres(this IDistrib
IResourceBuilder<ParameterResource>? password = null,
int? port = null)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(name);

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

var postgresServer = new PostgresServerResource(name, userName?.Resource, passwordParameter);
Expand All @@ -56,6 +59,9 @@ public static IResourceBuilder<PostgresServerResource> AddPostgres(this IDistrib
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<PostgresDatabaseResource> AddDatabase(this IResourceBuilder<PostgresServerResource> 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 @@ -73,6 +79,8 @@ public static IResourceBuilder<PostgresDatabaseResource> AddDatabase(this IResou
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<T> WithPgAdmin<T>(this IResourceBuilder<T> builder, Action<IResourceBuilder<PgAdminContainerResource>>? configureContainer = null, string? containerName = null) where T : PostgresServerResource
{
ArgumentNullException.ThrowIfNull(builder);

if (builder.ApplicationBuilder.Resources.OfType<PgAdminContainerResource>().SingleOrDefault() is { } existingPgAdminResource)
{
var builderForExistingResource = builder.ApplicationBuilder.CreateResourceBuilder(existingPgAdminResource);
Expand Down Expand Up @@ -108,6 +116,8 @@ public static IResourceBuilder<T> WithPgAdmin<T>(this IResourceBuilder<T> builde
/// <returns>The resource builder for PGAdmin.</returns>
public static IResourceBuilder<PgAdminContainerResource> WithHostPort(this IResourceBuilder<PgAdminContainerResource> builder, int? port)
{
ArgumentNullException.ThrowIfNull(builder);

return builder.WithEndpoint("http", endpoint =>
{
endpoint.Port = port;
Expand All @@ -133,7 +143,12 @@ private static void SetPgAdminEnvironmentVariables(EnvironmentCallbackContext co
/// <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<PostgresServerResource> WithDataVolume(this IResourceBuilder<PostgresServerResource> builder, string? name = null, bool isReadOnly = false)
=> builder.WithVolume(name ?? VolumeNameGenerator.CreateVolumeName(builder, "data"), "/var/lib/postgresql/data", isReadOnly);
{
ArgumentNullException.ThrowIfNull(builder);

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

/// <summary>
/// Adds a bind mount for the data folder to a PostgreSQL container resource.
Expand All @@ -143,7 +158,12 @@ public static IResourceBuilder<PostgresServerResource> WithDataVolume(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<PostgresServerResource> WithDataBindMount(this IResourceBuilder<PostgresServerResource> builder, string source, bool isReadOnly = false)
=> builder.WithBindMount(source, "/var/lib/postgresql/data", isReadOnly);
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(source);

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

/// <summary>
/// Adds a bind mount for the init folder to a PostgreSQL container resource.
Expand All @@ -153,5 +173,10 @@ public static IResourceBuilder<PostgresServerResource> WithDataBindMount(this IR
/// <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<PostgresServerResource> WithInitBindMount(this IResourceBuilder<PostgresServerResource> 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);
}
}
12 changes: 9 additions & 3 deletions src/Aspire.Hosting.PostgreSQL/PostgresDatabaseResource.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace Aspire.Hosting.ApplicationModel;

/// <summary>
Expand All @@ -9,12 +12,12 @@ namespace Aspire.Hosting.ApplicationModel;
/// <param name="name">The name of the resource.</param>
/// <param name="databaseName">The database name.</param>
/// <param name="postgresParentResource">The PostgreSQL parent resource associated with this database.</param>
public class PostgresDatabaseResource(string name, string databaseName, PostgresServerResource postgresParentResource) : Resource(name), IResourceWithParent<PostgresServerResource>, IResourceWithConnectionString
public class PostgresDatabaseResource(string name, string databaseName, PostgresServerResource postgresParentResource) : Resource(ThrowIfNull(name)), IResourceWithParent<PostgresServerResource>, IResourceWithConnectionString
{
/// <summary>
/// Gets the parent PostgresSQL container resource.
/// </summary>
public PostgresServerResource Parent { get; } = postgresParentResource;
public PostgresServerResource Parent { get; } = ThrowIfNull(postgresParentResource);

/// <summary>
/// Gets the connection string expression for the Postgres database.
Expand All @@ -25,5 +28,8 @@ public class PostgresDatabaseResource(string name, string databaseName, Postgres
/// <summary>
/// Gets the database name.
/// </summary>
public string DatabaseName { get; } = databaseName;
public string DatabaseName { get; } = ThrowIfNull(databaseName);

private static T ThrowIfNull<T>([NotNull] T? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
=> argument ?? throw new ArgumentNullException(paramName);
}
8 changes: 7 additions & 1 deletion src/Aspire.Hosting.PostgreSQL/PostgresServerResource.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace Aspire.Hosting.ApplicationModel;

/// <summary>
Expand All @@ -17,7 +20,7 @@ public class PostgresServerResource : ContainerResource, IResourceWithConnection
/// <param name="name">The name of the resource.</param>
/// <param name="userName">A parameter that contains the PostgreSQL server user name, or <see langword="null"/> to use a default value.</param>
/// <param name="password">A parameter that contains the PostgreSQL server password.</param>
public PostgresServerResource(string name, ParameterResource? userName, ParameterResource password) : base(name)
public PostgresServerResource(string name, ParameterResource? userName, ParameterResource password) : base(ThrowIfNull(name))
{
ArgumentNullException.ThrowIfNull(password);

Expand Down Expand Up @@ -92,4 +95,7 @@ internal void AddDatabase(string name, string databaseName)
{
_databases.TryAdd(name, databaseName);
}

private static string ThrowIfNull([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
=> argument ?? throw new ArgumentNullException(paramName);
}
Loading

0 comments on commit 4a71736

Please sign in to comment.