jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/952553 )
Change subject: [bugfix] Handle missing SDC mediainfo ......................................................................
[bugfix] Handle missing SDC mediainfo
Test if mediainfo exists. Added get_data_for_new_entity() for creating new mediainfo.
Bug: T345038 Change-Id: Ifdd4d707807f2dc6f901f10e3ae40d9537e00fe7 --- M pywikibot/page/_collections.py M pywikibot/page/_wikibase.py M tests/file_tests.py 3 files changed, 71 insertions(+), 1 deletion(-)
Approvals: Matěj Suchánek: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page/_collections.py b/pywikibot/page/_collections.py index a88ccf7..cea1f71 100644 --- a/pywikibot/page/_collections.py +++ b/pywikibot/page/_collections.py @@ -210,6 +210,8 @@ def fromJSON(cls, data, repo): """Construct a new ClaimCollection from JSON.""" this = cls(repo) + if data == []: # workaround for T222159 + return this for key, claims in data.items(): this[key] = [pywikibot.page.Claim.fromJSON(repo, claim) for claim in claims] diff --git a/pywikibot/page/_wikibase.py b/pywikibot/page/_wikibase.py index 959c0b1..5141e00 100644 --- a/pywikibot/page/_wikibase.py +++ b/pywikibot/page/_wikibase.py @@ -408,6 +408,12 @@
return self._file
+ def get_data_for_new_entity(self) -> dict: + """Return data required for creation of a new mediainfo.""" + self.id = 'M' + str(self.file.pageid) + self._content = {} + return super().get() + def get(self, force: bool = False) -> dict: """Fetch all MediaInfo entity data and cache it.
@@ -427,6 +433,8 @@ data = self.file.latest_revision.slots['mediainfo']['*'] except NoPageError as exc: raise NoWikibaseEntityError(self) from exc + except KeyError as exc: + raise NoWikibaseEntityError(self) from exc
self._content = jsonlib.loads(data) self.id = self._content['id'] diff --git a/tests/file_tests.py b/tests/file_tests.py index fb3bdf0..911c595 100755 --- a/tests/file_tests.py +++ b/tests/file_tests.py @@ -17,6 +17,7 @@ NoWikibaseEntityError, PageRelatedError, ) +from pywikibot import pagegenerators from tests import join_images_path from tests.aspects import TestCase
@@ -370,14 +371,61 @@ del item._file self.assertEqual(page, item.file)
- def test_data_item_not_existing(self): + def test_data_item_when_no_file_or_data_item(self): """Test data item associated to file that does not exist.""" page = pywikibot.FilePage(self.site, 'File:Notexisting_Albert Einstein.jpg') + self.assertFalse(page.exists()) item = page.data_item() + self.assertIsInstance(item, pywikibot.MediaInfo) + with self.assertRaises(NoWikibaseEntityError): item.get()
+ def test_data_item_when_file_exist_but_without_item(self): + """Test if data item is missing from file.""" + # Get latest uploaded files. + gen = pagegenerators.RecentChangesPageGenerator( + site=self.site, + namespaces=[6], # File namespace + changetype='new', + total=100 + ) + + # Seek to first page without mediainfo. + for page in gen: + if 'mediainfo' not in page.latest_revision.slots: + item = page.data_item() + self.assertIsInstance(item, pywikibot.MediaInfo) + + # Get fails as there is no mediainfo. + with self.assertRaises(NoWikibaseEntityError): + item.get() + + # Create new empty mediainfo. + item.get_data_for_new_entity() + self.assertIsInstance( + item.labels, pywikibot.page._collections.LanguageDict) + self.assertIsInstance( + item.statements, + pywikibot.page._collections.ClaimCollection) + + # break the loop after checking first file + break + + def test_data_list_to_dict_workaround(self): + """Test that T222159 workaround converts [] to {}.""" + page = pywikibot.FilePage(self.site, 'File:Albert Einstein.jpg') + item = page.data_item() + item.get(force=True) + item._content['labels'] = [] + item._content['statements'] = [] + item.get() + self.assertIsInstance( + item.labels, pywikibot.page._collections.LanguageDict) + self.assertIsInstance( + item.statements, pywikibot.page._collections.ClaimCollection) +
if __name__ == '__main__': # pragma: no cover with suppress(SystemExit):
pywikibot-commits@lists.wikimedia.org