Skip to content

Commit

Permalink
Feature/system.text.json (#104)
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 327e4bd commit 210b6c4
Show file tree
Hide file tree
Showing 26 changed files with 178 additions and 194 deletions.
8 changes: 4 additions & 4 deletions Huobi.Net.UnitTests/Huobi.Net.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
</PropertyGroup>

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

<ItemGroup>
Expand Down
15 changes: 8 additions & 7 deletions Huobi.Net.UnitTests/HuobiClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Huobi.Net.Clients.SpotApi;
using Huobi.Net.ExtensionMethods;
using CryptoExchange.Net.Objects.Sockets;
using NUnit.Framework.Legacy;

namespace Huobi.Net.UnitTests
{
Expand All @@ -27,9 +28,9 @@ public async Task ReceivingErrorResponse_Should_FailCall()
var result = await client.SpotApi.ExchangeData.GetAssetsAsync();

// assert
Assert.IsFalse(result.Success);
Assert.IsTrue(result.Error.ToString().Contains("Error!"));
Assert.IsTrue(result.Error.ToString().Contains("ErrorMessage"));
ClassicAssert.IsFalse(result.Success);
Assert.That(result.Error.ToString().Contains("Error!"));
Assert.That(result.Error.ToString().Contains("ErrorMessage"));
}

[TestCase]
Expand All @@ -42,8 +43,8 @@ public async Task ReceivingHttpErrorResponse_Should_FailCall()
var result = await client.SpotApi.ExchangeData.GetAssetsAsync();

// assert
Assert.IsFalse(result.Success);
Assert.IsTrue(result.Error.ToString().Contains("Error message"));
ClassicAssert.IsFalse(result.Success);
Assert.That(result.Error.ToString().Contains("Error message"));
}


Expand Down Expand Up @@ -84,7 +85,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 @@ -104,7 +105,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.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 Down
103 changes: 52 additions & 51 deletions Huobi.Net.UnitTests/HuobiSocketClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using NUnit.Framework.Legacy;

namespace Huobi.Net.UnitTests
{
[TestFixture]
public class HuobiSocketClientTests
{
[Test]
public async Task SubscribeV1_Should_SucceedIfSubbedResponse()
public void SubscribeV1_Should_SucceedIfSubbedResponse()
{
// arrange
var socket = new TestSocket();
Expand All @@ -29,11 +30,11 @@ public async Task SubscribeV1_Should_SucceedIfSubbedResponse()
// act
var subTask = client.SpotApi.SubscribeToPartialOrderBookUpdates1SecondAsync("ETHBTC", 1, test => { });
var id = JToken.Parse(socket.LastSendMessage)["id"];
await socket.InvokeMessage($"{{\"subbed\": \"test\", \"id\":\"{id}\", \"status\": \"ok\"}}");
socket.InvokeMessage($"{{\"subbed\": \"test\", \"id\":\"{id}\", \"status\": \"ok\"}}");
var subResult = subTask.Result;

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

[Test]
Expand All @@ -51,11 +52,11 @@ public async Task SubscribeV1_Should_FailIfNoResponse()
var subResult = await client.SpotApi.SubscribeToPartialOrderBookUpdates1SecondAsync("ETHBTC", 1, test => { });

// assert
Assert.IsFalse(subResult.Success);
ClassicAssert.IsFalse(subResult.Success);
}

[Test]
public async Task SubscribeV1_Should_FailIfErrorResponse()
public void SubscribeV1_Should_FailIfErrorResponse()
{
// arrange
var socket = new TestSocket();
Expand All @@ -65,15 +66,15 @@ public async Task SubscribeV1_Should_FailIfErrorResponse()
// act
var subTask = client.SpotApi.SubscribeToPartialOrderBookUpdates1SecondAsync("ETHBTC", 1, test => { });
var id = JToken.Parse(socket.LastSendMessage)["id"];
await socket.InvokeMessage($"{{\"status\": \"error\", \"id\": \"{id}\", \"err-code\": \"Fail\", \"err-msg\": \"failed\"}}");
socket.InvokeMessage($"{{\"status\": \"error\", \"id\": \"{id}\", \"err-code\": \"Fail\", \"err-msg\": \"failed\"}}");
var subResult = subTask.Result;

// assert
Assert.IsFalse(subResult.Success);
ClassicAssert.IsFalse(subResult.Success);
}

[Test]
public async Task SubscribeToDepthUpdates_Should_TriggerWithDepthUpdate()
public void SubscribeToDepthUpdates_Should_TriggerWithDepthUpdate()
{
// arrange
var socket = new TestSocket();
Expand All @@ -83,7 +84,7 @@ public async Task SubscribeToDepthUpdates_Should_TriggerWithDepthUpdate()
HuobiOrderBook result = null;
var subTask = client.SpotApi.SubscribeToPartialOrderBookUpdates1SecondAsync("ETHBTC", 1, test => result = test.Data);
var id = JToken.Parse(socket.LastSendMessage)["id"];
await socket.InvokeMessage($"{{\"subbed\": \"ethbtc\", \"status\": \"ok\", \"id\": \"{id}\"}}");
socket.InvokeMessage($"{{\"subbed\": \"ethbtc\", \"status\": \"ok\", \"id\": \"{id}\"}}");
var subResult = subTask.Result;

var expected = new HuobiOrderBook()
Expand All @@ -99,16 +100,16 @@ public async Task SubscribeToDepthUpdates_Should_TriggerWithDepthUpdate()
};

// act
await socket.InvokeMessage(SerializeExpected("market.ethbtc.depth.step1", expected));
socket.InvokeMessage(SerializeExpected("market.ethbtc.depth.step1", expected));

// assert
Assert.IsTrue(subResult.Success);
Assert.IsTrue(TestHelpers.AreEqual(expected.Asks.ToList()[0], result.Asks.ToList()[0]));
Assert.IsTrue(TestHelpers.AreEqual(expected.Bids.ToList()[0], result.Bids.ToList()[0]));
Assert.That(subResult.Success);
Assert.That(TestHelpers.AreEqual(expected.Asks.ToList()[0], result.Asks.ToList()[0]));
Assert.That(TestHelpers.AreEqual(expected.Bids.ToList()[0], result.Bids.ToList()[0]));
}

[Test]
public async Task SubscribeToDetailUpdates_Should_TriggerWithDetailUpdate()
public void SubscribeToDetailUpdates_Should_TriggerWithDetailUpdate()
{
// arrange
var socket = new TestSocket();
Expand All @@ -118,7 +119,7 @@ public async Task SubscribeToDetailUpdates_Should_TriggerWithDetailUpdate()
HuobiSymbolDetails result = null;
var subTask = client.SpotApi.SubscribeToSymbolDetailUpdatesAsync("ETHBTC", test => result = test.Data);
var id = JToken.Parse(socket.LastSendMessage)["id"];
await socket.InvokeMessage($"{{\"subbed\": \"ethbtc\", \"id\": \"{id}\", \"status\": \"ok\"}}");
socket.InvokeMessage($"{{\"subbed\": \"ethbtc\", \"id\": \"{id}\", \"status\": \"ok\"}}");
var subResult = subTask.Result;

var expected = new HuobiSymbolData()
Expand All @@ -133,15 +134,15 @@ public async Task SubscribeToDetailUpdates_Should_TriggerWithDetailUpdate()
};

// act
await socket.InvokeMessage(SerializeExpected("market.ethbtc.detail", expected));
socket.InvokeMessage(SerializeExpected("market.ethbtc.detail", expected));

// assert
Assert.IsTrue(subResult.Success);
Assert.IsTrue(TestHelpers.AreEqual(expected, result));
Assert.That(subResult.Success);
Assert.That(TestHelpers.AreEqual(expected, result));
}

[Test]
public async Task SubscribeToKlineUpdates_Should_TriggerWithKlineUpdate()
public void SubscribeToKlineUpdates_Should_TriggerWithKlineUpdate()
{
// arrange
var socket = new TestSocket();
Expand All @@ -151,7 +152,7 @@ public async Task SubscribeToKlineUpdates_Should_TriggerWithKlineUpdate()
HuobiSymbolData result = null;
var subTask = client.SpotApi.SubscribeToKlineUpdatesAsync("ETHBTC", KlineInterval.FiveMinutes, test => result = test.Data);
var id = JToken.Parse(socket.LastSendMessage)["id"];
await socket.InvokeMessage($"{{\"subbed\": \"ethbtc\", \"id\": \"{id}\", \"status\": \"ok\"}}");
socket.InvokeMessage($"{{\"subbed\": \"ethbtc\", \"id\": \"{id}\", \"status\": \"ok\"}}");
var subResult = subTask.Result;

var expected = new HuobiSymbolData()
Expand All @@ -166,15 +167,15 @@ public async Task SubscribeToKlineUpdates_Should_TriggerWithKlineUpdate()
};

// act
await socket.InvokeMessage(SerializeExpected("market.ethbtc.kline.5min", expected));
socket.InvokeMessage(SerializeExpected("market.ethbtc.kline.5min", expected));

// assert
Assert.IsTrue(subResult.Success);
Assert.IsTrue(TestHelpers.AreEqual(expected, result));
Assert.That(subResult.Success);
Assert.That(TestHelpers.AreEqual(expected, result));
}

[Test]
public async Task SubscribeToTickerUpdates_Should_TriggerWithTickerUpdate()
public void SubscribeToTickerUpdates_Should_TriggerWithTickerUpdate()
{
// arrange
var socket = new TestSocket();
Expand All @@ -184,7 +185,7 @@ public async Task SubscribeToTickerUpdates_Should_TriggerWithTickerUpdate()
IEnumerable<HuobiSymbolTicker> result = null;
var subTask = client.SpotApi.SubscribeToTickerUpdatesAsync((test => result = test.Data));
var id = JToken.Parse(socket.LastSendMessage)["id"];
await socket.InvokeMessage($"{{\"subbed\": \"test\", \"id\": \"{id}\", \"status\": \"ok\"}}");
socket.InvokeMessage($"{{\"subbed\": \"test\", \"id\": \"{id}\", \"status\": \"ok\"}}");
var subResult = subTask.Result;

var expected = new List<HuobiSymbolTicker>
Expand All @@ -202,15 +203,15 @@ public async Task SubscribeToTickerUpdates_Should_TriggerWithTickerUpdate()
};

// act
await socket.InvokeMessage(SerializeExpected("market.tickers", expected));
socket.InvokeMessage(SerializeExpected("market.tickers", expected));

// assert
Assert.IsTrue(subResult.Success);
Assert.IsTrue(TestHelpers.AreEqual(expected[0], result.First()));
Assert.That(subResult.Success);
Assert.That(TestHelpers.AreEqual(expected[0], result.First()));
}

[Test]
public async Task SubscribeToTradeUpdates_Should_TriggerWithTradeUpdate()
public void SubscribeToTradeUpdates_Should_TriggerWithTradeUpdate()
{
// arrange
var socket = new TestSocket();
Expand All @@ -220,7 +221,7 @@ public async Task SubscribeToTradeUpdates_Should_TriggerWithTradeUpdate()
HuobiSymbolTrade result = null;
var subTask = client.SpotApi.SubscribeToTradeUpdatesAsync("ethusdt", test => result = test.Data);
var id = JToken.Parse(socket.LastSendMessage)["id"];
await socket.InvokeMessage($"{{\"subbed\": \"test\", \"id\": \"{id}\", \"status\": \"ok\"}}");
socket.InvokeMessage($"{{\"subbed\": \"test\", \"id\": \"{id}\", \"status\": \"ok\"}}");
var subResult = subTask.Result;

var expected =
Expand All @@ -242,16 +243,16 @@ public async Task SubscribeToTradeUpdates_Should_TriggerWithTradeUpdate()
};

// act
await socket.InvokeMessage(SerializeExpected("market.ethusdt.trade.detail", expected));
socket.InvokeMessage(SerializeExpected("market.ethusdt.trade.detail", expected));

// assert
Assert.IsTrue(subResult.Success);
Assert.IsTrue(TestHelpers.AreEqual(expected, result, "Details"));
Assert.IsTrue(TestHelpers.AreEqual(expected.Details.ToList()[0], result.Details.ToList()[0]));
Assert.That(subResult.Success);
Assert.That(TestHelpers.AreEqual(expected, result, "Details"));
Assert.That(TestHelpers.AreEqual(expected.Details.ToList()[0], result.Details.ToList()[0]));
}

[Test]
public async Task SubscribeToAccountUpdates_Should_TriggerWithAccountUpdate()
public void SubscribeToAccountUpdates_Should_TriggerWithAccountUpdate()
{
// arrange
var socket = new TestSocket();
Expand All @@ -260,9 +261,9 @@ public async Task SubscribeToAccountUpdates_Should_TriggerWithAccountUpdate()

HuobiAccountUpdate result = null;
var subTask = client.SpotApi.SubscribeToAccountUpdatesAsync(test => result = test.Data);
await socket.InvokeMessage("{\"ch\": \"auth\", \"code\": 200, \"action\": \"req\"}");
socket.InvokeMessage("{\"ch\": \"auth\", \"code\": 200, \"action\": \"req\"}");
Thread.Sleep(100);
await socket.InvokeMessage($"{{\"action\": \"sub\", \"code\": 200, \"ch\": \"accounts.update#1\"}}");
socket.InvokeMessage($"{{\"action\": \"sub\", \"code\": 200, \"ch\": \"accounts.update#1\"}}");
var subResult = subTask.Result;

var expected = new HuobiAccountUpdate()
Expand All @@ -277,15 +278,15 @@ public async Task SubscribeToAccountUpdates_Should_TriggerWithAccountUpdate()
};

// act
await socket.InvokeMessage(SerializeExpectedAuth("accounts.update#1", expected));
socket.InvokeMessage(SerializeExpectedAuth("accounts.update#1", expected));

// assert
Assert.IsTrue(subResult.Success);
Assert.IsTrue(TestHelpers.AreEqual(expected, result));
Assert.That(subResult.Success);
Assert.That(TestHelpers.AreEqual(expected, result));
}

[Test]
public async Task SubscribeV2_Should_SucceedIfSubbedResponse()
public void SubscribeV2_Should_SucceedIfSubbedResponse()
{
// arrange
var socket = new TestSocket();
Expand All @@ -294,17 +295,17 @@ public async Task SubscribeV2_Should_SucceedIfSubbedResponse()

// act
var subTask = client.SpotApi.SubscribeToAccountUpdatesAsync(test => { });
await socket.InvokeMessage("{\"action\": \"req\", \"code\": 200, \"ch\": \"auth\"}");
socket.InvokeMessage("{\"action\": \"req\", \"code\": 200, \"ch\": \"auth\"}");
Thread.Sleep(10);
await socket.InvokeMessage("{\"action\": \"sub\", \"code\": 200, \"ch\": \"accounts.update#1\"}");
socket.InvokeMessage("{\"action\": \"sub\", \"code\": 200, \"ch\": \"accounts.update#1\"}");
var subResult = subTask.Result;

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

[Test]
public async Task SubscribeV2_Should_FailIfAuthErrorResponse()
public void SubscribeV2_Should_FailIfAuthErrorResponse()
{
// arrange
var socket = new TestSocket();
Expand All @@ -313,11 +314,11 @@ public async Task SubscribeV2_Should_FailIfAuthErrorResponse()

// act
var subTask = client.SpotApi.SubscribeToAccountUpdatesAsync(test => { });
await socket.InvokeMessage("{ \"action\": \"req\", \"ch\": \"auth\", \"code\": 400}");
socket.InvokeMessage("{ \"action\": \"req\", \"ch\": \"auth\", \"code\": 400}");
var subResult = subTask.Result;

// assert
Assert.IsFalse(subResult.Success);
ClassicAssert.IsFalse(subResult.Success);
}

//[Test]
Expand All @@ -330,14 +331,14 @@ public async Task SubscribeV2_Should_FailIfAuthErrorResponse()

// // act
// var subTask = client.SpotApi.SubscribeToAccountUpdatesAsync(test => { });
// await socket.InvokeMessage("{\"op\": \"auth\"}");
// socket.InvokeMessage("{\"op\": \"auth\"}");
// Thread.Sleep(10);
// var id = JToken.Parse(socket.LastSendMessage)["id"];
// await socket.InvokeMessage($"{{\"op\": \"sub\", \"cid\": \"{id}\", \"status\": \"error\", \"err-code\": 1, \"err-msg\": \"failed\"}}");
// socket.InvokeMessage($"{{\"op\": \"sub\", \"cid\": \"{id}\", \"status\": \"error\", \"err-code\": 1, \"err-msg\": \"failed\"}}");
// var subResult = subTask.Result;

// // assert
// Assert.IsFalse(subResult.Success);
// ClassicAssert.IsFalse(subResult.Success);
//}

[Test]
Expand All @@ -356,7 +357,7 @@ public void SubscribeV2_Should_FailIfNoResponse()
var subResult = subTask.Result;

// assert
Assert.IsFalse(subResult.Success);
ClassicAssert.IsFalse(subResult.Success);
}

public string SerializeExpected<T>(string channel, T data)
Expand Down
Loading

0 comments on commit 210b6c4

Please sign in to comment.