|
21 | 21 | import pytest
|
22 | 22 |
|
23 | 23 | from ibm_cloud_sdk_core import string_to_datetime, datetime_to_string, get_authenticator_from_environment
|
| 24 | +from ibm_cloud_sdk_core import string_to_datetime_list, datetime_to_string_list |
24 | 25 | from ibm_cloud_sdk_core import string_to_date, date_to_string
|
25 | 26 | from ibm_cloud_sdk_core import convert_model, convert_list
|
26 | 27 | from ibm_cloud_sdk_core import get_query_param
|
@@ -116,6 +117,63 @@ def test_datetime_to_string():
|
116 | 117 | res = datetime_to_string(date)
|
117 | 118 | assert res == '2017-03-06T16:00:04.159338Z'
|
118 | 119 |
|
| 120 | +def test_string_to_datetime_list(): |
| 121 | + # Assert ValueError is raised for invalid argument type |
| 122 | + with pytest.raises(ValueError): |
| 123 | + string_to_datetime_list(None) |
| 124 | + # If the specified string does not include a timezone, it is assumed to be UTC |
| 125 | + date_list = string_to_datetime_list([ '2017-03-06 16:00:04.159338' ]) |
| 126 | + assert date_list[0].day == 6 |
| 127 | + assert date_list[0].hour == 16 |
| 128 | + assert date_list[0].tzinfo.utcoffset(None) == datetime.timezone.utc.utcoffset(None) |
| 129 | + # Test date string with TZ specified as '+xxxx' |
| 130 | + date_list = string_to_datetime_list([ '2017-03-06 16:00:04.159338+0600' ]) |
| 131 | + assert date_list[0].day == 6 |
| 132 | + assert date_list[0].hour == 16 |
| 133 | + assert date_list[0].tzinfo.utcoffset(None).total_seconds() == 6 * 60 * 60 |
| 134 | + # Test date string with TZ specified as 'Z' |
| 135 | + date_list = string_to_datetime_list([ '2017-03-06 16:00:04.159338Z' ]) |
| 136 | + assert date_list[0].day == 6 |
| 137 | + assert date_list[0].hour == 16 |
| 138 | + assert date_list[0].tzinfo.utcoffset(None) == datetime.timezone.utc.utcoffset(None) |
| 139 | + # Test multiple datetimes in a list |
| 140 | + date_list = string_to_datetime_list([ '2017-03-06 16:00:04.159338', '2017-03-07 17:00:04.159338' ]) |
| 141 | + assert date_list[0].day == 6 |
| 142 | + assert date_list[0].hour == 16 |
| 143 | + assert date_list[0].tzinfo.utcoffset(None) == datetime.timezone.utc.utcoffset(None) |
| 144 | + assert date_list[1].day == 7 |
| 145 | + assert date_list[1].hour == 17 |
| 146 | + assert date_list[1].tzinfo.utcoffset(None) == datetime.timezone.utc.utcoffset(None) |
| 147 | + |
| 148 | + |
| 149 | +def test_datetime_to_string_list(): |
| 150 | + # Assert ValueError is raised for invalid argument type |
| 151 | + with pytest.raises(ValueError): |
| 152 | + datetime_to_string_list(None) |
| 153 | + # If specified datetime list item is None, return list of None |
| 154 | + assert datetime_to_string_list([None]) == [None] |
| 155 | + # If specified datetime list is empty, return empty list |
| 156 | + assert datetime_to_string_list([]) == [] |
| 157 | + # If the specified date list item is "naive", it is interpreted as a UTC date |
| 158 | + date_list = [ datetime.datetime(2017, 3, 6, 16, 0, 4, 159338) ] |
| 159 | + res = datetime_to_string_list(date_list) |
| 160 | + assert res == [ '2017-03-06T16:00:04.159338Z' ] |
| 161 | + # Test date list item with UTC timezone |
| 162 | + date_list = [ datetime.datetime(2017, 3, 6, 16, 0, 4, 159338, |
| 163 | + datetime.timezone.utc) ] |
| 164 | + res = datetime_to_string_list(date_list) |
| 165 | + assert res == [ '2017-03-06T16:00:04.159338Z' ] |
| 166 | + # Test date list item with non-UTC timezone |
| 167 | + tzn = datetime.timezone(datetime.timedelta(hours=-6)) |
| 168 | + date_list = [ datetime.datetime(2017, 3, 6, 10, 0, 4, 159338, tzn) ] |
| 169 | + res = datetime_to_string_list(date_list) |
| 170 | + assert res == [ '2017-03-06T16:00:04.159338Z' ] |
| 171 | + # Test specified date list with multiple items |
| 172 | + date_list = [ datetime.datetime(2017, 3, 6, 16, 0, 4, 159338), |
| 173 | + datetime.datetime(2017, 3, 6, 16, 0, 4, 159338, |
| 174 | + datetime.timezone.utc) ] |
| 175 | + res = datetime_to_string_list(date_list) |
| 176 | + assert res == [ '2017-03-06T16:00:04.159338Z', '2017-03-06T16:00:04.159338Z' ] |
119 | 177 |
|
120 | 178 | def test_date_conversion():
|
121 | 179 | date = string_to_date('2017-03-06')
|
|
0 commit comments