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_metadatasource.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import subprocess
  2. import tempfile
  3. import unittest
  4. from pathlib import Path
  5. from dataladmetadatamodel import version_string
  6. from dataladmetadatamodel.metadatasource import (
  7. MetadataSource,
  8. ImmediateMetadataSource,
  9. LocalGitMetadataSource
  10. )
  11. class TestFactory(unittest.TestCase):
  12. def test_immediate_factory(self):
  13. content_obj = {
  14. "info": "test-content"
  15. }
  16. json_obj = {
  17. "@": {
  18. "type": "ImmediateMetadataSource",
  19. "version": version_string
  20. },
  21. MetadataSource.TYPE_KEY: ImmediateMetadataSource.TYPE,
  22. "content": content_obj
  23. }
  24. metadata_object = MetadataSource.from_json_obj(json_obj)
  25. self.assertIsInstance(metadata_object, ImmediateMetadataSource)
  26. self.assertEqual(metadata_object.content, content_obj)
  27. def test_local_git_metadata_factory(self):
  28. repository_path = Path("/a/b/c/1")
  29. object_reference = "object-reference-1"
  30. json_obj = {
  31. "@": {
  32. "type": "LocalGitMetadataSource",
  33. "version": version_string
  34. },
  35. MetadataSource.TYPE_KEY: LocalGitMetadataSource.TYPE,
  36. "git_repository_path": repository_path,
  37. "object_reference": object_reference
  38. }
  39. metadata_object = MetadataSource.from_json_obj(json_obj)
  40. self.assertIsInstance(metadata_object, LocalGitMetadataSource)
  41. self.assertEqual(metadata_object.git_repository_path, repository_path)
  42. self.assertEqual(metadata_object.object_reference, object_reference)
  43. def test_immediate_round_trip(self):
  44. content = {"test": "test-content-2"}
  45. immediate_metadata = ImmediateMetadataSource(content)
  46. created_object = MetadataSource.from_json_obj(
  47. immediate_metadata.to_json_obj())
  48. self.assertEqual(immediate_metadata, created_object)
  49. def test_local_git_metadata_round_trip(self):
  50. repository_path = Path("/a/b/c/2")
  51. object_reference = "object-reference-2"
  52. local_git_metadata = LocalGitMetadataSource(
  53. repository_path,
  54. object_reference)
  55. created_object = MetadataSource.from_json_obj(
  56. local_git_metadata.to_json_obj())
  57. self.assertEqual(local_git_metadata, created_object)
  58. class TestFactoryFails(unittest.TestCase):
  59. def test_missing_type(self):
  60. json_obj = {
  61. "@": {
  62. "type": "ImmediateMetadataSource",
  63. "version": version_string
  64. }
  65. }
  66. self.assertEqual(MetadataSource.from_json_obj(json_obj), None)
  67. def test_unknown_type(self):
  68. json_obj = {
  69. "@": {
  70. "type": "ImmediateMetadataSource",
  71. "version": version_string
  72. },
  73. MetadataSource.TYPE_KEY: "....."
  74. }
  75. self.assertEqual(MetadataSource.from_json_obj(json_obj), None)
  76. def test_faulty_local_git_metadata(self):
  77. json_obj = {
  78. "@": {
  79. "type": "LocalGitMetadataSource",
  80. "version": version_string
  81. },
  82. MetadataSource.TYPE_KEY: LocalGitMetadataSource.TYPE
  83. }
  84. self.assertEqual(MetadataSource.from_json_obj(json_obj), None)
  85. def test_faulty_immediate_metadata(self):
  86. json_obj = {
  87. "@": {
  88. "type": "ImmediateMetadataSource",
  89. "version": version_string
  90. },
  91. MetadataSource.TYPE_KEY: ImmediateMetadataSource.TYPE
  92. }
  93. self.assertEqual(MetadataSource.from_json_obj(json_obj), None)
  94. def test_faulty_class_type(self):
  95. json_obj = {
  96. "@": {
  97. "type": ".....",
  98. "version": version_string
  99. }
  100. }
  101. self.assertEqual(MetadataSource.from_json_obj(json_obj), None)
  102. def test_faulty_class_version(self):
  103. json_obj = {
  104. "@": {
  105. "type": "LocalGitMetadataSource",
  106. "version": "..."
  107. }
  108. }
  109. self.assertEqual(MetadataSource.from_json_obj(json_obj), None)
  110. class TestDeepCopy(unittest.TestCase):
  111. def test_copy_immediate_source(self):
  112. immediate_metadata = ImmediateMetadataSource("test_content")
  113. copied_metadata = immediate_metadata.deepcopy()
  114. self.assertEqual(immediate_metadata, copied_metadata)
  115. def test_copy_local_git_source(self):
  116. file_content = "Test file content"
  117. with \
  118. tempfile.TemporaryDirectory() as original_dir, \
  119. tempfile.TemporaryDirectory() as copy_dir:
  120. subprocess.run(["git", "init", original_dir])
  121. subprocess.run(["git", "init", copy_dir])
  122. object_hash = subprocess.check_output(
  123. f"echo '{file_content}'|git --git-dir {original_dir}/.git "
  124. f"hash-object --stdin -w",
  125. shell=True)
  126. immediate_source = LocalGitMetadataSource(
  127. Path(original_dir),
  128. object_hash.decode().strip())
  129. copied_object_reference = immediate_source.copy_object_to(
  130. Path(copy_dir))
  131. self.assertEqual(
  132. immediate_source.object_reference,
  133. copied_object_reference)
  134. copied_file_content = subprocess.check_output(
  135. f"git --git-dir {copy_dir}/.git cat-file blob "
  136. f"{copied_object_reference}",
  137. shell=True)
  138. self.assertEqual(
  139. file_content,
  140. copied_file_content.decode().strip())
  141. if __name__ == '__main__':
  142. unittest.main()