Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding public API test coverage for Aspire.Hosting.Valkey #5054

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/Aspire.Hosting.Valkey/ValkeyBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public static IResourceBuilder<ValkeyResource> AddValkey(this IDistributedApplic
string name,
int? port = null)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(name);

var valkey = new ValkeyResource(name);
return builder.AddResource(valkey)
.WithEndpoint(port: port, targetPort: 6379, name: ValkeyResource.PrimaryEndpointName)
Expand Down Expand Up @@ -80,6 +83,8 @@ public static IResourceBuilder<ValkeyResource> AddValkey(this IDistributedApplic
public static IResourceBuilder<ValkeyResource> WithDataVolume(this IResourceBuilder<ValkeyResource> builder,
string? name = null, bool isReadOnly = false)
{
ArgumentNullException.ThrowIfNull(builder);

builder.WithVolume(name ?? VolumeNameGenerator.CreateVolumeName(builder, "data"), ValkeyContainerDataDirectory,
isReadOnly);
if (!isReadOnly)
Expand Down Expand Up @@ -111,6 +116,9 @@ public static IResourceBuilder<ValkeyResource> WithDataVolume(this IResourceBuil
public static IResourceBuilder<ValkeyResource> WithDataBindMount(this IResourceBuilder<ValkeyResource> builder,
string source, bool isReadOnly = false)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(source);

builder.WithBindMount(source, ValkeyContainerDataDirectory, isReadOnly);
if (!isReadOnly)
{
Expand Down Expand Up @@ -138,11 +146,16 @@ public static IResourceBuilder<ValkeyResource> WithDataBindMount(this IResourceB
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<ValkeyResource> WithPersistence(this IResourceBuilder<ValkeyResource> builder,
TimeSpan? interval = null, long keysChangedThreshold = 1)
=> builder.WithAnnotation(new CommandLineArgsCallbackAnnotation(context =>
{
ArgumentNullException.ThrowIfNull(builder);

return builder.WithAnnotation(new CommandLineArgsCallbackAnnotation(context =>
{
context.Args.Add("--save");
context.Args.Add((interval ?? TimeSpan.FromSeconds(60)).TotalSeconds.ToString(CultureInfo.InvariantCulture));
context.Args.Add(
(interval ?? TimeSpan.FromSeconds(60)).TotalSeconds.ToString(CultureInfo.InvariantCulture));
context.Args.Add(keysChangedThreshold.ToString(CultureInfo.InvariantCulture));
return Task.CompletedTask;
}), ResourceAnnotationMutationBehavior.Replace);
}
}
102 changes: 102 additions & 0 deletions tests/Aspire.Hosting.Valkey.Tests/ValkeyPublicApiTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// 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 Xunit;

namespace Aspire.Hosting.Valkey.Tests;

public class ValkeyPublicApiTests
{
#region ValkeyBuilderExtensions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove Addrees pls.


[Fact]
public void AddValkeyContainerShouldThrowWhenBuilderIsNull()
{
IDistributedApplicationBuilder builder = null!;
const string name = "Valkey";

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

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

[Fact]
public void AddValkeyContainerShouldThrowWhenNameIsNull()
{
IDistributedApplicationBuilder builder = new DistributedApplicationBuilder([]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use TestDistributedApplicationBuilder.Create();

string name = null!;

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

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

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

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

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

[Fact]
public void WithDataBindMountShouldThrowWhenBuilderIsNull()
{
IResourceBuilder<ValkeyResource> builder = null!;
const string source = "Valkey";

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

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

[Fact]
public void WithDataBindMountShouldThrowWhenSourceIsNull()
{
var distributedApplicationBuilder = new DistributedApplicationBuilder([]);
const string name = "Valkey";
var resource = new ValkeyResource(name);
var builder = distributedApplicationBuilder.AddResource(resource);
string source = null!;

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

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

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

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

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

#endregion

#region ValkeyResource

[Fact]
public void CtorValkeyResourceShouldThrowWhenNameIsNull()
{
string name = null!;

var action = () => new ValkeyResource(name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a static check in the class
private static string ThrowIfNull([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
=> argument ?? throw new ArgumentNullException(paramName);


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

#endregion
}
Loading