jenkins-bot has submitted this change and it was merged.
Change subject: Tests for ItemPage.iterlinks ......................................................................
Tests for ItemPage.iterlinks
These tests use a fabricated sitelinks file, which makes testing easier as we no longer depend on the actual WikiData data.
ItemPage.get() was adapted: its parent already prevents getting data from WD if _content exists, and its parent needs to parse _content too. It's much easier to mock data when we can just set _content and call .get().
Per Legoktm's comments, WD functionality from page_tests has been moved into this file. For this, assertType(self, obj, cls) had to be moved from TestPageObject to the new superclass PywikibotTestCase.
Change-Id: I5cd0e235db5bb0792d39178ee0c9e097fb053823 --- M pywikibot/page.py M tests/page_tests.py A tests/pages/Q60_only_sitelinks.wd A tests/utils.py A tests/wikibase_tests.py 5 files changed, 124 insertions(+), 39 deletions(-)
Approvals: Legoktm: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py index 63204e9..7956ae1 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -2510,8 +2510,7 @@ force will override caching args are the values of props """ - if force or not hasattr(self, '_content'): - super(ItemPage, self).get(force=force, *args) + super(ItemPage, self).get(force=force, *args)
#claims self.claims = {} diff --git a/tests/page_tests.py b/tests/page_tests.py index 337001a..13289d8 100644 --- a/tests/page_tests.py +++ b/tests/page_tests.py @@ -14,6 +14,8 @@ import pywikibot import pywikibot.page
+from utils import PywikibotTestCase + site = pywikibot.Site('en', 'wikipedia') mainpage = pywikibot.Page(pywikibot.page.Link("Main Page", site)) maintalk = pywikibot.Page(pywikibot.page.Link("Talk:Main Page", site)) @@ -109,11 +111,7 @@ self.assertNotEqual(hash(l1), hash(other))
-class TestPageObject(unittest.TestCase): - def assertType(self, obj, cls): - """Assert that obj is an instance of type cls""" - return self.assertTrue(isinstance(obj, cls)) - +class TestPageObject(PywikibotTestCase): def testGeneral(self): self.assertEqual(str(mainpage), "[[%s:%s]]" % (site.lang, mainpage.title())) @@ -290,38 +288,6 @@ self.assertType(p, pywikibot.Category) for p in mainpage.extlinks(): self.assertType(p, unicode) - - def testWikibase(self): - if not site.has_transcluded_data: - return - repo = site.data_repository() - item = pywikibot.ItemPage.fromPage(mainpage) - self.assertType(item, pywikibot.ItemPage) - self.assertEqual(item.getID(), 'q5296') - self.assertEqual(item.title(), 'Q5296') - self.assertTrue('en' in item.labels) - self.assertEqual(item.labels['en'], 'Main Page') - self.assertTrue('en' in item.aliases) - self.assertTrue('HomePage' in item.aliases['en']) - self.assertEqual(item.namespace(), 0) - item2 = pywikibot.ItemPage(repo, 'q5296') - self.assertEqual(item2.getID(), 'q5296') - self.assertEqual(item.labels['en'], 'Main Page') - prop = pywikibot.PropertyPage(repo, 'Property:P21') - self.assertEqual(prop.getType(), 'wikibase-item') - self.assertEqual(prop.namespace(), 120) - claim = pywikibot.Claim(repo, 'p21') - self.assertRaises(ValueError, claim.setTarget, value="test") - claim.setTarget(pywikibot.ItemPage(repo, 'q1')) - self.assertEqual(claim._formatDataValue(), {'entity-type': 'item', 'numeric-id': 1}) - - # test WikibasePage.__cmp__ - self.assertEqual(pywikibot.ItemPage.fromPage(mainpage), pywikibot.ItemPage(repo, 'q5296')) - - def testItemPageExtensionability(self): - class MyItemPage(pywikibot.ItemPage): - pass - self.assertIsInstance(MyItemPage.fromPage(mainpage), MyItemPage)
# methods that still need tests implemented or expanded:
diff --git a/tests/pages/Q60_only_sitelinks.wd b/tests/pages/Q60_only_sitelinks.wd new file mode 100644 index 0000000..03b15bc --- /dev/null +++ b/tests/pages/Q60_only_sitelinks.wd @@ -0,0 +1,30 @@ +{ + "lastrevid": 66097354, + "modified": "2013-08-17T17:17:36Z", + "ns": 0, + "pageid": 186, + "sitelinks": { + "afwiki": { + "site": "afwiki", + "title": "New York Stad" + }, + "enwiki": { + "site": "enwiki", + "title": "New York City" + }, + "enwikivoyage": { + "site": "enwikivoyage", + "title": "New York City" + }, + "eswiki": { + "site": "eswiki", + "title": "Nueva York" + }, + "eswikivoyage": { + "site": "eswikivoyage", + "title": "Ciudad de Nueva York" + } + }, + "title": "Q60", + "type": "item" +} diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 0000000..77f0d75 --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,6 @@ +import unittest + +class PywikibotTestCase(unittest.TestCase): + def assertType(self, obj, cls): + """Assert that obj is an instance of type cls""" + return self.assertTrue(isinstance(obj, cls)) \ No newline at end of file diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py new file mode 100644 index 0000000..5caaabf --- /dev/null +++ b/tests/wikibase_tests.py @@ -0,0 +1,84 @@ +# -*- coding: utf-8 -*- +""" +Tests for the Wikidata parts of the page module. +""" +# +# (C) Pywikipedia bot team, 2008-2013 +# +# Distributed under the terms of the MIT license. +# +__version__ = '$Id$' + +import os +import unittest +import pywikibot +import json + +from utils import PywikibotTestCase + +site = pywikibot.Site('en', 'wikipedia') +mainpage = pywikibot.Page(pywikibot.page.Link("Main Page", site)) +wikidata = site.data_repository() + +class TestGeneral(PywikibotTestCase): + def testWikibase(self): + if not site.has_transcluded_data: + return + repo = site.data_repository() + item = pywikibot.ItemPage.fromPage(mainpage) + self.assertType(item, pywikibot.ItemPage) + self.assertEqual(item.getID(), 'q5296') + self.assertEqual(item.title(), 'Q5296') + self.assertTrue('en' in item.labels) + self.assertEqual(item.labels['en'], 'Main Page') + self.assertTrue('en' in item.aliases) + self.assertTrue('HomePage' in item.aliases['en']) + self.assertEqual(item.namespace(), 0) + item2 = pywikibot.ItemPage(repo, 'q5296') + self.assertEqual(item2.getID(), 'q5296') + self.assertEqual(item.labels['en'], 'Main Page') + prop = pywikibot.PropertyPage(repo, 'Property:P21') + self.assertEqual(prop.getType(), 'wikibase-item') + self.assertEqual(prop.namespace(), 120) + claim = pywikibot.Claim(repo, 'p21') + self.assertRaises(ValueError, claim.setTarget, value="test") + claim.setTarget(pywikibot.ItemPage(repo, 'q1')) + self.assertEqual(claim._formatDataValue(), {'entity-type': 'item', 'numeric-id': 1}) + + # test WikibasePage.__cmp__ + self.assertEqual(pywikibot.ItemPage.fromPage(mainpage), pywikibot.ItemPage(repo, 'q5296')) + + def testItemPageExtensionability(self): + class MyItemPage(pywikibot.ItemPage): + pass + self.assertIsInstance(MyItemPage.fromPage(mainpage), MyItemPage) + + +class TestLinks(PywikibotTestCase): + """Test cases to test links stored in wikidata""" + def setUp(self): + self.wdp = pywikibot.ItemPage(wikidata, 'Q60') + self.wdp.id = 'q60' + self.wdp._content = json.load(open(os.path.join(os.path.split(__file__)[0], 'pages', 'Q60_only_sitelinks.wd'))) + self.wdp.get() + + def test_iterlinks_page_object(self): + page = [pg for pg in self.wdp.iterlinks() if pg.site.language() == 'af'][0] + self.assertEquals(page, pywikibot.Page(pywikibot.getSite('af', 'wikipedia'), u'New York Stad')) + + def test_iterlinks_filtering(self): + wikilinks = list(self.wdp.iterlinks('wikipedia')) + wvlinks = list(self.wdp.iterlinks('wikivoyage')) + + self.assertEquals(len(wikilinks), 3) + self.assertEquals(len(wvlinks), 2) + + +if __name__ == '__main__': + try: + try: + unittest.main() + except SystemExit: + pass + finally: + pywikibot.stopme()