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_datasettree.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import subprocess
  2. import tempfile
  3. import unittest
  4. from dataladmetadatamodel.datasettree import DatasetTree
  5. from dataladmetadatamodel.metadatapath import MetadataPath
  6. from dataladmetadatamodel.metadatarootrecord import MetadataRootRecord
  7. from dataladmetadatamodel.tests.utils import (
  8. get_uuid,
  9. assert_dataset_trees_equal,
  10. create_dataset_tree
  11. )
  12. file_test_paths = [
  13. MetadataPath("a/b/c"),
  14. MetadataPath("a/b/a"),
  15. MetadataPath("b"),
  16. MetadataPath("c/d/e"),
  17. MetadataPath("a/x")]
  18. dataset_test_paths = [
  19. MetadataPath("d1"),
  20. MetadataPath("d1/d1.1"),
  21. MetadataPath("d2"),
  22. MetadataPath("d2/d2.1/d2.1.1"),
  23. MetadataPath("d3/d3.1")]
  24. uuid_0 = get_uuid(0)
  25. class TestDatasetTree(unittest.TestCase):
  26. def test_add_metadata(self):
  27. paths = [
  28. MetadataPath("a/b/c"),
  29. MetadataPath("a/b/a"),
  30. MetadataPath("b"),
  31. MetadataPath("c/d/e")]
  32. dataset_tree = DatasetTree()
  33. mrr = MetadataRootRecord(uuid_0, "00112233", None, None)
  34. for path in paths:
  35. dataset_tree.add_dataset(path, mrr)
  36. returned_entries = tuple(dataset_tree.get_dataset_paths())
  37. returned_paths = [entry[0] for entry in returned_entries]
  38. self.assertEqual(sorted(paths), sorted(returned_paths))
  39. for entry in returned_entries:
  40. self.assertEqual(entry[1], mrr)
  41. def test_root_node(self):
  42. dataset_tree = DatasetTree()
  43. mrr = MetadataRootRecord(uuid_0, "00112233", None, None)
  44. dataset_tree.add_dataset(MetadataPath(""), mrr)
  45. self.assertEqual(dataset_tree.get_metadata_root_record(MetadataPath("")), mrr)
  46. returned_entries = tuple(dataset_tree.get_dataset_paths())
  47. self.assertEqual(len(returned_entries), 1)
  48. self.assertEqual(returned_entries[0][0], MetadataPath(""))
  49. self.assertEqual(returned_entries[0][1], mrr)
  50. class TestDeepCopy(unittest.TestCase):
  51. def test_copy_from_memory(self):
  52. with \
  53. tempfile.TemporaryDirectory() as original_dir, \
  54. tempfile.TemporaryDirectory() as copy_dir:
  55. subprocess.run(["git", "init", original_dir])
  56. subprocess.run(["git", "init", copy_dir])
  57. dataset_tree = create_dataset_tree(dataset_test_paths,
  58. file_test_paths)
  59. dataset_tree_copy = dataset_tree.deepcopy("git", copy_dir)
  60. dataset_tree_copy.read_in()
  61. assert_dataset_trees_equal(
  62. self,
  63. dataset_tree,
  64. dataset_tree_copy,
  65. True)
  66. def test_copy_from_backend(self):
  67. with \
  68. tempfile.TemporaryDirectory() as original_dir, \
  69. tempfile.TemporaryDirectory() as copy_dir:
  70. subprocess.run(["git", "init", original_dir])
  71. subprocess.run(["git", "init", copy_dir])
  72. dataset_tree = create_dataset_tree(dataset_test_paths,
  73. file_test_paths)
  74. dataset_tree.write_out(original_dir)
  75. dataset_tree_copy = dataset_tree.deepcopy("git", copy_dir)
  76. dataset_tree_copy.read_in()
  77. assert_dataset_trees_equal(
  78. self,
  79. dataset_tree,
  80. dataset_tree_copy,
  81. True)
  82. class TestSubTreeManipulation(unittest.TestCase):
  83. def get_mrr(self, n: int = 0) -> MetadataRootRecord:
  84. return MetadataRootRecord(uuid_0, f"00112233-{n}", None, None)
  85. def test_subtree_adding(self):
  86. mrr_1 = self.get_mrr(1)
  87. mrr_2 = self.get_mrr(2)
  88. tree = DatasetTree()
  89. tree.add_dataset(MetadataPath("a/b/c"), mrr_1)
  90. subtree = DatasetTree()
  91. subtree.add_dataset(MetadataPath("d/e/f"), mrr_2)
  92. tree.add_subtree(subtree, MetadataPath("a/x"))
  93. mrr = tree.get_metadata_root_record(MetadataPath("a/b/c"))
  94. self.assertEqual(mrr, mrr_1)
  95. mrr = tree.get_metadata_root_record(MetadataPath("a/x/d/e/f"))
  96. self.assertEqual(mrr, mrr_2)
  97. def test_subtree_adding_with_conversion(self):
  98. mrr_1 = self.get_mrr()
  99. mrr_2 = self.get_mrr()
  100. tree = DatasetTree()
  101. tree.add_dataset(MetadataPath("a/b/c"), mrr_1)
  102. subtree = DatasetTree()
  103. subtree.add_dataset(MetadataPath("e/f"), mrr_2)
  104. tree.add_subtree(subtree, MetadataPath("a/b/c/d"))
  105. node = tree.get_metadata_root_record(MetadataPath("a/b/c"))
  106. self.assertEqual(node, mrr_1)
  107. node = tree.get_metadata_root_record(MetadataPath("a/b/c/d/e/f"))
  108. self.assertEqual(node, mrr_2)
  109. def test_subtree_adding_on_existing_path(self):
  110. tree = DatasetTree()
  111. tree.add_dataset(MetadataPath("a/b/c/d"), self.get_mrr())
  112. subtree = DatasetTree()
  113. subtree.add_dataset(MetadataPath("e/f"), self.get_mrr())
  114. self.assertRaises(
  115. ValueError,
  116. tree.add_subtree,
  117. subtree, MetadataPath("a/b/c/d"))
  118. def test_subtree_deletion(self):
  119. mrr_1 = self.get_mrr()
  120. mrr_2 = self.get_mrr()
  121. tree = DatasetTree()
  122. tree.add_dataset(MetadataPath("a/b/c"), mrr_1)
  123. tree.add_dataset(MetadataPath("a/b/c/d/e/f"), mrr_2)
  124. self.assertIsNotNone(tree.get_metadata_root_record(MetadataPath("a/b/c/d/e/f")))
  125. tree.delete_subtree(MetadataPath("a/b/c/d/e/f"))
  126. self.assertIsNone(tree.get_metadata_root_record(MetadataPath("a/b/c/d/e/f")))
  127. self.assertIsNotNone(tree.mtree.get_object_at_path(MetadataPath("a/b/c/d/e")))
  128. tree.delete_subtree(MetadataPath("a/b/c"))
  129. self.assertIsNotNone(tree.mtree.get_object_at_path(MetadataPath("a/b")))
  130. self.assertIsNone(tree.get_metadata_root_record(MetadataPath("a/b/c")))
  131. self.assertIsNone(tree.get_metadata_root_record(MetadataPath("a/b/c/d")))
  132. self.assertIsNone(tree.get_metadata_root_record(MetadataPath("a/b/c/d/e")))
  133. if __name__ == '__main__':
  134. unittest.main()