From 83f0ae15a80c50c77efdd543b21e788323656fcc Mon Sep 17 00:00:00 2001 From: Adam Williamson Date: May 08 2020 20:38:38 +0000 Subject: Extend event test coverage This improves and extends test_event to substantially improve coverage. Signed-off-by: Adam Williamson --- diff --git a/tests/test_event.py b/tests/test_event.py index 57c0a6e..69a325b 100644 --- a/tests/test_event.py +++ b/tests/test_event.py @@ -18,7 +18,7 @@ # Author: Adam Williamson # these are all kinda inappropriate for pytest patterns -# pylint: disable=old-style-class, no-init, protected-access, no-self-use, unused-argument +# pylint: disable=old-style-class, no-init, protected-access, no-self-use, unused-argument, too-many-arguments # pylint: disable=invalid-name """Tests for event.py.""" @@ -26,12 +26,17 @@ from __future__ import unicode_literals from __future__ import print_function -import fedfind.release # note: we need the external 'mock', unittest.mock does not seem to # work correctly with the @patch decorator still import mock + +import fedfind.release +import mwclient.errors +import mwclient.page import pytest + import wikitcms.event +import wikitcms.listing import wikitcms.wiki as wk from wikitcms.exceptions import FedfindNotFoundError @@ -143,6 +148,20 @@ class TestEventFedfind: with pytest.raises(FedfindNotFoundError): assert event.ff_release_images + @mock.patch('fedfind.release.ModularCompose.exists', True) + @mock.patch('fedfind.release.ModularCompose.all_images', ['foo']) + def test_candidate_ff_release_modular(self): + """Straightforward ff_release test case for a modular + candidate compose which is properly synced to stage. + """ + event = wikitcms.event.ComposeEvent( + self.site, '27', 'Beta', '1.5', modular=True, cid='Fedora-Modular-27-20171108.2') + assert isinstance(event.ff_release, fedfind.release.ModularCompose) + assert isinstance(event.ff_release_images, fedfind.release.ModularCompose) + event = wikitcms.event.ComposeEvent(self.site, '27', 'Beta', '1.5', modular=True) + assert isinstance(event.ff_release, fedfind.release.ModularCompose) + assert isinstance(event.ff_release_images, fedfind.release.ModularCompose) + @pytest.mark.usefixtures("fakemwp") @mock.patch('wikitcms.page.Page.save', autospec=True) @mock.patch('wikitcms.page.SummaryPage.update_current', autospec=True) @@ -197,6 +216,9 @@ class TestEventCreate: assert fakesumup.call_count == 1 # we should call update_current for the event itself assert fakeevup.call_count == 1 + # verify we set createonly to True by default + for call in fakepagesave.call_args_list: + assert call[1]["createonly"] is True @mock.patch('fedfind.release.BranchedNightly.exists', False) @mock.patch('fedfind.release.BranchedNightly.all_images', []) @@ -214,17 +236,25 @@ class TestEventCreate: # we should call update_current for the event itself assert fakeevup.call_count == 1 - @mock.patch('wikitcms.page.mwp.Page.text', return_value="sometext") @mock.patch('fedfind.release.BranchedNightly.exists', False) @mock.patch('fedfind.release.BranchedNightly.all_images', []) - def test_event_create_check(self, fakepagetext, fakejson, fakeevup, fakepageup, fakesumup, fakepagesave): - """Test event creation when pages already exist and check is - True. We should raise an error in this case. Using the 'no - download page' case because the mocks are shorter. - """ - event = wikitcms.event.NightlyEvent(self.site, '27', 'Branched', '20171104.n.0') - with pytest.raises(ValueError): + def test_event_create_check(self, fakejson, fakeevup, fakepageup, fakesumup, fakepagesave): + """Test event creation with check=True.""" + event = wikitcms.event.NightlyEvent(self.site, "27", "Branched", "20171104.n.0") + # first, let's say the pages had no content, so creation + # should go ahead... + with mock.patch("mwclient.page.Page.text", return_value=""): event.create(check=True) + # we should save 5 test pages, plus the summary page and + # two category pages and AMI page - but no download page + assert fakepagesave.call_count == 9 + # now, let's say the pages *do* have content, we should get + # an exception now... + fakepagesave.reset_mock() + with mock.patch("mwclient.page.Page.text", return_value="foobar"): + with pytest.raises(ValueError): + event.create(check=True) + assert fakepagesave.call_count == 0 @mock.patch('fedfind.release.BranchedNightly.exists', False) @mock.patch('fedfind.release.BranchedNightly.all_images', []) @@ -239,3 +269,173 @@ class TestEventCreate: assert fakepageup.call_count == 2 # we should call update_current for the event itself assert fakeevup.call_count == 1 + # check correct handling of garbage list of testtypes + with pytest.raises(ValueError): + event.create(testtypes=["foo", "bar"]) + + @mock.patch('fedfind.release.BranchedNightly.exists', False) + @mock.patch('fedfind.release.BranchedNightly.all_images', []) + def test_event_create_force(self, fakejson, fakeevup, fakepageup, fakesumup, fakepagesave): + """Test event creation with force=True.""" + # we only need to test that we properly set createonly=None + # here, we don't need to confirm or mock its effects + event = wikitcms.event.NightlyEvent(self.site, "27", "Branched", "20171104.n.0") + event.create(force=True) + for call in fakepagesave.call_args_list: + assert call[1]["createonly"] is None + + @mock.patch('fedfind.release.BranchedNightly.exists', False) + @mock.patch('fedfind.release.BranchedNightly.all_images', []) + def test_event_create_existing(self, fakejson, fakeevup, fakepageup, fakesumup, fakepagesave): + """Test the handling when a page we try to create during + event creation already exists. The expected behaviour is that + we just log the issue and continue. + """ + event = wikitcms.event.NightlyEvent(self.site, "27", "Branched", "20171104.n.0") + fakepagesave.side_effect = mwclient.errors.APIError("articleexists", "Article exists", {}) + event.create() + # but if the error is something *else*, we should raise it + fakepagesave.side_effect = mwclient.errors.APIError("frobbled", "Article is frobbled", {}) + with pytest.raises(mwclient.errors.APIError): + event.create() + fakepagesave.reset_mock() + + @mock.patch('fedfind.release.BranchedNightly.exists', False) + @mock.patch('fedfind.release.BranchedNightly.all_images', []) + def test_event_create_nocurrent(self, fakejson, fakeevup, fakepageup, fakesumup, fakepagesave): + """Test that setting current=False correctly skips current + redirect updates. + """ + event = wikitcms.event.NightlyEvent(self.site, "27", "Branched", "20171104.n.0") + event.create(current=False) + # we should save 5 test pages, plus the summary page and + # two category pages and AMI page - but no download page + assert fakepagesave.call_count == 9 + # we should not call update_current for test pages + assert fakepageup.call_count == 0 + # we should not call update_current for the event itself + assert fakeevup.call_count == 0 + + +class TestEventOther: + """Tests for properties of Event instances and other methods.""" + site = wk.Wiki('fjajah', do_init=False, force_login=False) + + @mock.patch("mwclient.listing.GeneratorList.__next__") + def test_event_result_pages(self, fakenext, fakemwp): + """Test for result_pages property.""" + event = wikitcms.event.NightlyEvent(self.site, "27", "Branched", "20171104.n.0") + # we need to mock the output of the generator the method uses + # it's an 'allresults' generator (so it uses the Test Results + # namespace) and the prefix should be "Fedora 27 Branched + # 20171104.n.0 ", so what we're expecting to get back is all + # the actual result pages plus the summary page, and we have + # to test the method correctly filters out the summary + evp1 = mwclient.page.Page(self.site, "Test Results:Fedora 27 Branched 20171104.n.0 Base") + evp2 = mwclient.page.Page(self.site, "Test Results:Fedora 27 Branched 20171104.n.0 Cloud") + evs = mwclient.page.Page(self.site, "Test Results:Fedora 27 Branched 20171104.n.0 Summary") + fakenext.side_effect = [evp1, evp2, evs, StopIteration] + rps = event.result_pages + assert len(rps) == 2 + (rp1, rp2) = rps + assert isinstance(rp1, wikitcms.page.NightlyPage) + assert isinstance(rp2, wikitcms.page.NightlyPage) + assert rp1.testtype == "Base" + assert rp2.testtype == "Cloud" + assert rp1.modular is rp2.modular is False + # modular + event = wikitcms.event.NightlyEvent( + self.site, "27", "Branched", "20171123.n.1", modular=True + ) + evp1 = mwclient.page.Page(self.site, + "Test Results:Fedora Modular 27 Branched 20171123.n.1 Base") + evp2 = mwclient.page.Page(self.site, + "Test Results:Fedora Modular 27 Branched 20171123.n.1 Server") + evs = mwclient.page.Page(self.site, + "Test Results:Fedora Modular 27 Branched 20171123.n.1 Summary") + fakenext.side_effect = [evp1, evp2, evs, StopIteration] + rps = event.result_pages + assert len(rps) == 2 + (rp1, rp2) = rps + assert isinstance(rp1, wikitcms.page.NightlyPage) + assert isinstance(rp2, wikitcms.page.NightlyPage) + assert rp1.testtype == "Base" + assert rp2.testtype == "Server" + assert rp1.modular is rp2.modular is True + + @mock.patch("mwclient.page.Page.save", autospec=True) + def test_event_update_current(self, fakesave, fakemwp): + """Test for update_current() method.""" + event = wikitcms.event.NightlyEvent(self.site, "27", "Branched", "20171104.n.0") + event.update_current() + assert fakesave.call_args[0][1] == ( + "{{tempdoc}}\n{{#switch: {{{1|full}}}\n| full = 27 Branched 20171104.n.0" + "\n| release = 27\n| milestone = Branched\n| compose =\n| date = 20171104.n.0\n" + "}}\n[[Category: Fedora Templates]]" + ) + assert fakesave.call_args[0][2] == "relval: update to current event" + assert fakesave.call_args[1]["createonly"] is None + assert fakesave.call_args[0][0].name == "Template:CurrentFedoraCompose" + # modular, non-nightly + event = wikitcms.event.ComposeEvent(self.site, "27", "Beta", "1.5", modular=True) + event.update_current() + assert fakesave.call_args[0][1] == ( + "{{tempdoc}}\n{{#switch: {{{1|full}}}\n| full = 27 Beta 1.5" + "\n| release = 27\n| milestone = Beta\n| compose = 1.5\n| date =\n" + "}}\n[[Category: Fedora Templates]]" + ) + assert fakesave.call_args[0][2] == "relval: update to current event" + assert fakesave.call_args[1]["createonly"] is None + assert fakesave.call_args[0][0].name == "Template:CurrentFedoraModularCompose" + + def test_event_from_page(self, fakemwp): + """Test for from_page classmethod.""" + pg = self.site.pages["Test Results:Fedora 27 Branched 20171104.n.0 Base"] + ev = wikitcms.event.NightlyEvent.from_page(pg) + assert ev.version == "27 Branched 20171104.n.0" + assert ev.modular is False + # modular + pg = self.site.pages["Test Results:Fedora Modular 27 Branched 20171123.n.1 Base"] + ev = wikitcms.event.NightlyEvent.from_page(pg) + assert ev.version == "27 Branched 20171123.n.1" + assert ev.modular is True + + def test_event_category_page(self, fakemwp): + """Test for category_page property.""" + # compose + event = wikitcms.event.ComposeEvent(self.site, "27", "Beta", "1.2") + cat = event.category_page + assert isinstance(cat, wikitcms.listing.ValidationCategory) + # this is a sufficient proxy that we got the right thing + assert cat.checkname == "Category:Fedora 27 Beta Test Results" + # modular compose + event = wikitcms.event.ComposeEvent(self.site, "27", "Beta", "1.5", modular=True) + cat = event.category_page + assert cat.checkname == "Category:Fedora Modular 27 Beta Test Results" + # nightly + event = wikitcms.event.NightlyEvent(self.site, "27", "Branched", "20171104.n.0") + cat = event.category_page + assert cat.checkname == "Category:Fedora 27 Nightly Test Results" + # modular nightly + event = wikitcms.event.NightlyEvent( + self.site, "27", "Branched", "20171123.n.1", modular=True + ) + cat = event.category_page + assert cat.checkname == "Category:Fedora Modular 27 Nightly Test Results" + + # yes, I looked up the correct date. don't make fun of me! + @mock.patch("wikitcms.page.ComposePage.creation_date", "20170922") + def test_event_creation_date(self, fakemwp): + """Test for creation_date property/attribute.""" + # compose + evp = self.site.pages["Test Results:Fedora 27 Beta 1.2 Base"] + with mock.patch("wikitcms.event.ComposeEvent.result_pages", [evp]): + event = wikitcms.event.ComposeEvent(self.site, "27", "Beta", "1.2") + assert event.creation_date == "20170922" + with mock.patch("wikitcms.event.ComposeEvent.result_pages", []): + # this is when the event doesn't exist + event = wikitcms.event.ComposeEvent(self.site, "27", "Beta", "1.2") + assert event.creation_date == "" + # nightly - it's just an attribute here + event = wikitcms.event.NightlyEvent(self.site, "27", "Branched", "20171104.n.0") + assert event.creation_date == "20171104"