Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Runtime.InteropServices;
using ExchangeRateUpdater.Common.Extensions;

namespace ExchangeRate.Tests.Common.Extensions
{
public class CzechTimeZoneExtensionsTest
{
[Fact]
public void GetCzechTimeZone_ReturnsCorrectTimeZone()
{
// Act
var tz = CzechTimeZoneExtensions.GetCzechTimeZone();

// Assert
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
Assert.Equal("Central Europe Standard Time", tz.Id);
else
Assert.Equal("Europe/Prague", tz.Id);
}

[Fact]
public void GetNextCzechBankUpdateUtc_ReturnsFutureTime()
{
// Act
var nextUpdate = CzechTimeZoneExtensions.GetNextCzechBankUpdateUtc();

// Assert
Assert.True(nextUpdate > DateTimeOffset.UtcNow);
// Should be at 14:30 Czech time
var czechTz = CzechTimeZoneExtensions.GetCzechTimeZone();
var local = TimeZoneInfo.ConvertTime(nextUpdate, czechTz);
Assert.Equal(14, local.Hour);
Assert.Equal(30, local.Minute);
}

[Fact]
public void GetNextMonthUpdateUtc_ReturnsFirstDayOfNextMonthAtMidnight()
{
// Act
var nextMonth = CzechTimeZoneExtensions.GetNextMonthUpdateUtc();

// Assert
Assert.True(nextMonth > DateTimeOffset.UtcNow);

var czechTz = CzechTimeZoneExtensions.GetCzechTimeZone();
var local = TimeZoneInfo.ConvertTime(nextMonth, czechTz);

Assert.Equal(0, local.Hour);
Assert.Equal(0, local.Minute);
Assert.Equal(1, local.Day);
}


[Fact]
public void GetNextMonthUpdateUtc_ForDecember_SetsToJanuaryNextYear()
{
// Arrange: Simulate a date in December
var decemberDate = new DateTimeOffset(DateTime.UtcNow.Year, 12, 15, 10, 0, 0, TimeSpan.Zero);

// Act: Call the logic as if today is December
var nextMonth = CzechTimeZoneExtensions.GetNextMonthUpdateUtc(decemberDate);

// Assert: Should be January 1st of next year at midnight Czech time
var czechTz = CzechTimeZoneExtensions.GetCzechTimeZone();
var local = TimeZoneInfo.ConvertTime(nextMonth, czechTz);

Assert.Equal(1, local.Day);
Assert.Equal(1, local.Month);
Assert.Equal(decemberDate.Year + 1, local.Year);
Assert.Equal(0, local.Hour);
Assert.Equal(0, local.Minute);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
namespace ExchangeRate.Tests.CommonTest.Extensions
{
public class DateTimeExtensionsTests
{
[Fact]
public void GetPreviousYearMonthUtc_ReturnsPreviousMonth_ForNonJanuary()
{
// Arrange
var testDate = new DateTime(2024, 5, 15);
var expected = "2024-04";
var originalNow = DateTime.UtcNow;

// Act
var result = "";
System.Func<DateTime> originalUtcNow = () => DateTime.UtcNow;
try
{

var prevMonth = testDate.Month == 1
? new DateTime(testDate.Year - 1, 12, 1)
: new DateTime(testDate.Year, testDate.Month - 1, 1);
result = prevMonth.ToString("yyyy-MM");
}
finally
{

}

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

[Fact]
public void GetPreviousYearMonthUtc_ReturnsDecemberOfPreviousYear_ForJanuary()
{
// Arrange
var testDate = new DateTime(2024, 1, 10);
var expected = "2023-12";

// Act
var prevMonth = testDate.Month == 1
? new DateTime(testDate.Year - 1, 12, 1)
: new DateTime(testDate.Year, testDate.Month - 1, 1);
var result = prevMonth.ToString("yyyy-MM");

// Assert
Assert.Equal(expected, result);
}
}
}
34 changes: 34 additions & 0 deletions jobs/Backend/ExchangeRate.Tests/ExchangeRate.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.5" />
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>

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

<ItemGroup>
<ProjectReference Include="..\Task\ExchangeRateUpdater.csproj" />
</ItemGroup>

</Project>
Loading