test_constants.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Copyright (C) PyZMQ Developers
  2. # Distributed under the terms of the Modified BSD License.
  3. import json
  4. from unittest import TestCase
  5. import pytest
  6. import zmq
  7. from zmq.utils import constant_names
  8. from zmq.sugar import constants as sugar_constants
  9. from zmq.backend import constants as backend_constants
  10. all_set = set(constant_names.all_names)
  11. class TestConstants(TestCase):
  12. def _duplicate_test(self, namelist, listname):
  13. """test that a given list has no duplicates"""
  14. dupes = {}
  15. for name in set(namelist):
  16. cnt = namelist.count(name)
  17. if cnt > 1:
  18. dupes[name] = cnt
  19. if dupes:
  20. self.fail("The following names occur more than once in %s: %s" % (listname, json.dumps(dupes, indent=2)))
  21. def test_duplicate_all(self):
  22. return self._duplicate_test(constant_names.all_names, "all_names")
  23. def _change_key(self, change, version):
  24. """return changed-in key"""
  25. return "%s-in %d.%d.%d" % tuple([change] + list(version))
  26. def test_duplicate_changed(self):
  27. all_changed = []
  28. for change in ("new", "removed"):
  29. d = getattr(constant_names, change + "_in")
  30. for version, namelist in d.items():
  31. all_changed.extend(namelist)
  32. self._duplicate_test(namelist, self._change_key(change, version))
  33. self._duplicate_test(all_changed, "all-changed")
  34. def test_changed_in_all(self):
  35. missing = {}
  36. for change in ("new", "removed"):
  37. d = getattr(constant_names, change + "_in")
  38. for version, namelist in d.items():
  39. key = self._change_key(change, version)
  40. for name in namelist:
  41. if name not in all_set:
  42. if key not in missing:
  43. missing[key] = []
  44. missing[key].append(name)
  45. if missing:
  46. self.fail(
  47. "The following names are missing in `all_names`: %s" % json.dumps(missing, indent=2)
  48. )
  49. def test_no_negative_constants(self):
  50. for name in sugar_constants.__all__:
  51. self.assertNotEqual(getattr(zmq, name), sugar_constants._UNDEFINED)
  52. def test_undefined_constants(self):
  53. all_aliases = []
  54. for alias_group in sugar_constants.aliases:
  55. all_aliases.extend(alias_group)
  56. for name in all_set.difference(all_aliases):
  57. raw = getattr(backend_constants, name)
  58. if raw == sugar_constants._UNDEFINED:
  59. self.assertRaises(AttributeError, getattr, zmq, name)
  60. else:
  61. self.assertEqual(getattr(zmq, name), raw)
  62. def test_new(self):
  63. zmq_version = zmq.zmq_version_info()
  64. for version, new_names in constant_names.new_in.items():
  65. should_have = zmq_version >= version
  66. for name in new_names:
  67. try:
  68. value = getattr(zmq, name)
  69. except AttributeError:
  70. if should_have:
  71. self.fail("AttributeError: zmq.%s" % name)
  72. else:
  73. if not should_have:
  74. self.fail("Shouldn't have: zmq.%s=%s" % (name, value))
  75. @pytest.mark.skipif(not zmq.DRAFT_API, reason="Only test draft API if built with draft API")
  76. def test_draft(self):
  77. zmq_version = zmq.zmq_version_info()
  78. for version, new_names in constant_names.draft_in.items():
  79. should_have = zmq_version >= version
  80. for name in new_names:
  81. try:
  82. value = getattr(zmq, name)
  83. except AttributeError:
  84. if should_have:
  85. self.fail("AttributeError: zmq.%s" % name)
  86. else:
  87. if not should_have:
  88. self.fail("Shouldn't have: zmq.%s=%s" % (name, value))
  89. def test_removed(self):
  90. zmq_version = zmq.zmq_version_info()
  91. for version, new_names in constant_names.removed_in.items():
  92. should_have = zmq_version < version
  93. for name in new_names:
  94. try:
  95. value = getattr(zmq, name)
  96. except AttributeError:
  97. if should_have:
  98. self.fail("AttributeError: zmq.%s" % name)
  99. else:
  100. if not should_have:
  101. self.fail("Shouldn't have: zmq.%s=%s" % (name, value))