test_versionlist.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import subprocess
  2. import tempfile
  3. import unittest
  4. from pathlib import Path
  5. from dataladmetadatamodel.metadatapath import MetadataPath
  6. from dataladmetadatamodel.versionlist import (
  7. VersionList,
  8. VersionRecord
  9. )
  10. from dataladmetadatamodel.mapper.gitmapper.objectreference import flush_object_references
  11. from .utils import (
  12. assert_dataset_trees_equal,
  13. create_dataset_tree
  14. )
  15. file_test_paths = [
  16. MetadataPath("a/b/c"),
  17. MetadataPath("a/b/a"),
  18. MetadataPath("b"),
  19. MetadataPath("c/d/e"),
  20. MetadataPath("a/x")]
  21. dataset_test_paths = [
  22. [
  23. MetadataPath(""),
  24. MetadataPath("d1"),
  25. MetadataPath("d1/d1.1"),
  26. MetadataPath("d2"),
  27. MetadataPath("d2/d2.1/d2.1.1"),
  28. MetadataPath("d3/d3.1")
  29. ],
  30. [
  31. MetadataPath(""),
  32. MetadataPath("e1"),
  33. MetadataPath("e1/e1.1"),
  34. MetadataPath("e2"),
  35. MetadataPath("e2/e2.1/e2.1.1"),
  36. MetadataPath("e3/e3.1")
  37. ]
  38. ]
  39. class TestVersionList(unittest.TestCase):
  40. def test_basic(self):
  41. VersionList()
  42. def test_copy_end_to_end(self):
  43. with \
  44. tempfile.TemporaryDirectory() as original_dir, \
  45. tempfile.TemporaryDirectory() as copy_dir:
  46. subprocess.run(["git", "init", original_dir])
  47. subprocess.run(["git", "init", copy_dir])
  48. dataset_trees = [
  49. create_dataset_tree(
  50. dataset_test_paths[index],
  51. file_test_paths)
  52. for index in range(2)]
  53. dataset_trees[0].write_out(original_dir)
  54. dataset_trees[0].purge()
  55. dataset_trees[1].write_out(original_dir)
  56. version_list = VersionList(initial_set={
  57. "v1": VersionRecord(
  58. "0.1",
  59. MetadataPath("a"),
  60. dataset_trees[0]
  61. ),
  62. "v2": VersionRecord(
  63. "0.2",
  64. MetadataPath("b"),
  65. dataset_trees[1]
  66. )
  67. })
  68. version_list.write_out(original_dir)
  69. path_prefix = "x/y/z"
  70. copied_version_list = version_list.deepcopy(
  71. new_destination=copy_dir,
  72. path_prefix=MetadataPath(path_prefix))
  73. # Check that the mapping state of the original
  74. # has not changed.
  75. self.assertFalse(dataset_trees[0].mtree.mapped)
  76. self.assertTrue(dataset_trees[1].mtree.mapped)
  77. # Read the copied version list
  78. copied_version_list.read_in()
  79. # Check that the copied elements are unmapped
  80. for _, (_, _, copied_dataset_tree) in copied_version_list.versioned_elements:
  81. self.assertFalse(copied_dataset_tree.mtree.mapped)
  82. # Compare the version lists, taking modified dataset tree paths into account
  83. for primary_version, (_, path, dataset_tree) in version_list.versioned_elements:
  84. expected_path = path_prefix / path
  85. _, copy_path, copied_dataset_tree = copied_version_list.get_versioned_element(primary_version)
  86. self.assertEqual(expected_path, copy_path)
  87. dataset_tree.read_in()
  88. copied_dataset_tree.read_in()
  89. assert_dataset_trees_equal(self, dataset_tree, copied_dataset_tree, True)
  90. # NB! dataset tree copying is tested in dataset-tree related unit tests
  91. if __name__ == '__main__':
  92. unittest.main()