test_versionlistmapper.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import unittest
  2. from unittest import mock
  3. from dataladmetadatamodel.mapper.gitmapper.gitblobcache import hash_blob
  4. from dataladmetadatamodel.metadata import Metadata
  5. from dataladmetadatamodel.metadatapath import MetadataPath
  6. from dataladmetadatamodel.metadatarootrecord import MetadataRootRecord
  7. from dataladmetadatamodel.tests.utils import (
  8. create_file_tree,
  9. get_location,
  10. get_uuid,
  11. )
  12. from dataladmetadatamodel.versionlist import (
  13. VersionList,
  14. VersionRecord,
  15. )
  16. test_realm_name = "ewkd0iasd"
  17. class TestVersionListMapper(unittest.TestCase):
  18. def test_basic_unmapping(self):
  19. with mock.patch("dataladmetadatamodel.mapper.gitmapper."
  20. "versionlistmapper.git_save_json") as save_json, \
  21. mock.patch("dataladmetadatamodel.mapper.gitmapper."
  22. "metadatamapper.git_save_str") as save_str, \
  23. mock.patch("dataladmetadatamodel.mapper.gitmapper."
  24. "versionlistmapper.git_update_ref"):
  25. save_json.configure_mock(return_value=get_location(0))
  26. save_str.configure_mock(return_value=get_location(1))
  27. version_list = VersionList()
  28. version_list.write_out("/tmp/t1")
  29. representation = save_json.call_args[0][1]
  30. self.assertEqual(representation, [])
  31. def test_complex_unmapping(self):
  32. with mock.patch("dataladmetadatamodel.mapper.gitmapper."
  33. "versionlistmapper.git_save_json") as save_json, \
  34. mock.patch("dataladmetadatamodel.mapper.gitmapper."
  35. "metadatamapper.git_save_str") as save_str, \
  36. mock.patch("dataladmetadatamodel.mapper.gitmapper."
  37. "metadatarootrecordmapper.git_save_json") as save_json_2, \
  38. mock.patch("dataladmetadatamodel.mapper.gitmapper."
  39. "mtreenodemapper.git_save_tree_node") as save_tree_node, \
  40. mock.patch("dataladmetadatamodel.mapper.gitmapper."
  41. "gitblobcache.git_save_file_list") as file_list_save, \
  42. mock.patch("dataladmetadatamodel.mapper.gitmapper."
  43. "versionlistmapper.git_update_ref"):
  44. save_json.return_value = get_location(0)
  45. save_str.return_value = get_location(1)
  46. save_json_2.return_value = get_location(3)
  47. save_tree_node.return_value = get_location(4)
  48. file_list_save.side_effect = lambda r, l: [
  49. hash_blob(open(e, "rb").read())
  50. for e in l
  51. ]
  52. version_list = VersionList(initial_set={
  53. "v0": VersionRecord(
  54. str("1.1"),
  55. MetadataPath("subset0"),
  56. MetadataRootRecord(
  57. get_uuid(0),
  58. "version1",
  59. Metadata(),
  60. create_file_tree(
  61. [MetadataPath("a/b"),
  62. MetadataPath("d/e")]
  63. )
  64. )
  65. )
  66. })
  67. version_list.write_out("/tmp/t1")
  68. representation = save_json.call_args[0][1]
  69. self.assertEqual(
  70. representation,
  71. [{
  72. 'primary_data_version': 'v0',
  73. 'time_stamp': '1.1',
  74. 'path': 'subset0',
  75. 'dataset_tree': {
  76. '@': {
  77. 'type': 'Reference',
  78. 'version': '2.0'
  79. },
  80. 'class_name': 'MetadataRootRecord',
  81. 'location': get_location(3)
  82. }
  83. }]
  84. )
  85. if __name__ == '__main__':
  86. unittest.main()