Skip to content

Commit 021e18e

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 2abf3b8 commit 021e18e

File tree

1 file changed

+39
-91
lines changed

1 file changed

+39
-91
lines changed

test/test_graph/test_namespace_rebinding.py

Lines changed: 39 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
from test.data import context1, context2, tarek
2+
13
import pytest
24

3-
from rdflib.term import URIRef
45
from rdflib import ConjunctiveGraph, Graph, Literal
6+
from rdflib.namespace import OWL, Namespace
57
from rdflib.plugins.stores.memory import Memory
6-
from rdflib.namespace import Namespace
7-
from rdflib.namespace import OWL
8-
from test.data import tarek, context1, context2
8+
from rdflib.term import URIRef
99

1010
foaf1_uri = URIRef("http://xmlns.com/foaf/0.1/")
1111
foaf2_uri = URIRef("http://xmlns.com/foaf/2.0/")
@@ -34,19 +34,13 @@ def test_binding_replace():
3434
g.bind("foaf", FOAF1)
3535
g.bind("foaf2", FOAF2)
3636
assert len(list(g.namespaces())) == 2
37-
assert list(g.namespaces()) == [
38-
('foaf', foaf1_uri),
39-
("foaf2", foaf2_uri)
40-
]
37+
assert list(g.namespaces()) == [('foaf', foaf1_uri), ("foaf2", foaf2_uri)]
4138

4239
# Now you want to "upgrade" to FOAF2=>foaf and try the obvious:
4340
g.bind("foaf", FOAF2)
4441

4542
# But that doesn't happen, instead a different prefix, `foaf1` is bound:
46-
assert list(g.namespaces()) == [
47-
("foaf", foaf1_uri),
48-
("foaf1", foaf2_uri)
49-
]
43+
assert list(g.namespaces()) == [("foaf", foaf1_uri), ("foaf1", foaf2_uri)]
5044

5145
# The rationale behind this behaviour is that the prefix "foaf" was already
5246
# bound to the FOAF1 namespace, so a different prefix for the FOAF2 namespace
@@ -59,9 +53,7 @@ def test_binding_replace():
5953
# "foaf2" prefix has gone and because the "foaf" prefix was rebound to a
6054
# different namespace, the old FOAF1 namespace has gone, leaving just:
6155

62-
assert list(g.namespaces()) == [
63-
("foaf", foaf2_uri)
64-
]
56+
assert list(g.namespaces()) == [("foaf", foaf2_uri)]
6557

6658
# Now, if you wish, you can re-bind the FOAF1.0 namespace to a prefix
6759
# of your choice
@@ -80,10 +72,7 @@ def test_binding_replace():
8072

8173
# Which, as things stand, results in:
8274

83-
assert list(g.namespaces()) == [
84-
("foaf", foaf2_uri),
85-
('foaf1', foaf1_uri)
86-
]
75+
assert list(g.namespaces()) == [("foaf", foaf2_uri), ('foaf1', foaf1_uri)]
8776

8877
# In which the attempted change from `oldfoaf` to (the already
8978
# bound-to-a different-namespace `foaf`) was intercepted and
@@ -93,19 +82,13 @@ def test_binding_replace():
9382
g.bind("oldfoaf", FOAF1, replace=True)
9483

9584
# The bindings are again as desired
96-
assert list(g.namespaces()) == [
97-
("foaf", foaf2_uri),
98-
('oldfoaf', foaf1_uri)
99-
]
85+
assert list(g.namespaces()) == [("foaf", foaf2_uri), ('oldfoaf', foaf1_uri)]
10086

10187
# Next time through, set override to False
10288
g.bind("foaf", FOAF1, override=False)
10389

10490
# And the bindings will remain as desired
105-
assert list(g.namespaces()) == [
106-
("foaf", foaf2_uri),
107-
('oldfoaf', foaf1_uri)
108-
]
91+
assert list(g.namespaces()) == [("foaf", foaf2_uri), ('oldfoaf', foaf1_uri)]
10992

11093
# 3. Parsing data with prefix=>namespace bindings
11194
# Let's see the situation regarding namespace bindings
@@ -125,10 +108,7 @@ def test_binding_replace():
125108
# non-clashing prefix of `foaf1` is rebound to FOAF1 in
126109
# place of the existing `oldfoaf` prefix
127110

128-
assert list(g.namespaces()) == [
129-
("foaf", foaf2_uri),
130-
('foaf1', foaf1_uri)
131-
]
111+
assert list(g.namespaces()) == [("foaf", foaf2_uri), ('foaf1', foaf1_uri)]
132112

133113
# Yes, namespace bindings in parsed data replace existing
134114
# bindings (i.e. `oldfoaf` was replaced by `foaf1`), just
@@ -253,7 +233,9 @@ def test_parse_rebinds_prefix():
253233
assert ('friend-of-a-friend', foaf1_uri) in list(g.namespaces())
254234

255235

256-
@pytest.mark.xfail(reason="Automatic handling of unknown predicates not automatically registered with namespace manager")
236+
@pytest.mark.xfail(
237+
reason="Automatic handling of unknown predicates not automatically registered with namespace manager"
238+
)
257239
def test_automatic_handling_of_unknown_predicates():
258240

259241
# AUTOMATIC HANDLING OF UNKNOWN PREDICATES
@@ -297,30 +279,20 @@ def test_multigraph_bindings():
297279
g1 = Graph(store, identifier=context1, bind_namespaces="none")
298280

299281
g1.bind('foaf', FOAF1)
300-
assert list(g1.namespaces()) == [
301-
('foaf', foaf1_uri)
302-
]
303-
assert list(store.namespaces()) == [
304-
('foaf', foaf1_uri)
305-
]
282+
assert list(g1.namespaces()) == [('foaf', foaf1_uri)]
283+
assert list(store.namespaces()) == [('foaf', foaf1_uri)]
306284

307285
g1.add((tarek, FOAF1.name, Literal("tarek")))
308286

309-
assert list(store.namespaces()) == [
310-
('foaf', foaf1_uri)
311-
]
287+
assert list(store.namespaces()) == [('foaf', foaf1_uri)]
312288

313289
g2 = Graph(store, identifier=context2, bind_namespaces="none")
314290
g2.parse(data=data, format="n3")
315291

316292
# The parser-caused rebind is in the underlying store and all objects
317293
# that use the store see the changed binding:
318-
assert list(store.namespaces()) == [
319-
('friend-of-a-friend', foaf1_uri)
320-
]
321-
assert list(g1.namespaces()) == [
322-
('friend-of-a-friend', foaf1_uri)
323-
]
294+
assert list(store.namespaces()) == [('friend-of-a-friend', foaf1_uri)]
295+
assert list(g1.namespaces()) == [('friend-of-a-friend', foaf1_uri)]
324296

325297
# Including newly-created objects that use the store
326298
cg = ConjunctiveGraph(store=store)
@@ -336,11 +308,15 @@ def test_multigraph_bindings():
336308
cg.store.add_graph(g1)
337309
cg.store.add_graph(g2)
338310

339-
assert "@prefix friend-of-a-friend: <http://xmlns.com/foaf/0.1/>" in cg.serialize(format="n3")
311+
assert "@prefix friend-of-a-friend: <http://xmlns.com/foaf/0.1/>" in cg.serialize(
312+
format="n3"
313+
)
340314

341315
# In the notation3 format, the statement asserting tarek's name
342316
# now references the changed prefix:
343-
assert '<urn:example:tarek> friend-of-a-friend:name "tarek" .' in cg.serialize(format="n3")
317+
assert '<urn:example:tarek> friend-of-a-friend:name "tarek" .' in cg.serialize(
318+
format="n3"
319+
)
344320

345321
# Add foaf2 binding if not already bound
346322
cg.bind("foaf2", FOAF2, override=False)
@@ -354,81 +330,59 @@ def test_multigraph_bindings():
354330
def test_new_namespace_new_prefix():
355331
g = Graph(bind_namespaces="none")
356332
g.bind("foaf", FOAF1)
357-
assert list(g.namespaces()) == [
358-
('foaf', foaf1_uri)
359-
]
333+
assert list(g.namespaces()) == [('foaf', foaf1_uri)]
360334

361335

362336
def test_change_prefix_override_true():
363337
g = Graph(bind_namespaces="none")
364338

365339
g.bind("foaf", FOAF1)
366-
assert list(g.namespaces()) == [
367-
('foaf', foaf1_uri)
368-
]
340+
assert list(g.namespaces()) == [('foaf', foaf1_uri)]
369341

370342
g.bind("oldfoaf", FOAF1)
371343
# Changed
372-
assert list(g.namespaces()) == [
373-
("oldfoaf", foaf1_uri)
374-
]
344+
assert list(g.namespaces()) == [("oldfoaf", foaf1_uri)]
375345

376346

377347
def test_change_prefix_override_false():
378348
g = Graph(bind_namespaces="none")
379349

380350
g.bind("foaf", FOAF1)
381-
assert list(g.namespaces()) == [
382-
('foaf', foaf1_uri)
383-
]
351+
assert list(g.namespaces()) == [('foaf', foaf1_uri)]
384352

385353
g.bind("oldfoaf", FOAF1, override=False)
386354
# No change
387-
assert list(g.namespaces()) == [
388-
("foaf", foaf1_uri)
389-
]
355+
assert list(g.namespaces()) == [("foaf", foaf1_uri)]
390356

391357

392358
def test_change_namespace_override_true():
393359
g = Graph(bind_namespaces="none")
394360

395361
g.bind("foaf", FOAF1)
396-
assert list(g.namespaces()) == [
397-
('foaf', foaf1_uri)
398-
]
362+
assert list(g.namespaces()) == [('foaf', foaf1_uri)]
399363

400364
g.bind("foaf", FOAF2)
401365
# Different prefix used
402-
assert list(g.namespaces()) == [
403-
("foaf", foaf1_uri),
404-
("foaf1", foaf2_uri)
405-
]
366+
assert list(g.namespaces()) == [("foaf", foaf1_uri), ("foaf1", foaf2_uri)]
406367

407368

408369
def test_change_namespace_override_false():
409370
g = Graph(bind_namespaces="none")
410371

411372
g.bind("foaf", FOAF1)
412-
assert list(g.namespaces()) == [
413-
('foaf', foaf1_uri)
414-
]
373+
assert list(g.namespaces()) == [('foaf', foaf1_uri)]
415374

416375
# Different namespace so override is irrelevant in this case
417376
g.bind("foaf", FOAF2, override=False)
418377
# Different prefix used
419-
assert list(g.namespaces()) == [
420-
("foaf", foaf1_uri),
421-
("foaf1", foaf2_uri)
422-
]
378+
assert list(g.namespaces()) == [("foaf", foaf1_uri), ("foaf1", foaf2_uri)]
423379

424380

425381
def test_new_namespace_override_false():
426382
g = Graph(bind_namespaces="none")
427383

428384
g.bind("foaf", FOAF2)
429-
assert list(g.namespaces()) == [
430-
('foaf', foaf2_uri)
431-
]
385+
assert list(g.namespaces()) == [('foaf', foaf2_uri)]
432386

433387
# Namespace not already bound so override is irrelevant in this case
434388
g.bind("owl", OWL, override=False)
@@ -438,6 +392,7 @@ def test_new_namespace_override_false():
438392
('owl', URIRef('http://www.w3.org/2002/07/owl#')),
439393
]
440394

395+
441396
# @pytest.mark.xfail()
442397
def test_change_namespace_and_prefix():
443398

@@ -448,21 +403,14 @@ def test_change_namespace_and_prefix():
448403
g = Graph(bind_namespaces="none")
449404

450405
g.bind("foaf", FOAF1)
451-
assert list(g.namespaces()) == [
452-
('foaf', foaf1_uri)
453-
]
406+
assert list(g.namespaces()) == [('foaf', foaf1_uri)]
454407

455408
g.bind("foaf", FOAF2, replace=True)
456-
assert list(g.namespaces()) == [
457-
("foaf", foaf2_uri)
458-
]
409+
assert list(g.namespaces()) == [("foaf", foaf2_uri)]
459410

460411
g.bind("foaf1", FOAF1)
461412

462-
assert list(g.namespaces()) == [
463-
("foaf", foaf2_uri),
464-
("foaf1", foaf1_uri)
465-
]
413+
assert list(g.namespaces()) == [("foaf", foaf2_uri), ("foaf1", foaf1_uri)]
466414

467415
foaf3_uri = URIRef("http://xmlns.com/foaf/3.0/")
468416
FOAF3 = Namespace("http://xmlns.com/foaf/3.0/")

0 commit comments

Comments
 (0)