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.MongoDB.Driver #5171

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ public static void AddMongoDBClient(
string connectionName,
Action<MongoDBSettings>? configureSettings = null,
Action<MongoClientSettings>? configureClientSettings = null)
=> builder.AddMongoDBClient(DefaultConfigSectionName, configureSettings, configureClientSettings, connectionName, serviceKey: null);
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentException.ThrowIfNullOrEmpty(connectionName);

builder.AddMongoDBClient(DefaultConfigSectionName, configureSettings, configureClientSettings, connectionName, serviceKey: null);
}

/// <summary>
/// Registers <see cref="IMongoClient"/> and <see cref="IMongoDatabase"/> instances for connecting MongoDB database with MongoDB.Driver client.
Expand All @@ -52,6 +57,7 @@ public static void AddKeyedMongoDBClient(
Action<MongoDBSettings>? configureSettings = null,
Action<MongoClientSettings>? configureClientSettings = null)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentException.ThrowIfNullOrEmpty(name);

builder.AddMongoDBClient(
Expand Down
82 changes: 82 additions & 0 deletions tests/Aspire.MongoDB.Driver.Tests/MongoDBDriverPublicApiTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.Hosting;
using Xunit;

namespace Aspire.MongoDB.Driver.Tests;

public class MongoDBClientPublicApiTests
{
[Fact]
public void AddMongoDBClientShouldThrowWhenBuilderIsNull()
{
IHostApplicationBuilder builder = null!;
var connectionName = "mongodb";

var action = () => builder.AddMongoDBClient(connectionName);

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

[Fact]
public void AddMongoDBClientShouldThrowWhenNameIsNull()
{
var builder = Host.CreateEmptyApplicationBuilder(null);
string connectionName = null!;

var action = () => builder.AddMongoDBClient(connectionName);

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

[Fact]
public void AddMongoDBClientShouldThrowWhenNameIsEmpty()
{
var builder = Host.CreateEmptyApplicationBuilder(null);
string connectionName = "";

var action = () => builder.AddMongoDBClient(connectionName);

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

[Fact]
public void AddKeyedMongoDBClientShouldThrowWhenBuilderIsNull()
{
IHostApplicationBuilder builder = null!;
var connectionName = "mongodb";

var action = () => builder.AddKeyedMongoDBClient(connectionName);

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

[Fact]
public void AddKeyedMongoDBClientShouldThrowWhenNameIsNull()
{
var builder = Host.CreateEmptyApplicationBuilder(null);
string name = null!;

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

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

[Fact]
public void AddKeyedMongoDBClientShouldThrowWhenNameIsEmpty()
{
var builder = Host.CreateEmptyApplicationBuilder(null);
string name = "";

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

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