Skip to content

Commit 4a71736

Browse files
authored
Adding public API test coverage (#5149)
1 parent 19fe787 commit 4a71736

File tree

6 files changed

+288
-8
lines changed

6 files changed

+288
-8
lines changed

src/Aspire.Hosting.PostgreSQL/PgAdminConfigWriterHook.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ internal sealed class PgAdminConfigWriterHook : IDistributedApplicationLifecycle
1212
{
1313
public Task AfterEndpointsAllocatedAsync(DistributedApplicationModel appModel, CancellationToken cancellationToken)
1414
{
15+
ArgumentNullException.ThrowIfNull(appModel);
16+
1517
var adminResource = appModel.Resources.OfType<PgAdminContainerResource>().Single();
1618
var serverFileMount = adminResource.Annotations.OfType<ContainerMountAnnotation>().Single(v => v.Target == "/pgadmin4/servers.json");
1719
var postgresInstances = appModel.Resources.OfType<PostgresServerResource>();
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
using System.Runtime.CompilerServices;
46
using Aspire.Hosting.ApplicationModel;
57

68
namespace Aspire.Hosting.Postgres;
@@ -9,6 +11,8 @@ namespace Aspire.Hosting.Postgres;
911
/// Represents a container resource for PGAdmin.
1012
/// </summary>
1113
/// <param name="name">The name of the container resource.</param>
12-
public sealed class PgAdminContainerResource(string name) : ContainerResource(name)
14+
public sealed class PgAdminContainerResource(string name) : ContainerResource(ThrowIfNull(name))
1315
{
16+
private static string ThrowIfNull([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
17+
=> argument ?? throw new ArgumentNullException(paramName);
1418
}

src/Aspire.Hosting.PostgreSQL/PostgresBuilderExtensions.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public static IResourceBuilder<PostgresServerResource> AddPostgres(this IDistrib
3131
IResourceBuilder<ParameterResource>? password = null,
3232
int? port = null)
3333
{
34+
ArgumentNullException.ThrowIfNull(builder);
35+
ArgumentNullException.ThrowIfNull(name);
36+
3437
var passwordParameter = password?.Resource ?? ParameterResourceBuilderExtensions.CreateDefaultPasswordParameter(builder, $"{name}-password");
3538

3639
var postgresServer = new PostgresServerResource(name, userName?.Resource, passwordParameter);
@@ -56,6 +59,9 @@ public static IResourceBuilder<PostgresServerResource> AddPostgres(this IDistrib
5659
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
5760
public static IResourceBuilder<PostgresDatabaseResource> AddDatabase(this IResourceBuilder<PostgresServerResource> builder, string name, string? databaseName = null)
5861
{
62+
ArgumentNullException.ThrowIfNull(builder);
63+
ArgumentNullException.ThrowIfNull(name);
64+
5965
// Use the resource name as the database name if it's not provided
6066
databaseName ??= name;
6167

@@ -73,6 +79,8 @@ public static IResourceBuilder<PostgresDatabaseResource> AddDatabase(this IResou
7379
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
7480
public static IResourceBuilder<T> WithPgAdmin<T>(this IResourceBuilder<T> builder, Action<IResourceBuilder<PgAdminContainerResource>>? configureContainer = null, string? containerName = null) where T : PostgresServerResource
7581
{
82+
ArgumentNullException.ThrowIfNull(builder);
83+
7684
if (builder.ApplicationBuilder.Resources.OfType<PgAdminContainerResource>().SingleOrDefault() is { } existingPgAdminResource)
7785
{
7886
var builderForExistingResource = builder.ApplicationBuilder.CreateResourceBuilder(existingPgAdminResource);
@@ -108,6 +116,8 @@ public static IResourceBuilder<T> WithPgAdmin<T>(this IResourceBuilder<T> builde
108116
/// <returns>The resource builder for PGAdmin.</returns>
109117
public static IResourceBuilder<PgAdminContainerResource> WithHostPort(this IResourceBuilder<PgAdminContainerResource> builder, int? port)
110118
{
119+
ArgumentNullException.ThrowIfNull(builder);
120+
111121
return builder.WithEndpoint("http", endpoint =>
112122
{
113123
endpoint.Port = port;
@@ -133,7 +143,12 @@ private static void SetPgAdminEnvironmentVariables(EnvironmentCallbackContext co
133143
/// <param name="isReadOnly">A flag that indicates if this is a read-only volume.</param>
134144
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
135145
public static IResourceBuilder<PostgresServerResource> WithDataVolume(this IResourceBuilder<PostgresServerResource> builder, string? name = null, bool isReadOnly = false)
136-
=> builder.WithVolume(name ?? VolumeNameGenerator.CreateVolumeName(builder, "data"), "/var/lib/postgresql/data", isReadOnly);
146+
{
147+
ArgumentNullException.ThrowIfNull(builder);
148+
149+
return builder.WithVolume(name ?? VolumeNameGenerator.CreateVolumeName(builder, "data"),
150+
"/var/lib/postgresql/data", isReadOnly);
151+
}
137152

138153
/// <summary>
139154
/// Adds a bind mount for the data folder to a PostgreSQL container resource.
@@ -143,7 +158,12 @@ public static IResourceBuilder<PostgresServerResource> WithDataVolume(this IReso
143158
/// <param name="isReadOnly">A flag that indicates if this is a read-only mount.</param>
144159
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
145160
public static IResourceBuilder<PostgresServerResource> WithDataBindMount(this IResourceBuilder<PostgresServerResource> builder, string source, bool isReadOnly = false)
146-
=> builder.WithBindMount(source, "/var/lib/postgresql/data", isReadOnly);
161+
{
162+
ArgumentNullException.ThrowIfNull(builder);
163+
ArgumentNullException.ThrowIfNull(source);
164+
165+
return builder.WithBindMount(source, "/var/lib/postgresql/data", isReadOnly);
166+
}
147167

148168
/// <summary>
149169
/// Adds a bind mount for the init folder to a PostgreSQL container resource.
@@ -153,5 +173,10 @@ public static IResourceBuilder<PostgresServerResource> WithDataBindMount(this IR
153173
/// <param name="isReadOnly">A flag that indicates if this is a read-only mount.</param>
154174
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
155175
public static IResourceBuilder<PostgresServerResource> WithInitBindMount(this IResourceBuilder<PostgresServerResource> builder, string source, bool isReadOnly = true)
156-
=> builder.WithBindMount(source, "/docker-entrypoint-initdb.d", isReadOnly);
176+
{
177+
ArgumentNullException.ThrowIfNull(builder);
178+
ArgumentNullException.ThrowIfNull(source);
179+
180+
return builder.WithBindMount(source, "/docker-entrypoint-initdb.d", isReadOnly);
181+
}
157182
}

src/Aspire.Hosting.PostgreSQL/PostgresDatabaseResource.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
using System.Runtime.CompilerServices;
6+
47
namespace Aspire.Hosting.ApplicationModel;
58

69
/// <summary>
@@ -9,12 +12,12 @@ namespace Aspire.Hosting.ApplicationModel;
912
/// <param name="name">The name of the resource.</param>
1013
/// <param name="databaseName">The database name.</param>
1114
/// <param name="postgresParentResource">The PostgreSQL parent resource associated with this database.</param>
12-
public class PostgresDatabaseResource(string name, string databaseName, PostgresServerResource postgresParentResource) : Resource(name), IResourceWithParent<PostgresServerResource>, IResourceWithConnectionString
15+
public class PostgresDatabaseResource(string name, string databaseName, PostgresServerResource postgresParentResource) : Resource(ThrowIfNull(name)), IResourceWithParent<PostgresServerResource>, IResourceWithConnectionString
1316
{
1417
/// <summary>
1518
/// Gets the parent PostgresSQL container resource.
1619
/// </summary>
17-
public PostgresServerResource Parent { get; } = postgresParentResource;
20+
public PostgresServerResource Parent { get; } = ThrowIfNull(postgresParentResource);
1821

1922
/// <summary>
2023
/// Gets the connection string expression for the Postgres database.
@@ -25,5 +28,8 @@ public class PostgresDatabaseResource(string name, string databaseName, Postgres
2528
/// <summary>
2629
/// Gets the database name.
2730
/// </summary>
28-
public string DatabaseName { get; } = databaseName;
31+
public string DatabaseName { get; } = ThrowIfNull(databaseName);
32+
33+
private static T ThrowIfNull<T>([NotNull] T? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
34+
=> argument ?? throw new ArgumentNullException(paramName);
2935
}

src/Aspire.Hosting.PostgreSQL/PostgresServerResource.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
using System.Runtime.CompilerServices;
6+
47
namespace Aspire.Hosting.ApplicationModel;
58

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

@@ -92,4 +95,7 @@ internal void AddDatabase(string name, string databaseName)
9295
{
9396
_databases.TryAdd(name, databaseName);
9497
}
98+
99+
private static string ThrowIfNull([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
100+
=> argument ?? throw new ArgumentNullException(paramName);
95101
}

0 commit comments

Comments
 (0)