Jump to content
Sign in to follow this  
Sherlock

Trouble with testing dates

Recommended Posts

As the title says, I'm having trouble with running a unit test to test dates. So here is my test:

  [TestFixture]
  TMyTestClass = class
  public

    [Test]
    // Test with TestCase Attribute to supply parameters.
    [Test]
    [TestCase('Test Thu-Sat', '2023-06-22 17:44:23.456, 2023-06-24 17:44:23.456, 0')]
    [TestCase('Test Thu-Sun', '2023-06-22 17:44:23.456, 2023-06-25 17:44:23.456, 1')]
    procedure Test(const AValue1: TDateTime; const AValue2: TDateTime; const AExp: Integer);
  end;
  
procedure TMyTestClass.Test(const AValue1, AValue2: TDateTime; const AExp: Integer);
begin
  Assert.IsTrue(Unit34.CheckForDates(AValue1, AValue2) = AExp);
end;

Now when I run this it alwas behaves as though the dates are not translated correctly. And sure enough the debugger says both AValues are 1899. When I run DUnitXs own Test - specifically the DUnitX.Tests.Example.pas the date there gets translated just fine. And the keen eye might notice the same time in my code as in DUnitXs...I copied and only changed the date. What am I doing wrong?

 

Share this post


Link to post

OK, so it's a locale settings issue. Apparently the DUnitX tests do something to the date format, that catch this. As soon as I use my native german date format, it works. Hrmpf

[TestCase('Test Do-Sa', '22.06.2023 17:44:23.456, 24.06.2023 17:44:23.456, 0')]

 

And @Der schöne Günther I will do just that then. Thank you!

Share this post


Link to post

Not to be "that guy" but, um... it would seem my initial dates where indeed ISO 8601 formatted. So my guess now is that the DUnitX used in D11.3 is more than two years old?

Share this post


Link to post
1 hour ago, Sherlock said:

So my guess now is that the DUnitX used in D11.3 is more than two years old?

At least more than three🙄.

Judging by the readme file, it is newer than September 2016, but as you noticed, older than June 2020 when my change was added.

Edited by Der schöne Günther
  • Like 1

Share this post


Link to post
1 hour ago, Sherlock said:

Not to be "that guy" but, um... it would seem my initial dates where indeed ISO 8601 formatted. So my guess now is that the DUnitX used in D11.3 is more than two years old?

Almost correct... you missed "T" character between the date and time parts:

2023-06-22 17:44:23.456

should be

2023-06-22T17:44:23.456

to represent a valid ISO 8601 date. (And probably a timezone but it is usually not required)

 

Share this post


Link to post

I made it so that it works both with and without the T as it's more readable without it.

    [Test]
    [TestCase('Date, space, time', '1988-10-21 17:44:23.456')]
    [TestCase('Date, T, time', '1988-10-21T17:44:23.456')]
    [TestCase('Date, T, time, Z', '1988-10-21T17:44:23.456Z')]
    [TestCase('Date, T, time, offset (1)', '1988-10-21T17:44:23.456+02:30')]
    [TestCase('Date, T, time, offset (2)', '1988-10-21T17:44:23.456+0230')]
    procedure TestDateTimeArgument(dateTime: TDateTime);

https://github.com/VSoftTechnologies/DUnitX/blob/c348ea5010822368975c0f10aa1d16969c6ba6bd/Tests/DUnitX.Tests.Example.pas#L68-L74

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
×