Add round-trip and leap-year coverage tests for DateTimeConverter (pre/post 1970)#135
Add round-trip and leap-year coverage tests for DateTimeConverter (pre/post 1970)#135Rutuja-Patil-Bosch wants to merge 27 commits into
Conversation
…lse_isValidDateTimeFormat EpochToDateTime_Before1970_LeapYear_January_AdjustsDaysSum invalid_day_values invalid_month_values
removed redundnat checks
Feature: bgsw_baselib_ut
|
The created documentation from the pull request is available at: docu-html |
|
@Rutuja-Patil-Bosch please avoid bot based commits. Feel free to contact me directly. |
Review findings are fixed for eclipse-score#135 Changes are: 1. Test cases are spitted to get more clarity with asserts 2. Instead of loop added hardcoded values to avoid dead/unreachable code 3. RecordProperty are updated with only spec listed variables
|
@gierer, please have a look at this PR |
| return; | ||
| auto dateTimeConverted = score::common::epochToDateTime(epoch); | ||
| ASSERT_NE(dateTimeConverted, nullptr); | ||
| ASSERT_EQ(dateTimeConverted->m_year, 1968); |
There was a problem hiding this comment.
Please check all values. Something like "ASSERT_EQ(dateTimeConverted, dateTimeOriginal );
There was a problem hiding this comment.
@gierer For this, std::make_shared(1968, 12, 31, 0, 0, 1);
then for day check this will be ASSERT_EQ(dateTimeConverted->m_day, 30);
Reason: Expected by current algorithm: pre-1970 leap-year path decrements daysSum once.
I'll add for checks for all values
| auto dateTimeConverted = score::common::epochToDateTime(epoch); | ||
| ASSERT_NE(dateTimeConverted, nullptr); | ||
| //After 1970 year is adjusted based on daysome | ||
| ASSERT_EQ(dateTimeConverted->m_year, 1973); |
There was a problem hiding this comment.
@gierer This verifies existing implementation behavior (not roundtrip preservation : the value may change after converting to epoch and back and this test accepts/document that current behavior.).
For input 1972-12-31 00:00:01, epochToDateTime() currently returns year 1973.
There was a problem hiding this comment.
The conversion should be either correct or return an error.
A test is meant to verify correct behaviour of the code. So if the code has a bug it needs to be fixed, instead of breaking the test as well. Code coverage gained through incorrect assertions has zero value — it doesn't verify correctness and creates a false sense of quality.
There was a problem hiding this comment.
@gierer : Should I update code in the same PR? or different PR is required ?
| RecordProperty("TestType", "requirements-based"); | ||
|
|
||
| time_t epoch{}; | ||
| auto d = std::make_shared<DateTimeType>(1968, 1, 1, 0, 0, 1); |
| ASSERT_TRUE(score::common::dateTimeToEpoch(d, &epoch)); | ||
|
|
||
| auto dt = score::common::epochToDateTime(epoch); | ||
| //InvalidDateTimeFormat due to day some adjustment & returns nullptr |
There was a problem hiding this comment.
Why is this no longer valid? Is this a bug in the implementation?
There was a problem hiding this comment.
@gierer This may indicate a converter defect.
This test documents current implementation behavior (known pre-1970 leap-year boundary issue), not roundtrip correctness.
For 1968-01-01 00:00:01, epochToDateTime() currently returns nullptr after daysSum adjustments in the pre-1970 leap-year path.
| if (!score::common::dateTimeToEpoch(dateTimeOriginal, &epoch)) | ||
| return; |
There was a problem hiding this comment.
| if (!score::common::dateTimeToEpoch(dateTimeOriginal, &epoch)) | |
| return; | |
| ASSERT_TRUE(score::common::dateTimeToEpoch(dateTimeOriginal, &epoch)) |
There was a problem hiding this comment.
Pull request overview
This PR adds additional unit tests for score::common::DateTimeConverter to increase coverage around invalid date inputs and leap-year / pre-/post-1970 conversion edge cases.
Changes:
- Added tests for invalid month/day values in
isValidDateTimeFormat. - Added a test ensuring
dateTimeToEpochreturnsfalseand does not mutate the output epoch when the input is invalid. - Added a few DateTime→epoch→DateTime “round-trip” tests targeting 1968 and 1972 leap-year boundaries.
Comments suppressed due to low confidence (3)
score/datetime_converter/datetime_converter_test.cpp:526
- Same issue here: the test returns early if
dateTimeToEpochfails, which makes the test pass without validating anything. Use an assertion so failures are visible and the remainder of the test always executes when appropriate.
time_t epoch{};
auto dateTimeOriginal =
std::make_shared<DateTimeType>(1972, 12, 31, 0, 0, 1);
if (!score::common::dateTimeToEpoch(dateTimeOriginal, &epoch))
return;
auto dateTimeConverted = score::common::epochToDateTime(epoch);
score/datetime_converter/datetime_converter_test.cpp:530
- The assertion expects the year to change from 1972 to 1973 after a DateTime→epoch→DateTime round-trip, which contradicts the test description (“remain unchanged”) and will lock in incorrect behavior. The round-trip expectation should match the original datetime fields.
auto dateTimeConverted = score::common::epochToDateTime(epoch);
ASSERT_NE(dateTimeConverted, nullptr);
//After 1970 year is adjusted based on daysome
ASSERT_EQ(dateTimeConverted->m_year, 1973);
score/datetime_converter/datetime_converter_test.cpp:546
- This test expects
epochToDateTimeto return nullptr for a valid date (1968-01-01) even thoughdateTimeToEpochsucceeded, which would encode a conversion bug rather than catch it. If this is intended behavior, it should be documented at the API level; otherwise the test should assert that the round-trip returns a valid DateTime equal to the input.
RecordProperty("Description",
"Verify that conversion from DateTime to Epoch to DateTime for pre-1970 leap year boundary (1968-01-01) results in an invalid DateTime, returning nullptr due to daysSum adjustment.");
RecordProperty("TestType", "requirements-based");
time_t epoch{};
auto d = std::make_shared<DateTimeType>(1968, 1, 1, 0, 0, 1);
ASSERT_TRUE(score::common::dateTimeToEpoch(d, &epoch));
auto dt = score::common::epochToDateTime(epoch);
//InvalidDateTimeFormat due to day some adjustment & returns nullptr
EXPECT_EQ(dt, nullptr);
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| time_t epoch{}; | ||
| auto dateTimeOriginal = | ||
| std::make_shared<DateTimeType>(1968, 12, 31, 0, 0, 1); | ||
| if (!score::common::dateTimeToEpoch(dateTimeOriginal, &epoch)) | ||
| return; | ||
| auto dateTimeConverted = score::common::epochToDateTime(epoch); |
| auto dateTimeOriginal = | ||
| std::make_shared<DateTimeType>(1968, 12, 31, 0, 0, 1); | ||
| if (!score::common::dateTimeToEpoch(dateTimeOriginal, &epoch)) | ||
| return; | ||
| auto dateTimeConverted = score::common::epochToDateTime(epoch); | ||
| ASSERT_NE(dateTimeConverted, nullptr); | ||
| ASSERT_EQ(dateTimeConverted->m_year, 1968); | ||
| } |
| //After 1970 year is adjusted based on daysome | ||
| ASSERT_EQ(dateTimeConverted->m_year, 1973); | ||
|
|
||
| } | ||
|
|
||
| TEST_F(DateTimeConverterTest, EpochToDateTime_Pre1970_LeapYear_Jan1_InvalidConversion_ReturnsNull) | ||
| { | ||
| RecordProperty("Description", | ||
| "Verify that conversion from DateTime to Epoch to DateTime for pre-1970 leap year boundary (1968-01-01) results in an invalid DateTime, returning nullptr due to daysSum adjustment."); | ||
| RecordProperty("TestType", "requirements-based"); | ||
|
|
||
| time_t epoch{}; | ||
| auto d = std::make_shared<DateTimeType>(1968, 1, 1, 0, 0, 1); | ||
| ASSERT_TRUE(score::common::dateTimeToEpoch(d, &epoch)); | ||
|
|
||
| auto dt = score::common::epochToDateTime(epoch); | ||
| //InvalidDateTimeFormat due to day some adjustment & returns nullptr | ||
| EXPECT_EQ(dt, nullptr); |
|
|
||
| if (!score::common::dateTimeToEpoch(dateTimeOriginal, &epoch)) | ||
| return; | ||
|
|
||
| auto dateTimeConverted = score::common::epochToDateTime(epoch); | ||
| ASSERT_NE(dateTimeConverted, nullptr); | ||
| //After 1970 year is adjusted based on daysome | ||
| ASSERT_EQ(dateTimeConverted->m_year, 1973); | ||
|
|
| TEST_F(DateTimeConverterTest, EpochToDateTime_Pre1970_LeapYear_DaysSumDecrementedInJanuary) | ||
| { | ||
| RecordProperty("Description", "Verify that datetime values remain unchanged after roundtrip conversion (DateTime to Epoch to DateTime) for pre 1970 leap years."); | ||
| RecordProperty("TestType", "requirements-based"); | ||
|
|
||
| time_t epoch{}; | ||
| auto dateTimeOriginal = | ||
| std::make_shared<DateTimeType>(1968, 12, 31, 0, 0, 1); | ||
| if (!score::common::dateTimeToEpoch(dateTimeOriginal, &epoch)) | ||
| return; | ||
| auto dateTimeConverted = score::common::epochToDateTime(epoch); | ||
| ASSERT_NE(dateTimeConverted, nullptr); | ||
| ASSERT_EQ(dateTimeConverted->m_year, 1968); | ||
| } | ||
|
|
||
| TEST_F(DateTimeConverterTest, EpochToDateTime_Post1970_LeapYear_DaysSumDecremented) | ||
| { | ||
| RecordProperty("Description", | ||
| "Verify that datetime values remain unchanged after roundtrip conversion (DateTime to Epoch to DateTime) for post-1970 leap years."); | ||
| RecordProperty("TestType", "requirements-based"); | ||
|
|
||
| time_t epoch{}; | ||
| auto dateTimeOriginal = | ||
| std::make_shared<DateTimeType>(1972, 12, 31, 0, 0, 1); | ||
|
|
||
| if (!score::common::dateTimeToEpoch(dateTimeOriginal, &epoch)) | ||
| return; | ||
|
|
||
| auto dateTimeConverted = score::common::epochToDateTime(epoch); | ||
| ASSERT_NE(dateTimeConverted, nullptr); | ||
| //After 1970 year is adjusted based on daysome | ||
| ASSERT_EQ(dateTimeConverted->m_year, 1973); | ||
|
|
||
| } | ||
|
|
||
| TEST_F(DateTimeConverterTest, EpochToDateTime_Pre1970_LeapYear_Jan1_InvalidConversion_ReturnsNull) | ||
| { | ||
| RecordProperty("Description", |
| TEST_F(DateTimeConverterTest, EpochToDateTime_Pre1970_LeapYear_DaysSumDecrementedInJanuary) | ||
| { | ||
| RecordProperty("Description", "Verify that datetime values remain unchanged after roundtrip conversion (DateTime to Epoch to DateTime) for pre 1970 leap years."); | ||
| RecordProperty("TestType", "requirements-based"); | ||
|
|
Summary
This PR extends unit test coverage for DateTimeConverter by adding round-trip validation and additional leap-year scenarios, especially around pre-1970 and post-1970 boundaries.
Changes
Motivation
The added tests ensure correctness of:
This improves confidence in DateTimeConverter for:
Notes
Test Scope
Impact