jenkins-bot has submitted this change and it was merged.

Change subject: Fix ImagePage exceptions and add file tests
......................................................................


Fix ImagePage exceptions and add file tests

- Site.loadimageinfo produced KeyError on missing pages
- ImagePage.fileIsShared produced KeyError on missing pages
- Add tests to cover files, especially the different scenanios
  of local and shared file pages.

Change-Id: I7d6516aa97b71ba4b2a7a9e5f9347989ed1dd609
---
M pywikibot/page.py
M pywikibot/site.py
A tests/file_tests.py
3 files changed, 103 insertions(+), 0 deletions(-)

Approvals:
  John Vandenberg: Looks good to me, but someone else must approve
  Ladsgroup: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/page.py b/pywikibot/page.py
index aface5b..d35de4f 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -1826,6 +1826,8 @@
         # TODO: put the URLs to family file
         if not self.site.has_image_repository:
             return False
+        elif not self.exists():
+            raise pywikibot.NoPage(self)
         elif 'wikitravel_shared' in self.site.shared_image_repository():
             return self.fileUrl().startswith(
                 u'http://wikitravel.org/upload/shared/')
diff --git a/pywikibot/site.py b/pywikibot/site.py
index 47d170d..16415fb 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -1702,6 +1702,12 @@
                     u"loadimageinfo: Query on %s returned data on '%s'"
                     % (page, pageitem['title']))
             api.update_page(page, pageitem)
+            if "missing" in pageitem:
+                raise pywikibot.NoPage(page)
+            if "imageinfo" not in pageitem:
+                raise Error(
+                    u"loadimageinfo: Query on %s returned no imageinfo"
+                    % page)
             return (pageitem['imageinfo']
                     if history else pageitem['imageinfo'][0])
 
diff --git a/tests/file_tests.py b/tests/file_tests.py
new file mode 100644
index 0000000..ae91d12
--- /dev/null
+++ b/tests/file_tests.py
@@ -0,0 +1,95 @@
+# -*- coding: utf-8  -*-
+#
+# (C) Pywikibot team, 2014
+#
+# Distributed under the terms of the MIT license.
+#
+__version__ = '$Id$'
+
+import pywikibot
+
+from tests.utils import unittest, SiteTestCase
+
+commons = pywikibot.Site('commons', 'commons')
+
+
+class TestShareFiles(SiteTestCase):
+
+    def testSharedOnly(self):
+        title = 'File:Sepp Maier 1.JPG'
+
+        itwp = pywikibot.Site('it', 'wikipedia')
+        itwp_file = pywikibot.ImagePage(itwp, title)
+        for using in itwp_file.usingPages():
+            self.assertType(using, pywikibot.Page)
+
+        commons_file = pywikibot.ImagePage(commons, title)
+
+        self.assertFalse(itwp_file.exists())
+        self.assertTrue(commons_file.exists())
+
+        self.assertRaises(pywikibot.NoPage, itwp_file.fileIsShared)
+        self.assertTrue(commons_file.fileIsShared())
+        self.assertTrue(commons_file.fileUrl())
+
+        self.assertRaises(pywikibot.NoPage, itwp_file.fileUrl)
+        self.assertRaises(pywikibot.NoPage, itwp_file.get)
+
+    def testLocalOnly(self):
+        title = 'File:April Fools Day Adminship discussion (2005).png'
+
+        enwp = pywikibot.Site('en', 'wikipedia')
+        enwp_file = pywikibot.ImagePage(enwp, title)
+        for using in enwp_file.usingPages():
+            self.assertType(using, pywikibot.Page)
+
+        commons_file = pywikibot.ImagePage(commons, title)
+
+        self.assertTrue(enwp_file.fileUrl())
+        self.assertTrue(enwp_file.exists())
+        self.assertFalse(commons_file.exists())
+
+        self.assertFalse(enwp_file.fileIsShared())
+        self.assertRaises(pywikibot.NoPage, commons_file.fileIsShared)
+
+        self.assertRaises(pywikibot.NoPage, commons_file.fileUrl)
+        self.assertRaises(pywikibot.NoPage, commons_file.get)
+
+    def testOnBoth(self):
+        title = 'File:Pulsante spam.png'
+
+        itwp = pywikibot.Site('it', 'wikipedia')
+        itwp_file = pywikibot.ImagePage(itwp, title)
+        for using in itwp_file.usingPages():
+            self.assertType(using, pywikibot.Page)
+
+        commons_file = pywikibot.ImagePage(commons, title)
+
+        self.assertTrue(itwp_file.fileUrl())
+        self.assertTrue(itwp_file.exists())
+        self.assertTrue(commons_file.exists())
+
+        self.assertFalse(itwp_file.fileIsShared())
+        self.assertTrue(commons_file.fileIsShared())
+
+    def testNonFileLocal(self):
+        """Test file page, without local file, existing on the local wiki."""
+        title = 'File:Sepp Maier 1.JPG'
+
+        testwp = pywikibot.Site('test', 'wikipedia')
+        testwp_file = pywikibot.ImagePage(testwp, title)
+
+        self.assertTrue(testwp_file.fileUrl())
+        self.assertTrue(testwp_file.exists())
+        self.assertTrue(testwp_file.fileIsShared())
+
+        commons_file = pywikibot.ImagePage(commons, title)
+        self.assertEqual(testwp_file.fileUrl(),
+                         commons_file.fileUrl())
+
+
+if __name__ == '__main__':
+    try:
+        unittest.main()
+    except SystemExit:
+        pass

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I7d6516aa97b71ba4b2a7a9e5f9347989ed1dd609
Gerrit-PatchSet: 5
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <jay...@gmail.com>
Gerrit-Reviewer: John Vandenberg <jay...@gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgr...@gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhall...@arctus.nl>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
Pywikibot-commits mailing list
Pywikibot-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits

Reply via email to