Tobias47n9e has uploaded a new change for review.

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

Change subject: Add tests for Claim fromJSON-methods
......................................................................

Add tests for Claim fromJSON-methods

This patch adds tests to 'page_tests.py' for the Claim class in
'page.py'. The test creates 3 Claim objects using the 'fromJSON',
'referenceFromJSON' and 'qualifierFromJSON' methods. The
objects are created from dictionaries. For each object the tests
make sure that 'claim_obj.isReference' and 'claim_obj.isQualifier'
have the correct True and False value.

The tests were written for 244834 and 239780

Change-Id: I12b4fc688cc38380fef0b20ac44b1a129b649892
---
M tests/page_tests.py
1 file changed, 77 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/41/247041/1

diff --git a/tests/page_tests.py b/tests/page_tests.py
index cf25ff0..c3fb74e 100644
--- a/tests/page_tests.py
+++ b/tests/page_tests.py
@@ -24,7 +24,7 @@
 
 from tests.aspects import (
     unittest, TestCase, DefaultSiteTestCase, SiteAttributeTestCase,
-    DefaultDrySiteTestCase, DeprecationTestCase,
+    DefaultDrySiteTestCase, DeprecationTestCase, WikidataTestCase,
 )
 
 
@@ -963,6 +963,82 @@
         self.assertEqual(pywikibot.page.html2unicode('&#7f'), '&#7f')
         self.assertEqual(pywikibot.page.html2unicode('&#x70y'), '&#x70y')
 
+class TestFromJsonMethods(WikidataTestCase):
+
+    """
+    Test the fromJSON methods for claim, qualifier, reference
+
+    The tests make sure that the objects created from JSON are correctly
+    classified as claim (isReference=False, isQualifier=False), as
+    qualifier and as reference.
+    """
+
+    sites = {
+        'wikidata': {
+            'family': 'wikidata',
+            'code': 'wikidata',
+        }
+    }
+
+    def test_reference_from_json(self):
+        """
+        Create reference from JSON. Check isReference, isQualifier are correct.
+        """
+        repo = self.get_repo()
+        ref_json = {'hash': '123',
+                    'property': 'P31',
+                    'datavalue': {'type': 'wikibase-entityid',
+                    'value': {'numeric-id': 5, 'entity-type': 'item'}},
+                    'snaktype': 'value',
+                    'datatype': 'wikibase-item',
+                    'snaks': {'P31': [{'property': 'P31',
+                                       'datavalue': {'type': 
'wikibase-entityid',
+                                       'value': {'numeric-id': 5,
+                                       'entity-type': 'item'}},
+                                       'snaktype': 'value',
+                                       'datatype': 'wikibase-item'}]}}
+        source_claim_dict = pywikibot.Claim.referenceFromJSON(repo, ref_json)
+        self.assertEqual(source_claim_dict["P31"][0].isReference, True)
+        self.assertEqual(source_claim_dict["P31"][0].isQualifier, False)
+
+    def test_claim_from_json(self):
+        """
+        Create claim from JSON. Check isReference, isQualifier are correct.
+        """
+        repo = self.get_repo()
+        claim_json = {'type': 'statement',
+                      'mainsnak': {'snaktype': 'value',
+                                   'datavalue': {'type': 'wikibase-entityid',
+                                                 'value': {'numeric-id': 5,
+                                                           'entity-type': 
'item'}},
+                                   'datatype': 'wikibase-item',
+                                   'property': 'P31'},
+                      'rank': 'normal'}
+        claim_obj = pywikibot.Claim.fromJSON(repo, claim_json)
+        self.assertEqual(claim_obj.isReference, False)
+        self.assertEqual(claim_obj.isQualifier, False)
+
+    def test_qualifier_from_json(self):
+        """
+        Create qualifier from JSON. Check isReference, isQualifier are correct.
+        """
+        repo = self.get_repo()
+        qual_json = {'hash': '123',
+                     'property': 'P31',
+                     'datavalue': {'type': 'wikibase-entityid',
+                     'value': {'numeric-id': 5, 'entity-type': 'item'}},
+                     'snaktype': 'value',
+                     'datatype': 'wikibase-item',
+                     'snaks': {'P31': [{'property': 'P31',
+                                       'datavalue': {'type': 
'wikibase-entityid',
+                                       'value': {'numeric-id': 5,
+                                       'entity-type': 'item'}},
+                                       'snaktype': 'value',
+                                       'datatype': 'wikibase-item'}]}}
+        qual_from_json = pywikibot.Claim.qualifierFromJSON(repo, qual_json)
+        self.assertEqual(qual_from_json.isReference, False)
+        self.assertEqual(qual_from_json.isQualifier, True)
+
 
 if __name__ == '__main__':
     try:

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

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

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

Reply via email to