test_validation_integration.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. """
  2. This file tests built-in odml validations.
  3. """
  4. import sys
  5. import unittest
  6. try:
  7. from StringIO import StringIO
  8. except ImportError:
  9. from io import StringIO
  10. import odml
  11. class TestValidationIntegration(unittest.TestCase):
  12. def setUp(self):
  13. # Redirect stdout to test messages
  14. self.stdout_orig = sys.stdout
  15. self.capture = StringIO()
  16. sys.stdout = self.capture
  17. self.msg_base = "Property values cardinality violated"
  18. def tearDown(self):
  19. # Reset stdout; resetting using 'sys.__stdout__' fails on windows
  20. sys.stdout = self.stdout_orig
  21. self.capture.close()
  22. def _clear_output(self):
  23. self.capture.seek(0)
  24. self.capture.truncate()
  25. def _get_captured_output(self):
  26. out = [txt.strip() for txt in self.capture.getvalue().split('\n') if txt]
  27. # Buffer reset
  28. self.capture.seek(0)
  29. self.capture.truncate()
  30. return out
  31. def test_property_values_cardinality(self):
  32. # -- Test assignment validation warnings
  33. doc = odml.Document()
  34. sec = odml.Section(name="sec", type="sec_type", parent=doc)
  35. # Making sure only the required warnings are tested
  36. self._clear_output()
  37. # -- Test cardinality validation warnings on Property init
  38. # Test warning when setting invalid minimum
  39. _ = odml.Property(name="prop_card_min", values=[1], val_cardinality=(2, None), parent=sec)
  40. output = self._get_captured_output()
  41. test_msg = "%s (minimum %s values, %s found)" % (self.msg_base, 2, 1)
  42. self.assertEqual(len(output), 1)
  43. self.assertIn(test_msg, output[0])
  44. # Test warning when setting invalid maximum
  45. _ = odml.Property(name="prop_card_max", values=[1, 2, 3], val_cardinality=2, parent=sec)
  46. output = self._get_captured_output()
  47. test_msg = "%s (maximum %s values, %s found)" % (self.msg_base, 2, 3)
  48. self.assertEqual(len(output), 1)
  49. self.assertIn(test_msg, output[0])
  50. # Test no warning on valid init
  51. prop_card = odml.Property(name="prop_card", values=[1, 2],
  52. val_cardinality=(1, 5), parent=sec)
  53. output = self._get_captured_output()
  54. self.assertEqual(output, [])
  55. # -- Test cardinality validation warnings on cardinality updates
  56. # Test warning when setting minimally required values cardinality
  57. prop_card.val_cardinality = (3, None)
  58. output = self._get_captured_output()
  59. test_msg = "%s (minimum %s values, %s found)" % (self.msg_base, 3, 2)
  60. self.assertEqual(len(output), 1)
  61. self.assertIn(test_msg, output[0])
  62. # Test warning when setting maximally required values cardinality
  63. prop_card.values = [1, 2, 3]
  64. prop_card.val_cardinality = 2
  65. output = self._get_captured_output()
  66. test_msg = "%s (maximum %s values, %s found)" % (self.msg_base, 2, 3)
  67. self.assertEqual(len(output), 1)
  68. self.assertIn(test_msg, output[0])
  69. # Test no warning on valid cardinality
  70. prop_card.val_cardinality = (1, 10)
  71. output = self._get_captured_output()
  72. self.assertEqual(output, [])
  73. # Test no warning when setting cardinality to None
  74. prop_card.val_cardinality = None
  75. output = self._get_captured_output()
  76. self.assertEqual(output, [])
  77. # -- Test cardinality validation warnings on values updates
  78. # Test warning when violating minimally required values cardinality
  79. prop_card.val_cardinality = (3, None)
  80. prop_card.values = [1, 2]
  81. output = self._get_captured_output()
  82. test_msg = "%s (minimum %s values, %s found)" % (self.msg_base, 3, 2)
  83. self.assertEqual(len(output), 1)
  84. self.assertIn(test_msg, output[0])
  85. # Test warning when violating maximally required values cardinality
  86. prop_card.val_cardinality = (None, 2)
  87. prop_card.values = [1, 2, 3]
  88. output = self._get_captured_output()
  89. test_msg = "%s (maximum %s values, %s found)" % (self.msg_base, 2, 3)
  90. self.assertEqual(len(output), 1)
  91. self.assertIn(test_msg, output[0])
  92. # Test no warning when setting correct number of values
  93. prop_card.values = [1, 2]
  94. output = self._get_captured_output()
  95. self.assertEqual(output, [])
  96. # Test no warning when setting values to None
  97. prop_card.values = None
  98. output = self._get_captured_output()
  99. self.assertEqual(output, [])