jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/378377 )
Change subject: Fix test_unconnected and test_unconnected_with_repo
......................................................................
Fix test_unconnected and test_unconnected_with_repo
site_tests.test_unconnected:
Sometimes local site is not synchronized with data repository. In
those situations unconnected_pages may return a page that is actually
connected to wikidata and test_unconnected may fail. (T175987)
To avoid this situation, instead of confirming that unconnected pages have
no item on wikidata, only confirm that the right query is sent, i.e. with
right parameters and via UnconnectedPages.
It's Mediawiki's job to ensure the correct results when a correct request
is sent.
pagegenerators_tests.test_unconnected_with_repo:
Instead of repeating the logic of site_tests.test_unconnected, just
assert that unconnected_pages is called with right parameters.
Now that these tests are not time-dependant anymore we can cache the results.
Add `cached = True` to TestUnconnectedPageGenerator and put test_unconnected
in TestSiteGenerators class (which is cached).
Also no need to check for `self.site.hostname() == 'test.wikipedia.org'`
condition because we are not relying on the result of the related data
repositoy in these tests.
Remove unused imports.
Bug: T163619
Change-Id: I625401eb7f5ad02852f5652df706d2264dd6a6f9
---
M tests/pagegenerators_tests.py
M tests/site_tests.py
2 files changed, 30 insertions(+), 57 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/tests/pagegenerators_tests.py b/tests/pagegenerators_tests.py
index 20d1deb..b88770a 100755
--- a/tests/pagegenerators_tests.py
+++ b/tests/pagegenerators_tests.py
@@ -9,11 +9,15 @@
import calendar
import datetime
-import json
import logging
import sys
from distutils.version import LooseVersion
+
+try:
+ from unittest.mock import patch
+except ImportError:
+ from mock import patch
import pywikibot
from pywikibot import pagegenerators, date
@@ -1455,34 +1459,21 @@
"""Test UnconnectedPageGenerator."""
+ cached = True
+
def test_unconnected_with_repo(self):
- """Test that the ItemPage returned raises
NoPage."""
+ """Test UnconnectedPageGenerator."""
if not self.site.data_repository():
raise unittest.SkipTest('Site is not using a Wikibase repository')
- if self.site.hostname() == 'test.wikipedia.org':
- raise unittest.SkipTest('test.wikipedia is misconfigured; T85358')
- cnt = 0
- # Pages which have been connected recently may still be reported as
- # unconnected. So try on a version that is a few minutes older if the
- # tested site appears as a sitelink.
- start_time = \
- datetime.datetime.utcnow() - datetime.timedelta(minutes=30)
- for page in pagegenerators.UnconnectedPageGenerator(self.site, 3):
- try:
- item = pywikibot.ItemPage.fromPage(page)
- except pywikibot.NoPage:
- pass
- else:
- revisions = list(item.revisions(total=1, starttime=start_time,
- content=True))
- if revisions:
- sitelinks = json.loads(revisions[0].text)['sitelinks']
- self.assertNotIn(
- self.site.dbName(), sitelinks,
- 'Page "{0}" is connected to {1} on Wikibase '
- 'repository'.format(page.title(), item))
- cnt += 1
- self.assertLessEqual(cnt, 3)
+ upgen = pagegenerators.UnconnectedPageGenerator(self.site, 3)
+
+ def unconnected_pages(total=None):
+ """Assert unconnected_pages is called
correctly."""
+ self.assertEqual(total, 3)
+ yield
+
+ with patch.object(self.site, 'unconnected_pages', unconnected_pages):
+ self.assertEqual(tuple(upgen), (None,))
def test_unconnected_without_repo(self):
"""Test that it raises a ValueError on sites without
repository."""
diff --git a/tests/site_tests.py b/tests/site_tests.py
index add575c..996389d 100644
--- a/tests/site_tests.py
+++ b/tests/site_tests.py
@@ -7,14 +7,13 @@
#
from __future__ import absolute_import, unicode_literals
-import json
import os
import pickle
import re
import sys
from collections import Iterable, Mapping
-from datetime import datetime, timedelta
+from datetime import datetime
import pywikibot
@@ -1034,38 +1033,21 @@
self.fail(
'NotImplementedError not raised for {0}'.format(item))
-
-class TestSiteGeneratorsUncached(DefaultSiteTestCase):
-
- """Test cases for Site methods."""
-
def test_unconnected(self):
- """Test that the ItemPage returned raises
NoPage."""
+ """Test site.unconnected_pages method."""
if not self.site.data_repository():
raise unittest.SkipTest('Site is not using a Wikibase repository')
- if self.site.hostname() == 'test.wikipedia.org':
- raise unittest.SkipTest('test.wikipedia is misconfigured; T85358')
- cnt = 0
- # Pages which have been connected recently may still be reported as
- # unconnected. So try on a version that is a few minutes older if the
- # tested site appears as a sitelink.
- start_time = datetime.utcnow() - timedelta(minutes=30)
- for page in self.site.unconnected_pages(total=3):
- try:
- item = pywikibot.ItemPage.fromPage(page)
- except pywikibot.NoPage:
- pass
- else:
- revisions = list(item.revisions(total=1, starttime=start_time,
- content=True))
- if revisions:
- sitelinks = json.loads(revisions[0].text)['sitelinks']
- self.assertNotIn(
- self.site.dbName(), sitelinks,
- 'Page "{0}" is connected to {1} on Wikibase '
- 'repository'.format(page.title(), item))
- cnt += 1
- self.assertLessEqual(cnt, 3)
+ upgen = self.site.unconnected_pages(total=3)
+ self.assertDictEqual(
+ upgen.request._params, {
+ 'gqppage': ['UnconnectedPages'],
+ 'prop': ['info', 'imageinfo',
'categoryinfo'],
+ 'inprop': ['protection'],
+ 'iiprop': ['timestamp', 'user',
'comment', 'url', 'size',
+ 'sha1', 'metadata'],
+ 'generator': ['querypage'], 'action':
['query'],
+ 'indexpageids': [True], 'continue': [True]})
+ self.assertLessEqual(len(tuple(upgen)), 3)
class TestImageUsage(DefaultSiteTestCase):
--
To view, visit
https://gerrit.wikimedia.org/r/378377
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I625401eb7f5ad02852f5652df706d2264dd6a6f9
Gerrit-PatchSet: 6
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Magul <tomasz.magulski(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot <>