Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

test_mtreenode.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import subprocess
  2. import tempfile
  3. import time
  4. import unittest
  5. from dataladmetadatamodel.metadatapath import MetadataPath
  6. from dataladmetadatamodel.mtreenode import MTreeNode
  7. from dataladmetadatamodel.text import Text
  8. default_paths = [
  9. "x",
  10. "a/0",
  11. "a/1",
  12. "a/b/0",
  13. "a/b/1",
  14. "a/b/2"
  15. ]
  16. class TestMTreeNode(unittest.TestCase):
  17. def test_basic(self):
  18. MTreeNode(Text)
  19. def test_adding(self):
  20. mtree_node = MTreeNode(Text)
  21. for path in default_paths:
  22. mtree_node.add_child_at(Text(f"content of: {path}"),
  23. MetadataPath(path))
  24. all_children = list(mtree_node.get_paths_recursive())
  25. self.assertEqual(len(default_paths), len(all_children))
  26. for path, child in all_children:
  27. self.assertIn(str(path), default_paths)
  28. self.assertIsInstance(child, Text)
  29. self.assertEqual(child.content, f"content of: {str(path)}")
  30. def test_get_paths_recursive(self):
  31. mtree_node = MTreeNode(Text)
  32. for path in default_paths:
  33. mtree_node.add_child_at(Text(f"content of: {path}"),
  34. MetadataPath(path))
  35. results = list(mtree_node.get_paths_recursive())
  36. returned_paths = set(map(lambda e: str(e[0]), results))
  37. self.assertSetEqual(set(default_paths), returned_paths)
  38. def test_get_paths_recursive_intermediate(self):
  39. mtree_node = MTreeNode(Text)
  40. for path in default_paths:
  41. mtree_node.add_child_at(Text(f"content of: {path}"),
  42. MetadataPath(path))
  43. results = list(mtree_node.get_paths_recursive(True))
  44. intermediate = set([
  45. "/".join(path.split("/")[:i])
  46. for path in default_paths
  47. for i in range(len(path))
  48. ] + default_paths)
  49. returned_paths = set(map(lambda e: str(e[0]), results))
  50. self.assertSetEqual(intermediate, returned_paths)
  51. class TestMTreeNodeMapping(unittest.TestCase):
  52. def test_adding_to_massive_tree(self):
  53. with tempfile.TemporaryDirectory() as metadata_store:
  54. subprocess.run(["git", "init", metadata_store])
  55. mtree = MTreeNode(leaf_class=Text)
  56. start_time = time.time()
  57. for first_part in range(10):
  58. for second_part in range(10):
  59. for third_part in range(10):
  60. metadata_path = MetadataPath(f"{first_part:03}/"
  61. f"{second_part:03}/"
  62. f"{third_part:03}")
  63. mtree.add_child_at(
  64. Text(content=f"content of: {str(metadata_path)}"),
  65. metadata_path)
  66. initialisation_duration = time.time() - start_time
  67. print(f"Initialised: {initialisation_duration:4f}")
  68. start_time = time.time()
  69. reference = mtree.write_out(metadata_store)
  70. write_out_duration = time.time() - start_time
  71. print(f"Written out: {write_out_duration:4f}")
  72. mtree = MTreeNode(leaf_class=Text,
  73. realm=metadata_store,
  74. reference=reference)
  75. start_time = time.time()
  76. mtree.read_in()
  77. read_in_duration = time.time() - start_time
  78. print(f"Read in: {read_in_duration:4f}")
  79. metadata_path = MetadataPath("5/5/xxx")
  80. start_time = time.time()
  81. mtree.add_child_at(
  82. Text(content=f"content of: {str(metadata_path)}"),
  83. metadata_path)
  84. add_duration = time.time() - start_time
  85. print(f"Added single entry: {add_duration:4f}")
  86. start_time = time.time()
  87. mtree.write_out()
  88. write_out_2nd_duration = time.time() - start_time
  89. print(f"Written out single entry: {write_out_2nd_duration:4f}")
  90. if __name__ == '__main__':
  91. unittest.main()