diff --git a/tests/Core.Tests/ConfigTests.cs b/tests/Core.Tests/ConfigTests.cs index 8fa44c87..b7585676 100644 --- a/tests/Core.Tests/ConfigTests.cs +++ b/tests/Core.Tests/ConfigTests.cs @@ -1,18 +1,41 @@ -using SurrealDB.Shared.Tests; +using SurrealDB.Shared.Tests; namespace SurrealDB.Core.Tests; public class ConfigTests { [Fact] public void Build_with_endpoint() { - Config cfg = Config.Create().WithEndpoint($"{TestHelper.Loopback}:{TestHelper.Port}").Build(); + Config cfg = Config.Create() + .WithEndpoint($"{TestHelper.Loopback}:{TestHelper.Port}") + .Build(); + + TestHelper.ValidateEndpoint(cfg.Endpoint); + } + [Fact] + public void Build_with_endpoint_Not_Chained() { + var cfgBuilder = Config.Create(); + cfgBuilder.WithEndpoint($"{TestHelper.Loopback}:{TestHelper.Port}"); + var cfg = cfgBuilder.Build(); TestHelper.ValidateEndpoint(cfg.Endpoint); } [Fact] public void Build_with_address_and_port() { - Config cfg = Config.Create().WithAddress(TestHelper.Loopback).WithPort(TestHelper.Port).Build(); + Config cfg = Config.Create() + .WithAddress(TestHelper.Loopback) + .WithPort(TestHelper.Port) + .Build(); + + TestHelper.ValidateEndpoint(cfg.Endpoint); + } + + [Fact] + public void Build_with_address_and_port_Not_Chained() { + var cfgBuilder = Config.Create(); + cfgBuilder.WithAddress(TestHelper.Loopback); + cfgBuilder.WithPort(TestHelper.Port); + var cfg = cfgBuilder.Build(); TestHelper.ValidateEndpoint(cfg.Endpoint); } @@ -26,4 +49,125 @@ public void Build_last_option_should_overwrite_prior() { TestHelper.ValidateEndpoint(cfg.Endpoint); } + + [Fact] + public void Build_last_option_should_overwrite_prior_Not_Chained() { + var cfgBuilder = Config.Create(); + cfgBuilder.WithEndpoint($"0.0.0.0:{TestHelper.Port}"); + cfgBuilder.WithAddress(TestHelper.Loopback); + var cfg = cfgBuilder.Build(); + + TestHelper.ValidateEndpoint(cfg.Endpoint); + } + + [Fact] + public void Build_with_Rpc() { + Config cfg = Config.Create() + .WithAddress(TestHelper.Loopback).WithPort(TestHelper.Port) + .WithRpc() + .Build(); + + TestHelper.ValidateEndpoint(cfg.Endpoint); + cfg.RpcEndpoint.Should().NotBeNull(); + cfg.RestEndpoint.Should().BeNull(); + } + + [Fact] + public void Build_with_Rpc_Not_Chained() { + var cfgBuilder = Config.Create(); + cfgBuilder.WithAddress(TestHelper.Loopback); + cfgBuilder.WithPort(TestHelper.Port); + cfgBuilder.WithRpc(); + var cfg = cfgBuilder.Build(); + + TestHelper.ValidateEndpoint(cfg.Endpoint); + cfg.RpcEndpoint.Should().NotBeNull(); + cfg.RestEndpoint.Should().BeNull(); + } + + [Fact] + public void Build_with_Rest() { + Config cfg = Config.Create() + .WithAddress(TestHelper.Loopback) + .WithPort(TestHelper.Port) + .WithRest() + .Build(); + + TestHelper.ValidateEndpoint(cfg.Endpoint); + cfg.RestEndpoint.Should().NotBeNull(); + cfg.RpcEndpoint.Should().BeNull(); + } + + [Fact] + public void Build_with_Rest_Not_Chained() { + var cfgBuilder = Config.Create(); + cfgBuilder.WithAddress(TestHelper.Loopback); + cfgBuilder.WithPort(TestHelper.Port); + cfgBuilder.WithRest(); + var cfg = cfgBuilder.Build(); + + TestHelper.ValidateEndpoint(cfg.Endpoint); + cfg.RestEndpoint.Should().NotBeNull(); + cfg.RpcEndpoint.Should().BeNull(); + } + + [Fact] + public void Build_with_Basic_Auth() { + var username = "TestUsername"; + var password = "TestPassword"; + + Config cfg = Config.Create() + .WithAddress(TestHelper.Loopback) + .WithPort(TestHelper.Port) + .WithBasicAuth(username, password) + .Build(); + + TestHelper.ValidateEndpoint(cfg.Endpoint); + cfg.Username.Should().Be(username); + cfg.Password.Should().Be(password); + } + + [Fact] + public void Build_with_Basic_Auth_Not_Chained() { + var username = "TestUsername"; + var password = "TestPassword"; + + var cfgBuilder = Config.Create(); + cfgBuilder.WithAddress(TestHelper.Loopback); + cfgBuilder.WithPort(TestHelper.Port); + cfgBuilder.WithBasicAuth(username, password); + var cfg = cfgBuilder.Build(); + + TestHelper.ValidateEndpoint(cfg.Endpoint); + cfg.Username.Should().Be(username); + cfg.Password.Should().Be(password); + } + + [Fact] + public void Build_with_Jwt_Auth() { + var jwtToken = "a.jwt.token"; + + Config cfg = Config.Create() + .WithAddress(TestHelper.Loopback) + .WithPort(TestHelper.Port) + .WithJwtAuth(jwtToken) + .Build(); + + TestHelper.ValidateEndpoint(cfg.Endpoint); + cfg.JsonWebToken.Should().Be(jwtToken); + } + + [Fact] + public void Build_with_Jwt_Auth_Not_Chained() { + var jwtToken = "a.jwt.token"; + + var cfgBuilder = Config.Create(); + cfgBuilder.WithAddress(TestHelper.Loopback); + cfgBuilder.WithPort(TestHelper.Port); + cfgBuilder.WithJwtAuth(jwtToken); + var cfg = cfgBuilder.Build(); + + TestHelper.ValidateEndpoint(cfg.Endpoint); + cfg.JsonWebToken.Should().Be(jwtToken); + } } diff --git a/tests/Core.Tests/Core.Tests.csproj b/tests/Core.Tests/Core.Tests.csproj index 0902e7e2..bf04fe00 100644 --- a/tests/Core.Tests/Core.Tests.csproj +++ b/tests/Core.Tests/Core.Tests.csproj @@ -5,9 +5,14 @@ SurrealDB.Core.Tests + + + + + diff --git a/tests/Core.Tests/ServicesTests.cs b/tests/Core.Tests/ServicesTests.cs new file mode 100644 index 00000000..689e1f7e --- /dev/null +++ b/tests/Core.Tests/ServicesTests.cs @@ -0,0 +1,50 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; + +using SurrealDB.Abstractions; +using SurrealDB.Driver.Rest; +using SurrealDB.Driver.Rpc; +using SurrealDB.Extensions.Service; +using SurrealDB.Shared.Tests; + +namespace SurrealDB.Core.Tests; + +public class ServicesTests { + [Fact] + public void BuildRpcService() { + var services = new ServiceCollection(); + + services.AddSurrealDB(static b => b + .WithEndpoint("127.0.0.1:8082") + .WithDatabase("test") + .WithNamespace("test") + .WithRpc()); + + var serviceProvider = services.BuildServiceProvider(); + + var test = serviceProvider.GetRequiredService(); + + Assert.NotNull(serviceProvider.GetService()); + Assert.NotNull(serviceProvider.GetService>()); + Assert.NotNull(serviceProvider.GetService()); + } + + [Fact] + public void BuildRestService() { + var services = new ServiceCollection(); + + services.AddSurrealDB(static b => b + .WithEndpoint("127.0.0.1:8082") + .WithDatabase("test") + .WithNamespace("test") + .WithRest()); + + var serviceProvider = services.BuildServiceProvider(); + + var test = serviceProvider.GetRequiredService(); + + Assert.NotNull(serviceProvider.GetService()); + Assert.NotNull(serviceProvider.GetService>()); + Assert.NotNull(serviceProvider.GetService()); + } +}