test_mtreenodemapper.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import subprocess
  2. import tempfile
  3. import unittest
  4. from typing import List
  5. from unittest import mock
  6. from dataladmetadatamodel.metadatapath import MetadataPath
  7. from dataladmetadatamodel.mtreenode import MTreeNode
  8. from dataladmetadatamodel.text import Text
  9. from dataladmetadatamodel.mapper.reference import Reference
  10. tree_location = "a000000000000000000000000000000000000000"
  11. file_location = "a001000000000000000000000000000000000001"
  12. def create_tree(file_names: List[str],
  13. sub_dir_names: List[str]
  14. ) -> MTreeNode:
  15. root_node = MTreeNode(Text)
  16. for sub_dir_name in sub_dir_names:
  17. sub_tree_node = MTreeNode(Text)
  18. for file_name in file_names:
  19. sub_tree_node.add_child(
  20. file_name,
  21. Text(f"content of: /{sub_dir_name}/{file_name}"))
  22. root_node.add_child(sub_dir_name, sub_tree_node)
  23. return root_node
  24. class TestMTreeNodeMapper(unittest.TestCase):
  25. def test_basic_unmapping(self):
  26. realm = "/tmp/t1"
  27. file_names = ["a", "b", "c"]
  28. sub_dir_names = ["sub0", "sub1", "sub2"]
  29. root_node = create_tree(file_names, sub_dir_names)
  30. with \
  31. mock.patch(
  32. "dataladmetadatamodel.mapper.gitmapper"
  33. ".mtreenodemapper.git_save_tree_node") as save_tree_node, \
  34. mock.patch(
  35. "dataladmetadatamodel.mapper.gitmapper"
  36. ".textmapper.git_save_str") as save_str:
  37. save_tree_node.configure_mock(return_value=tree_location)
  38. save_str.configure_mock(return_value=file_location)
  39. root_node.write_out(realm, "git")
  40. # Expect one (root) plus three (sub-dir trees) calls
  41. self.assertEqual(save_tree_node.call_count, 4)
  42. expected_sub_tree_calls = [
  43. mock.call(
  44. realm,
  45. [
  46. ("100644", "blob", file_location, file_name)
  47. for file_name in file_names
  48. ]
  49. )
  50. for _ in range(len(sub_dir_names))
  51. ]
  52. save_tree_node.assert_has_calls(expected_sub_tree_calls, any_order=True)
  53. expected_root_calls = [
  54. mock.call(
  55. realm,
  56. [
  57. ("040000", "tree", tree_location, sub_dir_name)
  58. for sub_dir_name in sub_dir_names
  59. ]
  60. )
  61. ]
  62. save_tree_node.assert_has_calls(expected_root_calls, any_order=True)
  63. def test_none_mapping_out(self):
  64. tree = MTreeNode(Text)
  65. reference = tree.write_out("/tmp/t1")
  66. self.assertTrue(reference.is_none_reference())
  67. def test_none_mapping_in(self):
  68. reference = Reference.get_none_reference("MTreeNode")
  69. tree = MTreeNode(Text, "/tmp/t1", reference)
  70. tree.read_in()
  71. self.assertEqual(len(tree.child_nodes), 0)
  72. def test_mapping_end_to_end(self):
  73. file_names = ["a", "b", "c"]
  74. sub_dir_names = ["sub0", "sub1", "sub2"]
  75. with tempfile.TemporaryDirectory() as realm:
  76. subprocess.run(["git", "init", realm])
  77. root_node = create_tree(file_names, sub_dir_names)
  78. reference = root_node.write_out(realm)
  79. new_root_node = MTreeNode(
  80. leaf_class=Text,
  81. realm=realm,
  82. reference=reference)
  83. new_root_node.read_in()
  84. tree_elements = list(new_root_node.get_paths_recursive())
  85. self.assertEqual(len(tree_elements),
  86. len(file_names) * len(sub_dir_names))
  87. self.assertEqual(
  88. [str(tree_element[0]) for tree_element in tree_elements],
  89. [
  90. sub_dir_name + "/" + file_name
  91. for sub_dir_name in sub_dir_names
  92. for file_name in file_names
  93. ]
  94. )
  95. self.assertEqual(
  96. [
  97. tree_element[1].read_in().content
  98. for tree_element in tree_elements
  99. ],
  100. [
  101. f"content of: /{sub_dir_name}/{file_name}"
  102. for sub_dir_name in sub_dir_names
  103. for file_name in file_names
  104. ]
  105. )
  106. if __name__ == '__main__':
  107. unittest.main()