Skip to content

Commit e8ab958

Browse files
committed
Fix tests:
- Fix test due to using deprecated rdflib ConjunctiveGraph instead of Dataset - Comment out 3 tests that were targeting endpoints that are not available anymore. Apart for http://zbw.eu/beta/sparql/stw/query that errors on POST queries but works on GET, might need to be investigated - Fix CLI tests failing due to slightly different result in response, now checking the length of results bindings, this will be more reliable in the future and is a good marker for a successful query - Fix tests for error message by checking if the error is in the message (instead of checking only the end of the message) - Added pytest and using it to run the tests. It is a more modern and reliable solution that works with the current code. unittest had a bug with the 3 tests where warnings are caught (when running each test alone, they work, but when running all with unittest catching the warnings stops working, but it works with pytest). I fixed the bug (so now all tests works with pytest and unittest, but the trust in unittest is lost). Fixes #192 - Added python 3.11 and 3.12 to the github actions tests
1 parent b3c0d30 commit e8ab958

23 files changed

+4466
-4721
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ jobs:
1313
fail-fast: false
1414
max-parallel: 1
1515
matrix:
16-
python-version: ["3.7", "3.8", "3.9", "3.10"]
16+
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
1717
steps:
18-
- uses: actions/checkout@v2
18+
- uses: actions/checkout@v4
1919
- name: Set up Python ${{ matrix.python-version }}
20-
uses: actions/setup-python@v2
20+
uses: actions/setup-python@v5
2121
with:
2222
python-version: ${{ matrix.python-version }}
2323
- name: Install dependencies
@@ -26,7 +26,7 @@ jobs:
2626
pip install '.[dev]'
2727
- name: Mypy
2828
run: |
29-
mypy --show-error-context --show-error-codes
29+
mypy --show-error-context
3030
- name: Test [wrapper]
3131
if: ${{ always() }}
3232
run: |

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ doc
1111
.settings/
1212
.venv/
1313
.tox/
14+
.coverage

README.rst

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ You can install SPARQLWrapper from GitHub::
3535
You can install SPARQLWrapper from Debian::
3636

3737
$ sudo apt-get install python-sparqlwrapper
38-
38+
3939
.. note::
4040

4141
Be aware that there could be a gap between the latest version of SPARQLWrapper
4242
and the version available as Debian package.
4343

44-
Also, the source code of the package can be downloaded
44+
Also, the source code of the package can be downloaded
4545
in ``.zip`` and ``.tar.gz`` formats from `GitHub SPARQLWrapper releases <https://github.com/RDFLib/sparqlwrapper/releases>`_.
4646
Documentation is included in the distribution.
4747

@@ -125,9 +125,9 @@ This query gets a boolean response from DBPedia's SPARQL endpoint:
125125
126126
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
127127
sparql.setQuery("""
128-
ASK WHERE {
128+
ASK WHERE {
129129
<http://dbpedia.org/resource/Asturias> rdfs:label "Asturias"@es
130-
}
130+
}
131131
""")
132132
sparql.setReturnFormat(XML)
133133
results = sparql.query().convert()
@@ -307,7 +307,7 @@ Return formats
307307

308308
The expected return formats differs per query type (``SELECT``, ``ASK``, ``CONSTRUCT``, ``DESCRIBE``...).
309309

310-
.. note:: From the `SPARQL specification <https://www.w3.org/TR/sparql11-protocol/#query-success>`_,
310+
.. note:: From the `SPARQL specification <https://www.w3.org/TR/sparql11-protocol/#query-success>`_,
311311
*The response body of a successful query operation with a 2XX response is either:*
312312

313313
* ``SELECT`` and ``ASK``: a SPARQL Results Document in XML, JSON, or CSV/TSV format.
@@ -430,12 +430,12 @@ in the return value). This features becomes particularly useful when the ``OPTIO
430430
GET or POST
431431
^^^^^^^^^^^
432432

433-
By default, all SPARQL services are invoked using HTTP **GET** verb. However,
433+
By default, all SPARQL services are invoked using HTTP **GET** verb. However,
434434
**POST** might be useful if the size of the query
435435
extends a reasonable size; this can be set in the query instance.
436436

437437
Note that some combinations may not work yet with all SPARQL processors
438-
(e.g., there are implementations where **POST + JSON return** does not work).
438+
(e.g., there are implementations where **POST + JSON return** does not work).
439439
Hopefully, this problem will eventually disappear.
440440

441441

@@ -914,8 +914,7 @@ Organizations involved:
914914
* `Salzburg Research <http://www.salzburgresearch.at>`_
915915
* `Foundation CTIC <http://www.fundacionctic.org/>`_
916916

917-
918-
.. |Build Status| image:: https://secure.travis-ci.org/RDFLib/sparqlwrapper.svg?branch=master
919-
:target: https://travis-ci.org/RDFLib/sparqlwrapper
917+
.. |Build Status| image:: https://github.com/RDFLib/sparqlwrapper/actions/workflows/test.yml/badge.svg
918+
:target: https://github.com/RDFLib/sparqlwrapper/actions/workflows/test.yml
920919
.. |PyPi version| image:: https://badge.fury.io/py/SPARQLWrapper.svg
921920
:target: https://pypi.python.org/pypi/SPARQLWrapper

SPARQLWrapper/Wrapper.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
from SPARQLWrapper import __agent__
4040

4141
if TYPE_CHECKING:
42-
from rdflib import Graph
42+
from rdflib import Dataset, Graph
4343

4444

4545

@@ -649,7 +649,7 @@ def setUseKeepAlive(self) -> None:
649649
:raises ImportError: when could not be imported ``keepalive.HTTPHandler``.
650650
"""
651651
try:
652-
from keepalive import HTTPHandler # type: ignore[import]
652+
from keepalive import HTTPHandler # type: ignore[import-not-found]
653653

654654
if urllib.request._opener and any( # type: ignore[attr-defined]
655655
isinstance(h, HTTPHandler) for h in urllib.request._opener.handlers # type: ignore[attr-defined]
@@ -1071,21 +1071,21 @@ def _convertXML(self) -> Document:
10711071
:return: converted result.
10721072
:rtype: :class:`xml.dom.minidom.Document`
10731073
"""
1074-
doc = parse(self.response)
1074+
doc: Any = parse(self.response)
10751075
rdoc = cast(Document, doc)
10761076
return rdoc
10771077

1078-
def _convertRDF(self) -> "Graph":
1078+
def _convertRDF(self) -> "Dataset":
10791079
"""
10801080
Convert a RDF/XML result into an RDFLib Graph. This method can be overwritten
10811081
in a subclass for a different conversion method.
10821082
10831083
:return: converted result.
10841084
:rtype: :class:`rdflib.graph.Graph`
10851085
"""
1086-
from rdflib import ConjunctiveGraph
1087-
retval = ConjunctiveGraph()
1088-
retval.parse(self.response, format="xml") # type: ignore[no-untyped-call]
1086+
from rdflib import Dataset
1087+
retval = Dataset()
1088+
retval.parse(self.response, format="xml")
10891089
return retval
10901090

10911091
def _convertN3(self) -> bytes:
@@ -1118,18 +1118,18 @@ def _convertTSV(self) -> bytes:
11181118
"""
11191119
return self.response.read()
11201120

1121-
def _convertJSONLD(self) -> "Graph":
1121+
def _convertJSONLD(self) -> "Dataset":
11221122
"""
11231123
Convert a RDF JSON-LD result into an RDFLib Graph. This method can be overwritten
11241124
in a subclass for a different conversion method.
11251125
11261126
:return: converted result
11271127
:rtype: :class:`rdflib.graph.Graph`
11281128
"""
1129-
from rdflib import ConjunctiveGraph
1129+
from rdflib import Dataset
11301130

1131-
retval = ConjunctiveGraph()
1132-
retval.parse(self.response, format="json-ld") # type: ignore[no-untyped-call]
1131+
retval = Dataset()
1132+
retval.parse(self.response, format="json-ld")
11331133
return retval
11341134

11351135
def convert(self) -> ConvertResult:

SPARQLWrapper/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def main(test: Optional[List[str]] = None) -> None:
145145
elif isinstance(results, bytes):
146146
# "csv", "tsv", "turtle", "n3"
147147
print(results.decode("utf-8"))
148-
elif isinstance(results, rdflib.graph.ConjunctiveGraph):
148+
elif isinstance(results, rdflib.graph.Dataset):
149149
# "rdf"
150150
print(results.serialize())
151151
else:

SPARQLWrapper/sparql_dataframe.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def get_sparql_typed_dict(
5858
row = {}
5959
for k in x:
6060
v = x[k]
61-
vv = rdflib.term.Literal(v.value, datatype=v.datatype).toPython() # type: ignore[no-untyped-call]
61+
vv = rdflib.term.Literal(v.value, datatype=v.datatype).toPython()
6262
row[k] = vv
6363
d.append(row)
6464
return d

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
[build-system]
22
requires = ["wheel", "setuptools"]
33
build-backend = "setuptools.build_meta"
4+
5+
[tool.pytest.ini_options]
6+
addopts = [
7+
"--cov=SPARQLWrapper",
8+
"--cov-report=term-missing",
9+
"--durations=10", # Show 10 slowest tests durations
10+
]

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ dev =
4949
mypy>=0.931
5050
pandas>=1.3.5
5151
pandas-stubs>=1.2.0.48
52+
pytest>=8.3.5
53+
pytest-cov>=6.1.1
5254

5355
pandas =
5456
pandas>=1.3.5

test/test.rq

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ WHERE {
55
?pl
66
a dbo:ProgrammingLanguage;
77
rdfs:label ?pllabel.
8-
FILTER(SUBSTR(STR(?pllabel), 1, 1) = "P")
8+
FILTER(SUBSTR(STR(?pllabel), 1, 5) = "PARLO")
99
} ORDER BY ?pllabel LIMIT 1

0 commit comments

Comments
 (0)