jenkins-bot has submitted this change and it was merged.
Change subject: Add support for math datatype ......................................................................
Add support for math datatype
Also changes test_WbMonolingualText_edit and adds testing for WbMonolingualText to wikibase_tests.py.
Note that math has not been added to wikibase_tests.py since it is not yet activated on wikidata.
Change-Id: Ia7345072a1be00920dac37edd15204137222e6a8 --- M pywikibot/page.py M tests/edit_failure_tests.py M tests/wikibase_edit_tests.py M tests/wikibase_tests.py 4 files changed, 67 insertions(+), 8 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py index 82a9028..244c300 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -4079,12 +4079,14 @@ 'time': pywikibot.WbTime, 'quantity': pywikibot.WbQuantity, 'monolingualtext': pywikibot.WbMonolingualText, + 'math': basestring, }
value_types = {'wikibase-item': 'wikibase-entityid', 'commonsMedia': 'string', 'url': 'string', 'globe-coordinate': 'globecoordinate', + 'math': 'string', }
def __init__(self, site, id=None, datatype=None): @@ -4626,7 +4628,7 @@ if self.type == 'wikibase-item': value = {'entity-type': 'item', 'numeric-id': self.getTarget().getID(numeric=True)} - elif self.type in ('string', 'url'): + elif self.type in ('string', 'url', 'math'): value = self.getTarget() elif self.type == 'commonsMedia': value = self.getTarget().title(withNamespace=False) diff --git a/tests/edit_failure_tests.py b/tests/edit_failure_tests.py index 395a516..811f303 100644 --- a/tests/edit_failure_tests.py +++ b/tests/edit_failure_tests.py @@ -151,6 +151,14 @@ claim = self._make_WbMonolingualText_claim(repo, text=123456, language='en') self.assertAPIError('invalid-snak', None, item.addClaim, claim)
+ def test_math_invalid_function(self): + """Attempt adding invalid latex to a math claim.""" + repo = self.get_repo() + item = pywikibot.ItemPage(repo, 'Q68') + claim = pywikibot.page.Claim(repo, 'P717', datatype='math') + claim.setTarget('\foo') + self.assertAPIError('modification-failed', None, item.addClaim, claim) +
if __name__ == '__main__': try: diff --git a/tests/wikibase_edit_tests.py b/tests/wikibase_edit_tests.py index a350cf7..18f74a4 100644 --- a/tests/wikibase_edit_tests.py +++ b/tests/wikibase_edit_tests.py @@ -177,9 +177,9 @@ self.assertEqual(new_item.getRedirectTarget(), target_item)
-class TestWbMonolingualText(WikibaseTestCase): +class TestWikibaseMakeClaim(WikibaseTestCase):
- """Run wikibase write tests for WbMonolingualText.""" + """Run wikibase write tests for claims."""
family = 'wikidata' code = 'test' @@ -187,15 +187,45 @@ user = True write = True
+ def _clean_item(self, repo, prop): + """ + Return an item without any existing claims of the given property. + + @param repo: repository to fetch item from + @type: pywikibot.site.DataSite + @param prop: P-value of the property to scrub + @type prop: str + @return: scrubbed item + @rtype: pywikibot.ItemPage + """ + item = pywikibot.ItemPage(repo, 'Q68') + item.get() + if prop in item.claims: + item.removeClaims(item.claims[prop]) + item.get(force=True) + return item + + def test_math_edit(self): + """Attempt adding a math claim with valid input.""" + testsite = self.get_repo() + item = self._clean_item(testsite, 'P717') + + # set new claim + claim = pywikibot.page.Claim(testsite, 'P717', datatype='math') + target = 'a^2 + b^2 = c^2' + claim.setTarget(target) + item.addClaim(claim) + + # confirm new claim + item.get(force=True) + claim = item.claims['P717'][0] + self.assertEqual(claim.getTarget(), target) + def test_WbMonolingualText_edit(self): """Attempt adding a monolingual text with valid input.""" # Clean the slate in preparation for test.""" testsite = self.get_repo() - item = pywikibot.ItemPage(testsite, 'Q68') - item.get() - if 'P271' in item.claims: - item.removeClaims(item.claims['P271']) - item.get(force=True) + item = self._clean_item(testsite, 'P271')
# set new claim claim = pywikibot.page.Claim(testsite, 'P271', datatype='monolingualtext') @@ -203,6 +233,11 @@ claim.setTarget(target) item.addClaim(claim)
+ # confirm new claim + item.get(force=True) + claim = item.claims['P271'][0] + self.assertEqual(claim.getTarget(), target) +
class TestWikibaseRemoveQualifier(WikibaseTestCase):
diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py index 3d844b7..4ae1542 100644 --- a/tests/wikibase_tests.py +++ b/tests/wikibase_tests.py @@ -645,13 +645,24 @@ """Test setting claim values."""
def test_set_website(self): + """Test setting claim of url type.""" wikidata = self.get_repo() claim = pywikibot.Claim(wikidata, 'P856') self.assertEqual(claim.type, 'url') claim.setTarget('https://en.wikipedia.org/') self.assertEqual(claim.target, 'https://en.wikipedia.org/')
+ def test_set_WbMonolingualText(self): + """Test setting claim of monolingualtext type.""" + wikidata = self.get_repo() + claim = pywikibot.Claim(wikidata, 'P1450') + self.assertEqual(claim.type, 'monolingualtext') + target = pywikibot.WbMonolingualText(text='Test this!', language='en') + claim.setTarget(target) + self.assertEqual(claim.target, target) + def test_set_date(self): + """Test setting claim of time type.""" wikidata = self.get_repo() claim = pywikibot.Claim(wikidata, 'P569') self.assertEqual(claim.type, 'time') @@ -661,11 +672,14 @@ self.assertEqual(claim.target.day, 1)
def test_set_incorrect_target_value(self): + """Test setting claim of the incorrect value.""" wikidata = self.get_repo() claim = pywikibot.Claim(wikidata, 'P569') self.assertRaises(ValueError, claim.setTarget, 'foo') claim = pywikibot.Claim(wikidata, 'P856') self.assertRaises(ValueError, claim.setTarget, pywikibot.WbTime(2001, site=wikidata)) + claim = pywikibot.Claim(wikidata, 'P1450') + self.assertRaises(ValueError, claim.setTarget, 'foo')
class TestItemBasePageMethods(WikidataTestCase, BasePageMethodsTestBase):
pywikibot-commits@lists.wikimedia.org