Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃巸 #HackTober - add tests for bitcoin prices 馃巸 #79

Merged
merged 5 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Interfaces/IWebClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
锘縰sing 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 @@
锘縰sing 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();
}
}
}