|
1 | 1 | from __future__ import with_statement |
2 | | -from etsy._core import API, MethodTableCache |
| 2 | +from etsy._core import API, MethodTableCache, missing |
3 | 3 | from cgi import parse_qs |
4 | 4 | from urlparse import urlparse |
5 | 5 | import os |
6 | 6 | from util import Test |
| 7 | +import tempfile |
7 | 8 |
|
8 | 9 |
|
9 | 10 |
|
10 | 11 | class MockAPI(API): |
11 | 12 | api_url = 'http://host' |
12 | 13 | api_version = 'v1' |
13 | 14 |
|
14 | | - def _get_method_table(self, *args): |
| 15 | + |
| 16 | + def etsy_home(self): |
| 17 | + return Test.scratch_dir |
| 18 | + |
| 19 | + |
| 20 | + def get_method_table(self, *args): |
15 | 21 | return [{'name': 'testMethod', |
16 | 22 | 'uri': '/test/{test_id}', |
17 | 23 | 'http_method': 'GET', |
@@ -210,30 +216,110 @@ def test_positional_argument_duplicated_in_kwargs(self): |
210 | 216 | self.assertEqual('Positional argument duplicated in kwargs: test_id', |
211 | 217 | msg) |
212 | 218 |
|
213 | | - |
| 219 | + |
214 | 220 | def test_api_key_and_key_file_both_passed(self): |
215 | | - pass |
| 221 | + msg = self.assertRaises(AssertionError, MockAPI, |
| 222 | + api_key='x', key_file='y') |
| 223 | + self.assertEqual('Keys can be read from a file or passed, but not both.', |
| 224 | + msg) |
| 225 | + |
216 | 226 |
|
217 | 227 |
|
218 | | - def test_methodcache_uses_etsy_home_if_exists(self): |
| 228 | +class MockAPI_NoMethods(MockAPI): |
| 229 | + def _get_methods(self, method_cache): |
219 | 230 | pass |
220 | 231 |
|
221 | 232 |
|
222 | | - def test_methodcache_uses_temp_dir_if_no_etsy_home(self): |
223 | | - pass |
224 | 233 |
|
| 234 | +class MethodTableCacheTests(Test): |
| 235 | + |
| 236 | + |
| 237 | + def cache(self, method_cache=missing): |
| 238 | + self.api = MockAPI_NoMethods('apikey') |
| 239 | + self._cache = MethodTableCache(self.api, method_cache) |
| 240 | + return self._cache |
| 241 | + |
| 242 | + |
| 243 | + def test_uses_etsy_home_if_exists(self): |
| 244 | + c = self.cache() |
| 245 | + self.assertEqual(os.path.dirname(c.filename), self.scratch_dir) |
| 246 | + |
| 247 | + |
| 248 | + def test_uses_temp_dir_if_no_etsy_home(self): |
| 249 | + self.delete_scratch() |
| 250 | + c = self.cache() |
| 251 | + self.assertEqual(os.path.dirname(c.filename), tempfile.gettempdir()) |
| 252 | + |
| 253 | + |
| 254 | + def test_uses_provided_file(self): |
| 255 | + fn = os.path.join(self.scratch_dir, 'foo.json') |
| 256 | + self.assertEqual(self.cache(method_cache=fn).filename, fn) |
| 257 | + |
| 258 | + |
| 259 | + def test_multiple_versions(self): |
| 260 | + c = self.cache() |
| 261 | + |
| 262 | + class MockAPI2(MockAPI): |
| 263 | + api_version = 'v3' |
| 264 | + |
| 265 | + self.assertNotEqual(MockAPI2('key').method_cache.filename, c.filename) |
| 266 | + |
| 267 | + |
| 268 | + def get_uncached(self): |
| 269 | + c = self.cache() |
| 270 | + return c.get() |
225 | 271 |
|
226 | | - def test_none_passed_does_not_cache(self): |
227 | | - pass |
228 | 272 |
|
| 273 | + def test_no_cache_file_returns_results(self): |
| 274 | + self.assertEqual(2, len(self.get_uncached())) |
229 | 275 |
|
230 | | - def test_methodcache_expired(self): |
231 | | - pass |
| 276 | + |
| 277 | + def test_no_cache_file_writes_cache(self): |
| 278 | + self.get_uncached() |
| 279 | + self.assertTrue(self._cache.wrote_cache) |
232 | 280 |
|
233 | 281 |
|
234 | | - def test_methodcache_caches_result(self): |
235 | | - pass |
| 282 | + def test_no_cache_file(self): |
| 283 | + self.get_uncached() |
| 284 | + self.assertFalse(self._cache.used_cache) |
| 285 | + |
| 286 | + |
| 287 | + def get_cached(self): |
| 288 | + c = self.cache() |
| 289 | + c.get() |
| 290 | + c = self.cache() |
| 291 | + return c.get() |
| 292 | + |
| 293 | + |
| 294 | + def test_caching(self): |
| 295 | + self.get_cached() |
| 296 | + self.assertTrue(self._cache.used_cache) |
236 | 297 |
|
| 298 | + |
| 299 | + def test_caching_returns_results(self): |
| 300 | + self.assertEqual(2, len(self.get_cached())) |
| 301 | + |
| 302 | + |
| 303 | + def test_caching_doesnt_overwrite_cache(self): |
| 304 | + self.get_cached() |
| 305 | + self.assertFalse(self._cache.wrote_cache) |
| 306 | + |
| 307 | + |
| 308 | + def test_expired(self): |
| 309 | + self.get_cached() |
| 310 | + fn = self._cache.filename |
| 311 | + s = os.stat(fn) |
| 312 | + os.utime(fn, (s.st_atime, s.st_mtime - 48*60*60)) |
| 313 | + c = self.cache() |
| 314 | + c.get() |
| 315 | + self.assertFalse(c.used_cache) |
| 316 | + |
237 | 317 |
|
238 | | - def test_etsy_home_exists_file_doesnt(self): |
239 | | - pass |
| 318 | + def test_none_passed_does_not_cache(self): |
| 319 | + self.get_cached() |
| 320 | + c = self.cache(method_cache=None) |
| 321 | + c.get() |
| 322 | + self.assertFalse(c.used_cache) |
| 323 | + |
| 324 | + |
| 325 | + |
0 commit comments