Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/system.text.json #66

Merged
merged 11 commits into from
Mar 16, 2024
Merged
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
8 changes: 4 additions & 4 deletions CoinEx.Net.UnitTests/CoinEx.Net.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,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
128 changes: 43 additions & 85 deletions CoinEx.Net.UnitTests/CoinExClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,93 +19,51 @@
using CoinEx.Net.Clients.SpotApi;
using CoinEx.Net.ExtensionMethods;
using CryptoExchange.Net.Objects.Sockets;
using NUnit.Framework.Legacy;

namespace CoinEx.Net.UnitTests
{
[TestFixture]
public class CoinExClientTests
{
//[Test]
//public async Task GetKlines_Should_RespondWithKlines()
//{
// // arrange
// CoinExKline[] expected = new CoinExKline[] {
// new CoinExKline(),
// new CoinExKline(),
// };
// var objects = TestHelpers.PrepareClient(() => Construct(), CreateRequest(expected));

// // act
// var result = await objects.Client.GetKlinesAsync("ETHBTC", KlineInterval.FiveMinute);

// // assert
// Assert.AreEqual(true, result.Success);
// TestHelpers.PublicInstancePropertiesEqual(expected[0], result.Data.ToList()[0]);
// TestHelpers.PublicInstancePropertiesEqual(expected[1], result.Data.ToList()[1]);
//}

//[Test]
//public async Task ReceivingCoinExError_Should_ReturnCoinExErrorAndNotSuccess()
//{
// // arrange
// var response = JsonConvert.SerializeObject(new CoinExApiResult<object>() { Code = 101, Data = new object(), Message = "Some error" });
// var objects = TestHelpers.PrepareClient(() => Construct(), response);

// // act
// var result = await objects.Client.GetSymbolsAsync();

// // assert
// Assert.IsFalse(result.Success);
// Assert.IsNotNull(result.Error);
// Assert.IsTrue(result.Error.ToString().Contains("Some error"));
//}

//[Test]
//public async Task ReceivingHttpError_Should_ReturnErrorAndNotSuccess()
//{
// // arrange
// var objects = TestHelpers.PrepareClient(() => Construct(), "Error request", HttpStatusCode.BadRequest);

// // act
// var result = await objects.Client.GetSymbolsAsync();

// // assert
// Assert.IsFalse(result.Success);
// Assert.IsNotNull(result.Error);
// Assert.IsTrue(result.Error.ToString().Contains("Error request"));
//}

//[Test]
//public async Task AuthenticatedRequests_Should_HaveAuthenticationHeader()
//{
// // arrange
// var objects = TestHelpers.PrepareClient(() => Construct(new CoinExClientOptions()
// {
// ApiCredentials = new ApiCredentials("test", "test")
// }), CreateRequest("{}"));

// // act
// var result = await objects.Client.GetBalancesAsync();

// // assert
// objects.Request.Verify(r => r.AddHeader("Authorization", It.IsAny<string>()));
//}

//[Test]
//public async Task PostRequests_Should_HaveContentBody()
//{
// // arrange
// var objects = TestHelpers.PrepareClient(() => Construct(new CoinExClientOptions()
// {
// ApiCredentials = new ApiCredentials("test", "test")
// }), CreateRequest("{}"));

// // act
// var result = await objects.Client.PlaceOrderAsync("BTCETH", OrderType.Limit, OrderSide.Buy, 1, 1);

// // assert
// objects.Request.Verify(r => r.SetContent(It.IsAny<string>(), It.IsAny<string>()));
//}

[TestCase()]
public async Task ReceivingError_Should_ReturnErrorAndNotSuccess()
{
// arrange
var client = TestHelpers.CreateClient();
var resultObj = new CoinExApiResult()
{
Code = 400001,
Message = "Error occured"
};

TestHelpers.SetResponse((CoinExRestClient)client, JsonConvert.SerializeObject(resultObj));

// act
var result = await client.SpotApi.ExchangeData.GetAssetsAsync();

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

[TestCase()]
public async Task ReceivingHttpErrorWithNoJson_Should_ReturnErrorAndNotSuccess()
{
// arrange
var client = TestHelpers.CreateClient();
TestHelpers.SetResponse((CoinExRestClient)client, "", System.Net.HttpStatusCode.BadRequest);

// act
var result = await client.SpotApi.ExchangeData.GetAssetsAsync();

// assert
ClassicAssert.IsFalse(result.Success);
ClassicAssert.IsNotNull(result.Error);
}

[Test]
public void ProvidingApiCredentials_Should_SaveApiCredentials()
Expand All @@ -115,7 +73,7 @@ public void ProvidingApiCredentials_Should_SaveApiCredentials()
var authProvider = new CoinExAuthenticationProvider(new ApiCredentials("TestKey", "TestSecret"), null);

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

[Test]
Expand All @@ -130,7 +88,7 @@ public void SigningString_Should_GiveCorrectSignResult(string input, string outp
var sign = authProvider.Sign(input);

// assert
Assert.AreEqual(sign, output);
Assert.That(sign == output);
}

[TestCase("BTCUSDT", true)]
Expand Down Expand Up @@ -163,7 +121,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 @@ -183,7 +141,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
36 changes: 18 additions & 18 deletions CoinEx.Net.UnitTests/CoinExSocketClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
// subTask.Wait(5000);

// // Assert
// Assert.IsTrue(subTask.IsCompleted);
// Assert.IsTrue(subTask.Result.Success);
// Assert.That(subTask.IsCompleted);
// Assert.That(subTask.Result.Success);
// }

// [Test]
Expand All @@ -56,7 +56,7 @@
// subTask.Wait();

// // Assert
// Assert.IsFalse(subTask.Result.Success);
// ClassicAssert.IsFalse(subTask.Result.Success);
// }

// [Test]
Expand Down Expand Up @@ -93,8 +93,8 @@
// InvokeSubUpdate(client, "state.update", receive);

// // Assert
// Assert.IsTrue(subTask.Result.Success);
// Assert.IsTrue(actual != null);
// Assert.That(subTask.Result.Success);
// Assert.That(actual != null);
// TestHelpers.PublicInstancePropertiesEqual(expected[0], actual[0]);
// TestHelpers.PublicInstancePropertiesEqual(expected[1], actual[1]);
// }
Expand Down Expand Up @@ -129,8 +129,8 @@
// InvokeSubUpdate(client, "deals.update", "ETHBTC", expected );

// // Assert
// Assert.IsTrue(subTask.Result.Success);
// Assert.IsTrue(actual != null);
// Assert.That(subTask.Result.Success);
// Assert.That(actual != null);
// TestHelpers.PublicInstancePropertiesEqual(expected, actual);
// }

Expand Down Expand Up @@ -163,8 +163,8 @@
// InvokeSubUpdate(client, "deals.update", "ETHBTC", expected);

// // Assert
// Assert.IsTrue(subTask.Result.Success);
// Assert.IsTrue(actual != null);
// Assert.That(subTask.Result.Success);
// Assert.That(actual != null);
// TestHelpers.PublicInstancePropertiesEqual(expected, actual);
// }

Expand Down Expand Up @@ -199,8 +199,8 @@


// // Assert
// Assert.IsTrue(subTask.Result.Success);
// Assert.IsTrue(actual != null);
// Assert.That(subTask.Result.Success);
// Assert.That(actual != null);
// TestHelpers.PublicInstancePropertiesEqual(expected, actual);
// }

Expand Down Expand Up @@ -234,8 +234,8 @@
// InvokeSubUpdate(client, "kline.update", expected);

// // Assert
// Assert.IsTrue(subTask.Result.Success);
// Assert.IsTrue(actual != null);
// Assert.That(subTask.Result.Success);
// Assert.That(actual != null);
// TestHelpers.PublicInstancePropertiesEqual(expected, actual);
// }

Expand Down Expand Up @@ -278,8 +278,8 @@
// InvokeSubUpdate(client, "asset.update", receive);

// // Assert
// Assert.IsTrue(subTask.Result.Success);
// Assert.IsTrue(actual != null);
// Assert.That(subTask.Result.Success);
// Assert.That(actual != null);
// TestHelpers.PublicInstancePropertiesEqual(expected, actual);
// }

Expand Down Expand Up @@ -313,8 +313,8 @@
// InvokeSubUpdate(client, "order.update", 1, expected);

// // Assert
// Assert.IsTrue(subTask.Result.Success);
// Assert.IsTrue(actual != null);
// Assert.That(subTask.Result.Success);
// Assert.That(actual != null);
// TestHelpers.PublicInstancePropertiesEqual(expected, actual);
// }

Expand Down Expand Up @@ -346,7 +346,7 @@
// //TestHelpers.CloseWebsocket(client);

// //// Assert
// //Assert.IsTrue(conWait.Result);
// //Assert.That(conWait.Result);
// }

// [Test]
Expand Down
1 change: 0 additions & 1 deletion CoinEx.Net.UnitTests/JsonTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CoinEx.Net.Interfaces;
using CoinEx.Net.Objects;
using CoinEx.Net.Testing;
using NUnit.Framework;
using System.Collections.Generic;
using System.Threading.Tasks;
Expand Down
17 changes: 10 additions & 7 deletions CoinEx.Net.UnitTests/JsonToObjectComparer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using CoinEx.Net.Testing;
using CryptoExchange.Net.Converters;
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 @@ -84,7 +85,7 @@ public async Task ProcessSubject<K>(
var result = (CallResult)await TestHelpers.InvokeAsync(method, getSubject(client), input.ToArray());

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

var resultProp = result.GetType().GetProperty("Data", BindingFlags.Public | BindingFlags.Instance);
if (resultProp == null)
Expand Down Expand Up @@ -226,10 +227,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 @@ -341,7 +340,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 Down Expand Up @@ -380,7 +381,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
6 changes: 3 additions & 3 deletions CoinEx.Net.UnitTests/TestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
using CoinEx.Net.Clients;
using CoinEx.Net.Objects.Options;

namespace CoinEx.Net.Testing
namespace CoinEx.Net.UnitTests
{
public class TestHelpers
{
Expand Down Expand Up @@ -128,7 +128,7 @@ public static object GetTestValue(Type type, int i)
return (decimal?)(i / 100m);

if (type == typeof(int))
return i+1;
return i + 1;

if (type == typeof(int?))
return (int?)i;
Expand Down Expand Up @@ -165,7 +165,7 @@ public static object GetTestValue(Type type, int i)
return result;
}

if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(List<>)))
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>))
{
var result = (IList)Activator.CreateInstance(type)!;
result.Add(GetTestValue(type.GetGenericArguments()[0], 0));
Expand Down
4 changes: 2 additions & 2 deletions CoinEx.Net/Clients/CoinExRestClient.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using CryptoExchange.Net;
using CoinEx.Net.Interfaces.Clients;
using CoinEx.Net.Interfaces.Clients;
using CoinEx.Net.Interfaces.Clients.SpotApi;
using CoinEx.Net.Clients.SpotApi;
using CryptoExchange.Net.Authentication;
using Microsoft.Extensions.Logging;
using System.Net.Http;
using System;
using CoinEx.Net.Objects.Options;
using CryptoExchange.Net.Clients;

namespace CoinEx.Net.Clients
{
Expand Down
Loading
Loading