From 581e043255d8160323438d3186b8e14a1e67d8a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?GOLDERWEB=20=E2=80=93=20Jonathan=20Golder?= Date: Sat, 28 Nov 2015 13:42:32 +0100 Subject: [PATCH 1/4] Add unitest to CountryList-Modul --- countrylist.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/countrylist.py b/countrylist.py index 38e5c9b..3a7eeeb 100644 --- a/countrylist.py +++ b/countrylist.py @@ -112,6 +112,9 @@ class CountryList(): Handles the parsing process """ + # Set revid + self.revid = self.page.latest_revision_id + # Parse page with mwparser self.generate_wikicode() @@ -356,3 +359,50 @@ class CountryListEntryError( CountryListError ): Handles errors occuring in class CountryList related to entrys """ pass + + +class CountryListUnitTest(): + """ + Defines Test-Functions for CountryList-Module + """ + + testcases = ( { "Link": mwparser.nodes.Wikilink( "Benutzer:JogoBot/Charts/Tests/Liste der Nummer-eins-Hits in Frankreich (2015)" ), # noqa + "revid": 148453827, + "interpret": "[[Adele (Sängerin)|Adele]]", + "titel": "[[Hello (Adele-Lied)|Hello]]", + "chartein": datetime( 2015, 10, 23 ) }, + { "Link": mwparser.nodes.Wikilink( "Benutzer:JogoBot/Charts/Tests/Liste der Nummer-eins-Hits in Belgien (2015)", "Wallonien"), # noqa + "revid": 148455281, + "interpret": "[[Nicky Jam]] & [[Enrique Iglesias (Sänger)|Enrique Iglesias]]", # noqa + "titel": "El perdón", + "chartein": datetime( 2015, 9, 12 ) } ) + + def __init__( self ): + pass + + def treat( self ): + + for case in type(self).testcases: + + self.countrylist = CountryList( case["Link"] ) + + if( self.countrylist.is_parsing_needed( case["revid"] ) or not + self.countrylist.is_parsing_needed( case["revid"] + 1 ) ): + raise Exception( + "CountryList.is_parsing_needed() does not work!" ) + + self.countrylist.parse() + + for key in case: + + if key == "Link": + continue + + if not case[key] == getattr(self.countrylist, key ): + raise Exception( key + " – " + str( + getattr(self.countrylist, key ) )) + + +if __name__ == "__main__": + test = CountryListUnitTest() + test.treat() From a250074caa3c9e2c64611f1ad119dbbd62142d5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?GOLDERWEB=20=E2=80=93=20Jonathan=20Golder?= Date: Sat, 28 Nov 2015 17:26:27 +0100 Subject: [PATCH 2/4] CountryList-module: Search current year via regex to also make parsing older lists possible --- countrylist.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/countrylist.py b/countrylist.py index 3a7eeeb..d222cd5 100644 --- a/countrylist.py +++ b/countrylist.py @@ -25,6 +25,7 @@ Provides a class for handling charts list per country and year """ +import re import locale from datetime import datetime @@ -96,15 +97,15 @@ class CountryList(): def find_year( self ): """ - Try to find the year related to CountryList + Try to find the year related to CountryList using regex """ - self.year = datetime.now().year + match = re.search( r"^.+\((\d{4})\)", self.page.title() ) - # Check if year is in page.title, if not try last year - if str( self.year ) not in self.page.title(): - self.year -= 1 - # If last year does not match, raise YearError - if str( self.year ) not in self.page.title(): + # We matched something + if match: + self.year = match.group() + + else: raise CountryListError( "CountryList year is errorneous!" ) def parse( self ): From 3349c9f3d30cb89c5b2f9f4ba255e4fc4eb2d767 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?GOLDERWEB=20=E2=80=93=20Jonathan=20Golder?= Date: Sat, 28 Nov 2015 18:16:04 +0100 Subject: [PATCH 3/4] Add __str__-method to CountryList-class --- countrylist.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/countrylist.py b/countrylist.py index d222cd5..d7e04a3 100644 --- a/countrylist.py +++ b/countrylist.py @@ -67,7 +67,7 @@ class CountryList(): # Check if page exits if not self.page.exists(): - return False + return None # Initialise attributes __attr = ( "wikicode", "entry", "chartein", "_chartein_raw", @@ -347,6 +347,23 @@ missing!" ) raise CountryListEntryError( "Template Parameter 'Interpret' is \ missing!" ) + def __str__( self ): + """ + Returns str repression for Object + """ + if self.parsed: + return ("CountryList( Link = \"{link}\", Revid = \"{revid}\", " + + "Interpret = \"{interpret}\", Titel = \"{titel}\", " + + "Chartein = \"{chartein}\" )").format( + link=repr(self.wikilink), + revid=self.revid, + interpret=self.interpret, + titel=self.titel, + chartein=repr(self.chartein)) + else: + return "CountryList( Link = \"{link}\" )".format( + link=repr(self.wikilink)) + class CountryListError( Exception ): """ From 4de21167172671bdfd51ea07e30d9d8bb156d9da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?GOLDERWEB=20=E2=80=93=20Jonathan=20Golder?= Date: Sat, 28 Nov 2015 18:17:19 +0100 Subject: [PATCH 4/4] Add possibility to manually check against any page in dewiki --- countrylist.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/countrylist.py b/countrylist.py index d7e04a3..5360137 100644 --- a/countrylist.py +++ b/countrylist.py @@ -395,10 +395,30 @@ class CountryListUnitTest(): "titel": "El perdón", "chartein": datetime( 2015, 9, 12 ) } ) - def __init__( self ): - pass + def __init__( self, page=None ): + """ + Constructor + Set attribute page + """ + if page: + self.page_link = mwparser.nodes.Wikilink( page ) + else: + self.page_link = None def treat( self ): + """ + Start testing either manually with page provided by cmd-arg page or + automatically with predefined test case + """ + if self.page_link: + self.man_test() + else: + self.auto_test() + + def auto_test( self ): + """ + Run automatic tests with predefined test data from wiki + """ for case in type(self).testcases: @@ -420,7 +440,35 @@ class CountryListUnitTest(): raise Exception( key + " – " + str( getattr(self.countrylist, key ) )) + def man_test( self ): + """ + Run manual test with page given in parameter + """ + self.countrylist = CountryList( self.page_link ) + + self.countrylist.parse() + + print( self.countrylist ) + print( "Since we have no data to compare, you need to manually " + + "check data above against given page to ensure correct " + + "working of module!" ) + + +def main(*args): + """ + Handling direct calls --> unittest + """ + # Process global arguments to determine desired site + local_args = pywikibot.handle_args(args) + + # Parse command line arguments + for arg in local_args: + if arg.startswith("-page:"): + page = arg[ len("-page:"): ] + + # Call unittest-class + test = CountryListUnitTest( page ) + test.treat() if __name__ == "__main__": - test = CountryListUnitTest() - test.treat() + main()