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.6 KB

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