|
| 1 | +"""Tests for TypeDirectedDocumentLoader.""" |
| 2 | + |
| 3 | +import json |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from pyld import ( |
| 9 | + FileDocumentLoader, |
| 10 | + FrozenDocumentLoader, |
| 11 | + SchemeDirectedDocumentLoader, |
| 12 | + TypeDirectedDocumentLoader, |
| 13 | + jsonld, |
| 14 | +) |
| 15 | +from pyld.jsonld import JsonLdError |
| 16 | + |
| 17 | +_CONTEXT_URL = 'https://example.com/context' |
| 18 | +_CONTEXT = {'@context': {'name': 'http://schema.org/name'}} |
| 19 | +_PERSON = { |
| 20 | + '@context': _CONTEXT_URL, |
| 21 | + 'name': 'Ada Lovelace', |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +def test_dispatches_path_to_file_loader(tmp_path): |
| 26 | + """A pathlib.Path is dispatched to the Path loader.""" |
| 27 | + path = tmp_path / 'person.jsonld' |
| 28 | + path.write_text(json.dumps(_PERSON), encoding='utf-8') |
| 29 | + loader = TypeDirectedDocumentLoader( |
| 30 | + { |
| 31 | + Path: FileDocumentLoader(), |
| 32 | + } |
| 33 | + ) |
| 34 | + |
| 35 | + result = loader(path, {}) |
| 36 | + |
| 37 | + assert result['contentType'] == 'application/ld+json' |
| 38 | + assert json.loads(result['document']) == _PERSON |
| 39 | + assert result['documentUrl'] == path.resolve().as_uri() |
| 40 | + |
| 41 | + |
| 42 | +def test_path_subclass_matches_path_registration(tmp_path): |
| 43 | + """A concrete Path subclass matches a Path registration via isinstance.""" |
| 44 | + path = tmp_path / 'person.jsonld' |
| 45 | + path.write_text(json.dumps(_PERSON), encoding='utf-8') |
| 46 | + assert type(path) is not Path |
| 47 | + assert isinstance(path, Path) |
| 48 | + loader = TypeDirectedDocumentLoader( |
| 49 | + { |
| 50 | + Path: FileDocumentLoader(), |
| 51 | + } |
| 52 | + ) |
| 53 | + |
| 54 | + result = loader(path, {}) |
| 55 | + |
| 56 | + assert json.loads(result['document']) == _PERSON |
| 57 | + |
| 58 | + |
| 59 | +def test_dispatches_str_to_nested_scheme_loader(): |
| 60 | + """A str URL is dispatched to the str loader (e.g. by-scheme).""" |
| 61 | + http = FrozenDocumentLoader(documents={_CONTEXT_URL: _CONTEXT}) |
| 62 | + loader = TypeDirectedDocumentLoader( |
| 63 | + { |
| 64 | + str: SchemeDirectedDocumentLoader(https=http), |
| 65 | + } |
| 66 | + ) |
| 67 | + |
| 68 | + result = loader(_CONTEXT_URL, {}) |
| 69 | + |
| 70 | + assert result['document'] == _CONTEXT |
| 71 | + assert result['documentUrl'] == _CONTEXT_URL |
| 72 | + |
| 73 | + |
| 74 | +def test_unregistered_type_raises(): |
| 75 | + """An unregistered input type raises JsonLdError naming registered types.""" |
| 76 | + loader = TypeDirectedDocumentLoader( |
| 77 | + { |
| 78 | + Path: FileDocumentLoader(), |
| 79 | + } |
| 80 | + ) |
| 81 | + |
| 82 | + with pytest.raises(JsonLdError) as exc: |
| 83 | + loader('https://example.com/person.jsonld', {}) |
| 84 | + |
| 85 | + assert exc.value.code == 'loading document failed' |
| 86 | + assert exc.value.type == 'jsonld.InvalidUrl' |
| 87 | + assert exc.value.details['types'] == ['Path'] |
| 88 | + |
| 89 | + |
| 90 | +def test_load_path_with_remote_context(tmp_path): |
| 91 | + """Load a Path whose @context is an https URL via composed loaders.""" |
| 92 | + path = tmp_path / 'person.jsonld' |
| 93 | + path.write_text(json.dumps(_PERSON), encoding='utf-8') |
| 94 | + file_loader = FileDocumentLoader() |
| 95 | + http = FrozenDocumentLoader(documents={_CONTEXT_URL: _CONTEXT}) |
| 96 | + loader = TypeDirectedDocumentLoader( |
| 97 | + { |
| 98 | + Path: file_loader, |
| 99 | + str: SchemeDirectedDocumentLoader( |
| 100 | + file=file_loader, |
| 101 | + http=http, |
| 102 | + https=http, |
| 103 | + ), |
| 104 | + } |
| 105 | + ) |
| 106 | + |
| 107 | + remote = jsonld.load_document(path, options={'documentLoader': loader}) |
| 108 | + expanded = jsonld.expand( |
| 109 | + remote['document'], |
| 110 | + options={ |
| 111 | + 'documentLoader': loader, |
| 112 | + 'base': remote['documentUrl'], |
| 113 | + }, |
| 114 | + ) |
| 115 | + |
| 116 | + assert expanded == [{'http://schema.org/name': [{'@value': 'Ada Lovelace'}]}] |
0 commit comments