test_util.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """
  2. This file tests odml util functions.
  3. """
  4. import unittest
  5. from odml.util import format_cardinality
  6. class TestUtil(unittest.TestCase):
  7. def test_format_cardinality(self):
  8. # Test empty set
  9. self.assertIsNone(format_cardinality(None))
  10. self.assertIsNone(format_cardinality([]))
  11. self.assertIsNone(format_cardinality({}))
  12. self.assertIsNone(format_cardinality(""))
  13. self.assertIsNone(format_cardinality(()))
  14. # Test empty tuple edge cases
  15. self.assertIsNone(format_cardinality((None, None)))
  16. self.assertIsNone(format_cardinality((0, 0)))
  17. self.assertIsNone(format_cardinality((None, 0)))
  18. self.assertIsNone(format_cardinality((0, None)))
  19. # Test single int max set
  20. self.assertEqual(format_cardinality(10), (None, 10))
  21. # Test tuple set
  22. set_val = (2, None)
  23. self.assertEqual(format_cardinality(set_val), set_val)
  24. set_val = (None, 2)
  25. self.assertEqual(format_cardinality(set_val), set_val)
  26. set_val = (2, 3)
  27. self.assertEqual(format_cardinality(set_val), set_val)
  28. # Test list simple list set
  29. set_val = [2, None]
  30. self.assertEqual(format_cardinality(set_val), tuple(set_val))
  31. set_val = [None, 2]
  32. self.assertEqual(format_cardinality(set_val), tuple(set_val))
  33. set_val = [2, 3]
  34. self.assertEqual(format_cardinality(set_val), tuple(set_val))
  35. # Test exact value tuple set
  36. set_val = (5, 5)
  37. self.assertEqual(format_cardinality(set_val), set_val)
  38. # Test set failures
  39. with self.assertRaises(ValueError):
  40. format_cardinality("a")
  41. with self.assertRaises(ValueError):
  42. format_cardinality([1])
  43. with self.assertRaises(ValueError):
  44. format_cardinality([1, 2, 3])
  45. with self.assertRaises(ValueError):
  46. format_cardinality({1: 2, 3: 4})
  47. with self.assertRaises(ValueError):
  48. format_cardinality(-1)
  49. with self.assertRaises(ValueError):
  50. format_cardinality((1, "b"))
  51. with self.assertRaises(ValueError):
  52. format_cardinality((1, 2, 3))
  53. with self.assertRaises(ValueError):
  54. format_cardinality((-1, 1))
  55. with self.assertRaises(ValueError):
  56. format_cardinality((1, -5))
  57. with self.assertRaises(ValueError) as exc:
  58. format_cardinality((5, 1))
  59. self.assertIn("Minimum larger than maximum ", str(exc))