Skip to content

Commit

Permalink
Updated tests to use 'real' age calculation.
Browse files Browse the repository at this point in the history
Updated timeprovider to use 0101 as default date.

Signed-off-by: Johannes Tegnér <[email protected]>
  • Loading branch information
Johannestegner committed Jan 9, 2025
1 parent dddc78f commit 165f249
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
7 changes: 6 additions & 1 deletion Personnummer.Tests/CoordinationNumberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,17 @@ public void TestParseInvalidCn(PersonnummerData ssn)
public void TestAgeCn(PersonnummerData ssn)
{
var timeProvider = new TestTimeProvider();
var localNow = timeProvider.GetLocalNow();

int day = int.Parse(ssn.LongFormat.Substring(ssn.LongFormat.Length - 6, 2)) - 60;
string strDay = day < 10 ? $"0{day}" : day.ToString();

DateTime dt = DateTime.ParseExact(ssn.LongFormat.Substring(0, ssn.LongFormat.Length - 6) + strDay, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None);
int years = timeProvider.GetLocalNow().Year - dt.Year;
var years = localNow.Year - dt.Year;
if (!(localNow.Month > dt.Month || (localNow.Month == dt.Month && localNow.Day >= dt.Day)))
{
years--;
}

Assert.Equal(years, Personnummer.Parse(ssn.SeparatedLong, new Personnummer.Options { AllowCoordinationNumber = true, TimeProvider = timeProvider }).Age);
Assert.Equal(years, Personnummer.Parse(ssn.SeparatedFormat, new Personnummer.Options { AllowCoordinationNumber = true, TimeProvider = timeProvider }).Age);
Expand Down
32 changes: 21 additions & 11 deletions Personnummer.Tests/PersonnummerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,35 @@ public void TestParseInvalid(PersonnummerData ssn)
public void TestAge(PersonnummerData ssn)
{
var timeProvider = new TestTimeProvider();
var localNow = timeProvider.GetLocalNow();
DateTime dt = DateTime.ParseExact(ssn.LongFormat.Substring(0, ssn.LongFormat.Length - 4), "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None);
int years = timeProvider.GetLocalNow().Year - dt.Year;

var years = localNow.Year - dt.Year;
if (!(localNow.Month > dt.Month || (localNow.Month == dt.Month && localNow.Day >= dt.Day)))
{
years--;
}

Assert.Equal(years, Personnummer.Parse(ssn.SeparatedLong, new Personnummer.Options { AllowCoordinationNumber = false, TimeProvider = timeProvider }).Age);
Assert.Equal(years, Personnummer.Parse(ssn.SeparatedFormat, new Personnummer.Options { AllowCoordinationNumber = false, TimeProvider = timeProvider }).Age);
Assert.Equal(years, Personnummer.Parse(ssn.LongFormat, new Personnummer.Options { AllowCoordinationNumber = false, TimeProvider = timeProvider }).Age);
// Du to age not being possible to fetch from >100 year short format without separator, we aught to check this here.
Assert.Equal(years > 99 ? years - 100 : years, Personnummer.Parse(ssn.ShortFormat, new Personnummer.Options { AllowCoordinationNumber = false, TimeProvider = timeProvider }).Age);
}

[Fact]
public void TestEdgeCasesAroundBirthday()
{
var timeProvider = new TestTimeProvider
{
Now = DateTimeOffset.Parse("2090-01-09 12:00")
};

Assert.Equal(10, new Personnummer("20800108-6670", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Had birthday yesterday
Assert.Equal(10, new Personnummer("20800109-8287", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Birthday today
Assert.Equal(9, new Personnummer("20800110-8516", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Upcoming Birthday tomorrow
}

#endif

[Theory]
Expand Down Expand Up @@ -236,15 +256,5 @@ public void TestParseTooShort()
}).Message
);
}

[Fact]
public void TestEdgeCasesAroundBirthday()
{
var timeProvider = new TestTimeProvider(); //TestTime is 2025-10-05
Assert.Equal(18, new Personnummer("20071004-3654", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Had birthday yesterday
Assert.Equal(18, new Personnummer("20071005-3653", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Birthday today
Assert.Equal(17, new Personnummer("20071006-3652", new Personnummer.Options() {TimeProvider = timeProvider} ).Age); // Upcoming Birthday tomorrow
}

}
}
2 changes: 1 addition & 1 deletion Personnummer.Tests/TestTimeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Personnummer.Tests;
public class TestTimeProvider : TimeProvider
{
internal DateTimeOffset Now { get; set; } = new(
new DateOnly(2025, 1, 5),
new DateOnly(2025, 1, 1),
new TimeOnly(0, 0, 0, 1),
TimeSpan.Zero
);
Expand Down
2 changes: 1 addition & 1 deletion Personnummer/Personnummer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public int Age
var now = _options.DateTimeNow;
var age = now.Year - Date.Year;

var hadBirthdayThisYear = (now.Month > Date.Month) ||
var hadBirthdayThisYear = now.Month > Date.Month ||
(now.Month == Date.Month && now.Day >= Date.Day);

if (!hadBirthdayThisYear)
Expand Down

0 comments on commit 165f249

Please sign in to comment.