|
1 | | -# Copyright (c) 2025 the Eclipse BaSyx Authors |
| 1 | +# Copyright (c) 2026 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. |
@@ -152,6 +152,106 @@ def test_writing_reading_example_aas(self) -> None: |
152 | 152 | os.unlink(filename) |
153 | 153 |
|
154 | 154 |
|
| 155 | +class AASXReaderTest(unittest.TestCase): |
| 156 | + def _create_test_aasx(self) -> str: |
| 157 | + data = example_aas.create_full_example() |
| 158 | + files = aasx.DictSupplementaryFileContainer() |
| 159 | + |
| 160 | + with open(os.path.join(os.path.dirname(__file__), 'TestFile.pdf'), 'rb') as f: |
| 161 | + files.add_file("/TestFile.pdf", f, "application/pdf") |
| 162 | + f.seek(0) |
| 163 | + |
| 164 | + # Core properties |
| 165 | + cp = pyecma376_2.OPCCoreProperties() |
| 166 | + cp.created = datetime.datetime.now() |
| 167 | + cp.creator = "Eclipse BaSyx Python Testing Framework" |
| 168 | + |
| 169 | + fd, filename = tempfile.mkstemp(suffix=".aasx") |
| 170 | + os.close(fd) |
| 171 | + |
| 172 | + with aasx.AASXWriter(filename) as writer: |
| 173 | + writer.write_aas( |
| 174 | + 'https://acplt.org/Test_AssetAdministrationShell', |
| 175 | + data, files, write_json=False |
| 176 | + ) |
| 177 | + writer.write_core_properties(cp) |
| 178 | + |
| 179 | + return filename |
| 180 | + |
| 181 | + def test_init_file_handling(self) -> None: |
| 182 | + # Missing file assertion test |
| 183 | + with self.assertRaises(FileNotFoundError): |
| 184 | + aasx.AASXReader("does_not_exist.aasx") |
| 185 | + |
| 186 | + # Invalid file assertion test |
| 187 | + fd, invalid_path = tempfile.mkstemp() |
| 188 | + os.write(fd, b"not a file") |
| 189 | + os.close(fd) |
| 190 | + |
| 191 | + try: |
| 192 | + with self.assertRaises(ValueError): |
| 193 | + aasx.AASXReader(invalid_path) |
| 194 | + finally: |
| 195 | + os.unlink(invalid_path) |
| 196 | + |
| 197 | + def test_reading_core_properties(self) -> None: |
| 198 | + filename = self._create_test_aasx() |
| 199 | + |
| 200 | + try: |
| 201 | + with aasx.AASXReader(filename) as reader: |
| 202 | + cp = reader.get_core_properties() |
| 203 | + |
| 204 | + self.assertIsInstance(cp.created, datetime.datetime) |
| 205 | + self.assertEqual(cp.creator, "Eclipse BaSyx Python Testing Framework") |
| 206 | + self.assertIsNone(cp.lastModifiedBy) |
| 207 | + finally: |
| 208 | + os.unlink(filename) |
| 209 | + |
| 210 | + def test_read_into(self) -> None: |
| 211 | + filename = self._create_test_aasx() |
| 212 | + |
| 213 | + try: |
| 214 | + objects: model.DictObjectStore[model.Identifiable] = model.DictObjectStore() |
| 215 | + files = aasx.DictSupplementaryFileContainer() |
| 216 | + |
| 217 | + with warnings.catch_warnings(record=True) as w: |
| 218 | + with aasx.AASXReader(filename) as reader: |
| 219 | + ids = reader.read_into(objects, files) |
| 220 | + |
| 221 | + assert isinstance(w, list) |
| 222 | + self.assertEqual(0, len(w)) # Ensure no warnings were raised |
| 223 | + |
| 224 | + self.assertGreater(len(ids), 0) # Ensure at least one AAS was read |
| 225 | + self.assertGreater(len(objects), 0) # Ensure objects were populated |
| 226 | + self.assertGreater(len(files), 0) |
| 227 | + self.assertEqual( |
| 228 | + files.get_content_type("/TestFile.pdf"), |
| 229 | + "application/pdf" |
| 230 | + ) |
| 231 | + finally: |
| 232 | + os.unlink(filename) |
| 233 | + |
| 234 | + def test_supplementary_file_integrity(self) -> None: |
| 235 | + filename = self._create_test_aasx() |
| 236 | + |
| 237 | + try: |
| 238 | + objects: model.DictObjectStore[model.Identifiable] = model.DictObjectStore() |
| 239 | + files = aasx.DictSupplementaryFileContainer() |
| 240 | + |
| 241 | + with aasx.AASXReader(filename) as reader: |
| 242 | + reader.read_into(objects, files) |
| 243 | + |
| 244 | + buf = io.BytesIO() |
| 245 | + files.write_file("/TestFile.pdf", buf) |
| 246 | + |
| 247 | + self.assertEqual( |
| 248 | + hashlib.sha1(buf.getvalue()).hexdigest(), |
| 249 | + "78450a66f59d74c073bf6858db340090ea72a8b1" |
| 250 | + ) |
| 251 | + finally: |
| 252 | + os.unlink(filename) |
| 253 | + |
| 254 | + |
155 | 255 | class AASXWriterReferencedSubmodelsTest(unittest.TestCase): |
156 | 256 |
|
157 | 257 | def test_only_referenced_submodels(self): |
|
0 commit comments