@@ -68,22 +68,22 @@ def test_supplementary_file_container(self) -> None:
6868class 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 )
76-
7776 # Create OPC/AASX core properties
77+ # create AASX metadata (core properties)
7878 cp = pyecma376_2 .OPCCoreProperties ()
7979 cp .created = datetime .datetime .now ()
8080 cp .creator = "Eclipse BaSyx Python Testing Framework"
8181
8282 # Write AASX file
83- for write_json in (False , True ):
83+ for write_json in (False , True ): # Loop over both XML and JSON modes
8484 with self .subTest (write_json = write_json ):
85- fd , filename = tempfile .mkstemp (suffix = ".aasx" )
86- os .close (fd )
85+ fd , filename = tempfile .mkstemp (suffix = ".aasx" ) # create temporary file
86+ os .close (fd ) # close file descriptor
8787
8888 # Write AASX file
8989 # the zipfile library reports errors as UserWarnings via the warnings library. Let's check for
@@ -126,3 +126,86 @@ def test_writing_reading_example_aas(self) -> None:
126126 "78450a66f59d74c073bf6858db340090ea72a8b1" )
127127
128128 os .unlink (filename )
129+
130+
131+ class AASXWriterReferencedSubmodelsTest (unittest .TestCase ):
132+
133+ def test_only_referenced_submodels (self ):
134+ """
135+ Test that verifies that all Submodels (referenced and unreferenced) are written to the AASX package when using
136+ the convenience function write_all_aas_objects().
137+ When calling the higher-level function write_aas(), however, only
138+ referenced Submodels in the ObjectStore should be included.
139+ """
140+ # Create referenced and unreferenced Submodels
141+ referenced_submodel = model .Submodel (id_ = "ref_submodel" )
142+ unreferenced_submodel = model .Submodel (id_ = "unref_submodel" )
143+
144+ aas = model .AssetAdministrationShell (
145+ id_ = "Test_AAS" ,
146+ asset_information = model .AssetInformation (
147+ asset_kind = model .AssetKind .INSTANCE ,
148+ global_asset_id = "http://acplt.org/Test_Asset"
149+ ),
150+ submodel = {model .ModelReference .from_referable (referenced_submodel )}
151+ )
152+
153+ # ObjectStore containing all objects
154+ object_store = model .DictObjectStore ([aas , referenced_submodel , unreferenced_submodel ])
155+
156+ # Empty SupplementaryFileContainer (no files needed)
157+ file_store = aasx .DictSupplementaryFileContainer ()
158+
159+ # --- Step 1: Check write_aas() behavior ---
160+ for write_json in (False , True ):
161+ with self .subTest (method = "write_aas" , write_json = write_json ):
162+ fd , filename = tempfile .mkstemp (suffix = ".aasx" )
163+ os .close (fd )
164+
165+ with warnings .catch_warnings (record = True ) as w :
166+ with aasx .AASXWriter (filename ) as writer :
167+ # write_aas only takes the AAS id and ObjectStore
168+ writer .write_aas (
169+ aas_ids = [aas .id ],
170+ object_store = object_store ,
171+ file_store = file_store ,
172+ write_json = write_json
173+ )
174+
175+ # Read back
176+ new_data : model .DictObjectStore [model .Identifiable ] = model .DictObjectStore ()
177+ new_files = aasx .DictSupplementaryFileContainer ()
178+ with aasx .AASXReader (filename ) as reader :
179+ reader .read_into (new_data , new_files )
180+
181+ # Assertions
182+ self .assertIn (referenced_submodel .id , new_data ) # referenced Submodel is included
183+ self .assertNotIn (unreferenced_submodel .id , new_data ) # unreferenced Submodel is excluded
184+
185+ os .unlink (filename )
186+
187+ # --- Step 2: Check write_all_aas_objects ---
188+ for write_json in (False , True ):
189+ with self .subTest (method = "write_all_aas_objects" , write_json = write_json ):
190+ fd , filename = tempfile .mkstemp (suffix = ".aasx" )
191+ os .close (fd )
192+
193+ with warnings .catch_warnings (record = True ) as w :
194+ with aasx .AASXWriter (filename ) as writer :
195+ writer .write_all_aas_objects (
196+ part_name = "/aasx/my_aas_part.xml" ,
197+ objects = object_store ,
198+ file_store = file_store ,
199+ write_json = write_json
200+ )
201+
202+ # Read back
203+ new_data : model .DictObjectStore [model .Identifiable ] = model .DictObjectStore ()
204+ new_files = aasx .DictSupplementaryFileContainer ()
205+ with aasx .AASXReader (filename ) as reader :
206+ reader .read_into (new_data , new_files )
207+
208+ # Assertions
209+ self .assertIn (referenced_submodel .id , new_data )
210+ self .assertIn (unreferenced_submodel .id , new_data ) # all objects are written
211+ os .unlink (filename )
0 commit comments