Skip to content

Commit

Permalink
Merge pull request #79 from 1RedOne/addTestsForBitcoinClient
Browse files Browse the repository at this point in the history
πŸŽƒ #HackTober - add tests for bitcoin prices πŸŽƒ
  • Loading branch information
aherd2985 committed Oct 10, 2020
2 parents f205be6 + 3030156 commit 3555e28
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 6 deletions.
21 changes: 21 additions & 0 deletions Interfaces/IWebClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
ο»Ώusing System;
using System.Collections.Generic;
using System.Text;

namespace UtilityBelt.Interfaces
{
public interface IWebClient : IDisposable
{
// Required methods (subset of `System.Net.WebClient` methods).
byte[] DownloadData(Uri address);

byte[] UploadData(Uri address, byte[] data);

string DownloadString(string address);
}

public interface IWebClientFactory
{
IWebClient Create();
}
}
30 changes: 30 additions & 0 deletions Models/SystemWebClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
ο»Ώusing System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using UtilityBelt.Interfaces;

namespace UtilityBelt.Models
{
/// <summary>
/// System web client.
/// </summary>
public class SystemWebClient : WebClient, IWebClient
{
}

/// <summary>
/// System web client factory.
/// </summary>
public class SystemWebClientFactory : IWebClientFactory
{
#region IWebClientFactory implementation

public IWebClient Create()
{
return new SystemWebClient();
}

#endregion IWebClientFactory implementation
}
}
77 changes: 77 additions & 0 deletions Tests/UtilityBeltxUnitTests/BitcoinPricesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Net;
using System.Text.Json;
using UtilityBelt;
using UtilityBelt.Interfaces;
using UtilityBelt.Models;
using UtilityBelt.Utilities;
using Xunit;

namespace UtilityBeltxUnitTests
{
public class BitCoinPricesTests
{
private readonly Mock<IWebClient> webClient = new Mock<IWebClient>();

public BitCoinPricesTests()
{
var mockBitCoinResult = getBitCoinPrice();
var jsonresult = JsonSerializer.Serialize(mockBitCoinResult);
this.webClient.Setup(x => x.DownloadString(It.IsAny<string>())).Returns(jsonresult);
}

[Fact]
public void BitCoinPricesReturnsPrice()
{
var bitCoinClient = new TestableBitcoinPrices(webClient.Object);
bitCoinClient.Run();
this.webClient.Verify(x => x.DownloadString(It.IsAny<string>()), Times.Once);
}

[Fact]
public void BitCoinPricesDoesNotThrowsIfNoResult()
{
this.webClient.Setup(x => x.DownloadString(It.IsAny<string>())).Returns("");

var bitCoinClient = new TestableBitcoinPrices(webClient.Object);
bitCoinClient.Run();
this.webClient.Verify(x => x.DownloadString(It.IsAny<string>()), Times.Once);
}

private BitcoinPrice getBitCoinPrice()
{
var mockBitCoinTime = new BitcoinTime();
mockBitCoinTime.Updated = "Just now";

var mockbitCoinBpi = new BitcoinBpi()
{
USD = new Currency()
{
Rate = "1000"
}
};

var mockBitCoinPrice = new BitcoinPrice
{
Bpi = mockbitCoinBpi,
Time = mockBitCoinTime
};

return mockBitCoinPrice;
}

private class TestableBitcoinPrices : BitcoinPrices
{
public IWebClient webClient { get; set; }

public TestableBitcoinPrices(IWebClient webClient) : base()
{
this.webClient = webClient;
}

protected override IWebClient GetWebClient() => this.webClient;
}
}
}
1 change: 1 addition & 0 deletions Tests/UtilityBeltxUnitTests/UtilityBeltxUnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PackageReference Include="AutoFixture.AutoMoq" Version="4.14.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="Moq" Version="4.14.6" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
Expand Down
27 changes: 21 additions & 6 deletions Utilities/BitcoinPrices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
using System;
using System.Collections.Generic;
using System.Composition;
using System.Linq.Expressions;
using System.Net;
using System.Text;
using System.Text.Json;
using UtilityBelt.Interfaces;
using UtilityBelt.Models;

namespace UtilityBelt.Utilities
{
[Export(typeof(IUtility))]
internal class BitcoinPrices : IUtility
public class BitcoinPrices : IUtility
{
public IList<string> Commands => new List<string> { "bitcoin", "bitcoin price" };

Expand All @@ -24,16 +26,29 @@ public void Run()
{
string content = string.Empty;
string bitUrl = "https://api.coindesk.com/v1/bpi/currentprice.json";
using (var wc = new WebClient())
BitcoinPrice bitFact = new BitcoinPrice();
using (var wc = GetWebClient())
{
content = wc.DownloadString(bitUrl);
}
BitcoinPrice bitFact = JsonSerializer.Deserialize<BitcoinPrice>(content);
Console.WriteLine();
try {
bitFact = JsonSerializer.Deserialize<BitcoinPrice>(content);
}
catch{
Console.WriteLine("Got no result or couldn't convert to bitcoin pricing");
}

Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("As Of - " + bitFact.Time.Updated);
Console.WriteLine("USD - $ " + bitFact.Bpi.USD.Rate);
Console.WriteLine(bitFact?.Disclaimer);
Console.WriteLine("As Of - " + bitFact?.Time?.Updated);
Console.WriteLine("USD - $ " + bitFact?.Bpi?.USD?.Rate);
Console.WriteLine();
}

protected virtual IWebClient GetWebClient()
{
var factory = new SystemWebClientFactory();
return factory.Create();
}
}
}

0 comments on commit 3555e28

Please sign in to comment.