Skip to content

Commit

Permalink
Increased code coverage - tests for most of the new/restored methods/…
Browse files Browse the repository at this point in the history
…properties were written/updated.
  • Loading branch information
OlegRa committed Sep 16, 2023
1 parent 8d1495a commit d269cb8
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 2 deletions.
27 changes: 27 additions & 0 deletions Alpaca.Markets.Tests/AlpacaCryptoDataClientTest.Other.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Alpaca.Markets.Tests;

public sealed partial class AlpacaCryptoDataClientTest
{
[Fact]
public async Task GetTopMarketMoversAsyncWorks()
{
const Int32 numberOfLosersAndGainers = 5;

using var mock = _mockClientsFactory.GetAlpacaCryptoDataClientMock();

mock.AddGet("/v1beta1/screener/crypto/movers", new JObject(
new JProperty("gainers", new JArray(Enumerable.Repeat(
Crypto.CreateStockMover(), numberOfLosersAndGainers))),
new JProperty("losers", new JArray(Enumerable.Repeat(
Crypto.CreateStockMover(), numberOfLosersAndGainers)))));

var movers = await mock.Client.GetTopMarketMoversAsync(numberOfLosersAndGainers);

Assert.NotNull(movers);
Assert.Equal(numberOfLosersAndGainers, movers.Losers.Count);
Assert.Equal(numberOfLosersAndGainers, movers.Gainers.Count);

movers.Gainers.Validate(Crypto);
movers.Losers.Validate(Crypto);
}
}
81 changes: 81 additions & 0 deletions Alpaca.Markets.Tests/AlpacaDataClientTest.Other.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
namespace Alpaca.Markets.Tests;

public sealed partial class AlpacaDataClientTest
{
private const Decimal Volume = 123_456M;

private const UInt64 TradeCount = 100UL;

[Fact]
public async Task GetTopMarketMoversAsyncWorks()
{
const Int32 numberOfLosersAndGainers = 5;

using var mock = _mockClientsFactory.GetAlpacaDataClientMock();

mock.AddGet("/v1beta1/screener/stocks/movers", new JObject(
new JProperty("gainers", new JArray(Enumerable.Repeat(
Stock.CreateStockMover(), numberOfLosersAndGainers))),
new JProperty("losers", new JArray(Enumerable.Repeat(
Stock.CreateStockMover(), numberOfLosersAndGainers)))));

var movers = await mock.Client.GetTopMarketMoversAsync(numberOfLosersAndGainers);

Assert.NotNull(movers);
Assert.Equal(numberOfLosersAndGainers, movers.Losers.Count);
Assert.Equal(numberOfLosersAndGainers, movers.Gainers.Count);

movers.Gainers.Validate(Stock);
movers.Losers.Validate(Stock);
}

[Fact]
public async Task ListMostActiveStocksByVolumeAsyncWorks()
{
const Int32 numberOfTopMostActiveStocks = 5;

using var mock = _mockClientsFactory.GetAlpacaDataClientMock();

mock.AddGet("/v1beta1/screener/stocks/most-actives", new JObject(
new JProperty("most_actives", new JArray(Enumerable.Repeat(
createActiveStock(), numberOfTopMostActiveStocks)))));

var activeStocksList = await mock.Client.ListMostActiveStocksByVolumeAsync(numberOfTopMostActiveStocks);

Assert.NotNull(activeStocksList);
Assert.Equal(numberOfTopMostActiveStocks, activeStocksList.Count);
Assert.All(activeStocksList, validateActiveStock);
}

[Fact]
public async Task ListMostActiveStocksByTradeCountAsyncWorks()
{
const Int32 numberOfTopMostActiveStocks = 5;

using var mock = _mockClientsFactory.GetAlpacaDataClientMock();

mock.AddGet("/v1beta1/screener/stocks/most-actives", new JObject(
new JProperty("most_actives", new JArray(Enumerable.Repeat(
createActiveStock(), numberOfTopMostActiveStocks)))));

var activeStocksList = await mock.Client.ListMostActiveStocksByTradeCountAsync(numberOfTopMostActiveStocks);

Assert.NotNull(activeStocksList);
Assert.Equal(numberOfTopMostActiveStocks, activeStocksList.Count);
Assert.All(activeStocksList, validateActiveStock);
}

private static JObject createActiveStock() =>
new(
new JProperty("trade_count", TradeCount),
new JProperty("volume", Volume),
new JProperty("symbol", Stock));

private static void validateActiveStock(
IActiveStock activeStock)
{
Assert.Equal(TradeCount, activeStock.TradeCount);
Assert.Equal(Volume, activeStock.Volume);
Assert.Equal(Stock, activeStock.Symbol);
}
}
21 changes: 21 additions & 0 deletions Alpaca.Markets.Tests/HistoricalDataHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public static void Validate<TItem>(
ITrade trade => trade.Validate(symbol),
IQuote quote => quote.Validate(symbol),
IAuction auction => auction.validate(symbol),
IMarketMover mover => mover.validate(symbol),
_ => throw new NotSupportedException(
$"The {typeof(TItem)} not supported yet!")
}));
Expand Down Expand Up @@ -326,6 +327,14 @@ public static JObject CreateCorrection(
new JProperty("z", _tape),
new JProperty("cs", Size),
new JProperty("os", Size));

public static JObject CreateStockMover(
this String symbol) =>
new(
new JProperty("percent_change", MidPrice),
new JProperty("change", AskPrice),
new JProperty("price", BidPrice),
new JProperty("symbol", symbol));

private static Boolean validate(
this IAuction auction,
Expand Down Expand Up @@ -353,6 +362,18 @@ private static Boolean validate(
return true;
}

private static Boolean validate(
this IMarketMover mover,
String symbol)
{
Assert.Equal(MidPrice, mover.PercentChange);
Assert.Equal(AskPrice, mover.Change);
Assert.Equal(BidPrice, mover.Price);
Assert.Equal(symbol, mover.Symbol);

return true;
}

private static JArray createItemsList(
Func<JObject> createItem) => new(createItem(), createItem());

Expand Down
4 changes: 2 additions & 2 deletions Alpaca.Markets/Messages/JsonMarketMovers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
internal sealed class JsonMarketMovers : IMarketMovers
{
[JsonProperty(PropertyName = "losers", Required = Required.Always)]
public List<JsonMarketMover> LosersList { get; set; } = new ();
public List<JsonMarketMover> LosersList { get; [ExcludeFromCodeCoverage] set; } = new ();

[JsonProperty(PropertyName = "gainers", Required = Required.Always)]
public List<JsonMarketMover> GainersList { get; set; } = new ();
public List<JsonMarketMover> GainersList { get; [ExcludeFromCodeCoverage] set; } = new ();

public IReadOnlyList<IMarketMover> Losers =>
LosersList.EmptyIfNull<IMarketMover>();
Expand Down

0 comments on commit d269cb8

Please sign in to comment.