Skip to content

Commit

Permalink
Feature/system.text.json (#1358)
Browse files Browse the repository at this point in the history
CryptoExchange.Net V7.1.0
  • Loading branch information
JKorf authored Mar 16, 2024
1 parent 9a9affb commit 7177ac8
Show file tree
Hide file tree
Showing 31 changed files with 330 additions and 325 deletions.
8 changes: 4 additions & 4 deletions Binance.Net.UnitTests/Binance.Net.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0"></PackageReference>
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="NUnit" Version="3.13.2"></PackageReference>
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0"></PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0"></PackageReference>
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="NUnit" Version="4.1.0"></PackageReference>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"></PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
31 changes: 16 additions & 15 deletions Binance.Net.UnitTests/BinanceClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using Binance.Net.ExtensionMethods;
using Binance.Net.Objects.Options;
using CryptoExchange.Net.Objects.Sockets;
using NUnit.Framework.Legacy;

namespace Binance.Net.UnitTests
{
Expand All @@ -42,8 +43,8 @@ public async Task GetServerTime_Should_RespondWithServerTimeDateTime(long milise
var result = await client.SpotApi.ExchangeData.GetServerTimeAsync();

// assert
Assert.AreEqual(true, result.Success);
Assert.AreEqual(expected, result.Data);
Assert.That(result.Success);
Assert.That(expected == result.Data);
}

[TestCase]
Expand All @@ -65,8 +66,8 @@ public async Task StartUserStream_Should_RespondWithListenKey()
var result = await client.SpotApi.Account.StartUserStreamAsync();

// assert
Assert.IsTrue(result.Success);
Assert.IsTrue(key.ListenKey == result.Data);
Assert.That(result.Success);
Assert.That(key.ListenKey == result.Data);
}

[TestCase]
Expand All @@ -83,7 +84,7 @@ public async Task KeepAliveUserStream_Should_Respond()
var result = await client.SpotApi.Account.KeepAliveUserStreamAsync("test");

// assert
Assert.IsTrue(result.Success);
Assert.That(result.Success);
}

[TestCase]
Expand All @@ -96,7 +97,7 @@ public async Task StopUserStream_Should_Respond()
var result = await client.SpotApi.Account.StopUserStreamAsync("test");

// assert
Assert.IsTrue(result.Success);
Assert.That(result.Success);
}

[TestCase()]
Expand Down Expand Up @@ -135,10 +136,10 @@ public async Task ReceivingBinanceError_Should_ReturnBinanceErrorAndNotSuccess()
var result = await client.SpotApi.ExchangeData.GetServerTimeAsync();

// assert
Assert.IsFalse(result.Success);
Assert.IsNotNull(result.Error);
Assert.IsTrue(result.Error.Code == 123);
Assert.IsTrue(result.Error.Message == "Error!");
ClassicAssert.IsFalse(result.Success);
ClassicAssert.IsNotNull(result.Error);
Assert.That(result.Error.Code == 123);
Assert.That(result.Error.Message == "Error!");
}

[Test]
Expand All @@ -149,7 +150,7 @@ public void ProvidingApiCredentials_Should_SaveApiCredentials()
var authProvider = new BinanceAuthenticationProvider(new ApiCredentials("TestKey", "TestSecret"));

// assert
Assert.AreEqual(authProvider.GetApiKey(), "TestKey");
Assert.That(authProvider.GetApiKey() == "TestKey");
}

[Test]
Expand All @@ -163,10 +164,10 @@ public void AddingAuthToRequest_Should_AddApiKeyHeader()
// act
var headers = new Dictionary<string, string>();
authProvider.AuthenticateRequest(new BinanceRestApiClient(new TraceLogger(), new BinanceRestOptions(), new BinanceRestOptions().SpotOptions), request.Uri, HttpMethod.Get, new Dictionary<string, object>(), true, ArrayParametersSerialization.MultipleValues,
HttpMethodParameterPosition.InUri, out var uriParameters, out var bodyParameters, out headers);
HttpMethodParameterPosition.InUri, RequestBodyFormat.Json, out var uriParameters, out var bodyParameters, out headers);

// assert
Assert.IsTrue(headers.First().Key == "X-MBX-APIKEY" && headers.First().Value == "TestKey");
Assert.That(headers.First().Key == "X-MBX-APIKEY" && headers.First().Value == "TestKey");
}

[TestCase("BTCUSDT", true)]
Expand Down Expand Up @@ -201,7 +202,7 @@ public void CheckRestInterfaces()
foreach (var method in implementation.GetMethods().Where(m => m.ReturnType.IsAssignableTo(typeof(Task))))
{
var interfaceMethod = clientInterface.GetMethod(method.Name, method.GetParameters().Select(p => p.ParameterType).ToArray());
Assert.NotNull(interfaceMethod, $"Missing interface for method {method.Name} in {implementation.Name} implementing interface {clientInterface.Name}");
ClassicAssert.NotNull(interfaceMethod, $"Missing interface for method {method.Name} in {implementation.Name} implementing interface {clientInterface.Name}");
methods++;
}
Debug.WriteLine($"{clientInterface.Name} {methods} methods validated");
Expand All @@ -221,7 +222,7 @@ public void CheckSocketInterfaces()
foreach (var method in implementation.GetMethods().Where(m => m.ReturnType.IsAssignableTo(typeof(Task<CallResult<UpdateSubscription>>))))
{
var interfaceMethod = clientInterface.GetMethod(method.Name, method.GetParameters().Select(p => p.ParameterType).ToArray());
Assert.NotNull(interfaceMethod, $"Missing interface for method {method.Name} in {implementation.Name} implementing interface {clientInterface.GetType().Name}");
ClassicAssert.NotNull(interfaceMethod, $"Missing interface for method {method.Name} in {implementation.Name} implementing interface {clientInterface.GetType().Name}");
methods++;
}
Debug.WriteLine($"{clientInterface.Name} {methods} methods validated");
Expand Down
73 changes: 37 additions & 36 deletions Binance.Net.UnitTests/BinanceSocketClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Binance.Net.Objects.Options;
using Binance.Net.Objects.Sockets;
using CryptoExchange.Net;
using NUnit.Framework.Legacy;

namespace Binance.Net.UnitTests
{
Expand All @@ -28,7 +29,7 @@ public async Task SubscribingToKlineStream_Should_TriggerWhenKlineStreamMessageI

IBinanceStreamKlineData result = null;
var subTask = client.SpotApi.ExchangeData.SubscribeToKlineUpdatesAsync("ETHBTC", KlineInterval.OneMinute, (test) => result = test.Data);
await socket.InvokeMessage(new BinanceSocketQueryResponse { Id = ExchangeHelpers.LastId() - 1 });
socket.InvokeMessage(new BinanceSocketQueryResponse { Id = ExchangeHelpers.LastId() - 1 });
await subTask;

var data = new BinanceCombinedStream<BinanceStreamKlineData>()
Expand Down Expand Up @@ -62,12 +63,12 @@ public async Task SubscribingToKlineStream_Should_TriggerWhenKlineStreamMessageI
};

// act
await socket.InvokeMessage(data);
socket.InvokeMessage(data);

// assert
Assert.IsNotNull(result);
Assert.IsTrue(TestHelpers.AreEqual(data.Data, result, "Data"));
Assert.IsTrue(TestHelpers.AreEqual(data.Data.Data, result.Data));
ClassicAssert.NotNull(result, null);
Assert.That(TestHelpers.AreEqual(data.Data, result, "Data"));
Assert.That(TestHelpers.AreEqual(data.Data.Data, result.Data));
}

[TestCase()]
Expand All @@ -79,7 +80,7 @@ public async Task SubscribingToContinuousKlineStream_Should_TriggerWhenContinuou

IBinanceStreamKlineData result = null;
var subTask = client.UsdFuturesApi.SubscribeToContinuousContractKlineUpdatesAsync("ETHBTC", ContractType.Perpetual, KlineInterval.OneMinute, (test) => result = test.Data);
await socket.InvokeMessage(new BinanceSocketQueryResponse { Id = ExchangeHelpers.LastId() - 1 });
socket.InvokeMessage(new BinanceSocketQueryResponse { Id = ExchangeHelpers.LastId() - 1 });
await subTask;

var data = new BinanceCombinedStream<BinanceStreamContinuousKlineData>()
Expand Down Expand Up @@ -114,12 +115,12 @@ public async Task SubscribingToContinuousKlineStream_Should_TriggerWhenContinuou
};

// act
await socket.InvokeMessage(data);
socket.InvokeMessage(data);

// assert
Assert.IsNotNull(result);
Assert.IsTrue(TestHelpers.AreEqual(data.Data, result, "Data"));
Assert.IsTrue(TestHelpers.AreEqual(data.Data.Data, result.Data));
ClassicAssert.IsNotNull(result);
Assert.That(TestHelpers.AreEqual(data.Data, result, "Data"));
Assert.That(TestHelpers.AreEqual(data.Data.Data, result.Data));
}

[TestCase()]
Expand All @@ -131,7 +132,7 @@ public async Task SubscribingToSymbolTicker_Should_TriggerWhenSymbolTickerStream

IBinanceTick result = null;
var subTask = client.SpotApi.ExchangeData.SubscribeToTickerUpdatesAsync("ETHBTC", (test) => result = test.Data);
await socket.InvokeMessage(new BinanceSocketQueryResponse { Id = ExchangeHelpers.LastId() - 1 });
socket.InvokeMessage(new BinanceSocketQueryResponse { Id = ExchangeHelpers.LastId() - 1 });
await subTask;

var data = new BinanceCombinedStream<BinanceStreamTick>()
Expand All @@ -153,11 +154,11 @@ public async Task SubscribingToSymbolTicker_Should_TriggerWhenSymbolTickerStream
};

// act
await socket.InvokeMessage(data);
socket.InvokeMessage(data);

// assert
Assert.IsNotNull(result);
Assert.IsTrue(TestHelpers.AreEqual(data.Data, result));
ClassicAssert.IsNotNull(result);
Assert.That(TestHelpers.AreEqual(data.Data, result));
}

[TestCase()]
Expand All @@ -169,7 +170,7 @@ public async Task SubscribingToAllSymbolTicker_Should_TriggerWhenAllSymbolTicker

IBinanceTick[] result = null;
var subTask = client.SpotApi.ExchangeData.SubscribeToAllTickerUpdatesAsync((test) => result = test.Data.ToArray());
await socket.InvokeMessage(new BinanceSocketQueryResponse { Id = ExchangeHelpers.LastId() - 1 });
socket.InvokeMessage(new BinanceSocketQueryResponse { Id = ExchangeHelpers.LastId() - 1 });
await subTask;

var data = new BinanceCombinedStream<BinanceStreamTick[]>
Expand All @@ -195,11 +196,11 @@ public async Task SubscribingToAllSymbolTicker_Should_TriggerWhenAllSymbolTicker
};

// act
await socket.InvokeMessage(data);
socket.InvokeMessage(data);

// assert
Assert.IsNotNull(result);
Assert.IsTrue(TestHelpers.AreEqual(data.Data[0], result[0]));
ClassicAssert.IsNotNull(result);
Assert.That(TestHelpers.AreEqual(data.Data[0], result[0]));
}

[TestCase()]
Expand All @@ -211,7 +212,7 @@ public async Task SubscribingToTradeStream_Should_TriggerWhenTradeStreamMessageI

BinanceStreamTrade result = null;
var subTask = client.SpotApi.ExchangeData.SubscribeToTradeUpdatesAsync("ETHBTC", (test) => result = test.Data);
await socket.InvokeMessage(new BinanceSocketQueryResponse { Id = ExchangeHelpers.LastId() - 1 });
socket.InvokeMessage(new BinanceSocketQueryResponse { Id = ExchangeHelpers.LastId() - 1 });
await subTask;

var data = new BinanceCombinedStream<BinanceStreamTrade>()
Expand All @@ -232,11 +233,11 @@ public async Task SubscribingToTradeStream_Should_TriggerWhenTradeStreamMessageI
};

// act
await socket.InvokeMessage(data);
socket.InvokeMessage(data);

// assert
Assert.IsNotNull(result);
Assert.IsTrue(TestHelpers.AreEqual(data.Data, result));
ClassicAssert.IsNotNull(result);
Assert.That(TestHelpers.AreEqual(data.Data, result));
}

[TestCase()]
Expand All @@ -248,7 +249,7 @@ public async Task SubscribingToUserStream_Should_TriggerWhenAccountUpdateStreamM

BinanceStreamBalanceUpdate result = null;
var subTask = client.SpotApi.Account.SubscribeToUserDataUpdatesAsync("test", null, null, null, (test) => result = test.Data);
await socket.InvokeMessage(new BinanceSocketQueryResponse { Id = ExchangeHelpers.LastId() - 1 });
socket.InvokeMessage(new BinanceSocketQueryResponse { Id = ExchangeHelpers.LastId() - 1 });
await subTask;

var data = new BinanceCombinedStream<BinanceStreamBalanceUpdate>
Expand All @@ -265,11 +266,11 @@ public async Task SubscribingToUserStream_Should_TriggerWhenAccountUpdateStreamM
};

// act
await socket.InvokeMessage(data);
socket.InvokeMessage(data);

// assert
Assert.IsNotNull(result);
Assert.IsTrue(TestHelpers.AreEqual(data.Data, result, "ListenKey"));
ClassicAssert.IsNotNull(result);
Assert.That(TestHelpers.AreEqual(data.Data, result, "ListenKey"));
}

[TestCase()]
Expand All @@ -281,7 +282,7 @@ public async Task SubscribingToUserStream_Should_TriggerWhenOcoOrderUpdateStream

BinanceStreamOrderList result = null;
var subTask = client.SpotApi.Account.SubscribeToUserDataUpdatesAsync("test", null, (test) => result = test.Data, null, null);
await socket.InvokeMessage(new BinanceSocketQueryResponse { Id = ExchangeHelpers.LastId() - 1 });
socket.InvokeMessage(new BinanceSocketQueryResponse { Id = ExchangeHelpers.LastId() - 1 });
await subTask;

var data = new BinanceCombinedStream<BinanceStreamOrderList>
Expand Down Expand Up @@ -317,13 +318,13 @@ public async Task SubscribingToUserStream_Should_TriggerWhenOcoOrderUpdateStream
};

// act
await socket.InvokeMessage(data);
socket.InvokeMessage(data);

// assert
Assert.IsNotNull(result);
Assert.IsTrue(TestHelpers.AreEqual(data.Data, result, "Orders", "ListenKey"));
Assert.IsTrue(TestHelpers.AreEqual(data.Data.Orders.ToList()[0], result.Orders.ToList()[0]));
Assert.IsTrue(TestHelpers.AreEqual(data.Data.Orders.ToList()[1], result.Orders.ToList()[1]));
ClassicAssert.IsNotNull(result);
Assert.That(TestHelpers.AreEqual(data.Data, result, "Orders", "ListenKey"));
Assert.That(TestHelpers.AreEqual(data.Data.Orders.ToList()[0], result.Orders.ToList()[0]));
Assert.That(TestHelpers.AreEqual(data.Data.Orders.ToList()[1], result.Orders.ToList()[1]));
}

[TestCase()]
Expand All @@ -335,7 +336,7 @@ public async Task SubscribingToUserStream_Should_TriggerWhenOrderUpdateStreamMes

BinanceStreamOrderUpdate result = null;
var subTask = client.SpotApi.Account.SubscribeToUserDataUpdatesAsync("test", (test) => result = test.Data, null, null, null);
await socket.InvokeMessage(new BinanceSocketQueryResponse { Id = ExchangeHelpers.LastId() - 1 });
socket.InvokeMessage(new BinanceSocketQueryResponse { Id = ExchangeHelpers.LastId() - 1 });
await subTask;

var data = new BinanceCombinedStream<BinanceStreamOrderUpdate>
Expand Down Expand Up @@ -369,11 +370,11 @@ public async Task SubscribingToUserStream_Should_TriggerWhenOrderUpdateStreamMes
};

// act
await socket.InvokeMessage(data);
socket.InvokeMessage(data);

// assert
Assert.IsNotNull(result);
Assert.IsTrue(TestHelpers.AreEqual(data.Data, result, "Balances", "ListenKey"));
ClassicAssert.IsNotNull(result);
Assert.That(TestHelpers.AreEqual(data.Data, result, "Balances", "ListenKey"));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Binance.Net.Objects.Options;
using CryptoExchange.Net;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Clients;
using CryptoExchange.Net.Objects;
using Microsoft.Extensions.Logging;
using System;
Expand Down
19 changes: 13 additions & 6 deletions Binance.Net.UnitTests/TestImplementations/JsonToObjectComparer.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using CryptoExchange.Net.Converters;
using CryptoExchange.Net.Converters.JsonNet;
using CryptoExchange.Net.Objects;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using NUnit.Framework.Legacy;
using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -76,9 +78,12 @@ public async Task ProcessSubject<K>(
CallResult result = (CallResult)await TestHelpers.InvokeAsync(method, getSubject(client), input.ToArray());

// asset
Assert.Null(result.Error, method.Name);
ClassicAssert.Null(result.Error, method.Name);

var resultData = result.GetType().GetProperty("Data", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).GetValue(result);
var dataProperty = result.GetType().GetProperty("Data", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
if (dataProperty == null)
continue;
var resultData = dataProperty.GetValue(result);
ProcessData(method.Name + (i == 0 ? "" : i.ToString()), resultData, json, parametersToSetNull, useNestedJsonPropertyForCompare, ignoreProperties);
}
}
Expand Down Expand Up @@ -199,10 +204,8 @@ private static void CheckObject(string method, JProperty prop, object obj, Dicti

// Property has a value
var property = resultProperties.SingleOrDefault(p => p.Item2?.PropertyName == prop.Name).p;
if (property is null)
property = resultProperties.SingleOrDefault(p => p.p.Name == prop.Name).p;
if (property is null)
property = resultProperties.SingleOrDefault(p => p.p.Name.ToUpperInvariant() == prop.Name.ToUpperInvariant()).p;
property ??= resultProperties.SingleOrDefault(p => p.p.Name == prop.Name).p;
property ??= resultProperties.SingleOrDefault(p => p.p.Name.ToUpperInvariant() == prop.Name.ToUpperInvariant()).p;

if (property is null)
{
Expand Down Expand Up @@ -326,7 +329,9 @@ private static void CheckPropertyValue(string method, JToken propValue, object p
{
if (info.GetCustomAttribute<JsonConverterAttribute>(true) == null
&& info.GetCustomAttribute<JsonPropertyAttribute>(true)?.ItemConverterType == null)
{
CheckValues(method, propertyName, (JValue)propValue, propertyValue);
}
}
}
}
Expand All @@ -345,7 +350,9 @@ private static void CheckValues(string method, string property, JValue jsonValue
// timestamp, hard to check..
}
else if (jsonValue.Value<string>().ToLowerInvariant() != objectValue.ToString().ToLowerInvariant())
{
throw new Exception($"{method}: {property} not equal: {jsonValue.Value<string>()} vs {objectValue.ToString()}");
}
}
else if (jsonValue.Type == JTokenType.Integer)
{
Expand Down
Loading

0 comments on commit 7177ac8

Please sign in to comment.