Skip to content

Commit f7707d8

Browse files
committed
Release 1.1.0
* Bug fix for GetQuoteAsync missing symbol * Added unit tests * Renamed and improved data model structure * Added response status for all methods * Improved null checks on all methods
1 parent 0bace8a commit f7707d8

24 files changed

+710
-133
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using FluentAssertions;
5+
using RichardSzalay.MockHttp;
6+
using TwelveDataSharp.Library.ResponseModels;
7+
using Xunit;
8+
9+
namespace TwelveDataSharp.UnitTests
10+
{
11+
public class GetAdxValuesAsync_Tests
12+
{
13+
[Fact]
14+
public async void GetAdxValuesAsync_Success_Test()
15+
{
16+
// Arrange
17+
var mockHttp = new MockHttpMessageHandler();
18+
mockHttp
19+
.When("https://api.twelvedata.com/*")
20+
.Respond("application/json", "{\"meta\":{\"symbol\":\"AAPL\",\"interval\":\"1min\",\"currency\":\"USD\",\"exchange_timezone\":\"America/New_York\",\"exchange\":\"NASDAQ\",\"type\":\"Common Stock\",\"indicator\":{\"name\":\"ADX - Average Directional Index\",\"time_period\":14}},\"values\":[{\"datetime\":\"2021-03-01 15:59:00\",\"adx\":\"15.69773\"}],\"status\":\"ok\"}");
21+
22+
TwelveDataClient twelveDataClient = new TwelveDataClient("TEST", mockHttp.ToHttpClient());
23+
24+
// Act
25+
var response = await twelveDataClient.GetAdxValuesAsync("TEST");
26+
27+
// Assert
28+
response?.ResponseStatus.Should().Be(Enums.TwelveDataClientResponseStatus.Ok);
29+
response?.ResponseMessage.Should().Be("RESPONSE_OK");
30+
response?.Symbol.Should().Be("AAPL");
31+
response?.Interval.Should().Be("1min");
32+
response?.Currency.Should().Be("USD");
33+
response?.ExchangeTimezone.Should().Be("America/New_York");
34+
response?.Exchange.Should().Be("NASDAQ");
35+
response?.Type.Should().Be("Common Stock");
36+
response?.IndicatorName.Should().Be("ADX - Average Directional Index");
37+
response?.TimePeriod.Should().Be(14);
38+
response?.Values[0]?.AdxValue.Should().Be(15.69773);
39+
response?.Values[0]?.Datetime.Should().Be(new DateTime(2021, 3, 1, 15, 59, 0));
40+
}
41+
42+
[Fact]
43+
public async void GetAdxValuesAsync_BadApiKey_Test()
44+
{
45+
// Arrange
46+
var mockHttp = new MockHttpMessageHandler();
47+
mockHttp
48+
.When("https://api.twelvedata.com/*")
49+
.Respond("application/json",
50+
"{\"code\":401,\"message\":\"**apikey** parameter is incorrect or not specified. You can get your free API Key instantly following this link: https://twelvedata.com/apikey. If you believe that everything is correct, you can email us at apikey@twelvedata.com\",\"status\":\"error\"}");
51+
52+
TwelveDataClient twelveDataClient = new TwelveDataClient("TEST", mockHttp.ToHttpClient());
53+
54+
// Act
55+
var response = await twelveDataClient.GetRealTimePriceAsync("TEST");
56+
57+
// Assert
58+
response?.ResponseStatus.Should().Be(Enums.TwelveDataClientResponseStatus.TwelveDataApiError);
59+
}
60+
61+
[Fact]
62+
public async void GetAdxValuesAsync_InvalidSymbol_Test()
63+
{
64+
// Arrange
65+
var mockHttp = new MockHttpMessageHandler();
66+
mockHttp
67+
.When("https://api.twelvedata.com/*")
68+
.Respond("application/json",
69+
"{\"code\":400,\"message\":\"**symbol** not found: FAKE. Please specify it correctly according to API Documentation.\",\"status\":\"error\",\"meta\":{\"symbol\":\"FAKE\",\"interval\":\"\",\"exchange\":\"\"}}");
70+
71+
TwelveDataClient twelveDataClient = new TwelveDataClient("TEST", mockHttp.ToHttpClient());
72+
73+
// Act
74+
var response = await twelveDataClient.GetAdxValuesAsync("TEST");
75+
76+
// Assert
77+
response?.ResponseStatus.Should().Be(Enums.TwelveDataClientResponseStatus.TwelveDataApiError);
78+
}
79+
80+
[Fact]
81+
public async void GetAdxValuesAsync_NullHttpClient_Test()
82+
{
83+
// Arrange
84+
TwelveDataClient twelveDataClient = new TwelveDataClient("TEST", null);
85+
86+
// Act
87+
var response = await twelveDataClient.GetAdxValuesAsync("TEST");
88+
89+
// Assert
90+
response?.ResponseStatus.Should().Be(Enums.TwelveDataClientResponseStatus.TwelveDataSharpError);
91+
}
92+
}
93+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using FluentAssertions;
5+
using RichardSzalay.MockHttp;
6+
using TwelveDataSharp.Library.ResponseModels;
7+
using Xunit;
8+
9+
namespace TwelveDataSharp.UnitTests
10+
{
11+
public class GetBollingerBandsAsync_Tests
12+
{
13+
[Fact]
14+
public async void GetBollingerBandsAsync_Success_Test()
15+
{
16+
// Arrange
17+
var mockHttp = new MockHttpMessageHandler();
18+
mockHttp
19+
.When("https://api.twelvedata.com/*")
20+
.Respond("application/json", "{\"meta\":{\"symbol\":\"AAPL\",\"interval\":\"1min\",\"currency\":\"USD\",\"exchange_timezone\":\"America/New_York\",\"exchange\":\"NASDAQ\",\"type\":\"Common Stock\",\"indicator\":{\"name\":\"BBANDS - Bollinger Bands®\",\"series_type\":\"close\",\"time_period\":20,\"sd\":2,\"ma_type\":\"SMA\"}},\"values\":[{\"datetime\":\"2021-03-01 15:59:00\",\"upper_band\":\"127.80482\",\"middle_band\":\"127.55686\",\"lower_band\":\"127.30890\"}],\"status\":\"ok\"}");
21+
22+
TwelveDataClient twelveDataClient = new TwelveDataClient("TEST", mockHttp.ToHttpClient());
23+
24+
// Act
25+
var response = await twelveDataClient.GetBollingerBandsAsync("TEST");
26+
27+
// Assert
28+
response?.ResponseStatus.Should().Be(Enums.TwelveDataClientResponseStatus.Ok);
29+
response?.ResponseMessage.Should().Be("RESPONSE_OK");
30+
response?.Symbol?.Should().Be("AAPL");
31+
response?.Interval.Should().Be("1min");
32+
response?.Currency.Should().Be("USD");
33+
response?.ExchangeTimezone.Should().Be("America/New_York");
34+
response?.Exchange.Should().Be("NASDAQ");
35+
response?.Type.Should().Be("Common Stock");
36+
response?.MovingAverageType.Should().Be("SMA");
37+
response?.IndicatorName.Should().Be("BBANDS - Bollinger Bands®");
38+
response?.SeriesType.Should().Be("close");
39+
response?.TimePeriod.Should().Be(20);
40+
response?.StandardDeviation.Should().Be(2);
41+
response?.Values[0]?.Datetime.Should().Be(new DateTime(2021, 3, 1, 15, 59, 0));
42+
response?.Values[0]?.LowerBand.Should().Be(127.30890);
43+
response?.Values[0]?.MiddleBand.Should().Be(127.55686);
44+
response?.Values[0].UpperBand.Should().Be(127.80482);
45+
}
46+
47+
[Fact]
48+
public async void GetBollingerBandsAsync_BadApiKey_Test()
49+
{
50+
// Arrange
51+
var mockHttp = new MockHttpMessageHandler();
52+
mockHttp
53+
.When("https://api.twelvedata.com/*")
54+
.Respond("application/json",
55+
"{\"code\":401,\"message\":\"**apikey** parameter is incorrect or not specified. You can get your free API Key instantly following this link: https://twelvedata.com/apikey. If you believe that everything is correct, you can email us at apikey@twelvedata.com\",\"status\":\"error\"}");
56+
57+
TwelveDataClient twelveDataClient = new TwelveDataClient("TEST", mockHttp.ToHttpClient());
58+
59+
// Act
60+
var response = await twelveDataClient.GetBollingerBandsAsync("TEST");
61+
62+
// Assert
63+
response?.ResponseStatus.Should().Be(Enums.TwelveDataClientResponseStatus.TwelveDataApiError);
64+
}
65+
66+
[Fact]
67+
public async void GetBollingerBandsAsync_InvalidSymbol_Test()
68+
{
69+
// Arrange
70+
var mockHttp = new MockHttpMessageHandler();
71+
mockHttp
72+
.When("https://api.twelvedata.com/*")
73+
.Respond("application/json",
74+
"{\"code\":400,\"message\":\"**symbol** not found: FAKE. Please specify it correctly according to API Documentation.\",\"status\":\"error\",\"meta\":{\"symbol\":\"FAKE\",\"interval\":\"\",\"exchange\":\"\"}}");
75+
76+
TwelveDataClient twelveDataClient = new TwelveDataClient("TEST", mockHttp.ToHttpClient());
77+
78+
// Act
79+
var response = await twelveDataClient.GetBollingerBandsAsync("TEST");
80+
81+
// Assert
82+
response?.ResponseStatus.Should().Be(Enums.TwelveDataClientResponseStatus.TwelveDataApiError);
83+
}
84+
85+
[Fact]
86+
public async void GetBollingerBandsAsync_NullHttpClient_Test()
87+
{
88+
// Arrange
89+
TwelveDataClient twelveDataClient = new TwelveDataClient("TEST", null);
90+
91+
// Act
92+
var response = await twelveDataClient.GetBollingerBandsAsync("TEST");
93+
94+
// Assert
95+
response?.ResponseStatus.Should().Be(Enums.TwelveDataClientResponseStatus.TwelveDataSharpError);
96+
}
97+
}
98+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using FluentAssertions;
5+
using RichardSzalay.MockHttp;
6+
using TwelveDataSharp.Library.ResponseModels;
7+
using Xunit;
8+
9+
namespace TwelveDataSharp.UnitTests
10+
{
11+
public class GetQuoteAsync_Tests
12+
{
13+
[Fact]
14+
public async void GetQuote_Success_Test()
15+
{
16+
// Arrange
17+
var mockHttp = new MockHttpMessageHandler();
18+
mockHttp
19+
.When("https://api.twelvedata.com/*")
20+
.Respond("application/json",
21+
"{\"symbol\":\"AAPL\",\"name\":\"Apple Inc\",\"exchange\":\"NASDAQ\",\"currency\":\"USD\",\"datetime\":\"2021-03-01\",\"open\":\"123.75000\",\"high\":\"127.93000\",\"low\":\"122.79000\",\"close\":\"127.79000\",\"volume\":\"116307692\",\"previous_close\":\"121.26000\",\"change\":\"6.53000\",\"percent_change\":\"5.38512\",\"average_volume\":\"115516862\",\"fifty_two_week\":{\"low\":\"53.15250\",\"high\":\"145.08000\",\"low_change\":\"74.63750\",\"high_change\":\"-17.29000\",\"low_change_percent\":\"140.42143\",\"high_change_percent\":\"-11.91756\",\"range\":\"53.152500 - 145.080002\"}}");
22+
23+
TwelveDataClient twelveDataClient = new TwelveDataClient("TEST", mockHttp.ToHttpClient());
24+
25+
// Act
26+
var response = await twelveDataClient.GetQuoteAsync("TEST");
27+
28+
// Assert
29+
response?.ResponseStatus.Should().Be(Enums.TwelveDataClientResponseStatus.Ok);
30+
response?.ResponseMessage.Should().Be("RESPONSE_OK");
31+
response?.Symbol.Should().Be("AAPL");
32+
response?.Open.Should().Be(123.75000);
33+
response?.Datetime.Should().Be(new DateTime(2021, 3, 1));
34+
response?.Exchange.Should().Be("NASDAQ");
35+
response?.Currency.Should().Be("USD");
36+
response?.Datetime.Should().Be(new DateTime(2021, 3, 1));
37+
response?.Open.Should().Be(123.75000);
38+
response?.High.Should().Be(127.93000);
39+
response?.Low.Should().Be(122.79000);
40+
response?.Close.Should().Be(127.79000);
41+
response?.PreviousClose.Should().Be(121.26000);
42+
response?.Change.Should().Be(6.53000);
43+
response?.Volume.Should().Be(116307692);
44+
response?.Change.Should().Be(6.53000);
45+
response?.PercentChange.Should().Be(5.38512);
46+
response?.AverageVolume.Should().Be(115516862);
47+
response?.FiftyTwoWeekHigh.Should().Be(145.08000);
48+
response?.FiftyTwoWeekLow.Should().Be(53.15250);
49+
response?.FiftyTwoWeekHighChange.Should().Be(-17.29000);
50+
response?.FiftyTwoWeekLowChange.Should().Be(74.63750);
51+
response?.FiftyTwoWeekHighChangePercent.Should().Be(-11.91756);
52+
response?.FiftyTwoWeekLowChangePercent.Should().Be(140.42143);
53+
response?.FiftyTwoWeekRange.Should().Be("53.152500 - 145.080002");
54+
}
55+
56+
[Fact]
57+
public async void GetQuote_InvalidSymbol_Test()
58+
{
59+
// Arrange
60+
var mockHttp = new MockHttpMessageHandler();
61+
mockHttp
62+
.When("https://api.twelvedata.com/*")
63+
.Respond("application/json",
64+
"{\"code\":400,\"message\":\"**symbol** not found: FAKE. Please specify it correctly according to API Documentation.\",\"status\":\"error\",\"meta\":{\"symbol\":\"FAKE\",\"interval\":\"\",\"exchange\":\"\"}}");
65+
66+
TwelveDataClient twelveDataClient = new TwelveDataClient("TEST", mockHttp.ToHttpClient());
67+
68+
// Act
69+
var response = await twelveDataClient.GetQuoteAsync("TEST");
70+
71+
// Assert
72+
response?.ResponseStatus.Should().Be(Enums.TwelveDataClientResponseStatus.TwelveDataApiError);
73+
}
74+
75+
[Fact]
76+
public async void GetQuote_BadApiKey_Test()
77+
{
78+
// Arrange
79+
var mockHttp = new MockHttpMessageHandler();
80+
mockHttp
81+
.When("https://api.twelvedata.com/*")
82+
.Respond("application/json",
83+
"{\"code\":401,\"message\":\"**apikey** parameter is incorrect or not specified. You can get your free API Key instantly following this link: https://twelvedata.com/apikey. If you believe that everything is correct, you can email us at apikey@twelvedata.com\",\"status\":\"error\"}");
84+
85+
TwelveDataClient twelveDataClient = new TwelveDataClient("TEST", mockHttp.ToHttpClient());
86+
87+
// Act
88+
var response = await twelveDataClient.GetRealTimePriceAsync("TEST");
89+
90+
// Assert
91+
response?.ResponseStatus.Should().Be(Enums.TwelveDataClientResponseStatus.TwelveDataApiError);
92+
}
93+
94+
[Fact]
95+
public async void GetQuote_NullHttpClient_Test()
96+
{
97+
// Arrange
98+
TwelveDataClient twelveDataClient = new TwelveDataClient("TEST", null);
99+
100+
// Act
101+
var response = await twelveDataClient.GetQuoteAsync("TEST");
102+
103+
// Assert
104+
response?.ResponseStatus.Should().Be(Enums.TwelveDataClientResponseStatus.TwelveDataSharpError);
105+
}
106+
}
107+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System;
2+
using FluentAssertions;
3+
using Moq;
4+
using RichardSzalay.MockHttp;
5+
using TwelveDataSharp.Library.ResponseModels;
6+
using Xunit;
7+
8+
namespace TwelveDataSharp.UnitTests
9+
{
10+
public class GetRealTimePriceAsync_Tests
11+
{
12+
[Fact]
13+
public async void GetRealTimePrice_Success_Test()
14+
{
15+
// Arrange
16+
var mockHttp = new MockHttpMessageHandler();
17+
mockHttp
18+
.When("https://api.twelvedata.com/*")
19+
.Respond("application/json", "{\"price\":\"127.79000\"}");
20+
21+
TwelveDataClient twelveDataClient = new TwelveDataClient("TEST", mockHttp.ToHttpClient());
22+
23+
// Act
24+
var response = await twelveDataClient.GetRealTimePriceAsync("TEST");
25+
26+
// Assert
27+
response?.ResponseStatus.Should().Be(Enums.TwelveDataClientResponseStatus.Ok);
28+
response?.ResponseMessage.Should().Be("RESPONSE_OK");
29+
response?.Price.Should().Be(127.7900);
30+
}
31+
32+
[Fact]
33+
public async void GetRealTimePrice_BadApiKey_Test()
34+
{
35+
// Arrange
36+
var mockHttp = new MockHttpMessageHandler();
37+
mockHttp
38+
.When("https://api.twelvedata.com/*")
39+
.Respond("application/json",
40+
"{\"code\":401,\"message\":\"**apikey** parameter is incorrect or not specified. You can get your free API Key instantly following this link: https://twelvedata.com/apikey. If you believe that everything is correct, you can email us at apikey@twelvedata.com\",\"status\":\"error\"}");
41+
42+
TwelveDataClient twelveDataClient = new TwelveDataClient("TEST", mockHttp.ToHttpClient());
43+
44+
// Act
45+
var response = await twelveDataClient.GetRealTimePriceAsync("TEST");
46+
47+
// Assert
48+
response?.ResponseStatus.Should().Be(Enums.TwelveDataClientResponseStatus.TwelveDataApiError);
49+
}
50+
51+
[Fact]
52+
public async void GetRealTimePrice_InvalidSymbol_Test()
53+
{
54+
// Arrange
55+
var mockHttp = new MockHttpMessageHandler();
56+
mockHttp
57+
.When("https://api.twelvedata.com/*")
58+
.Respond("application/json",
59+
"{\"code\":400,\"message\":\"**symbol** not found: FAKE. Please specify it correctly according to API Documentation.\",\"status\":\"error\",\"meta\":{\"symbol\":\"FAKE\",\"interval\":\"\",\"exchange\":\"\"}}");
60+
61+
TwelveDataClient twelveDataClient = new TwelveDataClient("TEST", mockHttp.ToHttpClient());
62+
63+
// Act
64+
var response = await twelveDataClient.GetRealTimePriceAsync("TEST");
65+
66+
// Assert
67+
response?.ResponseStatus.Should().Be(Enums.TwelveDataClientResponseStatus.TwelveDataApiError);
68+
}
69+
70+
[Fact]
71+
public async void GetRealTimePrice_NullHttpClient_Test()
72+
{
73+
// Arrange
74+
TwelveDataClient twelveDataClient = new TwelveDataClient("TEST", null);
75+
76+
// Act
77+
var response = await twelveDataClient.GetRealTimePriceAsync("TEST");
78+
79+
// Assert
80+
response?.ResponseStatus.Should().Be(Enums.TwelveDataClientResponseStatus.TwelveDataSharpError);
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)