15
15
Internal utils tests.
16
16
"""
17
17
from os import environ , path
18
- from firebase_functions .private .util import firebase_config , normalize_path
18
+ from firebase_functions .private .util import firebase_config , microsecond_timestamp_conversion , nanoseconds_timestamp_conversion , is_precision_timestamp , normalize_path
19
+ import datetime as _dt
19
20
20
21
test_bucket = "python-functions-testing.appspot.com"
21
22
test_config_file = path .join (path .dirname (path .realpath (__file__ )),
@@ -42,6 +43,68 @@ def test_firebase_config_loads_from_env_file():
42
43
"Failure, firebase_config did not load from env variable." )
43
44
44
45
46
+ def test_microsecond_conversion ():
47
+ """
48
+ Testing microsecond_timestamp_conversion works as intended
49
+ """
50
+ timestamps = [
51
+ ("2023-06-20T10:15:22.396358Z" , "2023-06-20T10:15:22.396358Z" ),
52
+ ("2021-02-20T11:23:45.987123Z" , "2021-02-20T11:23:45.987123Z" ),
53
+ ("2022-09-18T09:15:38.246824Z" , "2022-09-18T09:15:38.246824Z" ),
54
+ ("2010-09-18T09:15:38.246824Z" , "2010-09-18T09:15:38.246824Z" ),
55
+ ]
56
+
57
+ for input_timestamp , expected_output in timestamps :
58
+ expected_datetime = _dt .datetime .strptime (expected_output ,
59
+ "%Y-%m-%dT%H:%M:%S.%fZ" )
60
+ expected_datetime = expected_datetime .replace (tzinfo = _dt .timezone .utc )
61
+ assert microsecond_timestamp_conversion (
62
+ input_timestamp ) == expected_datetime
63
+
64
+
65
+ def test_nanosecond_conversion ():
66
+ """
67
+ Testing nanoseconds_timestamp_conversion works as intended
68
+ """
69
+ timestamps = [
70
+ ("2023-01-01T12:34:56.123456789Z" , "2023-01-01T12:34:56.123456Z" ),
71
+ ("2023-02-14T14:37:52.987654321Z" , "2023-02-14T14:37:52.987654Z" ),
72
+ ("2023-03-21T06:43:58.564738291Z" , "2023-03-21T06:43:58.564738Z" ),
73
+ ("2023-08-15T22:22:22.222222222Z" , "2023-08-15T22:22:22.222222Z" ),
74
+ ]
75
+
76
+ for input_timestamp , expected_output in timestamps :
77
+ expected_datetime = _dt .datetime .strptime (expected_output ,
78
+ "%Y-%m-%dT%H:%M:%S.%fZ" )
79
+ expected_datetime = expected_datetime .replace (tzinfo = _dt .timezone .utc )
80
+ assert nanoseconds_timestamp_conversion (
81
+ input_timestamp ) == expected_datetime
82
+
83
+
84
+ def test_is_nanoseconds_timestamp ():
85
+ """
86
+ Testing is_nanoseconds_timestamp works as intended
87
+ """
88
+ microsecond_timestamp1 = "2023-06-20T10:15:22.396358Z"
89
+ microsecond_timestamp2 = "2021-02-20T11:23:45.987123Z"
90
+ microsecond_timestamp3 = "2022-09-18T09:15:38.246824Z"
91
+ microsecond_timestamp4 = "2010-09-18T09:15:38.246824Z"
92
+
93
+ nanosecond_timestamp1 = "2023-01-01T12:34:56.123456789Z"
94
+ nanosecond_timestamp2 = "2023-02-14T14:37:52.987654321Z"
95
+ nanosecond_timestamp3 = "2023-03-21T06:43:58.564738291Z"
96
+ nanosecond_timestamp4 = "2023-08-15T22:22:22.222222222Z"
97
+
98
+ assert is_precision_timestamp (microsecond_timestamp1 ) is False
99
+ assert is_precision_timestamp (microsecond_timestamp2 ) is False
100
+ assert is_precision_timestamp (microsecond_timestamp3 ) is False
101
+ assert is_precision_timestamp (microsecond_timestamp4 ) is False
102
+ assert is_precision_timestamp (nanosecond_timestamp1 ) is True
103
+ assert is_precision_timestamp (nanosecond_timestamp2 ) is True
104
+ assert is_precision_timestamp (nanosecond_timestamp3 ) is True
105
+ assert is_precision_timestamp (nanosecond_timestamp4 ) is True
106
+
107
+
45
108
def test_normalize_document_path ():
46
109
"""
47
110
Testing "document" path passed to Firestore event listener
0 commit comments