1
0
mirror of https://github.com/JKorf/CryptoExchange.Net.git synced 2026-10-15 15:40:52 +00:00
This commit is contained in:
Jkorf
2026-09-11 13:22:48 +02:00
parent 911e09be61
commit 8419af278d
11 changed files with 431 additions and 31 deletions
@@ -1,5 +1,7 @@
using CryptoExchange.Net.SharedApis;
using CryptoExchange.Net.SharedApis;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using System.Linq;
namespace CryptoExchange.Net.UnitTests
{
@@ -16,7 +18,7 @@ namespace CryptoExchange.Net.UnitTests
Assert.Multiple(() =>
{
Assert.That(client.GetCapability<ITestCapability>(TradingMode.Spot), Is.SameAs(sharedApi));
Assert.That(client.GetCapability<ITestCapability>(TradingMode.Spot)?.Capability, Is.SameAs(sharedApi));
Assert.That(client.GetCapability<ITestCapability>(TradingMode.PerpetualLinear), Is.Null);
Assert.That(client.GetCapabilities<ITestCapability>(TradingMode.Spot), Has.Count.EqualTo(1));
Assert.That(client.GetCapabilities<ITestCapability>(TradingMode.PerpetualLinear), Is.Empty);
@@ -33,8 +35,73 @@ namespace CryptoExchange.Net.UnitTests
Assert.Multiple(() =>
{
Assert.That(client.GetCapability<ITestCapability>(TradingMode.Spot), Is.SameAs(sharedApi));
Assert.That(client.GetCapability<ITestCapability>(TradingMode.PerpetualLinear), Is.SameAs(sharedApi));
Assert.That(client.GetCapability<ITestCapability>(TradingMode.Spot)?.Capability, Is.SameAs(sharedApi));
Assert.That(client.GetCapability<ITestCapability>(TradingMode.PerpetualLinear)?.Capability, Is.SameAs(sharedApi));
});
}
[Test]
public void GetCapabilities_WithReference_ShouldReturnMatchingCapabilities()
{
var sharedApi = new TestSharedApi(
[TradingMode.Spot],
[TradingMode.Spot]);
var client = new TestSharedApiClient(sharedApi);
var result = client.GetCapabilities(
new SharedCapabilityReference<ITestCapability>(),
TradingMode.Spot);
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result[0].Capability, Is.SameAs(sharedApi));
Assert.That(result[0].Options, Is.SameAs(sharedApi.CapabilityOptions));
}
[Test]
public void Discover_ShouldReturnAllSharedApiInformation()
{
var restApi = new TestRestSharedApi();
var socketApi = new TestSocketSharedApi();
var client = new TestDiSharedApiClient(
SharedTransport.Socket,
restApi,
socketApi);
var result = client.Discover();
Assert.Multiple(() =>
{
Assert.That(result.PreferredTransport, Is.EqualTo(SharedTransport.Socket));
Assert.That(result.SharedApis, Has.Length.EqualTo(2));
Assert.That(result.SharedApis[0].Transport, Is.EqualTo(SharedTransport.Rest));
Assert.That(result.SharedApis[1].Transport, Is.EqualTo(SharedTransport.Socket));
Assert.That(result.SharedApis[0].SupportedTradingModes, Is.EqualTo(new[] { TradingMode.Spot }));
Assert.That(result.SharedApis[0].Capabilities, Has.Length.EqualTo(1));
Assert.That(result.SharedApis[0].Authenticated, Is.False);
});
}
[Test]
public void RegisterSharedApiClientCapabilities_ShouldResolvePreferredTransport()
{
var restApi = new TestRestSharedApi();
var socketApi = new TestSocketSharedApi();
var services = new ServiceCollection();
services.RegisterSharedApi<ITestRestSharedApi>(_ => restApi);
services.RegisterSharedApi<ITestSocketSharedApi>(_ => socketApi);
services.AddTransient<ITestSharedApiClient>(_ =>
new TestDiSharedApiClient(SharedTransport.Socket, restApi, socketApi));
services.RegisterSharedApiClientCapabilities<ITestSharedApiClient>();
using var provider = services.BuildServiceProvider();
Assert.Multiple(() =>
{
Assert.That(provider.GetRequiredService<ITestCapability>(), Is.SameAs(socketApi));
Assert.That(provider.GetServices<ITestCapability>().ToArray(), Is.EqualTo(new[] { socketApi }));
Assert.That(provider.GetRequiredService<ITestRestCapability>(), Is.SameAs(restApi));
Assert.That(provider.GetRequiredService<ITestSocketCapability>(), Is.SameAs(socketApi));
});
}
@@ -101,6 +168,28 @@ namespace CryptoExchange.Net.UnitTests
{
}
private interface ITestRestCapability : ITestCapability, ISharedRest
{
}
private interface ITestSocketCapability : ITestCapability, ISharedSocket
{
}
private interface ITestRestSharedApi : ITestRestCapability
{
}
private interface ITestSocketSharedApi : ITestSocketCapability
{
}
private interface ITestSharedApiClient : ISharedApiClientBase
{
ITestRestSharedApi Rest { get; }
ITestSocketSharedApi Socket { get; }
}
private sealed class TestCapabilityOptions : CapabilityOptions<SharedRequest, ITestCapability>
{
public override string Description => "Test capability";
@@ -159,10 +248,64 @@ namespace CryptoExchange.Net.UnitTests
public override SharedClientInfo Discover() => new();
}
private abstract class TestTransportSharedApi : SharedApiBase
{
protected TestTransportSharedApi(SharedTransport transport)
: base(
transport,
"TestExchange",
[TradingMode.Spot],
() => false,
(baseAsset, quoteAsset, tradingMode, deliverDate) => $"{baseAsset}{quoteAsset}")
{
SetCapabilities(new TestCapabilityOptions([TradingMode.Spot]));
}
public override SharedClientInfo Discover() => new()
{
Exchange = this.Exchange,
TypeName = GetType().Name,
SupportedTradingModes = this.SupportedTradingModes,
Transport = this.Transport,
Authenticated = this.Authenticated,
Capabilities = ((ISharedApi)this).Capabilities.ToArray()
};
}
private sealed class TestRestSharedApi : TestTransportSharedApi, ITestRestSharedApi
{
public TestRestSharedApi() : base(SharedTransport.Rest)
{
}
}
private sealed class TestSocketSharedApi : TestTransportSharedApi, ITestSocketSharedApi
{
public TestSocketSharedApi() : base(SharedTransport.Socket)
{
}
}
private sealed class TestDiSharedApiClient : SharedApiClientBase, ITestSharedApiClient
{
public ITestRestSharedApi Rest { get; }
public ITestSocketSharedApi Socket { get; }
public TestDiSharedApiClient(
SharedTransport preferredTransport,
ITestRestSharedApi rest,
ITestSocketSharedApi socket)
: base(preferredTransport, rest, socket)
{
Rest = rest;
Socket = socket;
}
}
private sealed class TestSharedApiClient : SharedApiClientBase
{
public TestSharedApiClient(params ISharedApiCapability[] sharedApis)
: base([SharedTransport.Rest, SharedTransport.Socket], sharedApis)
: base(SharedTransport.Rest, sharedApis)
{
}
}