def test_error_extraction(self): gw2api.set_session(requests.Session()) with self.assertRaises(requests.HTTPError) as context: gw2api.util.get_cached("unknown") self.assertIn("404 Client Error", str(context.exception)) with self.assertRaises(requests.HTTPError) as context: gw2api.util.get_cached("map_floor.json") self.assertIn("missing continent_id or floor", str(context.exception))
def test_cache(self): class CacheMockSession(MockSession): def __init__(self): super(CacheMockSession, self).__init__() self.get_called = 0 def get(self, url, **kwargs): self.get_called += 1 return super(CacheMockSession, self).get(url, **kwargs) session = CacheMockSession() session.add_mock_response("get", gw2api.BASE_URL + "test.json", "{\"foo\": \"bar\"}") cache_dir = tempfile.mkdtemp() cache_file = os.path.join(cache_dir, "test.json") try: gw2api.set_cache_dir(cache_dir) gw2api.set_cache_time(3600) gw2api.set_session(session) self.assertFalse(os.path.exists(cache_file), "cache file exists before request") # Call the webservice, test if the cache file is created. response = gw2api.util.get_cached("test.json") self.assertEqual(response, {"foo": "bar"}, "invalid response") self.assertEqual(session.get_called, 1, "invalid request count") self.assertTrue(os.path.exists(cache_file), "cache file does not exist") # Call the webservice again, test that the cache is used instead. response = gw2api.util.get_cached("test.json") self.assertEqual(response, {"foo": "bar"}, "invalid response") self.assertEqual(session.get_called, 1, "invalid request count") # Temporarily disable, test that the service is called. response = gw2api.util.get_cached("test.json", False) self.assertEqual(response, {"foo": "bar"}, "invalid response") self.assertEqual(session.get_called, 2, "invalid request count") finally: if os.path.exists(cache_file): os.unlink(cache_file) os.rmdir(cache_dir) gw2api.set_cache_dir(None)
def test_cache(self): class CacheMockSession(MockSession): def __init__(self): super(CacheMockSession, self).__init__() self.get_called = 0 def get(self, url, **kwargs): self.get_called += 1 return super(CacheMockSession, self).get(url, **kwargs) saved_session = gw2api.session quaggans = ["404", "aloha", "attack", "bear"] session = CacheMockSession() session.add_mock_response("get", gw2api.v2.BASE_URL + "quaggans", json.dumps(quaggans)) for name in quaggans: url = gw2api.v2.BASE_URL + "quaggans/" + name response = { "id": name, "url": "https://static.staticwars.com/quaggans/%s.jpg" % name } session.add_mock_response("get", url, json.dumps(response)) cache_dir = tempfile.mkdtemp() def exists(filename): return os.path.exists(os.path.join(cache_dir, filename)) gw2api.set_cache_dir(None) self.assertFalse(gw2api.v2.quaggans.has_cached("quaggans.json"), "endpoint reports cache exist") try: gw2api.set_cache_dir(cache_dir) gw2api.set_cache_time(3600) gw2api.set_session(session) self.assertFalse(exists("quaggans.json"), "cache file exists before request") self.assertFalse(gw2api.v2.quaggans.has_cached("quaggans.json"), "endpoint reports cache exist before request") # Call the webservice, test if the cache file is created. response = gw2api.v2.quaggans.get_ids() self.assertEqual(response, quaggans, "invalid response") self.assertEqual(session.get_called, 1, "invalid request count") self.assertTrue(exists("quaggans.json"), "cache file does not exist") self.assertTrue(gw2api.v2.quaggans.has_cached("quaggans.json"), "endpoint reports cache does not exist") # Call the webservice again, test that the cache is used instead. response = gw2api.v2.quaggans.get_ids() self.assertEqual(response, quaggans, "invalid response") self.assertEqual(session.get_called, 1, "invalid request count") finally: shutil.rmtree(cache_dir, ignore_errors=True) gw2api.set_cache_dir(None) gw2api.set_session(saved_session)