Exemple #1
0
def test_clear_named_graph(root_context: LDContext):
    """Update Octiron graph from a YAML file."""
    data_dir = Path(__file__).parent / 'data'

    octiron = Octiron(
        root_directory=data_dir,
        root_context=root_context,
    )

    octiron.update_from_file(
        path=data_dir / 'test.yaml',
        local_iri=LOCAL_IRI,
    )

    assert set(octiron.graph) == {
        (BNode(f'{LOCAL_IRI}/b0'), RDF.subject, Literal('s')),
        (BNode(f'{LOCAL_IRI}/b1'), RDF.predicate, Literal('p')),
        (BNode(f'{LOCAL_IRI}/b2'), RDF.object, Literal('o')),
        (LOCAL_IRI, LOCAL.given, Literal('spo')),
        (LOCAL_IRI, LOCAL.infer, BNode(f'{LOCAL_IRI}/b0')),
        (LOCAL_IRI, LOCAL.infer, BNode(f'{LOCAL_IRI}/b1')),
        (LOCAL_IRI, LOCAL.infer, BNode(f'{LOCAL_IRI}/b2')),
        (URIRef(LOCAL), RDF.type, OCTA.Directory),
        (LOCAL_IRI, OCTA.isChildOf, URIRef(LOCAL)),
        (LOCAL_IRI, OCTA.fileName, Literal('test.yaml')),
        (LOCAL_IRI, OCTA.subjectOf, LOCAL_IRI),
    }

    octiron.clear_named_graph(local_iri=LOCAL_IRI, )

    assert (LOCAL_IRI, LOCAL.given, Literal('spo')) not in set(octiron.graph)
Exemple #2
0
def test_yaml_list(root_context: LDContext):
    """Update Octiron graph from a YAML file."""
    data_dir = Path(__file__).parent / 'data'

    octiron = Octiron(
        root_directory=data_dir,
        root_context=root_context,
    )

    octiron.update_from_file(
        path=data_dir / 'test_yaml/list.yaml',
        local_iri=LOCAL_IRI,
    )

    assert (
        URIRef(RDF),
        LOCAL.prefix,
        Literal('rdf'),
    ) in octiron.graph

    triples = set(octiron.graph.triples((None, RDFS.label, None)))
    assert triples == {
        (BNode('local:test.yaml/b0'), RDFS.label, Literal('boo')),
        (BNode('local:test.yaml/b1'), RDFS.label, Literal('foo')),
    }
Exemple #3
0
def test_markdown_with_dollar_sign(root_context: LDContext):
    """Update Octiron graph from a Markdown file with a $context."""
    data_dir = Path(__file__).parent / 'data'

    octiron = Octiron(
        root_directory=data_dir,
        root_context=root_context,
    )

    octiron.update_from_file(
        path=data_dir / 'test_dollar_id.md',
        local_iri=LOCAL_IRI,
    )

    assert set(octiron.graph.quads()).issuperset({
        (
            LOCAL_IRI,
            RDFS.label,
            Literal('Hey, I am a test!'),
            Graph(identifier=LOCAL_IRI),
        ),
        (
            LOCAL_IRI,
            RDFS.domain,
            LOCAL.UnitTesting,
            Graph(identifier=LOCAL_IRI),
        ),
        (
            LOCAL_IRI,
            RDF.type,
            OCTA.Page,
            Graph(identifier=LOCAL_IRI),
        ),
    })
Exemple #4
0
def cached_octiron(docs_dir: Path) -> Octiron:
    """Retrieve cached Octiron instance or create it if absent."""
    return Octiron(
        root_directory=docs_dir,
        root_context=construct_root_context(
            namespaces=DEFAULT_NAMESPACES,
        ),
    )
Exemple #5
0
def test_file_in_docs_directory(root_context: LDContext):
    with tempfile.TemporaryDirectory() as temp_dir:
        temp_directory = Path(temp_dir)

        docs_directory = Path(temp_directory) / 'docs'
        docs_directory.mkdir()
        (docs_directory / 'context.yaml').write_text(data='')

        octiron = Octiron(
            root_directory=docs_directory,
            root_context=root_context,
        )
        assert list(octiron._find_context_files(docs_directory / 'posts')) == [
            temp_directory / 'docs/context.yaml',
        ]
Exemple #6
0
def test_file_in_upper_directory(root_context: LDContext):
    with tempfile.TemporaryDirectory() as temp_dir:
        temp_directory = Path(temp_dir)

        # Create a file that will not be accessible to the finder
        # because it is above the root_directory in structure
        (temp_directory / 'context.yaml').write_text(data='')

        docs_directory = Path(temp_directory) / 'docs'
        docs_directory.mkdir()

        octiron = Octiron(
            root_directory=docs_directory,
            root_context=root_context,
        )
        assert not list(octiron._find_context_files(docs_directory / 'posts'))
Exemple #7
0
def test_yaml_with_context(root_context: LDContext):
    """Update Octiron graph from a YAML file."""
    data_dir = Path(__file__).parent / 'data'

    octiron = Octiron(
        root_directory=data_dir,
        root_context=root_context,
    )

    octiron.update_from_file(
        path=data_dir / 'test_yaml/spo.yaml',
        local_iri=LOCAL_IRI,
    )

    assert (
        LOCAL_IRI,
        RDFS.label,
        Literal(
            'For any triple, its subject and object are Resources, and '
            'predicate is a Property.', ),
    ) in octiron.graph
Exemple #8
0
def test_markdown_with_context(root_context: LDContext):
    """Update Octiron graph from a Markdown file with a $context."""
    data_dir = Path(__file__).parent / 'data'

    octiron = Octiron(
        root_directory=data_dir,
        root_context=root_context,
    )

    octiron.update_from_file(
        path=data_dir / 'test_with_context.md',
        local_iri=LOCAL_IRI,
    )

    assert set(octiron.graph) == {
        (LOCAL_IRI, RDFS.label, Literal('Hey, I am a test!')),
        (LOCAL_IRI, RDF.type, OCTA.Page),
        (LOCAL_IRI, OCTA.fileName, Literal('test_with_context.md')),
        (LOCAL_IRI, OCTA.subjectOf, LOCAL_IRI),
        (LOCAL_IRI, OCTA.url, Literal('/test/')),
        (URIRef('local:test_with_context.md'), OCTA.isChildOf, URIRef(LOCAL)),
        (URIRef(LOCAL), RDF.type, OCTA.Directory),
    }
Exemple #9
0
def test_yaml_date(root_context: LDContext):
    """
    Update Octiron graph from a YAML file with a date in it.

    Created for https://github.com/digitalbazaar/pyld/issues/146
    """
    data_dir = Path(__file__).parent / 'data'

    octiron = Octiron(
        root_directory=data_dir,
        root_context=root_context,
    )

    octiron.update_from_file(
        path=data_dir / 'test_yaml/publication_date.yaml',
        local_iri=LOCAL_IRI,
    )

    assert (
        LOCAL.foo,
        LOCAL.publicationDate,
        Literal('2020-11-16'),
    ) in octiron.graph
Exemple #10
0
def test_markdown_without_id(root_context: LDContext):
    """Update Octiron graph from a Markdown file without a $id."""
    data_dir = Path(__file__).parent / 'data'

    octiron = Octiron(
        root_directory=data_dir,
        root_context=root_context,
    )

    octiron.update_from_file(
        path=data_dir / 'test_without_id.md',
        local_iri=LOCAL_IRI,
    )

    REAL_IRI = LOCAL['test_without_id.md']
    assert set(octiron.graph.triples((None, None, None))) == {
        (LOCAL_IRI, OCTA.title, Literal('Hey, I am a test!')),
        (LOCAL_IRI, RDF.type, OCTA.Page),
        (LOCAL_IRI, OCTA.url, Literal('/test/')),
        (LOCAL_IRI, OCTA.fileName, Literal('test_without_id.md')),
        (LOCAL_IRI, OCTA.subjectOf, LOCAL_IRI),
        (REAL_IRI, OCTA.isChildOf, URIRef(LOCAL)),
        (LOCAL.term(''), RDF.type, OCTA.Directory),
    }