test_metadatamapper.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import json
  2. import unittest
  3. from pathlib import Path
  4. from typing import cast
  5. from unittest import mock
  6. from .... import version_string
  7. from ....tests.utils import get_location
  8. from dataladmetadatamodel.mapper import get_mapper
  9. from dataladmetadatamodel.mapper.gitmapper.metadatamapper import MetadataGitMapper
  10. from dataladmetadatamodel.mapper.gitmapper.objectreference import flush_object_references
  11. from dataladmetadatamodel.metadata import (
  12. ExtractorConfiguration,
  13. Metadata
  14. )
  15. expected_reference_object = {
  16. "@": {
  17. "type": "Reference",
  18. "version": version_string
  19. },
  20. "class_name": "Metadata",
  21. "location": get_location(1)
  22. }
  23. expected_metadata_object = {
  24. "@": {
  25. "type": "Metadata",
  26. "version": version_string
  27. },
  28. "instance_sets": {
  29. "test-extractor": {
  30. "@": {
  31. "type": "MetadataInstanceSet",
  32. "version": version_string
  33. },
  34. "parameter_set": [
  35. {
  36. "@": {
  37. "type": "ExtractorConfiguration",
  38. "version": version_string
  39. },
  40. "version": "v3.4",
  41. "parameter": {"p1": "1"}
  42. }
  43. ],
  44. "instance_set": {
  45. "0": {
  46. "@": {
  47. "type": "MetadataInstance",
  48. "version": version_string
  49. },
  50. "time_stamp": 1.2,
  51. "author": "test-tool",
  52. "author_email": "test-tool@test.com",
  53. "configuration": {
  54. "@": {
  55. "type": "ExtractorConfiguration",
  56. "version": version_string
  57. },
  58. "version": "v3.4",
  59. "parameter": {"p1": "1"}
  60. },
  61. "metadata_content": {
  62. "key1": "this is metadata"
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. class TestMetadataMapper(unittest.TestCase):
  70. def test_basic_unmapping(self):
  71. metadata = Metadata()
  72. metadata.add_extractor_run(
  73. 1.2,
  74. "test-extractor",
  75. "test-tool",
  76. "test-tool@test.com",
  77. ExtractorConfiguration("v3.4", {"p1": "1"}),
  78. {"key1": "this is metadata"})
  79. with \
  80. mock.patch("dataladmetadatamodel.mapper.gitmapper"
  81. ".metadatamapper.git_save_str") as save_str, \
  82. mock.patch("dataladmetadatamodel.mapper.gitmapper"
  83. ".metadatamapper.add_blob_reference") as add_ref:
  84. save_str.return_value = get_location(1)
  85. realm = "/tmp/t1"
  86. reference = metadata.write_out(realm, "git")
  87. flush_object_references(Path("/tmp/t1"))
  88. self.assertEqual(len(save_str.call_args_list), 2)
  89. self.assertEqual(
  90. json.loads(save_str.call_args_list[0][0][1]),
  91. expected_metadata_object)
  92. self.assertEqual(
  93. json.loads(save_str.call_args_list[1][0][1]),
  94. expected_reference_object)
  95. # ensure that the object reference stores are updated
  96. add_ref.assert_called_once()
  97. def test_double_cache_detection(self):
  98. metadata_mapper: MetadataGitMapper = cast(
  99. MetadataGitMapper,
  100. get_mapper("Metadata", "git")
  101. )
  102. metadata_mapper.cache_realm("a")
  103. self.assertRaises(RuntimeError, metadata_mapper.cache_realm, "a")
  104. self.assertRaises(RuntimeError, metadata_mapper.flush_realm, "err")
  105. if __name__ == '__main__':
  106. unittest.main()