def setUp(self): super(TestAddNewClaim, self).setUp() # mock all writing calls add_qualifier_patcher = mock.patch( 'wikidatastuff.wikidata_stuff.WikidataStuff.add_qualifier') add_reference_patcher = mock.patch( 'wikidatastuff.wikidata_stuff.WikidataStuff.add_reference') add_claim_patcher = mock.patch( 'wikidatastuff.wikidata_stuff.pywikibot.ItemPage.addClaim') self.mock_add_qualifier = add_qualifier_patcher.start() self.mock_add_reference = add_reference_patcher.start() self.mock_add_claim = add_claim_patcher.start() self.addCleanup(add_qualifier_patcher.stop) self.addCleanup(add_reference_patcher.stop) self.addCleanup(add_claim_patcher.stop) # defaults self.ref = None self.prop = 'P509' # an unused property of type string self.value = 'A statement' self.qual_1 = Qualifier('P174', 'A qualifier') self.qual_2 = Qualifier('P664', 'Another qualifier') self.mock_ref_1 = mock.create_autospec(Reference) self.mock_ref_2 = mock.create_autospec(Reference)
def add_start_end_qualifiers(statement, start_val, end_val): """ Add start/end qualifiers to a statement if non-None, or return None. @param statement: The statement to decorate @type statement: Statement @param start_val: An ISO date string for the starting point @type start_val: basestring or None @param end_val: An ISO date string for the end point @type end_val: basestring or None @return: A statement decorated with start/end qualifiers @rtype: Statement, or None """ if not isinstance(statement, Statement): raise pywikibot.Error('Non-statement recieved: {}'.format(statement)) if statement.is_none(): return None # add qualifiers quals = [] if start_val: quals.append( Qualifier(prop=helpers.START_P, itis=helpers.iso_to_WbTime(start_val))) if end_val: quals.append( Qualifier(prop=helpers.END_P, itis=helpers.iso_to_WbTime(end_val))) for q in quals: statement.add_qualifier(q) return statement
def test_qualifier_hash(self): q_1 = Qualifier('P123', 'foo') q_2 = Qualifier('P124', 'foo') q_3 = Qualifier('P123', 'bar') q_same = Qualifier('P123', 'foo') self.assertEqual(q_1, q_same) self.assertNotEqual(q_1, q_2) self.assertNotEqual(q_1, q_3) self.assertNotEqual(q_2, q_3)
def setUp(self): wikidata = Site('test', 'wikidata') self.q_1 = Qualifier('P123', 'foo') self.q_2 = Qualifier('P123', 'bar') claim = Claim(wikidata, 'P55') claim.setTarget('foo') self.ref = Reference(source_test=[ claim, ])
def setUp(self): super(TestHasAllQualifiers, self).setUp() self.quals = [] # load claims self.claim_no_qual = self.wd_page.claims['P174'][2] # two qualifiers: P174:A qualifier, P664:Another qualifier self.claim = self.wd_page.claims['P174'][4] # load qualifiers self.qual_1 = Qualifier('P174', 'A qualifier') self.qual_2 = Qualifier('P664', 'Another qualifier') self.unmatched = Qualifier('P174', 'Unmatched')
def test_qualifier_equality(self): q = Qualifier('P123', 'foo') q_same = Qualifier('P123', 'foo') q_different = Qualifier('P123', 'bar') self.assertTrue(q == q_same) self.assertFalse(q != q_same) self.assertTrue(q == q) self.assertFalse(q != q) self.assertFalse(q == q_different) self.assertTrue(q != q_different) # Comparison with other classes always gives false, weird but expected self.assertFalse(q == 'foo') self.assertFalse(q != 'foo')
def setUp(self): super(TestHasQualifier, self).setUp() self.claim_no_qual = self.wd_page.claims['P174'][2] # one qualifier: P174:A qualifier self.claim_one_qual = self.wd_page.claims['P174'][0] # two qualifiers: P174:A qualifier, P664:Another qualifier self.claim_two_quals_diff_p = self.wd_page.claims['P174'][4] # two qualifiers: P174:A qualifier, P174:Another qualifier self.claim_two_quals_same_p = self.wd_page.claims['P174'][5] # load three claims to use when making references self.qual_1 = Qualifier('P174', 'A qualifier') self.qual_2 = Qualifier('P664', 'Another qualifier') self.qual_3 = Qualifier('P174', 'Another qualifier') self.unmatched_val = Qualifier('P174', 'Unmatched') self.unmatched_p = Qualifier('P0', 'A qualifier')
def setUp(self): super(TestMatchClaim, self).setUp() # defaults self.claims = [] self.qualifiers = [] self.force = False # Load claims to descriptive variables # note that only qualifiers + references matter, main value is ignored # the default qualifier in these is P174:"A qualifier" self.one_qual_no_ref = self.wd_page.claims['P174'][0] self.no_qual_two_ref = self.wd_page.claims['P174'][1] self.no_qual_no_ref = self.wd_page.claims['P174'][2] # Second qualifier P174:"Another qualifier" self.two_qual_no_ref = self.wd_page.claims['P174'][5] # load two qualifier self.matched_qualifier = Qualifier('P174', 'A qualifier') self.unmatched_qualifier = Qualifier('P174', 'Unmatched')
def test_format_protoclaims_single_with_multiple_qual(self): itis = Statement('dummy') qual_1 = Qualifier('P321', 'qual_dummy') qual_2 = Qualifier('P213', 'qual_dummy') itis._quals.add(qual_1) itis._quals.add(qual_2) self.preview_item.protoclaims = {'P123': itis} expected = ("{| class='wikitable'\n" "|-\n" "! Property\n" "! Value\n" "! Qualifiers\n" '|-\n' '| wd_template_1 \n' '| formatted_itis_1 \n' '| * formatted_qual_1 \n' '* formatted_qual_2 \n' "|}") self.assertEqual(self.preview_item.format_protoclaims(), expected) self.mock_wd_template.assert_called_once_with('P123') self.mock_format_itis.assert_called_once_with(itis) self.mock_format_qual.assert_has_calls( [mock.call(qual_1), mock.call(qual_2)], any_order=True) self.mock_format_ref.assert_not_called()
def test_add_new_claim_edit_correct_qualified_claim_with_ref(self): self.prop = 'P664' self.value = 'Duplicate_string_with_ref' statement = Statement(self.value) statement.add_qualifier(Qualifier('P174', 'qualifier')) expected_claim = 'Q27399$e63f47a3-45ea-e2fc-1363-8f6062205084' self.wd_stuff.add_new_claim(self.prop, statement, self.wd_page, self.ref) self.mock_add_claim.assert_not_called() self.mock_add_claim.mock_add_qualifier() self.mock_add_reference.assert_called_once() self.assertEqual( self.mock_add_reference.call_args[0][1].toJSON()['id'], expected_claim)
def test_add_new_claim_edit_correct_qualified_claim(self): self.prop = 'P664' self.value = 'Duplicate_string' statement = Statement(self.value) statement.add_qualifier(Qualifier('P174', 'qualifier')) expected_claim = 'Q27399$a9b83de1-49d7-d033-939d-f430a232ffd0' self.wd_stuff.add_new_claim(self.prop, statement, self.wd_page, self.ref) self.mock_add_claim.assert_not_called() self.mock_add_claim.mock_add_qualifier() self.mock_add_reference.assert_called_once() self.assertEqual( self.mock_add_reference.call_args[0][1].toJSON()['id'], expected_claim)
def setUp(self): super(TestAddQualifier, self).setUp() self.claim = self.wd_page.claims['P174'][2] self.qual = Qualifier('P174', 'A qualifier') qualifier_patcher = mock.patch( 'wikidatastuff.wikidata_stuff.pywikibot.Claim.addQualifier') make_claim_patcher = mock.patch( 'wikidatastuff.wikidata_stuff.WikidataStuff.make_simple_claim') has_qualifier_patcher = mock.patch( 'wikidatastuff.wikidata_stuff.WikidataStuff.has_qualifier') self.mock_add_qualifier = qualifier_patcher.start() self.mock_make_simple_claim = make_claim_patcher.start() self.mock_has_qualifier = has_qualifier_patcher.start() self.addCleanup(qualifier_patcher.stop) self.addCleanup(make_claim_patcher.stop) self.addCleanup(has_qualifier_patcher.stop)
def test_format_protoclaims_single_with_qual(self): itis = Statement('dummy') qual = Qualifier('P321', 'qual_dummy') itis._quals.add(qual) self.preview_item.protoclaims = {'P123': itis} expected = ("{| class='wikitable'\n" "|-\n" "! Property\n" "! Value\n" "! Qualifiers\n" '|-\n' '| wd_template_1 \n' '| formatted_itis_1 \n' '| formatted_qual_1 \n' "|}") self.assertEqual(self.preview_item.format_protoclaims(), expected) self.mock_wd_template.assert_called_once_with('P123') self.mock_format_itis.assert_called_once_with(itis) self.mock_format_qual.assert_called_once_with(qual) self.mock_format_ref.assert_not_called()
def test_format_qual_basic(self): qual = Qualifier('P123', 'val') self.assertEqual(PreviewItem.format_qual(qual), 'wd_template_1: formatted_itis') self.mock_wd_template.assert_called_once_with('P123') self.mock_format_itis.assert_called_once_with('val')
def test_qualifier_repr(self): q = Qualifier('P123', 'foo') self.assertEqual(repr(q), 'WD.Qualifier(P123, foo)')
def test_qualifier_init(self): q_int = Qualifier(123, 'foo') q_no_p = Qualifier('123', 'foo') q_with_p = Qualifier('P123', 'foo') self.assertEqual(q_int, q_no_p) self.assertEqual(q_with_p, q_no_p)