Skip to content

Commit bbbcc48

Browse files
authored
Merge pull request #23 from Open-MBEE/develop
aligned with syside 0.8.1
2 parents 3d9ce20 + 95ebd79 commit bbbcc48

File tree

5 files changed

+64
-25
lines changed

5 files changed

+64
-25
lines changed

examples/library.sysml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package lpkg {
2+
part def A {
3+
port p;
4+
}
5+
}
6+
7+
package pkg {
8+
private import lpkg::*;
9+
10+
part a:A;
11+
}

examples/t.sysml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
part m0001_2N {
3+
4+
part nx0001 {
5+
port scp_outside2;
6+
}
7+
8+
part tcs0001{
9+
port scp;
10+
}
11+
12+
interface tcs0001.scp to nx0001.scp_outside2;
13+
}

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "flexo_syside_lib"
7-
version = "0.3.0"
7+
version = "0.4.0"
88
description = "Using syside sysmlv2 with Flexo"
99
authors = [{name="Robert Karban", email="robert.karban@planetaryutilities.com"}]
1010
readme = "README.md"

src/flexo_syside_lib/core.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ def _wrap_elements_as_payload(data: List[Dict[str, Any]]) -> List[Dict[str, Any]
5050

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

55-
transformed.append({"payload": clean_element})
54+
transformed.append({
55+
"payload": clean_element,
56+
"identity": identity
57+
})
5658

5759
return transformed
5860

@@ -200,6 +202,8 @@ def convert_json_to_sysml_textual(json_flexo:str, debug:bool=False):
200202

201203
# 2) Ensure root namespace is first (this function expects a JSON string)
202204
json_import = _make_root_namespace_first(json_in)
205+
# with open("debug.json", "w", encoding="utf-8") as f:
206+
# f.write(json_import)
203207

204208
# 3) Deserialize
205209
try:

tests/test_core.py

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class TestUtilityFunctions:
1414

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

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

3536
elements = [
36-
{"@id": "ID1", "name": None},
37+
{"@id": "ID1", "@uri": "ignore", "name": None},
3738
{"name": "X", "props": [None, 1]},
3839
]
3940
wrapped = _wrap_elements_as_payload(elements)
40-
# check wrapper structure
41-
assert isinstance(wrapped, list)
42-
assert all("payload" in e for e in wrapped)
43-
44-
# identity should now be inside payload
45-
assert wrapped[0]["payload"]["identity"] == {"@id": "ID1"}
46-
47-
# @uri should be removed
48-
assert "@uri" not in wrapped[0]["payload"]
49-
50-
# None should become "" in name
41+
assert wrapped[0]["identity"] == {"@id": "ID1"}
5142
assert wrapped[0]["payload"]["name"] == ""
52-
53-
# props should remain list with None preserved
5443
assert wrapped[1]["payload"]["props"][0] is None
55-
assert wrapped[1]["payload"]["props"][1] == 1
5644

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

96+
# Sample SysML content
10897
sample_sysml = '''
10998
package TestPackage {
11099
part def Component {
@@ -113,16 +102,19 @@ def test_convert_sysml_string_to_json(self, mock_syside):
113102
}
114103
'''
115104

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

111+
# Mock SysIDE components
121112
mock_model = Mock()
122113
mock_diagnostics = Mock()
123114
mock_diagnostics.contains_errors.return_value = False
124115
mock_syside.load_model.return_value = (mock_model, mock_diagnostics)
125116

117+
# Mock the model serialization
126118
mock_model.user_docs = [Mock()]
127119
mock_locked = Mock()
128120
mock_locked.root_node = Mock()
@@ -131,8 +123,11 @@ def test_convert_sysml_string_to_json(self, mock_syside):
131123
mock_context_manager.__exit__ = Mock(return_value=None)
132124
mock_model.user_docs[0].lock.return_value = mock_context_manager
133125

126+
# Mock JSON writer
134127
mock_writer = Mock()
135128
mock_writer.result = json.dumps(sample_json)
129+
130+
# Mock serialization options
136131
mock_options = Mock()
137132

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

142137
payload, json_string = convert_sysml_string_textual_to_json(sample_sysml)
143138

139+
# Verify we get a payload structure
144140
assert isinstance(payload, list)
145141
assert len(payload) > 0
146142

147-
# Verify payload structure (updated)
143+
# Verify payload structure
148144
for item in payload:
149145
assert "payload" in item
150-
assert "identity" in item["payload"]
146+
assert "identity" in item
151147
assert isinstance(item["payload"], dict)
152-
assert isinstance(item["payload"]["identity"], dict)
148+
assert isinstance(item["identity"], dict)
153149

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

160+
# Sample SysML content
163161
sample_sysml = '''
164162
package TestPackage {
165163
part def Component {
@@ -168,16 +166,19 @@ def test_convert_sysml_file_to_json(self, mock_syside):
168166
}
169167
'''
170168

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

175+
# Mock SysIDE components
176176
mock_model = Mock()
177177
mock_diagnostics = Mock()
178178
mock_diagnostics.contains_errors.return_value = False
179179
mock_syside.try_load_model.return_value = (mock_model, mock_diagnostics)
180180

181+
# Mock the model serialization
181182
mock_model.user_docs = [Mock()]
182183
mock_locked = Mock()
183184
mock_locked.root_node = Mock()
@@ -186,8 +187,11 @@ def test_convert_sysml_file_to_json(self, mock_syside):
186187
mock_context_manager.__exit__ = Mock(return_value=None)
187188
mock_model.user_docs[0].lock.return_value = mock_context_manager
188189

190+
# Mock JSON writer
189191
mock_writer = Mock()
190192
mock_writer.result = json.dumps(sample_json)
193+
194+
# Mock serialization options
191195
mock_options = Mock()
192196

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

208+
# Verify we get a payload structure
204209
assert isinstance(payload, list)
205210
assert len(payload) > 0
206211

207-
# Verify payload structure (updated)
212+
# Verify payload structure
208213
for item in payload:
209214
assert "payload" in item
210-
assert "identity" in item["payload"]
215+
assert "identity" in item
211216
assert isinstance(item["payload"], dict)
212-
assert isinstance(item["payload"]["identity"], dict)
217+
assert isinstance(item["identity"], dict)
213218

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

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

228234
@patch('flexo_syside_lib.core.syside')
229235
def test_create_json_writer(self, mock_syside):
@@ -260,10 +266,15 @@ class TestLicenseHandling:
260266

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

265273
def test_utility_functions_work_without_syside(self):
266274
"""Test that utility functions work even if SysIDE is mocked."""
275+
# This test ensures our utility functions are truly independent
267276
from flexo_syside_lib.core import _replace_none_with_empty
277+
278+
# Test that the function works regardless of SysIDE state
268279
result = _replace_none_with_empty({"test": None})
269280
assert result == {"test": ""}

0 commit comments

Comments
 (0)