-
Notifications
You must be signed in to change notification settings - Fork 3
/
IntegrationTests.cs
318 lines (267 loc) · 10.4 KB
/
IntegrationTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using Microsoft.Extensions.Configuration;
using Kuna.Net.Objects.V4;
using Kuna.Net.Objects.V4.Requests;
using Microsoft.Extensions.Logging;
using Xunit;
using System.Diagnostics;
using CryptoExchange.Net.Objects;
using CryptoExchange.Net.Interfaces.CommonClients;
using Newtonsoft.Json;
namespace Kuna.Net.Tests
{
[TestCaseOrderer(
ordererTypeName: "Kuna.Net.Tests.PriorityOrderer",
ordererAssemblyName: "Kuna.Net.Tests")]
public class KunaV4ApiClientIntegrationTests
{
private readonly KunaV4RestApiClient _apiClient;
private readonly KunaSocketStream _socketClient;
private readonly static Guid ordId = Guid.NewGuid();
public KunaV4ApiClientIntegrationTests()
{
var config = new ConfigurationBuilder().AddJsonFile("keys.json", optional: true).Build();
var key = config["key"];
var secret = config["secret"];
var singleKey = config["sinle-api-key"];
KunaApiCredentials? cred = null;
if (!string.IsNullOrEmpty(singleKey))
{
cred = new KunaApiCredentials(singleKey);
}
if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(secret))
{
cred = new KunaApiCredentials(key, secret);
}
var loggerFactory = LoggerFactory.Create(builder =>
{
builder
.SetMinimumLevel(LogLevel.Trace)
.AddProvider(new TraceLoggerProvider());
});
_apiClient = (KunaV4RestApiClient)new KunaRestClient(
new KunaRestOptions()
{
OutputOriginalData = true,
// IsProAccount = true,
ApiCredentials = cred
},
loggerFactory
).ClientV4;
_socketClient = new KunaSocketClient(new KunaSocketClientOptions(), loggerFactory).MainSocketStream;
}
#region auth unnecessary
[Fact]
public async Task GetServerTimeAsyncTest()
{
// Arrange
var today = DateTime.UtcNow.Date;
// Act
var result = await _apiClient.GetServerTimeAsync();
// Assert
Assert.True(result.Success);
// Add more assertions based on the expected behavior of this method
Assert.Equal(today, result.Data.Date);
}
[Fact]
public async Task GetTickersAsyncTest()
{
// Arrange
// Act
var result = await _apiClient.GetTickersAsync();
// Assert
Assert.True(result.Success);
// Add more assertions based on the expected behavior of this method
Assert.IsType<KunaTicker>(result.Data.First());
}
[Fact]
public async Task GetTickersAsyncForBTCUAH_BTCSUDT_Test()
{
// Arrange
var symbol1 = "BTC_UAH";
var symbol2 = "BTC_USDT";
// Act
var result = await _apiClient.GetTickersAsync(default, symbol1, symbol2);
// Assert
Assert.True(result.Success);
// Add more assertions based on the expected behavior of this method
Assert.Contains(result.Data, t => t.Pair == symbol1);
Assert.Contains(result.Data, t => t.Pair == symbol2);
}
[Fact]
public async Task GetCurrenciesAsyncTest()
{
// Arrange
// Act
var result = await _apiClient.GetCurrenciesAsync();
// Assert
Assert.True(result.Success);
// Add more assertions based on the expected behavior of this method
Assert.Contains(result.Data, c => c.Type == CurrencyType.Crypto);
Assert.Contains(result.Data, c => c.Type == CurrencyType.Fiat);
}
[Fact]
public async Task GetCurrenciesAsyncOnlyCryptoTest()
{
// Arrange
// Act
var result = await _apiClient.GetCurrenciesAsync(CurrencyType.Crypto);
// Assert
Assert.True(result.Success);
// Add more assertions based on the expected behavior of this method
Assert.Contains(result.Data, c => c.Type == CurrencyType.Crypto);
Assert.DoesNotContain(result.Data, c => c.Type == CurrencyType.Fiat);
}
[Fact]
public async Task GetRecentPublicTradesAsyncTest()
{
// Arrange
var pair = "BTC_UAH";
var today = DateTime.UtcNow.Date;
// Act
var result = await _apiClient.GetRecentPublicTradesAsync(pair);
// Assert
Assert.True(result.Success);
// Add more assertions based on the expected behavior of this method
Assert.Contains(result.Data, c => c.Pair == pair);
Assert.Contains(result.Data, c => c.CreatedAt.Date == today);
}
[Fact]
public async Task GetTradingPairsAsyncTest()
{
// Arrange
var pair = "BTC_UAH";
// Act
var result = await _apiClient.GetTradingPairsAsync();
// Assert
Assert.True(result.Success);
// Add more assertions based on the expected behavior of this method
Assert.Contains(result.Data, c => c.Pair == pair);
}
[Fact]
public async Task GetOrderBookAsyncTest()
{
// Arrange
var pair = "BTC_UAH";
// Act
var result = await _apiClient.GetOrderBookAsync(pair, OrderBookLevel.Five);
// Assert
Assert.True(result.Success);
// Add more assertions based on the expected behavior of this method
Assert.True(result.Data.Asks.First().Price > result.Data.Bids.First().Price);
}
#endregion auth unnecessary
#region auth required
[Fact]
public async Task SendAsProTest()
{
// Arrange
var pair = "BTC_UAH";
// Act
_apiClient.SetProAccount(true);
var result = await _apiClient.GetOrderBookAsync(pair, OrderBookLevel.Twenty);
_apiClient.SetProAccount(false);
var proHeaderEntry = result.RequestHeaders?.FirstOrDefault(kv => kv.Key == KunaV4RestApiClient.ProParameter.Key);
// Assert
Assert.True(result.Success);
// Add more assertions based on the expected behavior of this method
Assert.Contains(proHeaderEntry.Value.Value, p => p.Equals(KunaV4RestApiClient.ProParameter.Value));
}
[Fact, TestPriority(1)]
public async Task PlaceOrderAsyncTest()
{
// Arrange
var symbol = "BTC_UAH";
var price = 10m;
var amount = 0.1m;
var side = KunaOrderSide.Buy;
var req = PlaceOrderRequest.LimitOrder(symbol, amount, side, price, ordId);
// Act
var result = await _apiClient.PlaceOrderAsync(req);
// Assert
Assert.True(result.Success);
// Add more assertions based on the expected behavior of this method
Assert.Equal(symbol, result.Data.Pair);
Assert.Equal(amount, result.Data.Quantity);
Assert.Equal(price, result.Data.Price);
}
[Fact, TestPriority(2)]
public async Task GetOrderAsyncTest()
{
// Arrange
var id = ordId;
// Act
var result = await _apiClient.GetOrderAsync(id);
// Assert
Assert.True(result.Success);
// Add more assertions based on the expected behavior of this method
Assert.Equal(id, result.Data.Id);
}
[Fact, TestPriority(2)]
public async Task GetOrderTradesAsync()
{
// Act
var result = await _apiClient.GetOrderTradesAsync(ordId);
// Assert
Assert.True(result.Success);
// Add more assertions based on the expected behavior of this method
}
[Fact, TestPriority(3)]
public async Task CancelOrdersAsync()
{
// Arrange
var id = ordId;
// Act
// var result = await _apiClient.CancelOrdersAsync(new[] {id});
var result = await _apiClient.CancelOrdersAsync(new[] {id});
// Assert
Assert.True(result.Success);
// Add more assertions based on the expected behavior of this method
Assert.Contains(result.Data, o => o.OrdId == id);
}
[Fact]
public async Task GetBalancesAsync()
{
// Act
var result = await _apiClient.GetBalancesAsync();
// Assert
Assert.True(result.Success);
// Add more assertions based on the expected behavior of this method
Assert.Contains(result.Data, c => c.Currency == "BTC");
}
[Fact]
public async Task IBaseRestClientGetBalancesAsync()
{
// Act
var result = await ((IBaseRestClient) _apiClient).GetBalancesAsync();
// Assert
Assert.True(result.Success);
// Add more assertions based on the expected behavior of this method
Assert.Contains(result.Data, c => c.Asset == "BTC");
}
[Fact, TestPriority(99)]
public async Task TestPro()
{
// Arrange
var reqNumb = 400;
var expectedSuccessfullyExecutedAmount = reqNumb;
var tasks = new Task<WebCallResult<IEnumerable<KunaUserTrade>>>[reqNumb];
var watch = Stopwatch.StartNew();
// Act
_apiClient.SetProAccount(true);
for (var i = 0; i < reqNumb; i++)
{
tasks[i] = _apiClient.GetOrderTradesAsync(ordId);
Thread.Sleep(10);
}
await Task.WhenAll(tasks);
watch.Stop();
var done = tasks.Where(t => t.Result.Success).Count();
// Assert
// expect that all the responses were successfully completed
Assert.True(done >= expectedSuccessfullyExecutedAmount);
// and that all the responses were sent in a minute (ratelimeter should allow 1200req/min)
Assert.True(watch.ElapsedMilliseconds < 1000*60);
}
#endregion auth required
}
}