|
18 | 18 | import datetime as _dt |
19 | 19 | from os import environ, path |
20 | 20 |
|
| 21 | +import pytest |
| 22 | + |
21 | 23 | from firebase_functions.private.util import ( |
22 | 24 | PrecisionTimestamp, |
23 | 25 | _unsafe_decode_id_token, |
|
28 | 30 | nanoseconds_timestamp_conversion, |
29 | 31 | normalize_path, |
30 | 32 | second_timestamp_conversion, |
| 33 | + timestamp_conversion, |
31 | 34 | ) |
32 | 35 |
|
33 | 36 | test_bucket = "python-functions-testing.appspot.com" |
@@ -187,3 +190,88 @@ def test_unsafe_decode_token(): |
187 | 190 | result = _unsafe_decode_id_token(test_token) |
188 | 191 | assert result["sub"] == "firebase" |
189 | 192 | assert result["name"] == "John Doe" |
| 193 | + |
| 194 | + |
| 195 | +def test_timestamp_conversion_with_object(): |
| 196 | + """ |
| 197 | + Testing timestamp_conversion works with objects that have seconds and nanoseconds attributes. |
| 198 | + """ |
| 199 | + class Timestamp: |
| 200 | + def __init__(self, seconds, nanoseconds): |
| 201 | + self.seconds = seconds |
| 202 | + self.nanoseconds = nanoseconds |
| 203 | + |
| 204 | + test_cases = [ |
| 205 | + (1672578896, 123456789), |
| 206 | + (1672578896, 0), |
| 207 | + (1672578896, 1_500_000_000), |
| 208 | + ] |
| 209 | + |
| 210 | + for seconds, nanoseconds in test_cases: |
| 211 | + timestamp_obj = Timestamp(seconds=seconds, nanoseconds=nanoseconds) |
| 212 | + result = timestamp_conversion(timestamp_obj) |
| 213 | + expected = _dt.datetime.fromtimestamp( |
| 214 | + seconds + nanoseconds / 1_000_000_000, tz=_dt.timezone.utc |
| 215 | + ) |
| 216 | + assert result == expected |
| 217 | + assert result.tzinfo == _dt.timezone.utc |
| 218 | + |
| 219 | + |
| 220 | +def test_timestamp_conversion_with_dict(): |
| 221 | + """ |
| 222 | + Testing timestamp_conversion works with dict objects containing seconds and nanoseconds keys. |
| 223 | + """ |
| 224 | + test_cases = [ |
| 225 | + (1687256122, 396358000), |
| 226 | + (1687256122, 0), |
| 227 | + ] |
| 228 | + |
| 229 | + for seconds, nanoseconds in test_cases: |
| 230 | + timestamp_dict = {"seconds": seconds, "nanoseconds": nanoseconds} |
| 231 | + result = timestamp_conversion(timestamp_dict) |
| 232 | + expected = _dt.datetime.fromtimestamp( |
| 233 | + seconds + nanoseconds / 1_000_000_000, tz=_dt.timezone.utc |
| 234 | + ) |
| 235 | + assert result == expected |
| 236 | + assert result.tzinfo == _dt.timezone.utc |
| 237 | + |
| 238 | + |
| 239 | +def test_timestamp_conversion_with_string(): |
| 240 | + """ |
| 241 | + Testing timestamp_conversion works with string inputs. |
| 242 | + """ |
| 243 | + test_cases = [ |
| 244 | + ("2023-01-01T12:34:56.123456789Z", nanoseconds_timestamp_conversion), |
| 245 | + ("2023-06-20T10:15:22.396358Z", microsecond_timestamp_conversion), |
| 246 | + ("2023-01-01T12:34:56Z", second_timestamp_conversion), |
| 247 | + ] |
| 248 | + |
| 249 | + for timestamp_str, conversion_func in test_cases: |
| 250 | + result = timestamp_conversion(timestamp_str) |
| 251 | + expected = conversion_func(timestamp_str) |
| 252 | + assert result == expected |
| 253 | + |
| 254 | + |
| 255 | +def test_timestamp_conversion_errors(): |
| 256 | + """ |
| 257 | + Testing timestamp_conversion raises appropriate errors for invalid inputs. |
| 258 | + """ |
| 259 | + class IncompleteTimestamp: |
| 260 | + def __init__(self, nanoseconds): |
| 261 | + self.nanoseconds = nanoseconds |
| 262 | + |
| 263 | + with pytest.raises(ValueError): |
| 264 | + timestamp_conversion(IncompleteTimestamp(nanoseconds=123456789)) |
| 265 | + |
| 266 | + with pytest.raises(ValueError) as context: |
| 267 | + timestamp_conversion(12345) |
| 268 | + assert "timestamp_conversion expects a string or a Timestamp-like object" in str(context.value) |
| 269 | + with pytest.raises(ValueError): |
| 270 | + timestamp_conversion({"nanoseconds": 123456789}) |
| 271 | + |
| 272 | + with pytest.raises(ValueError): |
| 273 | + timestamp_conversion("invalid_timestamp") |
| 274 | + |
| 275 | + with pytest.raises(ValueError): |
| 276 | + timestamp_conversion(None) |
| 277 | + |
0 commit comments