1
0
mirror of https://github.com/JKorf/CryptoExchange.Net.git synced 2026-08-13 17:33:02 +00:00

Initial unit test

This commit is contained in:
Jan Korf
2018-11-28 21:51:39 +01:00
parent 3641293fd6
commit 6a0c7c417b
9 changed files with 595 additions and 192 deletions
@@ -0,0 +1,55 @@
using System.Collections.Generic;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Logging;
using CryptoExchange.Net.Objects;
namespace CryptoExchange.Net.UnitTests
{
public class TestBaseClient: BaseClient
{
public TestBaseClient(): base(new ClientOptions(), null)
{
}
public TestBaseClient(ClientOptions exchangeOptions) : base(exchangeOptions, exchangeOptions.ApiCredentials == null ? null : new TestAuthProvider(exchangeOptions.ApiCredentials))
{
}
public void Log(LogVerbosity verbosity, string data)
{
log.Write(verbosity, data);
}
public CallResult<T> Deserialize<T>(string data)
{
return Deserialize<T>(data, false);
}
public string FillParameters(string path, params string[] values)
{
return FillPathParameter(path, values);
}
}
public class TestAuthProvider : AuthenticationProvider
{
public TestAuthProvider(ApiCredentials credentials) : base(credentials)
{
}
public override Dictionary<string, string> AddAuthenticationToHeaders(string uri, string method, Dictionary<string, object> parameters, bool signed)
{
return base.AddAuthenticationToHeaders(uri, method, parameters, signed);
}
public override Dictionary<string, object> AddAuthenticationToParameters(string uri, string method, Dictionary<string, object> parameters, bool signed)
{
return base.AddAuthenticationToParameters(uri, method, parameters, signed);
}
public override string Sign(string toSign)
{
return toSign;
}
}
}
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text;
namespace CryptoExchange.Net.UnitTests.TestImplementations
{
public class TestHelpers
{
[ExcludeFromCodeCoverage]
public static bool AreEqual<T>(T self, T to, params string[] ignore) where T : class
{
if (self != null && to != null)
{
var type = self.GetType();
var ignoreList = new List<string>(ignore);
foreach (var pi in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (ignoreList.Contains(pi.Name))
{
continue;
}
var selfValue = type.GetProperty(pi.Name).GetValue(self, null);
var toValue = type.GetProperty(pi.Name).GetValue(to, null);
if (pi.PropertyType.IsClass && !pi.PropertyType.Module.ScopeName.Equals("System.Private.CoreLib.dll"))
{
// Check of "CommonLanguageRuntimeLibrary" is needed because string is also a class
if (AreEqual(selfValue, toValue, ignore))
{
continue;
}
return false;
}
if (selfValue != toValue && (selfValue == null || !selfValue.Equals(toValue)))
{
return false;
}
}
return true;
}
return self == to;
}
}
}
@@ -0,0 +1,15 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
namespace CryptoExchange.Net.UnitTests.TestImplementations
{
public class TestObject
{
[JsonProperty("other")]
public string StringData { get; set; }
public int IntData { get; set; }
public decimal DecimalData { get; set; }
}
}
@@ -0,0 +1,115 @@
using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.Objects;
using Moq;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace CryptoExchange.Net.UnitTests.TestImplementations
{
public class TestRestClient: RestClient
{
public TestRestClient() : base(new ClientOptions(), null)
{
RequestFactory = new Mock<IRequestFactory>().Object;
}
public TestRestClient(ClientOptions exchangeOptions) : base(exchangeOptions, exchangeOptions.ApiCredentials == null ? null : new TestAuthProvider(exchangeOptions.ApiCredentials))
{
}
public void SetResponse(string responseData)
{
var expectedBytes = Encoding.UTF8.GetBytes(responseData);
var responseStream = new MemoryStream();
responseStream.Write(expectedBytes, 0, expectedBytes.Length);
responseStream.Seek(0, SeekOrigin.Begin);
var response = new Mock<IResponse>();
response.Setup(c => c.GetResponseStream()).Returns(responseStream);
var request = new Mock<IRequest>();
request.Setup(c => c.Headers).Returns(new WebHeaderCollection());
request.Setup(c => c.Uri).Returns(new Uri("http://www.test.com"));
request.Setup(c => c.GetResponse()).Returns(Task.FromResult(response.Object));
var factory = Mock.Get(RequestFactory);
factory.Setup(c => c.Create(It.IsAny<string>()))
.Returns(request.Object);
}
public void SetErrorWithoutResponse(HttpStatusCode code, string message)
{
var we = new WebException();
var r = new HttpWebResponse();
var re = new HttpResponseMessage();
typeof(HttpResponseMessage).GetField("_statusCode", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).SetValue(re, code);
typeof(HttpWebResponse).GetField("_httpResponseMessage", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).SetValue(r, re);
typeof(WebException).GetField("_message", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).SetValue(we, message);
typeof(WebException).GetField("_response", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).SetValue(we, r);
var response = new Mock<IResponse>();
response.Setup(c => c.GetResponseStream()).Throws(we);
var request = new Mock<IRequest>();
request.Setup(c => c.Headers).Returns(new WebHeaderCollection());
request.Setup(c => c.GetResponse()).Returns(Task.FromResult(response.Object));
var factory = Mock.Get(RequestFactory);
factory.Setup(c => c.Create(It.IsAny<string>()))
.Returns(request.Object);
}
public void SetErrorWithResponse(string responseData, HttpStatusCode code)
{
var expectedBytes = Encoding.UTF8.GetBytes(responseData);
var responseStream = new MemoryStream();
responseStream.Write(expectedBytes, 0, expectedBytes.Length);
responseStream.Seek(0, SeekOrigin.Begin);
var we = new WebException();
var r = new HttpWebResponse();
var re = new HttpResponseMessage();
typeof(HttpResponseMessage).GetField("_statusCode", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).SetValue(re, code);
typeof(HttpWebResponse).GetField("_httpResponseMessage", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).SetValue(r, re);
typeof(WebException).GetField("_message", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).SetValue(we, "");
typeof(WebException).GetField("_response", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).SetValue(we, r);
var response = new Mock<IResponse>();
response.Setup(c => c.GetResponseStream()).Returns(responseStream);
var request = new Mock<IRequest>();
request.Setup(c => c.Headers).Returns(new WebHeaderCollection());
request.Setup(c => c.GetResponse()).Returns(Task.FromResult(response.Object));
var factory = Mock.Get(RequestFactory);
factory.Setup(c => c.Create(It.IsAny<string>()))
.Returns(request.Object);
}
public async Task<CallResult<T>> Request<T>() where T:class
{
return await ExecuteRequest<T>(new Uri("http://www.test.com"));
}
}
public class ParseErrorTestRestClient: TestRestClient
{
public ParseErrorTestRestClient() { }
public ParseErrorTestRestClient(ClientOptions exchangeOptions) : base(exchangeOptions) { }
protected override Error ParseErrorResponse(string error)
{
var data = JToken.Parse(error);
return new ServerError((int)data["errorCode"], (string)data["errorMessage"]);
}
}
}