|
1 | 1 | import json |
| 2 | +import uuid |
2 | 3 | from typing import Any, Dict, Union |
3 | 4 | from uuid import UUID |
4 | 5 |
|
@@ -304,3 +305,53 @@ class have in common. {{Template:Editor/DrawIO |
304 | 305 | assert "File:OSW51ad8f9d660641f9880006c40f41cb56.png" in file_list |
305 | 306 | assert "File:OSW0bea84d54c074374a4b45dc84d9ba302.drawio.svg" in file_list |
306 | 307 | assert "File:OSW58baa09ec00b42ccb0779fe4d58ccf82.drawio.svg" in file_list |
| 308 | + |
| 309 | + |
| 310 | +def check_reproducible_uuid(): |
| 311 | + """Check if the UUID of an entity is reproducible by using the same name |
| 312 | + and overwriting the _init_uuid method. |
| 313 | + """ |
| 314 | + |
| 315 | + # Create two entities with the same name |
| 316 | + entity1 = model.Item(label=[model.Label(text="MyItem")]) |
| 317 | + entity2 = model.Item(label=[model.Label(text="MyItem")]) |
| 318 | + # and one with a different name |
| 319 | + entity3 = model.Item(label=[model.Label(text="MyItem2")]) |
| 320 | + |
| 321 | + # Check if the UUIDs are random / not the same |
| 322 | + assert entity1.uuid != entity2.uuid |
| 323 | + assert entity1.uuid != entity3.uuid |
| 324 | + assert entity2.uuid != entity3.uuid |
| 325 | + |
| 326 | + # a (random) UUID namespace in order to generate reproducible UUIDs |
| 327 | + namespace_uuid = UUID("0dd6c54a-b162-4552-bab9-9942ccaf4f41") |
| 328 | + |
| 329 | + # monkey patch the _init_uuid method to use the namespace UUID |
| 330 | + # this will generate the same UUID for the same name |
| 331 | + model.OswBaseModel._init_uuid = lambda **data: ( |
| 332 | + uuid.uuid5(namespace_uuid, data["name"]) if "name" in data else uuid.uuid4() |
| 333 | + ) |
| 334 | + |
| 335 | + # Create two entities with the same name |
| 336 | + entity1 = model.Item(label=[model.Label(text="MyItem")]) |
| 337 | + entity2 = model.Item(label=[model.Label(text="MyItem")]) |
| 338 | + # and one with a different name |
| 339 | + entity3 = model.Item(label=[model.Label(text="MyItem2")]) |
| 340 | + |
| 341 | + # Check if the UUIDs are the same |
| 342 | + assert entity1.uuid == entity2.uuid |
| 343 | + assert entity1.uuid != entity3.uuid |
| 344 | + |
| 345 | + # restore the original _init_uuid method |
| 346 | + model.OswBaseModel._init_uuid = lambda **data: uuid.uuid4() |
| 347 | + |
| 348 | + # Create two entities with the same name |
| 349 | + entity1 = model.Item(label=[model.Label(text="MyItem")]) |
| 350 | + entity2 = model.Item(label=[model.Label(text="MyItem")]) |
| 351 | + # and one with a different name |
| 352 | + entity3 = model.Item(label=[model.Label(text="MyItem2")]) |
| 353 | + |
| 354 | + # Check if the UUIDs are random / not the same |
| 355 | + assert entity1.uuid != entity2.uuid |
| 356 | + assert entity1.uuid != entity3.uuid |
| 357 | + assert entity2.uuid != entity3.uuid |
0 commit comments