Skip to content

Commit

Permalink
Added testing for entities_to_rdf
Browse files Browse the repository at this point in the history
  • Loading branch information
JosePizarro3 committed Feb 6, 2025
1 parent aa69f61 commit 11383d6
Showing 1 changed file with 114 additions and 0 deletions.
114 changes: 114 additions & 0 deletions tests/cli/test_entities_to_rdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import json
import os
import shutil

import pytest
from rdflib import BNode, Graph, Literal, Namespace, URIRef
from rdflib.namespace import DC, OWL, RDF, RDFS

from bam_masterdata.cli.entities_to_rdf import BAM, entities_to_rdf, rdf_graph_init
from bam_masterdata.logger import logger


def test_rdf_init():
"""
Test the `rdf_graph_init` function.
"""
graph = Graph()
rdf_graph_init(graph)

# Test how many nodes initialize in the graph
assert len(graph) == 30

# Check if base namespaces are bound correctly.
expected_namespaces = {"dc", "owl", "rdf", "rdfs", "bam", "prov"}
bound_namespaces = {prefix for prefix, _ in graph.namespaces()}
expected_namespaces.issubset(bound_namespaces)

# Ensure standard annotation properties exist with correct types.
annotation_props = [RDFS.label, RDFS.comment, DC.identifier]
for prop in annotation_props:
assert (prop, RDF.type, OWL.AnnotationProperty) in graph

# Verify bam:dataType and bam:propertyLabel exist with labels and comments.
custom_props = {
BAM["dataType"]: "Represents the data type of a property",
BAM["propertyLabel"]: "A UI-specific annotation used in openBIS",
}
for prop, comment_start in custom_props.items():
assert (prop, RDF.type, OWL.AnnotationProperty) in graph
assert (
prop,
RDFS.label,
Literal(f"bam:{prop.split('/')[-1]}", lang="en"),
) in graph
assert any(
o.startswith(comment_start)
for _, _, o in graph.triples((prop, RDFS.comment, None))
)

# Check that BAM object properties exist and have correct characteristics.
bam_props = {
BAM["hasMandatoryProperty"]: "The property must be mandatorily filled",
BAM["hasOptionalProperty"]: "The property is optionally filled",
BAM["referenceTo"]: "The property is referencing an object",
}
for prop, comment_start in bam_props.items():
assert (prop, RDF.type, OWL.ObjectProperty) in graph
assert any(
o.startswith(comment_start)
for _, _, o in graph.triples((prop, RDFS.comment, None))
)

# Ensure PropertyType and related objects exist with labels and comments.
prop_type_uri = BAM["PropertyType"]
assert (prop_type_uri, RDF.type, OWL.Thing) in graph
assert (prop_type_uri, RDFS.label, Literal("PropertyType", lang="en")) in graph
assert any(
o.startswith("A conceptual placeholder used to define")
for _, _, o in graph.triples((prop_type_uri, RDFS.comment, None))
)


def test_entities_to_rdf():
module_name = "object_types" # ! only one module for testing
module_path = os.path.join("./bam_masterdata/datamodel", f"{module_name}.py")

graph = Graph()
rdf_graph_init(graph)
entities_to_rdf(graph=graph, module_path=module_path, logger=logger)

# Testing
# ! this number is subject to change as the datamodel evolves
assert len(graph) == 5794

# Check Instrument entity
instrument_uri = BAM["Instrument"]
assert (instrument_uri, RDF.type, OWL.Thing) in graph
assert (instrument_uri, RDFS.label, Literal("Instrument", lang="en")) in graph
assert (
instrument_uri,
RDFS.comment,
Literal("Measuring Instrument", lang="en"),
) in graph
assert (
instrument_uri,
RDFS.comment,
Literal("Messgerät", lang="de"),
) in graph

# Check Camera entity (subclass of Instrument)
camera_uri = BAM["Camera"]
assert (camera_uri, RDF.type, OWL.Thing) in graph
assert (camera_uri, RDFS.subClassOf, instrument_uri) in graph
assert (camera_uri, RDFS.label, Literal("Camera", lang="en")) in graph
assert (
camera_uri,
RDFS.comment,
Literal("A generic camera device for recording video or photos", lang="en"),
) in graph
assert (
camera_uri,
RDFS.comment,
Literal("Eine generische Kamera für Video- oder Fotoaufnahmen", lang="de"),
) in graph

1 comment on commit 11383d6

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
bam_masterdata
   logger.py80100% 
bam_masterdata/cli
   cli.py1097070 36%
   entities_to_excel.py5433 94%
   entities_to_json.py3655 86%
   entities_to_rdf.py753030 60%
   fill_masterdata.py195181181 7%
bam_masterdata/datamodel
   collection_types.py370100% 
   dataset_types.py184184184 0%
   object_types.py15150100% 
   property_types.py8000100% 
   vocabulary_types.py137210100% 
bam_masterdata/excel
   excel_to_entities.py191167167 13%
bam_masterdata/metadata
   definitions.py870100% 
   entities.py8844 95%
bam_masterdata/openbis
   get_entities.py534343 19%
   login.py633 50%
bam_masterdata/utils
   utils.py8399 89%
TOTAL1724269996% 

Tests Skipped Failures Errors Time
83 1 💤 0 ❌ 0 🔥 20.033s ⏱️

Please sign in to comment.