|
1 | 1 | # Copyright (c) 2025 the Eclipse BaSyx Authors |
2 | | -# |
| 2 | +# |
3 | 3 | # This program and the accompanying materials are made available under the terms of the MIT License, available in |
4 | 4 | # the LICENSE file of this project. |
5 | 5 | # |
@@ -27,7 +27,7 @@ def test_supplementary_file_container(self) -> None: |
27 | 27 | self.assertEqual("/TestFile.pdf", new_name) |
28 | 28 | f.seek(0) |
29 | 29 | 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 |
31 | 31 | self.assertEqual("/TestFile.pdf", new_name) |
32 | 32 |
|
33 | 33 | with open(__file__, 'rb') as f: |
@@ -68,22 +68,23 @@ def test_supplementary_file_container(self) -> None: |
68 | 68 | class AASXWriterTest(unittest.TestCase): |
69 | 69 | def test_writing_reading_example_aas(self) -> None: |
70 | 70 | # 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 |
73 | 73 | 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 |
75 | 75 | f.seek(0) |
76 | 76 |
|
77 | | - # Create OPC/AASX core properties |
| 77 | + # Create OPC/AASX core properties |
| 78 | + # create AASX metadata (core properties) |
78 | 79 | cp = pyecma376_2.OPCCoreProperties() |
79 | 80 | cp.created = datetime.datetime.now() |
80 | 81 | cp.creator = "Eclipse BaSyx Python Testing Framework" |
81 | 82 |
|
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 |
84 | 85 | 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 |
87 | 88 |
|
88 | 89 | # Write AASX file |
89 | 90 | # 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: |
126 | 127 | "78450a66f59d74c073bf6858db340090ea72a8b1") |
127 | 128 |
|
128 | 129 | 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