Browse Source

missingnotice: Implement article selection

Issue #64 (https://git.golderweb.de/wiki/jogobot-red/issues/64)
Jonathan Golder 5 months ago
parent
commit
95af95aca6
2 changed files with 57 additions and 0 deletions
  1. 22
    0
      bots/missingnotice.py
  2. 35
    0
      tests/missingnotice_tests.py

+ 22
- 0
bots/missingnotice.py View File

@@ -51,6 +51,28 @@ ON `cl_from` = `page_id`
51 51
     def run( self ):
52 52
         print(type(self).get_categorized_articles() )
53 53
 
54
+    def treat_articles(self, articles):
55
+        """
56
+        Iterates over given articles and checks weather them are included in
57
+        self.categorized_articles (contain the notice)
58
+
59
+        @param articles Articles to check
60
+        @type articles iterable of pywikibot.page() objects
61
+
62
+        @returns Possibly empty list of wikitext links ("[[article]]")
63
+        @rtype list
64
+        """
65
+        links = list()
66
+
67
+        for article in articles:
68
+
69
+            if article.title(underscore=True, with_section=False ) not in \
70
+                    self.categorized_articles:
71
+
72
+                links.append( article.title(as_link=True, textlink=True) )
73
+
74
+        return links
75
+
54 76
     @classmethod
55 77
     def get_categorized_articles( cls ):
56 78
         """

+ 35
- 0
tests/missingnotice_tests.py View File

@@ -29,6 +29,8 @@ Test module bot/missingnotice.py
29 29
 import unittest
30 30
 from unittest import mock  # noqa
31 31
 
32
+import pywikibot
33
+
32 34
 import context  # noqa
33 35
 from bots.missingnotice import MissingNoticeBot  # noqa
34 36
 
@@ -38,6 +40,13 @@ class TestMissingNoticeBot(unittest.TestCase):
38 40
     Test class MissingNoticeBot
39 41
     """
40 42
 
43
+    def setUp(self):
44
+        genFactory = pywikibot.pagegenerators.GeneratorFactory()
45
+        self.MissingNoticeBot = MissingNoticeBot(genFactory)
46
+        self.MissingNoticeBot.categorized_articles = [ "Deutschland",
47
+                                                       "Max_Schlee",
48
+                                                       "Hodeng-Hodenger" ]
49
+
41 50
     @mock.patch( 'sqlalchemy.engine.Engine.execute',
42 51
                  return_value=( { "page_title": b"a", },
43 52
                                 { "page_title": b"b", },
@@ -54,6 +63,32 @@ class TestMissingNoticeBot(unittest.TestCase):
54 63
         self.assertTrue(execute_mock.called)
55 64
         self.assertEqual(result, ["a", "b", "c", "d"] )
56 65
 
66
+    def test_treat_articles( self ):
67
+        """
68
+        Test method treat_articles()
69
+        """
70
+
71
+        # articles with notice
72
+        a = pywikibot.Page(pywikibot.Site(), "Deutschland" )
73
+        b = pywikibot.Page(pywikibot.Site(), "Max_Schlee" )
74
+        c = pywikibot.Page(pywikibot.Site(), "Hodeng-Hodenger#Test" )
75
+        # articles without notice
76
+        x = pywikibot.Page(pywikibot.Site(), "Quodvultdeus" )
77
+        y = pywikibot.Page(pywikibot.Site(), "Zoo_Bremen" )
78
+        z = pywikibot.Page(pywikibot.Site(), "Nulka#Test" )
79
+
80
+        cases = ( ( ( a, b, c ), list() ),
81
+                  ( ( x, y, z ), [ "[[Quodvultdeus]]",
82
+                                   "[[Zoo Bremen]]",
83
+                                   "[[Nulka#Test]]" ]),
84
+                  ( ( a, b, y, z ), [ "[[Zoo Bremen]]",
85
+                                      "[[Nulka#Test]]" ]), )
86
+
87
+        for case in cases:
88
+            res = self.MissingNoticeBot.treat_articles( case[0] )
89
+
90
+            self.assertEqual( res, case[1] )
91
+
57 92
 
58 93
 if __name__ == '__main__':
59 94
     unittest.main()