|
19 | 19 |
|
20 | 20 | from google.api_core import datetime_helpers
|
21 | 21 |
|
| 22 | + |
22 | 23 | ONE_MINUTE_IN_MICROSECONDS = 60 * 1e6
|
23 | 24 |
|
24 | 25 |
|
@@ -148,3 +149,95 @@ def test_to_rfc3339_with_non_utc_ignore_zone():
|
148 | 149 | value = datetime.datetime(2016, 4, 5, 13, 30, 0, tzinfo=zone)
|
149 | 150 | expected = '2016-04-05T13:30:00.000000Z'
|
150 | 151 | assert datetime_helpers.to_rfc3339(value, ignore_zone=True) == expected
|
| 152 | + |
| 153 | + |
| 154 | +def test_datetimewithnanos_ctor_wo_nanos(): |
| 155 | + stamp = datetime_helpers.DatetimeWithNanoseconds( |
| 156 | + 2016, 12, 20, 21, 13, 47, 123456) |
| 157 | + assert stamp.year == 2016 |
| 158 | + assert stamp.month == 12 |
| 159 | + assert stamp.day == 20 |
| 160 | + assert stamp.hour == 21 |
| 161 | + assert stamp.minute == 13 |
| 162 | + assert stamp.second == 47 |
| 163 | + assert stamp.microsecond == 123456 |
| 164 | + assert stamp.nanosecond == 0 |
| 165 | + |
| 166 | + |
| 167 | +def test_datetimewithnanos_ctor_w_nanos(): |
| 168 | + stamp = datetime_helpers.DatetimeWithNanoseconds( |
| 169 | + 2016, 12, 20, 21, 13, 47, nanosecond=123456789) |
| 170 | + assert stamp.year == 2016 |
| 171 | + assert stamp.month == 12 |
| 172 | + assert stamp.day == 20 |
| 173 | + assert stamp.hour == 21 |
| 174 | + assert stamp.minute == 13 |
| 175 | + assert stamp.second == 47 |
| 176 | + assert stamp.microsecond == 123456 |
| 177 | + assert stamp.nanosecond == 123456789 |
| 178 | + |
| 179 | + |
| 180 | +def test_datetimewithnanos_ctor_w_micros_positional_and_nanos(): |
| 181 | + with pytest.raises(TypeError): |
| 182 | + datetime_helpers.DatetimeWithNanoseconds( |
| 183 | + 2016, 12, 20, 21, 13, 47, 123456, nanosecond=123456789) |
| 184 | + |
| 185 | + |
| 186 | +def test_datetimewithnanos_ctor_w_micros_keyword_and_nanos(): |
| 187 | + with pytest.raises(TypeError): |
| 188 | + datetime_helpers.DatetimeWithNanoseconds( |
| 189 | + 2016, 12, 20, 21, 13, 47, |
| 190 | + microsecond=123456, nanosecond=123456789) |
| 191 | + |
| 192 | + |
| 193 | +def test_datetimewithnanos_rfc339_wo_nanos(): |
| 194 | + stamp = datetime_helpers.DatetimeWithNanoseconds( |
| 195 | + 2016, 12, 20, 21, 13, 47, 123456) |
| 196 | + assert stamp.rfc3339() == '2016-12-20T21:13:47.123456Z' |
| 197 | + |
| 198 | + |
| 199 | +def test_datetimewithnanos_rfc339_w_nanos(): |
| 200 | + stamp = datetime_helpers.DatetimeWithNanoseconds( |
| 201 | + 2016, 12, 20, 21, 13, 47, nanosecond=123456789) |
| 202 | + assert stamp.rfc3339() == '2016-12-20T21:13:47.123456789Z' |
| 203 | + |
| 204 | + |
| 205 | +def test_datetimewithnanos_rfc339_w_nanos_no_trailing_zeroes(): |
| 206 | + stamp = datetime_helpers.DatetimeWithNanoseconds( |
| 207 | + 2016, 12, 20, 21, 13, 47, nanosecond=100000000) |
| 208 | + assert stamp.rfc3339() == '2016-12-20T21:13:47.1Z' |
| 209 | + |
| 210 | + |
| 211 | +def test_datetimewithnanos_from_rfc3339_w_invalid(): |
| 212 | + stamp = '2016-12-20T21:13:47' |
| 213 | + with pytest.raises(ValueError): |
| 214 | + datetime_helpers.DatetimeWithNanoseconds.from_rfc3339(stamp) |
| 215 | + |
| 216 | + |
| 217 | +def test_datetimewithnanos_from_rfc3339_wo_fraction(): |
| 218 | + timestamp = '2016-12-20T21:13:47Z' |
| 219 | + expected = datetime_helpers.DatetimeWithNanoseconds( |
| 220 | + 2016, 12, 20, 21, 13, 47, |
| 221 | + tzinfo=pytz.UTC) |
| 222 | + stamp = datetime_helpers.DatetimeWithNanoseconds.from_rfc3339(timestamp) |
| 223 | + assert (stamp == expected) |
| 224 | + |
| 225 | + |
| 226 | +def test_datetimewithnanos_from_rfc3339_w_partial_precision(): |
| 227 | + timestamp = '2016-12-20T21:13:47.1Z' |
| 228 | + expected = datetime_helpers.DatetimeWithNanoseconds( |
| 229 | + 2016, 12, 20, 21, 13, 47, |
| 230 | + microsecond=100000, |
| 231 | + tzinfo=pytz.UTC) |
| 232 | + stamp = datetime_helpers.DatetimeWithNanoseconds.from_rfc3339(timestamp) |
| 233 | + assert stamp == expected |
| 234 | + |
| 235 | + |
| 236 | +def test_datetimewithnanos_from_rfc3339_w_full_precision(): |
| 237 | + timestamp = '2016-12-20T21:13:47.123456789Z' |
| 238 | + expected = datetime_helpers.DatetimeWithNanoseconds( |
| 239 | + 2016, 12, 20, 21, 13, 47, |
| 240 | + nanosecond=123456789, |
| 241 | + tzinfo=pytz.UTC) |
| 242 | + stamp = datetime_helpers.DatetimeWithNanoseconds.from_rfc3339(timestamp) |
| 243 | + assert stamp == expected |
0 commit comments