Commit 80770927 authored by Fabien Amarger's avatar Fabien Amarger
Browse files

fix(test): Mock serialize_to_python to implement ordered serialization

parent 3da33d6d0976
from yams.buildobjs import *
class FirstClass(EntityType):
toto = String()
class SecondClass(EntityType):
toto = String()
......@@ -3,6 +6,3 @@
class SecondClass(EntityType):
toto = String()
class FirstClass(EntityType):
toto = String()
from yams.buildobjs import *
class FirstClass(EntityType):
pass
class SecondClass(EntityType):
pass
......@@ -3,9 +6,6 @@
class SecondClass(EntityType):
pass
class FirstClass(EntityType):
pass
class rel1(RelationDefinition):
subject = ('FirstClass', 'SecondClass',)
object = ('FirstClass', 'SecondClass',)
......
from yams.buildobjs import *
class FirstClass(EntityType):
pass
class SecondClass(EntityType):
pass
......@@ -3,9 +6,6 @@
class SecondClass(EntityType):
pass
class FirstClass(EntityType):
pass
class rel1(RelationDefinition):
subject = ('FirstClass', 'SecondClass',)
object = ('FirstClass', 'SecondClass',)
......
import logging
import unittest
import pathlib
......@@ -1,3 +2,4 @@
import unittest
import pathlib
from io import StringIO
from rdflib import Graph
......@@ -3,3 +5,3 @@
from rdflib import Graph
from typing import Optional
from typing import Optional, Dict
from owl2yams import owl_model_to_yams
......@@ -5,6 +7,7 @@
from owl2yams import owl_model_to_yams
from yams.serialize import serialize_to_python
from yams.schema import Schema
from logilab.common.graph import ordered_nodes
TEST_DATA = pathlib.Path(__file__).parent / "data"
......@@ -7,7 +10,58 @@
TEST_DATA = pathlib.Path(__file__).parent / "data"
def mock_serialize_to_python(schema: Schema) -> str:
out = StringIO()
w = out.write
w("from yams.buildobjs import *\n\n")
graph: Dict = {}
for entity in schema.entities():
graph.setdefault(entity, [])
for e in ordered_nodes(graph):
if not e.final:
if e._specialized_type:
base = e._specialized_type
else:
base = "EntityType"
w("class %s(%s):\n" % (e.type, base))
attr_defs = list(e.attribute_definitions())
if attr_defs:
for attr, obj in attr_defs:
w(" %s = %s()\n" % (attr.type, obj.type))
else:
w(" pass\n")
w("\n")
for r in schema.relations():
if not r.final:
if r.subjects() and r.objects():
w("class %s(RelationDefinition):\n" % r.type)
w(
" subject = (%s,)\n"
% ", ".join("'%s'" % x for x in sorted(r.subjects()))
)
w(
" object = (%s,)\n"
% ", ".join("'%s'" % x for x in sorted(r.objects()))
)
w("\n")
else:
logging.warning(
"relation definition %s missing subject/object" % r.type
)
return out.getvalue()
# This mock is needed since this issue is not fixed:
# https://forge.extranet.logilab.fr/open-source/yams/-/issues/3
serialize_to_python = mock_serialize_to_python
class Owl2YamsTC(unittest.TestCase):
def _load_owl_and_yams(
self,
......@@ -24,7 +78,7 @@
expected_output = TEST_DATA / yams_schema_file_name
with open(expected_output) as f:
expected_yams_schema = f.read()
self.assertEquals(serialize_to_python(schema), expected_yams_schema)
self.assertEqual(serialize_to_python(schema), expected_yams_schema)
return schema
def test_transform_class(self):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment