John Vandenberg has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/166367

Change subject: Fix CachedRequest not loading cached requests
......................................................................

Fix CachedRequest not loading cached requests

Use unicode for Request params created a new method _add_defaults.
    954825e

It needs to be called before trying to load a cache entry, otherwise
it attempts to load the wrong key.

Change-Id: Ibfdbebd3e903c7d8405bc7591e37aa4167ab0f90
---
M pywikibot/data/api.py
M tests/api_tests.py
2 files changed, 45 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/67/166367/1

diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py
index d3625ee..8fc41ad 100644
--- a/pywikibot/data/api.py
+++ b/pywikibot/data/api.py
@@ -737,6 +737,7 @@
 
     def _load_cache(self):
         """Return whether the cache can be used."""
+        self._add_defaults()
         try:
             with open(self._cachefile_path(), 'rb') as f:
                 uniquedescr, self._data, self._cachetime = pickle.load(f)
diff --git a/tests/api_tests.py b/tests/api_tests.py
index 9f00a2b..b1a1b82 100644
--- a/tests/api_tests.py
+++ b/tests/api_tests.py
@@ -131,22 +131,59 @@
 
     cached = False
 
-    def testResults(self):
+    def test_normal_use(self):
         mysite = self.get_site()
         mainpage = self.get_mainpage()
-        # Run the cached query twice to ensure the
-        # data returned is equal
+        # Run the cached query three times to ensure the
+        # data returned is equal, and the last two have
+        # the same cache time.
         params = {'action': 'query',
                   'prop': 'info',
                   'titles': mainpage.title(),
                   }
-        req = api.CachedRequest(datetime.timedelta(minutes=10),
-                                site=mysite, **params)
-        data = req.submit()
+        req1 = api.CachedRequest(datetime.timedelta(minutes=10),
+                                 site=mysite, **params)
+        data1 = req1.submit()
         req2 = api.CachedRequest(datetime.timedelta(minutes=10),
                                  site=mysite, **params)
         data2 = req2.submit()
-        self.assertEqual(data, data2)
+        req3 = api.CachedRequest(datetime.timedelta(minutes=10),
+                                 site=mysite, **params)
+        data3 = req3.submit()
+        self.assertEqual(data1, data2, data3)
+        self.assertIsNotNone(req2._cachetime)
+        self.assertIsNotNone(req3._cachetime)
+        self.assertEqual(req2._cachetime, req3._cachetime)
+
+    def test_internals(self):
+        mysite = self.get_site()
+        # Run tests on a missing page unique to this test run so it can
+        # not be cached the first request, but will be cached after.
+        now = datetime.datetime.now()
+        params = {'action': 'query',
+                  'prop': 'info',
+                  'titles': 'TestCachedRequest_test_internals ' + str(now),
+                  }
+        req = api.CachedRequest(datetime.timedelta(minutes=10),
+                                site=mysite, **params)
+        rv = req._load_cache()
+        self.assertFalse(rv)
+        self.assertIsNone(req._data)
+        self.assertIsNone(req._cachetime)
+
+        data = req.submit()
+
+        self.assertIsNotNone(req._data)
+        self.assertIsNone(req._cachetime)
+
+        rv = req._load_cache()
+
+        self.assertTrue(rv)
+        self.assertIsNotNone(req._data)
+        self.assertIsNotNone(req._cachetime)
+        self.assertGreater(req._cachetime, now)
+        self.assertEqual(req._data, data)
+
 
 if __name__ == '__main__':
     try:

-- 
To view, visit https://gerrit.wikimedia.org/r/166367
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibfdbebd3e903c7d8405bc7591e37aa4167ab0f90
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to