jenkins-bot has submitted this change and it was merged.
Change subject: TestDryPageGenerator re-used QueryGenerator ......................................................................
TestDryPageGenerator re-used QueryGenerator
api_tests.TestDryPageGenerator pushed emulated query response data directly into QueryGenerator as a dict, which also allowed the method test_limits to reuse the generator. This breaks the definition of a 'generator', and relied on QueryGenerator not fetching data if 'data' already existed.
Fixed the tests so that the insertion of the mock data doesnt depend on those quirks of QueryGenerator which should be removed.
Change-Id: Ic4e92dc6eefe578fa12cab2307e45e7e85f96d40 --- M tests/api_tests.py M tests/aspects.py 2 files changed, 35 insertions(+), 26 deletions(-)
Approvals: XZise: Looks good to me, approved jenkins-bot: Verified
diff --git a/tests/api_tests.py b/tests/api_tests.py index 7c6c273..9746229 100644 --- a/tests/api_tests.py +++ b/tests/api_tests.py @@ -8,7 +8,8 @@ __version__ = '$Id$'
import datetime -import pywikibot +import types + import pywikibot.data.api as api from pywikibot.tools import MediaWikiVersion
@@ -248,14 +249,21 @@
dry = True
+ # api.py sorts 'pages' using the string key, which is not a + # numeric comparison. + titles = ("Broadcaster (definition)", "Wiktionary", "Broadcaster.com", + "Wikipedia:Disambiguation") + def setUp(self): super(TestDryPageGenerator, self).setUp() mysite = self.get_site() self.gen = api.PageGenerator(site=mysite, generator="links", titles="User:R'n'B") - # following test data is copied from an actual api.php response - self.gen.data = { + # following test data is copied from an actual api.php response, + # but that query no longer matches this dataset. + # http://en.wikipedia.org/w/api.php?action=query&generator=links&title... + self.gen.request.submit = types.MethodType(lambda self: { "query": {"pages": {"296589": {"pageid": 296589, "ns": 0, "title": "Broadcaster.com" @@ -274,58 +282,55 @@ } } } - } + }, self.gen.request)
# On a dry site, the namespace objects only have canonical names. # Add custom_name for this site namespace, to match the live site. if 'Wikipedia' not in self.site._namespaces: self.site._namespaces[4].custom_name = 'Wikipedia'
- def testGeneratorResults(self): + def test_results(self): """Test that PageGenerator yields pages with expected attributes.""" - titles = ["Broadcaster.com", "Broadcaster (definition)", - "Wiktionary", "Wikipedia:Disambiguation"] - mysite = self.get_site() - results = [p for p in self.gen] - self.assertEqual(len(results), 4) - for page in results: - self.assertEqual(type(page), pywikibot.Page) - self.assertEqual(page.site, mysite) - self.assertIn(page.title(), titles) + self.assertPagelistTitles(self.gen, self.titles)
def test_initial_limit(self): self.assertEqual(self.gen.limit, None) # limit is initally None
- def test_limit_as_number(self): + def test_set_limit_as_number(self): for i in range(-2, 4): self.gen.set_maximum_items(i) self.assertEqual(self.gen.limit, i)
- def test_limit_as_string(self): + def test_set_limit_as_string(self): for i in range(-2, 4): self.gen.set_maximum_items(str(i)) self.assertEqual(self.gen.limit, i)
- def test_wrong_limit_setting(self): + def test_set_limit_not_number(self): with self.assertRaisesRegex( ValueError, "invalid literal for int() with base 10: 'test'"): self.gen.set_maximum_items('test')
- def test_limits(self): + def test_limit_equal_total(self): """Test that PageGenerator yields the requested amount of pages.""" - for i in range(4, 0, -1): - self.gen.set_maximum_items(i) # set total amount of pages - results = [p for p in self.gen] - self.assertEqual(len(results), i) + self.gen.set_maximum_items(4) + self.assertPagelistTitles(self.gen, self.titles)
+ def test_limit_one(self): + """Test that PageGenerator yields the requested amount of pages.""" + self.gen.set_maximum_items(1) + self.assertPagelistTitles(self.gen, self.titles[0:1]) + + def test_limit_zero(self): + """Test that a limit of zero is the same as limit None.""" self.gen.set_maximum_items(0) - results = [p for p in self.gen] - self.assertEqual(len(results), 4) # total=0 but 4 expected (really?) + self.assertPagelistTitles(self.gen, self.titles)
+ def test_limit_omit(self): + """Test that limit omitted is the same as limit None.""" self.gen.set_maximum_items(-1) - results = [p for p in self.gen] - self.assertEqual(len(results), 4) # total=-1 but 4 expected + self.assertPagelistTitles(self.gen, self.titles)
class TestPropertyGenerator(TestCase): diff --git a/tests/aspects.py b/tests/aspects.py index 4aad55e..4eadc78 100644 --- a/tests/aspects.py +++ b/tests/aspects.py @@ -169,6 +169,10 @@ working_set = collections.deque(titles)
for page in gen: + self.assertIsInstance(page, pywikibot.Page) + if site: + self.assertEqual(page.site, site) + title = page.title() self.assertIn(title, titles) if is_tuple:
pywikibot-commits@lists.wikimedia.org