Skip to content

Commit

Permalink
Added some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VibeNL committed Dec 4, 2024
1 parent 5589c9d commit b0479e2
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 5 deletions.
29 changes: 29 additions & 0 deletions Database.UnitTests/Database.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="Moq.EntityFrameworkCore" Version="8.0.1.2" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>

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

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

</Project>
148 changes: 148 additions & 0 deletions Database.UnitTests/Repository/CurrencyExchangeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Castle.Core.Logging;
using GhostfolioSidekick.Database;
using GhostfolioSidekick.Database.Repository;
using GhostfolioSidekick.Model;
using GhostfolioSidekick.Model.Market;
using GhostfolioSidekick.Model.Symbols;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Moq;
using Moq.EntityFrameworkCore;
using Xunit;

namespace Database.UnitTests.Repository
{
public class CurrencyExchangeTests
{
private readonly Mock<IDbContextFactory<DatabaseContext>> _dbContextFactoryMock;
private readonly Mock<ILogger<CurrencyExchange>> _loggerMock;
private readonly CurrencyExchange _currencyExchange;

public CurrencyExchangeTests()
{
_dbContextFactoryMock = new Mock<IDbContextFactory<DatabaseContext>>();
_loggerMock = new Mock<ILogger<CurrencyExchange>>();
_currencyExchange = new CurrencyExchange(_dbContextFactoryMock.Object, _loggerMock.Object);
}

[Fact]
public async Task ConvertMoney_SameCurrency_ReturnsSameMoney()
{
// Arrange
var money = new Money(Currency.USD, 100);
var currency = Currency.USD;
var date = DateOnly.FromDateTime(DateTime.Now);

// Act
var result = await _currencyExchange.ConvertMoney(money, currency, date);

// Assert
Assert.Equal(money, result);
}

[Fact]
public async Task ConvertMoney_KnownPair_ReturnsConvertedMoney()
{
// Arrange
var money = new Money(Currency.GBP, 100);
var currency = Currency.GBp;
var date = DateOnly.FromDateTime(DateTime.Now);
var exchangeRate = 100m;

// Act
var result = await _currencyExchange.ConvertMoney(money, currency, date);

// Assert
Assert.Equal(new Money(currency, money.Amount * exchangeRate), result);
}

[Fact]
public async Task ConvertMoney_ExchangeRateOnDate_ReturnsConvertedMoney()
{
// Arrange
var money = new Money(Currency.USD, 100);
var currency = Currency.EUR;
var date = DateOnly.FromDateTime(DateTime.Now);
var exchangeRate = 0.85m;

var dbContextMock = new Mock<DatabaseContext>();
dbContextMock.Setup(x => x.SymbolProfiles)
.ReturnsDbSet(new[]
{
new SymbolProfile
{
Symbol = $"{money.Currency.Symbol}{currency.Symbol}",
MarketData = new[]
{
new MarketData { Date = date, Close = new Money(currency, exchangeRate) }
}
}
});

_dbContextFactoryMock.Setup(x => x.CreateDbContextAsync(It.IsAny<CancellationToken>())).ReturnsAsync(dbContextMock.Object);

// Act
var result = await _currencyExchange.ConvertMoney(money, currency, date);

// Assert
Assert.Equal(new Money(currency, money.Amount * exchangeRate), result);
}

[Fact]
public async Task ConvertMoney_ExchangeRateNotOnDate_UsesPreviousRate()
{
// Arrange
var money = new Money(Currency.USD, 100);
var currency = Currency.EUR;
var date = DateOnly.FromDateTime(DateTime.Now);
var previousDate = date.AddDays(-1);
var exchangeRate = 0.85m;

var dbContextMock = new Mock<DatabaseContext>();
dbContextMock.Setup(x => x.SymbolProfiles)
.ReturnsDbSet(new[]
{
new SymbolProfile
{
Symbol = $"{money.Currency.Symbol}{currency.Symbol}",
MarketData = new[]
{
new MarketData { Date = previousDate, Close = new Money(currency, exchangeRate) }
}
}
});

_dbContextFactoryMock.Setup(x => x.CreateDbContextAsync(It.IsAny<CancellationToken>())).ReturnsAsync(dbContextMock.Object);

// Act
var result = await _currencyExchange.ConvertMoney(money, currency, date);

// Assert
Assert.Equal(new Money(currency, money.Amount * exchangeRate), result);
}

[Fact]
public async Task ConvertMoney_NoExchangeRate_UsesOneToOneRate()
{
// Arrange
var money = new Money(Currency.USD, 100);
var currency = Currency.EUR;
var date = DateOnly.FromDateTime(DateTime.Now);

var dbContextMock = new Mock<DatabaseContext>();
dbContextMock.Setup(x => x.SymbolProfiles)
.ReturnsDbSet(new SymbolProfile[0]);

_dbContextFactoryMock.Setup(x => x.CreateDbContextAsync(It.IsAny<CancellationToken>())).ReturnsAsync(dbContextMock.Object);

// Act
var result = await _currencyExchange.ConvertMoney(money, currency, date);

// Assert
Assert.Equal(new Money(currency, money.Amount), result);
}
}
}
10 changes: 5 additions & 5 deletions Database/DatabaseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ namespace GhostfolioSidekick.Database
{
public class DatabaseContext : DbContext
{
public DbSet<Platform> Platforms { get; set; }
public virtual DbSet<Platform> Platforms { get; set; }

public DbSet<Account> Accounts { get; set; }
public virtual DbSet<Account> Accounts { get; set; }

public DbSet<SymbolProfile> SymbolProfiles { get; set; }
public virtual DbSet<SymbolProfile> SymbolProfiles { get; set; }

public DbSet<Activity> Activities { get; set; }
public virtual DbSet<Activity> Activities { get; set; }

public DbSet<Holding> Holdings { get; set; }
public virtual DbSet<Holding> Holdings { get; set; }

public DatabaseContext()
{
Expand Down
6 changes: 6 additions & 0 deletions GhostfolioSidekick.sln
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YahooFinanceApi", "Librarie
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YahooFinanceApi.Tests", "LibrariesAsCode\YahooFinanceApi.Tests\YahooFinanceApi.Tests.csproj", "{24B59F80-9719-4AC9-861E-6C3426F78AA5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Database.UnitTests", "Database.UnitTests\Database.UnitTests.csproj", "{007DF41A-397A-4E0A-94FD-8C79EE3D2F49}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -139,6 +141,10 @@ Global
{24B59F80-9719-4AC9-861E-6C3426F78AA5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{24B59F80-9719-4AC9-861E-6C3426F78AA5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{24B59F80-9719-4AC9-861E-6C3426F78AA5}.Release|Any CPU.Build.0 = Release|Any CPU
{007DF41A-397A-4E0A-94FD-8C79EE3D2F49}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{007DF41A-397A-4E0A-94FD-8C79EE3D2F49}.Debug|Any CPU.Build.0 = Debug|Any CPU
{007DF41A-397A-4E0A-94FD-8C79EE3D2F49}.Release|Any CPU.ActiveCfg = Release|Any CPU
{007DF41A-397A-4E0A-94FD-8C79EE3D2F49}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit b0479e2

Please sign in to comment.