Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Költségvetés kezelő app - Székely Norbert (HYKCZK) #14

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
23 changes: 23 additions & 0 deletions HYKCZK/BudgetManager.Tests/BudgetManager.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="Moq" Version="4.17.2" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="17.0.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BudgetManager\BudgetManager.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using BudgetManager.Enum;
using BudgetManager.Model;
using BudgetManager.Provider;
using BudgetManager.Service;
using Moq;
using NUnit.Framework;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace BudgetManager.Tests.Provider.Tests
{
[TestFixture]
internal class ExchangeRateProviderTests
{
private const double HUF_EUR_RATE = 0.03d;
private const double HUF_USD_RATE = 0.037d;

private Mock<IExchangeRateApiService> _exchangeRateApiServiceMock;

private List<ExchangeRate> _exchangeRates;

[SetUp]
public void SetUp()
{
_exchangeRates = new List<ExchangeRate>
{
new (Currency.HUF, Currency.EUR, HUF_EUR_RATE),
new (Currency.HUF, Currency.USD, HUF_USD_RATE)
};

_exchangeRateApiServiceMock = new Mock<IExchangeRateApiService>(MockBehavior.Strict);
_exchangeRateApiServiceMock.Setup(m => m.FetchExchangeRatesAsync())
.Returns(() => Task.FromResult(_exchangeRates));
}

[Test]
[TestCase(Currency.HUF, Currency.EUR, HUF_EUR_RATE)]
[TestCase(Currency.HUF, Currency.USD, HUF_USD_RATE)]
public async Task GetExchangeRate_ExistingRates_ReturnsCorrectRate(Currency sourceCurrency, Currency targetCurrency, double expectedRate)
{
var sut = new ExchangeRateProvider(_exchangeRateApiServiceMock.Object);

var result = await sut.GetExchangeRate(sourceCurrency, targetCurrency);

Assert.That(result, Is.EqualTo(expectedRate));
}

[Test]
public void GetExchangeRate_ApiError_ThrowsInvalidOperationException()
{
_exchangeRateApiServiceMock.Setup(m => m.FetchExchangeRatesAsync())
.Returns(() => Task.FromResult(new List<ExchangeRate>()));

var sut = new ExchangeRateProvider(_exchangeRateApiServiceMock.Object);

async Task Actual()
{
await sut.GetExchangeRate(Currency.HUF, Currency.EUR);
}

Assert.ThrowsAsync<InvalidOperationException>(Actual);
}
}
}
167 changes: 167 additions & 0 deletions HYKCZK/BudgetManager.Tests/Service.Tests/BudgetServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
using BudgetManager.Model;
using BudgetManager.Provider;
using BudgetManager.Service;
using Moq;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.IO.Abstractions.TestingHelpers;
using BudgetManager.Enum;

namespace BudgetManager.Tests.Service.Tests
{
[TestFixture]
internal class BudgetServiceTests
{
private const string BUDGET_JSON_PATH = @"budget.json";
//private Mock<IFileSystem> _fileSystemMock;

private MockFileSystem _fileSystemMock;
private Mock<IConsole> _consoleMock;
private Mock<IExchangeRateProvider> _exchangeRateProviderMock;

private string _budgetJson;
private Budget _budget;
private decimal _totalIncome;
private decimal _totalCost;
private decimal _totalBudget;

[SetUp]
public void SetUp()
{
_budgetJson = @"
{
""incomes"": [
{
""amount"": 500000,
""description"": ""Salary"",
""accountedDateTime"": ""2021-07-14T01:10:16.5275273+02:00""
},
{
""amount"": 600000,
""description"": ""Salary"",
""accountedDateTime"": ""2021-07-14T01:23:30.5031482+02:00""
}
],
""costs"": [
{
""amount"": 1000000,
""description"": ""Kicsi kocsi Suzuki"",
""accountedDateTime"": ""2021-07-14T01:22:59.4319203+02:00""
},
{
""amount"": 10600,
""description"": ""Kaja"",
""accountedDateTime"": ""2021-07-14T22:28:29.8049198+02:00""
}
],
""currency"": ""HUF""
}
";

_budget = new Budget()
{
Incomes = new List<Transaction>
{
new Transaction(500_000M, "Salary", DateTime.Parse("2021-07-14T01:10:16.5275273+02:00")),
new Transaction(600_000M, "Salary", DateTime.Parse("2021-07-14T01:23:30.5031482+02:00"))
},
Costs = new List<Transaction>
{
new Transaction(1_000_000M, "Kicsi kocsi Suzuki", DateTime.Parse("2021-07-14T01:22:59.4319203+02:00")),
new Transaction(10_600M, "Kaja", DateTime.Parse("2021-07-14T22:28:29.8049198+02:00"))
},
Currency = Currency.HUF
};

_totalIncome = 1_100_000M;
_totalCost = 1_010_600M;
_totalBudget = 89_400M;


_fileSystemMock = new MockFileSystem(new Dictionary<string, MockFileData>());

_consoleMock = new Mock<IConsole>(MockBehavior.Strict);
_consoleMock.Setup(console => console.WriteLine(It.IsAny<string>()));

_exchangeRateProviderMock = new Mock<IExchangeRateProvider>(MockBehavior.Strict);
}

[Test]
public void Constructor_NoBudgetJson_ShouldCreateNewBudget()
{
var sut = new BudgetService(_fileSystemMock, _consoleMock.Object, _exchangeRateProviderMock.Object);

_consoleMock.Verify(m => m.WriteLine("Creating new budget."), Times.Once);

Assert.That(_fileSystemMock.File.Exists(BUDGET_JSON_PATH), Is.True);
}

[Test]
public void Constructor_InvalidBudgetJsonFile_ShouldCreateNewBudget()
{
_fileSystemMock.AddFile(BUDGET_JSON_PATH, new MockFileData("SOME INVALID JSON \"..;--"));
var sut = new BudgetService(_fileSystemMock, _consoleMock.Object, _exchangeRateProviderMock.Object);

_consoleMock.Verify(m => m.WriteLine("Creating new budget."), Times.Once);
_consoleMock.Verify(m => m.WriteLine($"[Error]: Could not read {BUDGET_JSON_PATH} file."), Times.Once);

Assert.That(_fileSystemMock.File.Exists(BUDGET_JSON_PATH), Is.True);
}

[Test]
public void Constructor_ValidBudgetJsonFile_ShouldParseSuccessfully()
{
_fileSystemMock.AddFile(BUDGET_JSON_PATH, new MockFileData(_budgetJson));
var sut = new BudgetService(_fileSystemMock, _consoleMock.Object, _exchangeRateProviderMock.Object);

_consoleMock.Verify(m => m.WriteLine($"JSON {BUDGET_JSON_PATH} was read successfully"), Times.Once);

Assert.That(sut.TotalIncome, Is.EqualTo(_totalIncome));
Assert.That(sut.TotalCost, Is.EqualTo(_totalCost));
Assert.That(sut.TotalBudget, Is.EqualTo(_totalBudget));
}

[Test]
public void AddIncome_OneTransaction_ShouldAddIncome()
{
var sut = new BudgetService(_fileSystemMock, _consoleMock.Object, _exchangeRateProviderMock.Object);

sut.AddIncome(new Transaction(1_000_000M, "Salary", DateTime.Now));

Assert.That(sut.TotalIncome, Is.EqualTo(1_000_000M));
}

[Test]
public void AddIncome_MultipleTransaction_ShouldAddIncomes()
{
var sut = new BudgetService(_fileSystemMock, _consoleMock.Object, _exchangeRateProviderMock.Object);

sut.AddIncome(new Transaction(1_000_000M, "Salary", DateTime.Now));
sut.AddIncome(new Transaction(1_000_000M, "Salary", DateTime.Now));

Assert.That(sut.TotalIncome, Is.EqualTo(2_000_000M));
}

[Test]
public void AddCost_OneTransaction_ShouldAddCost()
{
var sut = new BudgetService(_fileSystemMock, _consoleMock.Object, _exchangeRateProviderMock.Object);

sut.AddCost(new Transaction(1_000_000M, "Suzuki", DateTime.Now));

Assert.That(sut.TotalCost, Is.EqualTo(1_000_000M));
}

[Test]
public void AddCost_MultipleTransaction_ShouldAddCosts()
{
var sut = new BudgetService(_fileSystemMock, _consoleMock.Object, _exchangeRateProviderMock.Object);

sut.AddCost(new Transaction(1_000_000M, "Suzuki", DateTime.Now));
sut.AddCost(new Transaction(40_000M, "Party", DateTime.Now));

Assert.That(sut.TotalCost, Is.EqualTo(1_040_000M));
}
}
}
Loading