|
1 | 1 | from prefab_cloud_python.config_resolver import CriteriaEvaluator |
2 | 2 | from prefab_cloud_python.context import Context |
3 | 3 | import prefab_pb2 as Prefab |
| 4 | +from datetime import datetime, timezone, timedelta |
| 5 | +from unittest.mock import patch |
4 | 6 |
|
5 | 7 | project_env_id = 1 |
6 | 8 | test_env_id = 2 |
@@ -1019,6 +1021,92 @@ def test_stringifying_property_values_and_names(self): |
1019 | 1021 | == desired_value |
1020 | 1022 | ) |
1021 | 1023 |
|
| 1024 | + def test_prefab_current_time(self): |
| 1025 | + # Set up a fixed time for testing |
| 1026 | + test_time = datetime(2024, 1, 1, 12, 0, 0, tzinfo=timezone.utc) |
| 1027 | + test_time_millis = int(test_time.timestamp() * 1000) |
| 1028 | + |
| 1029 | + # Create a config that checks if current time is before a future time |
| 1030 | + future_time = test_time + timedelta(hours=1) |
| 1031 | + future_time_millis = int(future_time.timestamp() * 1000) |
| 1032 | + |
| 1033 | + config = Prefab.Config( |
| 1034 | + key=key, |
| 1035 | + rows=[ |
| 1036 | + default_row, |
| 1037 | + Prefab.ConfigRow( |
| 1038 | + project_env_id=project_env_id, |
| 1039 | + values=[ |
| 1040 | + Prefab.ConditionalValue( |
| 1041 | + criteria=[ |
| 1042 | + Prefab.Criterion( |
| 1043 | + operator=Prefab.Criterion.CriterionOperator.PROP_BEFORE, |
| 1044 | + property_name="prefab.current-time", |
| 1045 | + value_to_match=Prefab.ConfigValue(int=future_time_millis) |
| 1046 | + ) |
| 1047 | + ], |
| 1048 | + value=Prefab.ConfigValue(string=desired_value), |
| 1049 | + ) |
| 1050 | + ], |
| 1051 | + ), |
| 1052 | + ], |
| 1053 | + ) |
| 1054 | + |
| 1055 | + # Create a config that checks if current time is after a past time |
| 1056 | + past_time = test_time - timedelta(hours=1) |
| 1057 | + past_time_millis = int(past_time.timestamp() * 1000) |
| 1058 | + |
| 1059 | + config_past = Prefab.Config( |
| 1060 | + key=key, |
| 1061 | + rows=[ |
| 1062 | + default_row, |
| 1063 | + Prefab.ConfigRow( |
| 1064 | + project_env_id=project_env_id, |
| 1065 | + values=[ |
| 1066 | + Prefab.ConditionalValue( |
| 1067 | + criteria=[ |
| 1068 | + Prefab.Criterion( |
| 1069 | + operator=Prefab.Criterion.CriterionOperator.PROP_AFTER, |
| 1070 | + property_name="prefab.current-time", |
| 1071 | + value_to_match=Prefab.ConfigValue(int=past_time_millis) |
| 1072 | + ) |
| 1073 | + ], |
| 1074 | + value=Prefab.ConfigValue(string=desired_value), |
| 1075 | + ) |
| 1076 | + ], |
| 1077 | + ), |
| 1078 | + ], |
| 1079 | + ) |
| 1080 | + |
| 1081 | + with patch('time.time') as mock_time: |
| 1082 | + # Set the mock to return our test time |
| 1083 | + mock_time.return_value = test_time.timestamp() |
| 1084 | + |
| 1085 | + evaluator = CriteriaEvaluator( |
| 1086 | + config, project_env_id, resolver=None, base_client=None |
| 1087 | + ) |
| 1088 | + evaluator_past = CriteriaEvaluator( |
| 1089 | + config_past, project_env_id, resolver=None, base_client=None |
| 1090 | + ) |
| 1091 | + |
| 1092 | + # Test current time is before future time |
| 1093 | + evaluation = evaluator.evaluate(context({})) |
| 1094 | + assert evaluation.raw_config_value().string == desired_value |
| 1095 | + |
| 1096 | + # Test current time is after past time |
| 1097 | + evaluation = evaluator_past.evaluate(context({})) |
| 1098 | + assert evaluation.raw_config_value().string == desired_value |
| 1099 | + |
| 1100 | + # Test with a different time that's after the future time |
| 1101 | + mock_time.return_value = future_time.timestamp() + 3600 # 1 hour after future_time |
| 1102 | + evaluation = evaluator.evaluate(context({})) |
| 1103 | + assert evaluation.raw_config_value().string == default_value |
| 1104 | + |
| 1105 | + # Test with a different time that's before the past time |
| 1106 | + mock_time.return_value = past_time.timestamp() - 3600 # 1 hour before past_time |
| 1107 | + evaluation = evaluator_past.evaluate(context({})) |
| 1108 | + assert evaluation.raw_config_value().string == default_value |
| 1109 | + |
1022 | 1110 | @staticmethod |
1023 | 1111 | def mock_resolver(config): |
1024 | 1112 | return MockResolver(config) |
|
0 commit comments