test_reference.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import unittest
  2. from unittest.mock import patch
  3. from ..reference import Reference
  4. class TestReference(unittest.TestCase):
  5. def check_json_values(self, json_object):
  6. for value in json_object.values():
  7. if isinstance(value, (dict, list)):
  8. self.check_json_values(value)
  9. else:
  10. self.assertIsInstance(value, (str, int, float))
  11. def test_json_serialisation(self):
  12. reference = Reference("SomeClass", "some-location")
  13. json_object = reference.to_json_obj()
  14. self.check_json_values(json_object)
  15. self.assertEqual(json_object["class_name"], "SomeClass")
  16. self.assertEqual(json_object["location"], "some-location")
  17. def test_json_serialisation_deserialization(self):
  18. reference1 = Reference("SomeClass", "some-location")
  19. json_object = reference1.to_json_obj()
  20. reference2 = Reference.from_json_obj(json_object)
  21. self.assertEqual(reference1, reference2)
  22. def test_old_style_warning(self):
  23. reference1 = Reference("SomeClass", "some-location")
  24. json_object = reference1.to_json_obj()
  25. json_object["mapper_family"] = "git"
  26. json_object["realm"] = "/tmp/t"
  27. with patch("dataladmetadatamodel.mapper.reference.logger") as logger:
  28. reference2 = Reference.from_json_obj(json_object)
  29. self.assertEqual(reference1, reference2)
  30. logger.info.assert_called_once()
  31. def test_remote(self):
  32. for realm in ("git:x.com", "http://x.com/", "https://x.com/", "ssh:x@x.com"):
  33. self.assertTrue(Reference.is_remote(realm))
  34. self.assertFalse(Reference.is_local(realm))
  35. def test_local(self):
  36. for realm in ("a/b/c", "C:\\x", "c:\\x", "/a/b/c"):
  37. self.assertFalse(Reference.is_remote(realm))
  38. self.assertTrue(Reference.is_local(realm))
  39. if __name__ == '__main__':
  40. unittest.main()