Skip to content

Commit

Permalink
Remove testing and debug code from rdflib
Browse files Browse the repository at this point in the history
This patch removes code from `rdflib/` that does not seem like it belongs
in `rdflib/`, most of it is related to doctest, some of it belongs in
`test/` and was moved to `test/test_misc/test_collection.py`, and yet
more of it seems to just be there for debugging purposes, though it
would possibly be better to put that in a separate place if it is needed
again or to debug using tests if possible.

Other changes:
- Removed an invocation of `rdflib.util.test` from `test_util.py`. This
  seems like an attempt to invoke doctest however pytest takes care of
  that so this is not needed.
  • Loading branch information
aucampia committed Apr 19, 2022
1 parent e30e386 commit fa28388
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 195 deletions.
46 changes: 0 additions & 46 deletions rdflib/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,49 +256,3 @@ def clear(self):
graph.remove((container, RDF.rest, None))
container = rest
return self


def test():
import doctest

doctest.testmod()


if __name__ == "__main__":
test()

from rdflib import Graph

g = Graph()

c = Collection(g, BNode())

assert len(c) == 0

c = Collection(g, BNode(), [Literal("1"), Literal("2"), Literal("3"), Literal("4")])

assert len(c) == 4

assert c[1] == Literal("2"), c[1]

del c[1]

assert list(c) == [Literal("1"), Literal("3"), Literal("4")], list(c)

try:
del c[500]
except IndexError:
pass

c.append(Literal("5"))

print(list(c))

for i in c:
print(i)

del c[3]

c.clear()

assert len(c) == 0
10 changes: 0 additions & 10 deletions rdflib/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,3 @@ def dispatch(self, event):
raise ValueError("unknown event type: %s" % type(event))
for l_ in lst:
l_(event)


def test():
import doctest

doctest.testmod()


if __name__ == "__main__":
test()
10 changes: 0 additions & 10 deletions rdflib/extras/infixowl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2183,13 +2183,3 @@ def CommonNSBindings(graph, additionalNS={}):
for prefix, uri in list(additionalNS.items()):
namespace_manager.bind(prefix, uri, override=False)
graph.namespace_manager = namespace_manager


def test():
import doctest

doctest.testmod()


if __name__ == "__main__":
test()
10 changes: 0 additions & 10 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -2459,13 +2459,3 @@ def __enter__(self):
def __exit__(self, *exc):
if exc[0] is None:
self.graph.addN(self.batch)


def test():
import doctest

doctest.testmod()


if __name__ == "__main__":
test()
5 changes: 1 addition & 4 deletions rdflib/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,10 +487,7 @@ def neg_path(p):


if __name__ == "__main__":

import doctest

doctest.testmod()
pass
else:
# monkey patch
# (these cannot be directly in terms.py
Expand Down
31 changes: 0 additions & 31 deletions rdflib/plugins/parsers/notation3.py
Original file line number Diff line number Diff line change
Expand Up @@ -1975,34 +1975,3 @@ def parse(self, source, graph, encoding="utf-8"):
conj_graph.namespace_manager = graph.namespace_manager

TurtleParser.parse(self, source, conj_graph, encoding, turtle=False)


def _test(): # pragma: no cover
import doctest

doctest.testmod()


def main(): # pragma: no cover
g = ConjunctiveGraph()

sink = RDFSink(g)
base_uri = "file://" + os.path.join(os.getcwd(), sys.argv[1])

p = SinkParser(sink, baseURI=base_uri)
p._bindings[""] = p._baseURI + "#"
p.startDoc()

f = open(sys.argv[1], "rb")
rdbytes = f.read()
f.close()

p.feed(rdbytes)
p.endDoc()
for t in g.quads((None, None, None)):

print(t)


if __name__ == "__main__":
main()
18 changes: 0 additions & 18 deletions rdflib/plugins/sparql/algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -1464,21 +1464,3 @@ def pp(p, ind=" "):
# it's update, just a list
for x in q:
pp(x)


if __name__ == "__main__":
import os.path
import sys

from rdflib.plugins.sparql import parser

if os.path.exists(sys.argv[1]):
q = open(sys.argv[1]).read()
else:
q = sys.argv[1]

pq = parser.parseQuery(q)
print(pq)
print("--------")
tq = translateQuery(pq)
pprintAlgebra(tq)
14 changes: 0 additions & 14 deletions rdflib/plugins/sparql/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1553,17 +1553,3 @@ def parseUpdate(q):

q = expandUnicodeEscapes(q)
return UpdateUnit.parseString(q, parseAll=True)[0]


if __name__ == "__main__":
import sys

DEBUG = True
try:
q = Query.parseString(sys.argv[1])
print("\nSyntax Tree:\n")
print(q)
except ParseException as err:
print(err.line)
print(" " * (err.column - 1) + "^")
print(err)
14 changes: 0 additions & 14 deletions rdflib/plugins/sparql/parserutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,19 +269,5 @@ def prettify_parsetree(t, indent="", depth=0):
return "".join(out)


if __name__ == "__main__":
import sys

from pyparsing import Word, nums

Number = Word(nums)
Number.setParseAction(lambda x: int(x[0]))
Plus = Comp("plus", Param("a", Number) + "+" + Param("b", Number))
Plus.setEvalFn(lambda self, ctx: self.a + self.b)

r = Plus.parseString(sys.argv[1])
print(r)
print(r[0].eval({}))

# hurrah for circular imports
from rdflib.plugins.sparql.sparql import NotBoundError, SPARQLError
9 changes: 0 additions & 9 deletions rdflib/plugins/sparql/results/tsvresults.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,3 @@ def convertTerm(self, t):
raise Exception("I dont know how to handle this: %s" % (t,))
else:
return t


if __name__ == "__main__":
import sys

r = Result.parse(source=sys.argv[1], format="tsv")
print(r.vars)
print(r.bindings)
# print r.serialize(format='json')
6 changes: 0 additions & 6 deletions rdflib/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -2133,9 +2133,3 @@ def recurse():
else:
# should not happen, in fact
raise Exception("I dont know how to compare XML Node type: %s" % node.nodeType)


if __name__ == "__main__":
import doctest

doctest.testmod()
20 changes: 0 additions & 20 deletions rdflib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,23 +428,3 @@ def get_tree(
tree.append(t)

return (mapper(root), sorted(tree, key=sortkey))


def test():
import doctest

doctest.testmod()


if __name__ == "__main__":
# try to make the tests work outside of the time zone they were written in
# import os, time
# os.environ['TZ'] = 'US/Pacific'
# try:
# time.tzset()
# except AttributeError, e:
# print e
# pass
# tzset missing! see
# http://mail.python.org/pipermail/python-dev/2003-April/034480.html
test() # pragma: no cover
41 changes: 41 additions & 0 deletions test/test_misc/test_collection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import logging

import pytest

from rdflib import BNode, Graph, Literal
from rdflib.collection import Collection


def test_scenario() -> None:
# Taken from https://github.com/RDFLib/rdflib/blob/8a92d3565bf2e502a7c4cadb34b29db72c89d623/rdflib/collection.py#L272-L304
g = Graph()

c = Collection(g, BNode())

assert len(c) == 0

c = Collection(g, BNode(), [Literal("1"), Literal("2"), Literal("3"), Literal("4")])

assert len(c) == 4

assert c[1] == Literal("2"), c[1]

del c[1]

assert list(c) == [Literal("1"), Literal("3"), Literal("4")], list(c)

with pytest.raises(IndexError):
del c[500]

c.append(Literal("5"))

logging.debug("list(c) = %s", list(c))

for i in c:
logging.debug("i = %s", i)

del c[3]

c.clear()

assert len(c) == 0
3 changes: 0 additions & 3 deletions test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ def test_util_uniq(self):
r = util.uniq(base + base, strip=True)
assert sorted(r) == sorted(base)

def test_coverage_dodge(self):
util.test()


class TestUtilDateTime:
def setup_method(self):
Expand Down

0 comments on commit fa28388

Please sign in to comment.