Skip to content

Commit

Permalink
improves test refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfgangFahl committed Nov 24, 2023
1 parent ac2a1ca commit 1818ffd
Show file tree
Hide file tree
Showing 11 changed files with 307 additions and 302 deletions.
34 changes: 19 additions & 15 deletions tests/base_skg_test.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
'''
"""
Created on 2022-11-21
@author: wf
'''
from tests.basetest import Basetest
"""
from ngwidgets.basetest import Basetest
from skg.kg import SKG_Def


class BaseSkgTest(Basetest):
"""
extend the Basetest
"""

def setUp(self, debug=False, profile=True):
Basetest.setUp(self, debug=debug, profile=profile)
self.skg_def=SKG_Def()

def check_id_examples(self,id_examples,createFunc,checkItem,debug:bool=False):
self.skg_def = SKG_Def()

def check_id_examples(
self, id_examples, createFunc, checkItem, debug: bool = False
):
"""
check the given examples
Args:
id_examples(list): a list of dict with examples
createFunct(func): a function to be used to create items
checkItem(func): a function to be called for testing each item
debug(bool): if True show debug information
"""
for id_example in id_examples:
id_name=id_example["id_name"]
id_value=id_example["id_value"]
id_concept=id_example["concept"]
id_name = id_example["id_name"]
id_value = id_example["id_value"]
id_concept = id_example["concept"]
# use createFunc as a factory
items=createFunc(id_concept,id_name,id_value)
items = createFunc(id_concept, id_name, id_value)
if debug:
for item in items:
print(item)
self.assertEqual(1,len(items))
item=items[0]
self.assertEqual(item.concept.name,id_concept.name)
checkItem(item,id_name,id_value,debug)
self.assertEqual(1, len(items))
item = items[0]
self.assertEqual(item.concept.name, id_concept.name)
checkItem(item, id_name, id_value, debug)
39 changes: 21 additions & 18 deletions tests/testScholarGrid.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,46 @@
'''
"""
Created on 2023-01-04
@author: wf
'''
from tests.basetest import Basetest
"""
from ngwidgets.basetest import Basetest
from skg.scholargrid import ScholarGrid, ScholarQuery
from wikibot3rd.wikiuser import WikiUser
from spreadsheet.wbquery import WikibaseQuery
from skg.wikidata import Wikidata


class TestScholarGrid(Basetest):
"""
test Scholar Grid behavior
"""

def testScholarQuery(self):
"""
test the Wikibase Query for Scholars
"""
debug=True
sq=ScholarQuery.get()
debug = True
sq = ScholarQuery.get()
self.assertIsNotNone(sq)
self.assertTrue(isinstance(sq,WikibaseQuery))
self.assertTrue(isinstance(sq, WikibaseQuery))
if debug:
print(sq)

def testGetScholars(self):
"""
test getting scholars
"""
wikiUsers=WikiUser.getWikiUsers()
wikidata=Wikidata()
sparql=wikidata.sparql
for wikiId,expected in [("ceur-ws",10),("media",500)]:
wikiUsers = WikiUser.getWikiUsers()
wikidata = Wikidata()
sparql = wikidata.sparql
for wikiId, expected in [("ceur-ws", 10), ("media", 500)]:
if wikiId in wikiUsers:
scholarGrid=ScholarGrid(app=None,wikiUsers=wikiUsers,wikiId=wikiId,sparql=sparql)
scholars=scholarGrid.getScholars()
debug=self.debug
debug=True
scholarGrid = ScholarGrid(
app=None, wikiUsers=wikiUsers, wikiId=wikiId, sparql=sparql
)
scholars = scholarGrid.getScholars()
debug = self.debug
debug = True
if debug:
print(f"found {len(scholars)} scholars in {wikiId} wiki" )
self.assertTrue(len(scholars)>expected)
print(f"found {len(scholars)} scholars in {wikiId} wiki")
self.assertTrue(len(scholars) > expected)
57 changes: 29 additions & 28 deletions tests/test_crossref.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,53 @@
'''
"""
Created on 2022-11-17
@author: wf
'''
from tests.basetest import Basetest
"""
from ngwidgets.basetest import Basetest
from skg.crossref import Crossref
import json
from dataclasses import dataclass


@dataclass
class Example:
doi:str
author:str

doi: str
author: str


class TestCrossref(Basetest):
"""
test crossref access
"""

def setUp(self, debug=False, profile=True):
Basetest.setUp(self, debug=debug, profile=profile)
self.crossref=Crossref()


self.crossref = Crossref()

def test_crossref(self):
"""
test crossref
"""
debug=self.debug
#debug=True
doi_examples=[
Example("10.1007/11581116_19","Atanas Kiryakov"),
Example("10.1016/J.ARTMED.2017.07.002","Jean-Baptiste Lamy"),
Example("10.1145/2882903.2899389","Rihan Hai")
"""
debug = self.debug
# debug=True
doi_examples = [
Example("10.1007/11581116_19", "Atanas Kiryakov"),
Example("10.1016/J.ARTMED.2017.07.002", "Jean-Baptiste Lamy"),
Example("10.1145/2882903.2899389", "Rihan Hai"),
]
for example in doi_examples:
doi=example.doi
bib_entry=self.crossref.doiBibEntry(doi)
doi = example.doi
bib_entry = self.crossref.doiBibEntry(doi)
if debug:
print(bib_entry)
expected=f"author = {{{example.author}"
self.assertTrue(expected in bib_entry,expected)
meta_data=self.crossref.doiMetaData(doi)
expected = f"author = {{{example.author}"
self.assertTrue(expected in bib_entry, expected)
meta_data = self.crossref.doiMetaData(doi)
if debug:
print(json.dumps(meta_data,indent=2))
print(json.dumps(meta_data, indent=2))
self.assertTrue("DOI" in meta_data)
self.assertEqual(doi.lower(),meta_data["DOI"])
scite_entry=self.crossref.asScite(meta_data)
self.assertEqual(doi.lower(), meta_data["DOI"])
scite_entry = self.crossref.asScite(meta_data)
if debug:
print(scite_entry)

Expand All @@ -55,9 +56,9 @@ def test_cookies(self):
test session cookies default
"""
import requests

session = requests.Session()
cookie_dict=session.cookies.get_dict()
cookie_dict = session.cookies.get_dict()
if self.debug:
print(session.cookies.get_dict())
self.assertEqual({},cookie_dict)

self.assertEqual({}, cookie_dict)
112 changes: 60 additions & 52 deletions tests/test_doi.py
Original file line number Diff line number Diff line change
@@ -1,111 +1,119 @@
'''
"""
Created on 2022-11-22
@author: wf
'''
from tests.basetest import Basetest
"""
from ngwidgets.basetest import Basetest
from skg.doi import DOI
from skg.dblp import Dblp
from dataclasses import dataclass
from unittest import IsolatedAsyncioTestCase
import json


@dataclass
class DOIExample:
doi: str
isDoi: bool



class TestDOI(Basetest):
"""
test DOI access
"""

def testDOICheck(self):
"""
check DOI regex
"""
doi_examples=[
DOIExample("10.1007/11581116_19",True),
DOIExample("10.1109/TBDATA.2022.3224749",True),
DOIExample("xyz",False)]
doi_examples = [
DOIExample("10.1007/11581116_19", True),
DOIExample("10.1109/TBDATA.2022.3224749", True),
DOIExample("xyz", False),
]
for doi_example in doi_examples:
self.assertTrue(DOI.isDOI(doi_example.doi)==doi_example.isDoi,doi_example.doi)

self.assertTrue(
DOI.isDOI(doi_example.doi) == doi_example.isDoi, doi_example.doi
)

def test_dblp_dois(self):
"""
test dblp dois
"""
dblp=Dblp()
limit=5
debug=True
paper_records=dblp.get_paper_records("CEUR Workshop Proceedings","publishedin",limit=limit,debug=debug)
dblp = Dblp()
limit = 5
debug = True
paper_records = dblp.get_paper_records(
"CEUR Workshop Proceedings", "publishedin", limit=limit, debug=debug
)
for paper_record in paper_records:
print(paper_record)

def testDOILookup(self):
"""
test DOI lookup
test DOI lookup
"""
debug=True
dois=["10.1109/TBDATA.2022.3224749"]
expected=["@article{Li_2023,","@inproceedings{Faruqui_2015,"]
for i,doi in enumerate(dois):
doi_obj=DOI(doi)
result=doi_obj.doi2bibTex()
debug = True
dois = ["10.1109/TBDATA.2022.3224749"]
expected = ["@article{Li_2023,", "@inproceedings{Faruqui_2015,"]
for i, doi in enumerate(dois):
doi_obj = DOI(doi)
result = doi_obj.doi2bibTex()
if debug:
print(result)
self.assertTrue(result.startswith(expected[i]))

def testCiteproc(self):
"""
cite proc lookup
"""
dois=["10.3115/v1/N15-1184",
"10.1007/s13042-022-01686-5",
"10.3390/info13120562",
"10.48550/arXiv.2211.16865"]
debug=True
"""
dois = [
"10.3115/v1/N15-1184",
"10.1007/s13042-022-01686-5",
"10.3390/info13120562",
"10.48550/arXiv.2211.16865",
]
debug = True
for doi in dois:
doi_obj=DOI(doi)
json_data=doi_obj.doi2Citeproc()
doi_obj = DOI(doi)
json_data = doi_obj.doi2Citeproc()
if debug:
print(json.dumps(json_data,indent=2))
print(json.dumps(json_data, indent=2))
self.assertTrue("DOI" in json_data)
self.assertEqual(doi.lower(),json_data["DOI"].lower())
self.assertEqual(doi.lower(), json_data["DOI"].lower())

def testDataCiteLookup(self):
"""
test the dataCite Lookup api
"""
debug=True
dois=["10.5438/0012"]
debug = True
dois = ["10.5438/0012"]
for doi in dois:
doi_obj=DOI(doi)
json_data=doi_obj.dataCiteLookup()
doi_obj = DOI(doi)
json_data = doi_obj.dataCiteLookup()
if debug:
print(json.dumps(json_data,indent=2))
print(json.dumps(json_data, indent=2))
self.assertTrue("data" in json_data)
data=json_data["data"]
data = json_data["data"]
self.assertTrue("id" in data)
self.assertEquals(doi,data["id"])
self.assertEquals(doi, data["id"])
pass




def testScite(self):
"""
test Semantic Cite conversion
"""
dois=["10.1007/978-3-031-21595-7_17","10.48550/ARXIV.2211.16865"]
debug=self.debug
dois = ["10.1007/978-3-031-21595-7_17", "10.48550/ARXIV.2211.16865"]
debug = self.debug
for doi in dois:
doi_obj=DOI(doi)
markup=doi_obj.asScite()
doi_obj = DOI(doi)
markup = doi_obj.asScite()
if debug:
print(markup)
self.assertTrue(f"|doi={doi}" in markup)

#class TestDOILookup(IsolatedAsyncioTestCase):


# class TestDOILookup(IsolatedAsyncioTestCase):
# """
# test DOI lookup
# """
# """
Loading

0 comments on commit 1818ffd

Please sign in to comment.