Skip to content
This repository has been archived by the owner on Dec 18, 2023. It is now read-only.

Added config and services tests #75

Open
wants to merge 8 commits into
base: develop
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
150 changes: 147 additions & 3 deletions tests/Core.Tests/ConfigTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
Expand All @@ -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);
}
}
5 changes: 5 additions & 0 deletions tests/Core.Tests/Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
<AssemblyName>SurrealDB.Core.Tests</AssemblyName>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Common\Common.csproj" />
<ProjectReference Include="..\..\src\Configuration\Configuration.csproj" />
<ProjectReference Include="..\..\src\Extensions\Service\Service.csproj" />
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>

Expand Down
50 changes: 50 additions & 0 deletions tests/Core.Tests/ServicesTests.cs
Original file line number Diff line number Diff line change
@@ -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<SurrealOptions>();

Assert.NotNull(serviceProvider.GetService<IDatabase>());
Assert.NotNull(serviceProvider.GetService<IDatabase<RpcResponse>>());
Assert.NotNull(serviceProvider.GetService<DatabaseRpc>());
}

[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<SurrealOptions>();

Assert.NotNull(serviceProvider.GetService<IDatabase>());
Assert.NotNull(serviceProvider.GetService<IDatabase<RestResponse>>());
Assert.NotNull(serviceProvider.GetService<DatabaseRest>());
}
}