Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # |
| 4 | # Copyright 2014 Google Inc. All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | |
| 18 | """Discovery document cache tests.""" |
| 19 | |
| 20 | import datetime |
| 21 | import unittest2 as unittest |
| 22 | |
| 23 | import mock |
| 24 | |
| 25 | from googleapiclient.discovery_cache import DISCOVERY_DOC_MAX_AGE |
| 26 | from googleapiclient.discovery_cache.base import Cache |
Jon Wayne Parrott | 36d4e1b | 2016-10-17 13:31:33 -0700 | [diff] [blame] | 27 | |
| 28 | try: |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 29 | from googleapiclient.discovery_cache.file_cache import Cache as FileCache |
Jon Wayne Parrott | 36d4e1b | 2016-10-17 13:31:33 -0700 | [diff] [blame] | 30 | except ImportError: |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 31 | FileCache = None |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 32 | |
| 33 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 34 | @unittest.skipIf(FileCache is None, "FileCache unavailable.") |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 35 | class FileCacheTest(unittest.TestCase): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 36 | @mock.patch( |
| 37 | "googleapiclient.discovery_cache.file_cache.FILENAME", |
| 38 | new="google-api-python-client-discovery-doc-tests.cache", |
| 39 | ) |
| 40 | def test_expiration(self): |
| 41 | def future_now(): |
| 42 | return datetime.datetime.now() + datetime.timedelta( |
| 43 | seconds=DISCOVERY_DOC_MAX_AGE |
| 44 | ) |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 45 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 46 | mocked_datetime = mock.MagicMock() |
| 47 | mocked_datetime.datetime.now.side_effect = future_now |
| 48 | cache = FileCache(max_age=DISCOVERY_DOC_MAX_AGE) |
| 49 | first_url = "url-1" |
| 50 | first_url_content = "url-1-content" |
| 51 | cache.set(first_url, first_url_content) |
Takashi Matsuo | 3012512 | 2015-08-19 11:42:32 -0700 | [diff] [blame] | 52 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 53 | # Make sure the content is cached. |
| 54 | self.assertEqual(first_url_content, cache.get(first_url)) |
| 55 | |
| 56 | # Simulate another `set` call in the future date. |
| 57 | with mock.patch( |
| 58 | "googleapiclient.discovery_cache.file_cache.datetime", new=mocked_datetime |
| 59 | ): |
| 60 | cache.set("url-2", "url-2-content") |
| 61 | |
| 62 | # Make sure the content is expired |
| 63 | self.assertEqual(None, cache.get(first_url)) |