|
18 | 18 | import datetime |
19 | 19 | import os |
20 | 20 | import pickle |
| 21 | +import pytz |
21 | 22 | import random |
22 | 23 | import threading |
23 | 24 | import zlib |
|
40 | 41 | USE_REDIS_CACHE = bool(os.environ.get("REDIS_CACHE_URL")) |
41 | 42 |
|
42 | 43 |
|
| 44 | +def _assert_contemporaneous(timestamp1, timestamp2, delta_margin=2): |
| 45 | + delta_margin = datetime.timedelta(seconds=delta_margin) |
| 46 | + assert delta_margin > abs(timestamp1 - timestamp2) |
| 47 | + |
| 48 | + |
43 | 49 | @pytest.mark.usefixtures("client_context") |
44 | 50 | def test_retrieve_entity(ds_entity): |
45 | 51 | entity_id = test_utils.system.unique_resource_id() |
@@ -1043,6 +1049,59 @@ class SomeKind(ndb.Model): |
1043 | 1049 | assert isinstance(retrieved.updated_at, datetime.datetime) |
1044 | 1050 |
|
1045 | 1051 |
|
| 1052 | +@pytest.mark.usefixtures("client_context") |
| 1053 | +def test_insert_autonow_property_with_tz(dispose_of): |
| 1054 | + """Regression test for #517 |
| 1055 | +
|
| 1056 | + https://github.com/googleapis/python-ndb/issues/517 |
| 1057 | + """ |
| 1058 | + |
| 1059 | + class SomeKind(ndb.Model): |
| 1060 | + created_at = ndb.DateTimeProperty(auto_now_add=True, tzinfo=pytz.utc) |
| 1061 | + updated_at = ndb.DateTimeProperty(auto_now=True, tzinfo=pytz.utc) |
| 1062 | + |
| 1063 | + now = datetime.datetime.now(pytz.utc) |
| 1064 | + entity = SomeKind() |
| 1065 | + key = entity.put() |
| 1066 | + dispose_of(key._key) |
| 1067 | + |
| 1068 | + _assert_contemporaneous(entity.created_at, now) |
| 1069 | + _assert_contemporaneous(entity.updated_at, now) |
| 1070 | + |
| 1071 | + retrieved = key.get() |
| 1072 | + |
| 1073 | + _assert_contemporaneous(retrieved.created_at, now) |
| 1074 | + _assert_contemporaneous(retrieved.updated_at, now) |
| 1075 | + |
| 1076 | + |
| 1077 | +@pytest.mark.usefixtures("client_context") |
| 1078 | +def test_insert_datetime_property_with_tz(dispose_of): |
| 1079 | + """Regression test for #517 |
| 1080 | +
|
| 1081 | + https://github.com/googleapis/python-ndb/issues/517 |
| 1082 | + """ |
| 1083 | + |
| 1084 | + class SomeKind(ndb.Model): |
| 1085 | + alarm1 = ndb.DateTimeProperty(tzinfo=pytz.utc) |
| 1086 | + alarm2 = ndb.DateTimeProperty(tzinfo=pytz.utc) |
| 1087 | + |
| 1088 | + now = datetime.datetime.now(pytz.utc) |
| 1089 | + entity = SomeKind( |
| 1090 | + alarm1=now, |
| 1091 | + alarm2=datetime.datetime.utcnow(), # naive |
| 1092 | + ) |
| 1093 | + key = entity.put() |
| 1094 | + dispose_of(key._key) |
| 1095 | + |
| 1096 | + _assert_contemporaneous(entity.alarm1, now) |
| 1097 | + _assert_contemporaneous(entity.alarm2, now) |
| 1098 | + |
| 1099 | + retrieved = key.get() |
| 1100 | + |
| 1101 | + _assert_contemporaneous(retrieved.alarm1, now) |
| 1102 | + _assert_contemporaneous(retrieved.alarm2, now) |
| 1103 | + |
| 1104 | + |
1046 | 1105 | @pytest.mark.usefixtures("client_context") |
1047 | 1106 | def test_insert_nested_autonow_property(dispose_of): |
1048 | 1107 | class OtherKind(ndb.Model): |
|
0 commit comments