|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Unit tests for the runtime module.""" |
| 16 | + |
| 17 | +import time |
| 18 | +import uuid |
| 19 | +import unittest |
| 20 | +from unittest.mock import MagicMock, patch |
| 21 | + |
| 22 | +from google.adk import runtime |
| 23 | + |
| 24 | + |
| 25 | +class TestRuntime(unittest.TestCase): |
| 26 | + |
| 27 | + def tearDown(self): |
| 28 | + # Reset providers to default after each test |
| 29 | + runtime.set_time_provider(time.time) |
| 30 | + runtime.set_id_provider(lambda: str(uuid.uuid4())) |
| 31 | + |
| 32 | + def test_default_time_provider(self): |
| 33 | + # Verify it returns a float that is close to now |
| 34 | + now = time.time() |
| 35 | + rt_time = runtime.get_time() |
| 36 | + self.assertIsInstance(rt_time, float) |
| 37 | + self.assertAlmostEqual(rt_time, now, delta=1.0) |
| 38 | + |
| 39 | + def test_default_id_provider(self): |
| 40 | + # Verify it returns a string uuid |
| 41 | + uid = runtime.new_uuid() |
| 42 | + self.assertIsInstance(uid, str) |
| 43 | + # Should be parseable as uuid |
| 44 | + uuid.UUID(uid) |
| 45 | + |
| 46 | + def test_custom_time_provider(self): |
| 47 | + # Test override |
| 48 | + mock_time = 123456789.0 |
| 49 | + runtime.set_time_provider(lambda: mock_time) |
| 50 | + self.assertEqual(runtime.get_time(), mock_time) |
| 51 | + |
| 52 | + def test_custom_id_provider(self): |
| 53 | + # Test override |
| 54 | + mock_id = "test-id-123" |
| 55 | + runtime.set_id_provider(lambda: mock_id) |
| 56 | + self.assertEqual(runtime.new_uuid(), mock_id) |
0 commit comments