-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b7930b
commit 6cc1eec
Showing
33 changed files
with
2,955 additions
and
2,028 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,78 @@ | ||
''' | ||
""" | ||
Created on 2021-10-21 | ||
@author: wf | ||
''' | ||
""" | ||
import getpass | ||
import os | ||
from unittest import TestCase | ||
import time | ||
import getpass | ||
import warnings | ||
from unittest import TestCase | ||
|
||
from wikibot3rd.wikiuser import WikiUser | ||
|
||
|
||
class Profiler: | ||
''' | ||
""" | ||
simple profiler | ||
''' | ||
def __init__(self,msg,profile=True): | ||
''' | ||
""" | ||
|
||
def __init__(self, msg, profile=True): | ||
""" | ||
construct me with the given msg and profile active flag | ||
Args: | ||
msg(str): the message to show if profiling is active | ||
profile(bool): True if messages should be shown | ||
''' | ||
self.msg=msg | ||
self.profile=profile | ||
self.starttime=time.time() | ||
""" | ||
self.msg = msg | ||
self.profile = profile | ||
self.starttime = time.time() | ||
if profile: | ||
print(f"Starting {msg} ...") | ||
warnings.simplefilter("ignore", ResourceWarning) | ||
def time(self,extraMsg=""): | ||
''' | ||
|
||
def time(self, extraMsg=""): | ||
""" | ||
time the action and print if profile is active | ||
''' | ||
elapsed=time.time()-self.starttime | ||
""" | ||
elapsed = time.time() - self.starttime | ||
if self.profile: | ||
print(f"{self.msg}{extraMsg} took {elapsed:5.3f} s") | ||
return elapsed | ||
|
||
|
||
class BaseTest(TestCase): | ||
''' | ||
""" | ||
base Test class | ||
''' | ||
def setUp(self,debug=False,profile=True): | ||
''' | ||
""" | ||
|
||
def setUp(self, debug=False, profile=True): | ||
""" | ||
setUp test environment | ||
''' | ||
""" | ||
TestCase.setUp(self) | ||
self.debug=debug | ||
msg=f"test {self._testMethodName} ... with debug={self.debug}" | ||
self.debug = debug | ||
msg = f"test {self._testMethodName} ... with debug={self.debug}" | ||
# make sure there is an EventCorpus.db to speed up tests | ||
self.profiler=Profiler(msg=msg,profile=profile) | ||
self.wikiId = 'smwcopy' | ||
self.profiler = Profiler(msg=msg, profile=profile) | ||
self.wikiId = "smwcopy" | ||
|
||
def tearDown(self): | ||
self.profiler.time() | ||
pass | ||
@staticmethod | ||
|
||
@staticmethod | ||
def isInPublicCI(): | ||
''' | ||
""" | ||
are we running in a public Continuous Integration Environment? | ||
''' | ||
""" | ||
return getpass.getuser() in ["travis", "runner"] | ||
|
||
def inPublicCI(self): | ||
return BaseTest.isInPublicCI() | ||
|
||
def getWikiUser(self, wikiId:str=None) -> WikiUser: | ||
def getWikiUser(self, wikiId: str = None) -> WikiUser: | ||
""" | ||
Get WikiUser for given wikiId | ||
|
@@ -86,26 +88,45 @@ def getWikiUser(self, wikiId:str=None) -> WikiUser: | |
return wikiUser | ||
|
||
def getSMW_WikiUser(self, wikiId="or", save=False) -> WikiUser: | ||
''' | ||
""" | ||
get semantic media wiki users for SemanticMediawiki.org and openresearch.org | ||
''' | ||
""" | ||
iniFile = WikiUser.iniFilePath(wikiId) | ||
wikiUser = None | ||
if not os.path.isfile(iniFile): | ||
wikiDict = None | ||
if wikiId == "or": | ||
wikiDict = {"wikiId": wikiId, "email": "[email protected]", "url": "https://www.openresearch.org", | ||
"scriptPath": "/mediawiki/", "version": "MediaWiki 1.31.1"} | ||
wikiDict = { | ||
"wikiId": wikiId, | ||
"email": "[email protected]", | ||
"url": "https://www.openresearch.org", | ||
"scriptPath": "/mediawiki/", | ||
"version": "MediaWiki 1.31.1", | ||
} | ||
if wikiId == "cr": | ||
wikiDict = {"wikiId": wikiId, "email": "[email protected]", "url": "http://cr.bitplan.com", | ||
"scriptPath": "", "version": "MediaWiki 1.35.5"} | ||
wikiDict = { | ||
"wikiId": wikiId, | ||
"email": "[email protected]", | ||
"url": "http://cr.bitplan.com", | ||
"scriptPath": "", | ||
"version": "MediaWiki 1.35.5", | ||
} | ||
if wikiId in ["orclone", "orcopy"]: | ||
wikiDict = {"wikiId": wikiId, "email": "[email protected]", | ||
"url": "https://confident.dbis.rwth-aachen.de", "scriptPath": "/or/", | ||
"version": "MediaWiki 1.35.1"} | ||
wikiDict = { | ||
"wikiId": wikiId, | ||
"email": "[email protected]", | ||
"url": "https://confident.dbis.rwth-aachen.de", | ||
"scriptPath": "/or/", | ||
"version": "MediaWiki 1.35.1", | ||
} | ||
if wikiId == "smwcopy": | ||
wikiDict = {"wikiId": wikiId, "email": "[email protected]", "url": "https://smw.bitplan.com/", | ||
"scriptPath": "", "version": "MediaWiki 1.35.5"} | ||
wikiDict = { | ||
"wikiId": wikiId, | ||
"email": "[email protected]", | ||
"url": "https://smw.bitplan.com/", | ||
"scriptPath": "", | ||
"version": "MediaWiki 1.35.5", | ||
} | ||
if wikiDict is None: | ||
raise Exception(f"wikiId {wikiId} is not known") | ||
else: | ||
|
@@ -114,4 +135,4 @@ def getSMW_WikiUser(self, wikiId="or", save=False) -> WikiUser: | |
wikiUser.save() | ||
else: | ||
wikiUser = WikiUser.ofWikiId(wikiId, lenient=True) | ||
return wikiUser | ||
return wikiUser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,75 @@ | ||
''' | ||
""" | ||
Created on 2020-08-21 | ||
@author: wf | ||
''' | ||
from datetime import datetime,date | ||
""" | ||
import unittest | ||
from wikibot3rd.mwTable import MediaWikiTable | ||
from datetime import date, datetime | ||
|
||
from tests.basetest import BaseTest | ||
from wikibot3rd.mwTable import MediaWikiTable | ||
|
||
|
||
class Test_MediaWikiTable(BaseTest): | ||
''' | ||
""" | ||
test mediawiki table creation | ||
''' | ||
def dob(self,isoDateString): | ||
''' get the date of birth from the given iso date state''' | ||
#if sys.version_info >= (3, 7): | ||
""" | ||
|
||
def dob(self, isoDateString): | ||
"""get the date of birth from the given iso date state""" | ||
# if sys.version_info >= (3, 7): | ||
# dt=datetime.fromisoformat(isoDateString) | ||
#else: | ||
dt=datetime.strptime(isoDateString,"%Y-%m-%d") | ||
return dt.date() | ||
# else: | ||
dt = datetime.strptime(isoDateString, "%Y-%m-%d") | ||
return dt.date() | ||
|
||
def test_MediaWikiTable(self): | ||
''' | ||
""" | ||
see | ||
''' | ||
listOfDicts=[ | ||
{'name': 'Elizabeth Alexandra Mary Windsor', 'born': self.dob('1926-04-21'), 'numberInLine': 0, 'wikidataurl': 'https://www.wikidata.org/wiki/Q9682' }, | ||
{'name': 'Charles, Prince of Wales', 'born': self.dob('1948-11-14'), 'numberInLine': 1, 'wikidataurl': 'https://www.wikidata.org/wiki/Q43274' }, | ||
{'name': 'George of Cambridge', 'born': self.dob('2013-07-22'), 'numberInLine': 3, 'wikidataurl': 'https://www.wikidata.org/wiki/Q1359041'}, | ||
{'name': 'Harry Duke of Sussex', 'born': self.dob('1984-09-15'), 'numberInLine': 6, 'wikidataurl': 'https://www.wikidata.org/wiki/Q152316'} | ||
""" | ||
listOfDicts = [ | ||
{ | ||
"name": "Elizabeth Alexandra Mary Windsor", | ||
"born": self.dob("1926-04-21"), | ||
"numberInLine": 0, | ||
"wikidataurl": "https://www.wikidata.org/wiki/Q9682", | ||
}, | ||
{ | ||
"name": "Charles, Prince of Wales", | ||
"born": self.dob("1948-11-14"), | ||
"numberInLine": 1, | ||
"wikidataurl": "https://www.wikidata.org/wiki/Q43274", | ||
}, | ||
{ | ||
"name": "George of Cambridge", | ||
"born": self.dob("2013-07-22"), | ||
"numberInLine": 3, | ||
"wikidataurl": "https://www.wikidata.org/wiki/Q1359041", | ||
}, | ||
{ | ||
"name": "Harry Duke of Sussex", | ||
"born": self.dob("1984-09-15"), | ||
"numberInLine": 6, | ||
"wikidataurl": "https://www.wikidata.org/wiki/Q152316", | ||
}, | ||
] | ||
today=date.today() | ||
today = date.today() | ||
for person in listOfDicts: | ||
born=person['born'] | ||
age=(today - born).days / 365.2425 | ||
person['age']=age | ||
person['ofAge']=age>=18 | ||
for nlMode in [True,False]: | ||
colFormats={'age':'%5.1f years'} | ||
mwTable=MediaWikiTable(withNewLines=nlMode,colFormats=colFormats) | ||
born = person["born"] | ||
age = (today - born).days / 365.2425 | ||
person["age"] = age | ||
person["ofAge"] = age >= 18 | ||
for nlMode in [True, False]: | ||
colFormats = {"age": "%5.1f years"} | ||
mwTable = MediaWikiTable(withNewLines=nlMode, colFormats=colFormats) | ||
mwTable.fromListOfDicts(listOfDicts) | ||
wikiMarkup=mwTable.asWikiMarkup() | ||
wikiMarkup = mwTable.asWikiMarkup() | ||
if self.debug: | ||
print (wikiMarkup) | ||
print(wikiMarkup) | ||
self.assertTrue('{|class="wikitable sortable"' in wikiMarkup) | ||
pass | ||
|
||
|
||
if __name__ == "__main__": | ||
#import sys;sys.argv = ['', 'Test.test_MediaWikiTable'] | ||
unittest.main() | ||
# import sys;sys.argv = ['', 'Test.test_MediaWikiTable'] | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,59 @@ | ||
import json | ||
|
||
from tests.basetest import BaseTest | ||
from wikibot3rd.pagehistory import PageHistory | ||
import json | ||
|
||
|
||
class TestPageHistory(BaseTest): | ||
''' | ||
""" | ||
test querying the wiki api for the history of a page | ||
''' | ||
""" | ||
|
||
def setUp(self, debug=False, profile=True): | ||
super(TestPageHistory, self).setUp(debug=debug, profile=profile) | ||
self.wikiId = "cr" | ||
self.getWikiUser() | ||
self.testPage = "POPL97" | ||
#self.debug=True | ||
# self.debug=True | ||
|
||
def getPageHistory(self): | ||
""" | ||
get the pageHistory of the testPage | ||
""" | ||
pageHistory = PageHistory(pageTitle=self.testPage, wikiId=self.wikiId) | ||
if self.debug: | ||
print(json.dumps(pageHistory.revisions,indent=2,default=str)) | ||
print(json.dumps(pageHistory.revisions, indent=2, default=str)) | ||
return pageHistory | ||
|
||
def test_PageHistory(self): | ||
""" | ||
tests the extraction of the page revisions | ||
""" | ||
pageHistory=self.getPageHistory() | ||
pageHistory = self.getPageHistory() | ||
self.assertGreaterEqual(len(pageHistory.revisions), 7) | ||
|
||
def test_exists(self): | ||
""" | ||
tests if a page exists and has at least one page revision | ||
""" | ||
pageHistory=self.getPageHistory() | ||
pageHistory = self.getPageHistory() | ||
self.assertTrue(pageHistory.exists()) | ||
|
||
def test_getFirstUser(self): | ||
""" | ||
tests getFirstUser() | ||
""" | ||
pageHistory=self.getPageHistory() | ||
pageHistory = self.getPageHistory() | ||
# test pageCreator | ||
expectedPageCreator = "Wf" | ||
self.assertEqual(expectedPageCreator, pageHistory.getFirstUser()) | ||
expectedUserLimitedGroup = "Wf" | ||
self.assertEqual(expectedUserLimitedGroup, pageHistory.getFirstUser(limitedUserGroup=["Wf","Th"])) | ||
pageHistory.revisions.sort(key=lambda r: int(getattr(r, "revid", 0)), reverse=True) | ||
self.assertEqual( | ||
expectedUserLimitedGroup, | ||
pageHistory.getFirstUser(limitedUserGroup=["Wf", "Th"]), | ||
) | ||
pageHistory.revisions.sort( | ||
key=lambda r: int(getattr(r, "revid", 0)), reverse=True | ||
) | ||
latestUser = pageHistory.revisions[0].user | ||
self.assertEqual(latestUser, pageHistory.getFirstUser(reverse=True)) |
Oops, something went wrong.