Skip to content

Commit

Permalink
Added testing and github actions
Browse files Browse the repository at this point in the history
  • Loading branch information
osm0512 committed Jul 2, 2024
1 parent 11a0ce4 commit dfefb3e
Show file tree
Hide file tree
Showing 104 changed files with 1,556 additions and 28 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/run_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Run Unit Test via Pytest

on: [push]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements_dev.txt ]; then pip install -r requirements_dev.txt; fi
- name: Test with pytest
run: |
coverage run -m pytest -v -s
- name: Generate Coverage Report
run: |
coverage report -m
32 changes: 32 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import argparse
import os
from src.file_parser import parse_json_schema
from src.json_schema_to_shacl import JsonSchemaToShacl


def main():
"""Main function to execute"""
parser = argparse.ArgumentParser(
description="Translate JSON Schema to SHACL")
parser.add_argument("json_file", type=str,
help="Path to JSON Schema file")
args = parser.parse_args()

file_path = args.json_file
schema = parse_json_schema(file_path)

json_converter = JsonSchemaToShacl()

if schema is not None:
json_converter.translate(schema)
# Remove the last component of the path
base_path = os.path.dirname(file_path)
base_name = os.path.splitext(os.path.basename(file_path))[
0] # Remove the .json extension
# Construct the new file name with .shape.ttl extension
file_name = os.path.join(base_path, base_name + "_shape.ttl")
json_converter.shacl.serialize(format="turtle", destination=file_name)


if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
pythonpath = .
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
jsonpath-ng==1.6.1
owlrl==6.0.2
pyshacl==0.26.0
rdflib==7.0.0
Empty file added requirements_dev.txt
Empty file.
Empty file added src/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion src/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def build_in_types(json_type):

def format_types(format_type):
format_type_map = {
"date-time": "dateTime ",
"date-time": "dateTime",
"date": "date",
"time": "time",
"duration": "duration"
Expand Down
31 changes: 4 additions & 27 deletions src/json_schema_to_shacl.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import argparse
from rdflib import Graph, Namespace, Literal, URIRef, BNode
from jsonpath_ng import parse
# from pyshacl import validate
from file_parser import parse_json_schema
from constants import build_in_types, format_types
from utils import check_type, check_if_object_has_properties_or_restrictions
from .constants import build_in_types, format_types
from .utils import check_type, check_if_object_has_properties_or_restrictions


class JsonSchemaToShacl:
Expand Down Expand Up @@ -968,7 +966,7 @@ def trans_complex(self, element: dict, ref_name: str = None) -> URIRef:

return subject

def translate(self, element: dict) -> None:
def translate(self, element: dict) -> Graph:
"""Function to translate JSON Schema to SHACL"""
self.schema = element
type_element = check_type(element)
Expand All @@ -979,25 +977,4 @@ def translate(self, element: dict) -> None:
elif type_element == "SimpleType":
self.trans_simple(element)


def main():
"""Main function to execute"""
parser = argparse.ArgumentParser(
description="Translate JSON Schema to SHACL")
parser.add_argument("json_file", type=str,
help="Path to JSON Schema file")
args = parser.parse_args()

file_path = args.json_file
schema = parse_json_schema(file_path)

json_converter = JsonSchemaToShacl()

if schema is not None:
json_converter.translate(schema)
file_name = f"{file_path}.shape.ttl"
json_converter.shacl.serialize(format="turtle", destination=file_name)


if __name__ == "__main__":
main()
return self.shacl
4 changes: 4 additions & 0 deletions test/1-SimpleType/mapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "number",
"name": "credit_card"
}
18 changes: 18 additions & 0 deletions test/1-SimpleType/test_1simpletype.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os
from rdflib import compare, Graph
from src.file_parser import parse_json_schema
from src.json_schema_to_shacl import JsonSchemaToShacl


def test_1simpletype():
expected_graph = Graph()
expected_graph.parse(os.path.join(os.path.dirname(
os.path.realpath(__file__)), 'test_shape.ttl'), format="turtle")

result_graph = Graph()
json_schema = parse_json_schema(
os.path.join(os.path.dirname(os.path.realpath(__file__)), 'mapping.json'))
result_graph = JsonSchemaToShacl().translate(
json_schema)

assert compare.isomorphic(expected_graph, result_graph)
8 changes: 8 additions & 0 deletions test/1-SimpleType/test_shape.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<http://example.com/PropertyShape/credit_card> a sh:PropertyShape ;
sh:datatype xsd:decimal ;
sh:name "credit_card" ;
sh:path <http://example.com/credit_card> .

8 changes: 8 additions & 0 deletions test/10-item/mapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "array",
"name" :"test",
"items": {
"type": "number"
}
}

18 changes: 18 additions & 0 deletions test/10-item/test_10item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os
from rdflib import compare, Graph
from src.file_parser import parse_json_schema
from src.json_schema_to_shacl import JsonSchemaToShacl


def test_10item():
expected_graph = Graph()
expected_graph.parse(os.path.join(os.path.dirname(
os.path.realpath(__file__)), 'test_shape.ttl'), format="turtle")

result_graph = Graph()
json_schema = parse_json_schema(
os.path.join(os.path.dirname(os.path.realpath(__file__)), 'mapping.json'))
result_graph = JsonSchemaToShacl().translate(
json_schema)

assert compare.isomorphic(expected_graph, result_graph)
10 changes: 10 additions & 0 deletions test/10-item/test_shape.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<http://example.com/NodeShape/test> sh:property <http://example.com/PropertyShape/test> .

<http://example.com/PropertyShape/test> a sh:PropertyShape ;
sh:datatype xsd:decimal ;
sh:name "test" ;
sh:path <http://example.com/test> .

11 changes: 11 additions & 0 deletions test/11-prefixItems/mapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"type": "array",
"name" :"test",
"prefixItems": [
{ "type": "number" },
{ "type": "string" },
{ "enum": ["Street", "Avenue", "Boulevard"] },
{ "enum": ["NW", "NE", "SW", "SE"] }
]
}

18 changes: 18 additions & 0 deletions test/11-prefixItems/test_11prefixitems.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os
from rdflib import compare, Graph
from src.file_parser import parse_json_schema
from src.json_schema_to_shacl import JsonSchemaToShacl


def test_11prefixitems():
expected_graph = Graph()
expected_graph.parse(os.path.join(os.path.dirname(
os.path.realpath(__file__)), 'test_shape.ttl'), format="turtle")

result_graph = Graph()
json_schema = parse_json_schema(
os.path.join(os.path.dirname(os.path.realpath(__file__)), 'mapping.json'))
result_graph = JsonSchemaToShacl().translate(
json_schema)

assert compare.isomorphic(expected_graph, result_graph)
35 changes: 35 additions & 0 deletions test/11-prefixItems/test_shape.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<http://example.com/NodeShape/test> a sh:NodeShape ;
sh:name "test" ;
sh:property <http://example.com/PropertyShape/testP0>,
<http://example.com/PropertyShape/testP1>,
<http://example.com/PropertyShape/testP2>,
<http://example.com/PropertyShape/testP3> .

<http://example.com/PropertyShape/testP0> a sh:PropertyShape ;
sh:datatype xsd:decimal ;
sh:name "testP0" ;
sh:path <http://example.com/testP0> .

<http://example.com/PropertyShape/testP1> a sh:PropertyShape ;
sh:datatype xsd:string ;
sh:name "testP1" ;
sh:path <http://example.com/testP1> .

<http://example.com/PropertyShape/testP2> a sh:PropertyShape ;
sh:in_ "Avenue",
"Boulevard",
"Street" ;
sh:name "testP2" ;
sh:path <http://example.com/testP2> .

<http://example.com/PropertyShape/testP3> a sh:PropertyShape ;
sh:in_ "NE",
"NW",
"SE",
"SW" ;
sh:name "testP3" ;
sh:path <http://example.com/testP3> .

13 changes: 13 additions & 0 deletions test/12-unevaluatedItems/mapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "array",
"name": "test",
"prefixItems": [
{
"type": "string"
},
{
"type": "number"
}
],
"unevaluatedItems": false
}
18 changes: 18 additions & 0 deletions test/12-unevaluatedItems/test_12unevaluateditems.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os
from rdflib import compare, Graph
from src.file_parser import parse_json_schema
from src.json_schema_to_shacl import JsonSchemaToShacl


def test_12unevaluateditems():
expected_graph = Graph()
expected_graph.parse(os.path.join(os.path.dirname(
os.path.realpath(__file__)), 'test_shape.ttl'), format="turtle")

result_graph = Graph()
json_schema = parse_json_schema(
os.path.join(os.path.dirname(os.path.realpath(__file__)), 'mapping.json'))
result_graph = JsonSchemaToShacl().translate(
json_schema)

assert compare.isomorphic(expected_graph, result_graph)
18 changes: 18 additions & 0 deletions test/12-unevaluatedItems/test_shape.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<http://example.com/NodeShape/test> a sh:NodeShape ;
sh:name "test" ;
sh:property <http://example.com/PropertyShape/testP0>,
<http://example.com/PropertyShape/testP1> .

<http://example.com/PropertyShape/testP0> a sh:PropertyShape ;
sh:datatype xsd:string ;
sh:name "testP0" ;
sh:path <http://example.com/testP0> .

<http://example.com/PropertyShape/testP1> a sh:PropertyShape ;
sh:datatype xsd:decimal ;
sh:name "testP1" ;
sh:path <http://example.com/testP1> .

11 changes: 11 additions & 0 deletions test/13-contains/mapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"type": "array",
"name": "test",
"items": {
"type": "number"
},
"contains": {
"type": "number"
}
}

18 changes: 18 additions & 0 deletions test/13-contains/test_13contains.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os
from rdflib import compare, Graph
from src.file_parser import parse_json_schema
from src.json_schema_to_shacl import JsonSchemaToShacl


def test_13contains():
expected_graph = Graph()
expected_graph.parse(os.path.join(os.path.dirname(
os.path.realpath(__file__)), 'test_shape.ttl'), format="turtle")

result_graph = Graph()
json_schema = parse_json_schema(
os.path.join(os.path.dirname(os.path.realpath(__file__)), 'mapping.json'))
result_graph = JsonSchemaToShacl().translate(
json_schema)

assert compare.isomorphic(expected_graph, result_graph)
16 changes: 16 additions & 0 deletions test/13-contains/test_shape.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<http://example.com/NodeShape/test> sh:property <http://example.com/PropertyShape/testP0>,
<http://example.com/PropertyShape/testP1> .

<http://example.com/PropertyShape/testP0> a sh:PropertyShape ;
sh:datatype xsd:decimal ;
sh:name "testP0" ;
sh:path <http://example.com/testP0> .

<http://example.com/PropertyShape/testP1> a sh:PropertyShape ;
sh:datatype xsd:decimal ;
sh:name "testP1" ;
sh:path <http://example.com/testP1> .

8 changes: 8 additions & 0 deletions test/14-uniqueItems/mapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "array",
"name": "test",
"items": {
"type": "number"
},
"uniqueItems": true
}
Loading

0 comments on commit dfefb3e

Please sign in to comment.