Skip to content

Commit

Permalink
Sorr task592 extend unit testing of str to timestamp (#593)
Browse files Browse the repository at this point in the history
* Changed function docstring

* Added UTC timezone unit test

* fixed docstring

* Made test 5 into test 2 and removed original test 2.

* Changed docstrings

* Changed to check_string()

* added test for check_string

* Fixed typo

* revert some test ouput files to their version in master

* Added new line after test class docstring
  • Loading branch information
shab95 authored Nov 7, 2023
1 parent acf0de6 commit d3a83a5
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 27 deletions.
5 changes: 5 additions & 0 deletions helpers/hdatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,11 @@ def str_to_timestamp(
"""
Convert timestamp as string to `pd.Timestamp`.
Localize input time to the specified timezone.
E.g., `timestamp_as_str = "20230523_150513"`:
- `tz = "UTC"` -> "2023-05-23 15:05:13+0000"
- `tz = "US/Eastern"` -> "2023-05-23 15:05:13-0400"
:param timestamp_as_str: string datetime (e.g., 20230523_150513)
:param tz: timezone info (e.g., "US/Eastern")
:param datetime_format: datetime format (e.g., %Y%m%d_%H%M%S)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
date='2022-11-01' doesn't have the right format: time data '2022-11-01' does not match format '%Y%m%d'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
date='2022-11-01' doesn't have the right format: time data '2022-11-01' does not match format '%Y%m%d'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
time data '28-07-2023 15:05:13' does not match format '%Y%m%d_%H%M%S' (match)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unknown string format: qwe28abc07-201234 present at position 0
60 changes: 33 additions & 27 deletions helpers/test/test_hdatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,84 +626,90 @@ def test3(self) -> None:


class Test_str_to_timestamp1(hunitest.TestCase):
"""
Test if string representation of datetime is converted correctly
"""

def test1(self) -> None:
"""
Test valid datetime with format.
- `datetime_str` has a valid format
- `datetime_format` has a valid pattern for `datetime_str`
"""
datetime_str = "20230728_150513"
timezone_info = "US/Eastern"
format = "%Y%m%d_%H%M%S"
datetime_format = "%Y%m%d_%H%M%S"
actual = hdateti.str_to_timestamp(
datetime_str, timezone_info, datetime_format=format
datetime_str, timezone_info, datetime_format=datetime_format
)
expected = pd.Timestamp("2023-07-28 15:05:13-0400", tz="US/Eastern")
self.assertEqual(actual, expected)

def test2(self) -> None:
"""
Test valid datetime without format.
- `datetime_str` has a valid format
- `datetime_format` has an valid pattern for `datetime_str`
- `timezone_info` is UTC
"""
datetime_str = "2023-07-28 15:05:13"
timezone_info = "US/Eastern"
expected = pd.Timestamp("2023-07-28 15:05:13-0400", tz="US/Eastern")
actual = hdateti.str_to_timestamp(datetime_str, timezone_info)
datetime_str = "20230728_150513"
timezone_info = "UTC"
format = "%Y%m%d_%H%M%S"
actual = hdateti.str_to_timestamp(datetime_str, timezone_info, datetime_format=format)
expected = pd.Timestamp('2023-07-28 15:05:13+0000', tz='UTC')
self.assertEqual(actual, expected)

def test3(self) -> None:
"""
Test invalid datetime with format.
- `datetime_str` has a valid format
- `datetime_format` has an invalid pattern for `datetime_str`
"""
datetime_str = "28-07-2023 15:05:13"
timezone_info = "US/Eastern"
format = "%Y%m%d_%H%M%S"
# Invalid datetime, should raise a ValueError.
datetime_format = "%Y%m%d_%H%M%S"
# The datetime format does not match the string representation of datetime.
with self.assertRaises(ValueError) as err:
hdateti.str_to_timestamp(
datetime_str, timezone_info, datetime_format=format
datetime_str, timezone_info, datetime_format=datetime_format
)
actual = str(err.exception)
expected = "time data '28-07-2023 15:05:13' does not match format '%Y%m%d_%H%M%S' (match)"
self.assert_equal(actual, expected)
self.check_string(actual)

def test4(self) -> None:
"""
Test invalid datetime without format.
- `datetime_str` has an invalid format
- `datetime_format` is not defined
"""
datetime_str = "qwe28abc07-201234"
timezone_info = "US/Eastern"
# Invalid datetime, should raise a ValueError.
with self.assertRaises(ValueError) as err:
hdateti.str_to_timestamp(datetime_str, timezone_info)
actual = str(err.exception)
expected = (
"Unknown string format: qwe28abc07-201234 present at position 0"
)
self.assert_equal(actual, expected)

self.check_string(actual)


# #############################################################################
# Test_dassert_str_is_date
# #############################################################################


class Test_dassert_str_is_date(hunitest.TestCase):
"""
Test that the function checks a string representation of date correctly.
"""

def test1(self) -> None:
"""
Test valid date.
- date has a valid format
"""
date_str = "20221101"
hdateti.dassert_str_is_date(date_str)

def test2(self) -> None:
"""
Test invalid date.
- date has an invalid format
"""
date = "2022-11-01"
with self.assertRaises(ValueError) as err:
hdateti.dassert_str_is_date(date)
actual = str(err.exception)
expected = (
"date='2022-11-01' doesn't have the right format: "
"time data '2022-11-01' does not match format '%Y%m%d'"
)
self.assert_equal(actual, expected)
self.check_string(actual)

0 comments on commit d3a83a5

Please sign in to comment.