Skip to content

Commit eda1ca3

Browse files
committed
Unittest for AASXWriter.write_all_aas_objects
1 parent 2455821 commit eda1ca3

File tree

1 file changed

+95
-10
lines changed

1 file changed

+95
-10
lines changed

sdk/test/adapter/aasx/test_aasx.py

Lines changed: 95 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Copyright (c) 2025 the Eclipse BaSyx Authors
2-
#
2+
#
33
# This program and the accompanying materials are made available under the terms of the MIT License, available in
44
# the LICENSE file of this project.
55
#
@@ -27,7 +27,7 @@ def test_supplementary_file_container(self) -> None:
2727
self.assertEqual("/TestFile.pdf", new_name)
2828
f.seek(0)
2929
container.add_file("/TestFile.pdf", f, "application/pdf")
30-
# Name should not be modified, since there is still no conflict
30+
# Name should not be modified, since there is still no conflict
3131
self.assertEqual("/TestFile.pdf", new_name)
3232

3333
with open(__file__, 'rb') as f:
@@ -68,22 +68,23 @@ def test_supplementary_file_container(self) -> None:
6868
class AASXWriterTest(unittest.TestCase):
6969
def test_writing_reading_example_aas(self) -> None:
7070
# Create example data and file_store
71-
data = example_aas.create_full_example()
72-
files = aasx.DictSupplementaryFileContainer()
71+
data = example_aas.create_full_example() # creates a complete, valid example AAS
72+
files = aasx.DictSupplementaryFileContainer() # in-memory store for attached files
7373
with open(os.path.join(os.path.dirname(__file__), 'TestFile.pdf'), 'rb') as f:
74-
files.add_file("/TestFile.pdf", f, "application/pdf")
74+
files.add_file("/TestFile.pdf", f, "application/pdf") # add a real supplementary pdf file
7575
f.seek(0)
7676

77-
# Create OPC/AASX core properties
77+
# Create OPC/AASX core properties
78+
# create AASX metadata (core properties)
7879
cp = pyecma376_2.OPCCoreProperties()
7980
cp.created = datetime.datetime.now()
8081
cp.creator = "Eclipse BaSyx Python Testing Framework"
8182

82-
# Write AASX file
83-
for write_json in (False, True):
83+
# Write AASX file
84+
for write_json in (False, True): # Loop over both XML and JSON modes
8485
with self.subTest(write_json=write_json):
85-
fd, filename = tempfile.mkstemp(suffix=".aasx")
86-
os.close(fd)
86+
fd, filename = tempfile.mkstemp(suffix=".aasx") # create temporary file
87+
os.close(fd) # close file descriptor
8788

8889
# Write AASX file
8990
# the zipfile library reports errors as UserWarnings via the warnings library. Let's check for
@@ -126,3 +127,87 @@ def test_writing_reading_example_aas(self) -> None:
126127
"78450a66f59d74c073bf6858db340090ea72a8b1")
127128

128129
os.unlink(filename)
130+
131+
132+
class AASXWriterReferencedSubmodelsTest(unittest.TestCase):
133+
134+
def test_only_referenced_submodels(self):
135+
"""
136+
Test that verifies that all Submodels (referenced and unreferenced) are written to the AASX package when using
137+
the convenience function write_all_aas_objects().
138+
When calling the higher-level function write_aas(), however, only
139+
referenced Submodels in the ObjectStore should be included.
140+
"""
141+
# Create referenced and unreferenced Submodels
142+
referenced_submodel = model.Submodel(id_="ref_submodel")
143+
unreferenced_submodel = model.Submodel(id_="unref_submodel")
144+
145+
aas = model.AssetAdministrationShell(
146+
id_="Test_AAS",
147+
asset_information=model.AssetInformation(
148+
asset_kind=model.AssetKind.INSTANCE,
149+
global_asset_id="http://acplt.org/Test_Asset"
150+
),
151+
submodel={model.ModelReference.from_referable(referenced_submodel)}
152+
)
153+
154+
# ObjectStore containing all objects
155+
object_store = model.DictObjectStore([aas, referenced_submodel, unreferenced_submodel])
156+
157+
# Empty SupplementaryFileContainer (no files needed)
158+
file_store = aasx.DictSupplementaryFileContainer()
159+
160+
# --- Step 1: Check write_aas() behavior ---
161+
for write_json in (False, True):
162+
with self.subTest(method="write_aas", write_json=write_json):
163+
fd, filename = tempfile.mkstemp(suffix=".aasx")
164+
os.close(fd)
165+
166+
with warnings.catch_warnings(record=True) as w:
167+
with aasx.AASXWriter(filename) as writer:
168+
# write_aas only takes the AAS id and ObjectStore
169+
writer.write_aas(
170+
aas_ids=[aas.id],
171+
object_store=object_store,
172+
file_store=file_store,
173+
write_json=write_json
174+
)
175+
176+
# Read back
177+
new_data: model.DictObjectStore[model.Identifiable] = model.DictObjectStore()
178+
new_files = aasx.DictSupplementaryFileContainer()
179+
with aasx.AASXReader(filename) as reader:
180+
reader.read_into(new_data, new_files)
181+
182+
# Assertions
183+
self.assertIn(referenced_submodel.id, new_data) # referenced Submodel is included
184+
self.assertNotIn(unreferenced_submodel.id, new_data) # unreferenced Submodel is excluded
185+
186+
os.unlink(filename)
187+
188+
# --- Step 2: Check write_all_aas_objects ---
189+
for write_json in (False, True):
190+
with self.subTest(method="write_all_aas_objects", write_json=write_json):
191+
fd, filename = tempfile.mkstemp(suffix=".aasx")
192+
os.close(fd)
193+
194+
with warnings.catch_warnings(record=True) as w:
195+
with aasx.AASXWriter(filename) as writer:
196+
writer.write_all_aas_objects(
197+
part_name="/aasx/my_aas_part.xml",
198+
objects=object_store,
199+
file_store=file_store,
200+
write_json=write_json
201+
)
202+
203+
# Read back
204+
new_data: model.DictObjectStore[model.Identifiable] = model.DictObjectStore()
205+
new_files = aasx.DictSupplementaryFileContainer()
206+
with aasx.AASXReader(filename) as reader:
207+
reader.read_into(new_data, new_files)
208+
209+
# Assertions
210+
self.assertIn(referenced_submodel.id, new_data)
211+
self.assertIn(unreferenced_submodel.id, new_data) # all objects are written
212+
213+
os.unlink(filename)

0 commit comments

Comments
 (0)