Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/flexo_syside_lib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ def _wrap_elements_as_payload(data: List[Dict[str, Any]]) -> List[Dict[str, Any]

# Add identity
identity = {"@id": clean_element.get("@id")} if "@id" in clean_element else {}
clean_element["identity"] = identity

transformed.append({
"payload": clean_element,
"identity": identity
})
transformed.append({"payload": clean_element})

return transformed

Expand Down
55 changes: 22 additions & 33 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class TestUtilityFunctions:

def test_replace_none_with_empty(self):
"""Test the _replace_none_with_empty utility function."""
# Import here to ensure license is set up
from flexo_syside_lib.core import _replace_none_with_empty

data = {"a": None, "b": [1, None, {"c": None}]}
Expand All @@ -34,13 +33,26 @@ def test_wrap_elements_as_payload(self):
from flexo_syside_lib.core import _wrap_elements_as_payload

elements = [
{"@id": "ID1", "@uri": "ignore", "name": None},
{"@id": "ID1", "name": None},
{"name": "X", "props": [None, 1]},
]
wrapped = _wrap_elements_as_payload(elements)
assert wrapped[0]["identity"] == {"@id": "ID1"}
# check wrapper structure
assert isinstance(wrapped, list)
assert all("payload" in e for e in wrapped)

# identity should now be inside payload
assert wrapped[0]["payload"]["identity"] == {"@id": "ID1"}

# @uri should be removed
assert "@uri" not in wrapped[0]["payload"]

# None should become "" in name
assert wrapped[0]["payload"]["name"] == ""

# props should remain list with None preserved
assert wrapped[1]["payload"]["props"][0] is None
assert wrapped[1]["payload"]["props"][1] == 1

def test_make_root_namespace_first(self):
"""Test the _make_root_namespace_first utility function."""
Expand Down Expand Up @@ -93,7 +105,6 @@ def test_convert_sysml_string_to_json(self, mock_syside):
"""Test converting SysML string to JSON with mocked SysIDE."""
from flexo_syside_lib.core import convert_sysml_string_textual_to_json

# Sample SysML content
sample_sysml = '''
package TestPackage {
part def Component {
Expand All @@ -102,19 +113,16 @@ def test_convert_sysml_string_to_json(self, mock_syside):
}
'''

# Sample JSON data
sample_json = [
{"@id": "ns1", "@type": "Namespace", "qualifiedName": "2020-01-01T00:00:00Z"},
{"@id": "comp1", "@type": "Component", "name": "TestComponent"},
]

# Mock SysIDE components
mock_model = Mock()
mock_diagnostics = Mock()
mock_diagnostics.contains_errors.return_value = False
mock_syside.load_model.return_value = (mock_model, mock_diagnostics)

# Mock the model serialization
mock_model.user_docs = [Mock()]
mock_locked = Mock()
mock_locked.root_node = Mock()
Expand All @@ -123,11 +131,8 @@ def test_convert_sysml_string_to_json(self, mock_syside):
mock_context_manager.__exit__ = Mock(return_value=None)
mock_model.user_docs[0].lock.return_value = mock_context_manager

# Mock JSON writer
mock_writer = Mock()
mock_writer.result = json.dumps(sample_json)

# Mock serialization options
mock_options = Mock()

with patch('flexo_syside_lib.core._create_json_writer', return_value=mock_writer), \
Expand All @@ -136,18 +141,16 @@ def test_convert_sysml_string_to_json(self, mock_syside):

payload, json_string = convert_sysml_string_textual_to_json(sample_sysml)

# Verify we get a payload structure
assert isinstance(payload, list)
assert len(payload) > 0

# Verify payload structure
# Verify payload structure (updated)
for item in payload:
assert "payload" in item
assert "identity" in item
assert "identity" in item["payload"]
assert isinstance(item["payload"], dict)
assert isinstance(item["identity"], dict)
assert isinstance(item["payload"]["identity"], dict)

# Verify JSON string is valid
parsed_json = json.loads(json_string)
assert isinstance(parsed_json, list)
assert len(parsed_json) > 0
Expand All @@ -157,7 +160,6 @@ def test_convert_sysml_file_to_json(self, mock_syside):
"""Test converting SysML file to JSON with mocked SysIDE."""
from flexo_syside_lib.core import convert_sysml_file_textual_to_json

# Sample SysML content
sample_sysml = '''
package TestPackage {
part def Component {
Expand All @@ -166,19 +168,16 @@ def test_convert_sysml_file_to_json(self, mock_syside):
}
'''

# Sample JSON data
sample_json = [
{"@id": "ns1", "@type": "Namespace", "qualifiedName": "2020-01-01T00:00:00Z"},
{"@id": "comp1", "@type": "Component", "name": "TestComponent"},
]

# Mock SysIDE components
mock_model = Mock()
mock_diagnostics = Mock()
mock_diagnostics.contains_errors.return_value = False
mock_syside.try_load_model.return_value = (mock_model, mock_diagnostics)

# Mock the model serialization
mock_model.user_docs = [Mock()]
mock_locked = Mock()
mock_locked.root_node = Mock()
Expand All @@ -187,11 +186,8 @@ def test_convert_sysml_file_to_json(self, mock_syside):
mock_context_manager.__exit__ = Mock(return_value=None)
mock_model.user_docs[0].lock.return_value = mock_context_manager

# Mock JSON writer
mock_writer = Mock()
mock_writer.result = json.dumps(sample_json)

# Mock serialization options
mock_options = Mock()

with patch('flexo_syside_lib.core._create_json_writer', return_value=mock_writer), \
Expand All @@ -205,18 +201,16 @@ def test_convert_sysml_file_to_json(self, mock_syside):
try:
payload, json_string = convert_sysml_file_textual_to_json(temp_file)

# Verify we get a payload structure
assert isinstance(payload, list)
assert len(payload) > 0

# Verify payload structure
# Verify payload structure (updated)
for item in payload:
assert "payload" in item
assert "identity" in item
assert "identity" in item["payload"]
assert isinstance(item["payload"], dict)
assert isinstance(item["identity"], dict)
assert isinstance(item["payload"]["identity"], dict)

# Verify JSON string is valid
parsed_json = json.loads(json_string)
assert isinstance(parsed_json, list)
assert len(parsed_json) > 0
Expand All @@ -229,7 +223,7 @@ def test_convert_json_to_sysml_textual_invalid_input(self):
from flexo_syside_lib.core import convert_json_to_sysml_textual

with pytest.raises(TypeError, match="json_flexo must be dict/list/str"):
convert_json_to_sysml_textual(123) # Invalid type
convert_json_to_sysml_textual(123)

@patch('flexo_syside_lib.core.syside')
def test_create_json_writer(self, mock_syside):
Expand Down Expand Up @@ -266,15 +260,10 @@ class TestLicenseHandling:

def test_import_without_license_graceful_failure(self):
"""Test that import fails gracefully when no license is available."""
# This test verifies that the import behavior is predictable
# In CI, we'll mock syside to avoid license issues
pass

def test_utility_functions_work_without_syside(self):
"""Test that utility functions work even if SysIDE is mocked."""
# This test ensures our utility functions are truly independent
from flexo_syside_lib.core import _replace_none_with_empty

# Test that the function works regardless of SysIDE state
result = _replace_none_with_empty({"test": None})
assert result == {"test": ""}