PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/master/buildbot/test/unit/test_test_util_validation.py

https://gitlab.com/murder187ss/buildbot
Python | 200 lines | 172 code | 12 blank | 16 comment | 3 complexity | 24ed92dd10c31261f248075138d5d325 MD5 | raw file
  1. # This file is part of Buildbot. Buildbot is free software: you can
  2. # redistribute it and/or modify it under the terms of the GNU General Public
  3. # License as published by the Free Software Foundation, version 2.
  4. #
  5. # This program is distributed in the hope that it will be useful, but WITHOUT
  6. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  7. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  8. # details.
  9. #
  10. # You should have received a copy of the GNU General Public License along with
  11. # this program; if not, write to the Free Software Foundation, Inc., 51
  12. # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  13. #
  14. # Copyright Buildbot Team Members
  15. import datetime
  16. from twisted.python import log
  17. from twisted.trial import unittest
  18. from buildbot.test.util import validation
  19. from buildbot.util import UTC
  20. class VerifyDict(unittest.TestCase):
  21. def doValidationTest(self, validator, good, bad):
  22. for g in good:
  23. log.msg('expect %r to be good' % (g,))
  24. msgs = list(validator.validate('g', g))
  25. self.assertEqual(msgs, [], 'messages for %r' % (g,))
  26. for b in bad:
  27. log.msg('expect %r to be bad' % (b,))
  28. msgs = list(validator.validate('b', b))
  29. self.assertNotEqual(msgs, [], 'no messages for %r' % (b,))
  30. log.msg('..got messages:')
  31. for msg in msgs:
  32. log.msg(" " + msg)
  33. def test_IntValidator(self):
  34. self.doValidationTest(validation.IntValidator(),
  35. good=[
  36. 1, 10 ** 100
  37. ], bad=[
  38. 1.0, "one", "1", None
  39. ])
  40. def test_BooleanValidator(self):
  41. self.doValidationTest(validation.BooleanValidator(),
  42. good=[
  43. True, False
  44. ], bad=[
  45. "yes", "no", 1, 0, None
  46. ])
  47. def test_StringValidator(self):
  48. self.doValidationTest(validation.StringValidator(),
  49. good=[
  50. u"unicode only"
  51. ], bad=[
  52. None, "bytestring"
  53. ])
  54. def test_BinaryValidator(self):
  55. self.doValidationTest(validation.BinaryValidator(),
  56. good=[
  57. "bytestring"
  58. ], bad=[
  59. None, u"no unicode"
  60. ])
  61. def test_DateTimeValidator(self):
  62. self.doValidationTest(validation.DateTimeValidator(),
  63. good=[
  64. datetime.datetime(
  65. 1980, 6, 15, 12, 31, 15, tzinfo=UTC),
  66. ], bad=[
  67. None, 198847493,
  68. # no timezone
  69. datetime.datetime(1980, 6, 15, 12, 31, 15),
  70. ])
  71. def test_IdentifierValidator(self):
  72. self.doValidationTest(validation.IdentifierValidator(50),
  73. good=[
  74. u"linux", u"Linux", u"abc123", u"a" * 50,
  75. ], bad=[
  76. None, u'', 'linux', u'a/b', u'\N{SNOWMAN}', u"a.b.c.d",
  77. u"a-b_c.d9", 'spaces not allowed', u"a" * 51,
  78. u"123 no initial digits",
  79. ])
  80. def test_NoneOk(self):
  81. self.doValidationTest(
  82. validation.NoneOk(validation.BooleanValidator()),
  83. good=[
  84. True, False, None
  85. ], bad=[
  86. 1, "yes"
  87. ])
  88. def test_DictValidator(self):
  89. self.doValidationTest(validation.DictValidator(
  90. a=validation.BooleanValidator(),
  91. b=validation.StringValidator(),
  92. optionalNames=['b']),
  93. good=[
  94. {'a': True},
  95. {'a': True, 'b': u'xyz'},
  96. ],
  97. bad=[
  98. None, 1, "hi",
  99. {},
  100. {'a': 1},
  101. {'a': 1, 'b': u'xyz'},
  102. {'a': True, 'b': 999},
  103. {'a': True, 'b': u'xyz', 'c': 'extra'},
  104. ])
  105. def test_DictValidator_names(self):
  106. v = validation.DictValidator(
  107. a=validation.BooleanValidator())
  108. self.assertEqual(list(v.validate('v', {'a': 1})), [
  109. "v['a'] (1) is not a boolean"
  110. ])
  111. def test_ListValidator(self):
  112. self.doValidationTest(
  113. validation.ListValidator(validation.BooleanValidator()),
  114. good=[
  115. [],
  116. [True],
  117. [False, True],
  118. ], bad=[
  119. None,
  120. ['a'],
  121. [True, 'a'],
  122. 1, "hi"
  123. ])
  124. def test_ListValidator_names(self):
  125. v = validation.ListValidator(validation.BooleanValidator())
  126. self.assertEqual(list(v.validate('v', ['a'])), [
  127. "v[0] ('a') is not a boolean"
  128. ])
  129. def test_SourcedPropertiesValidator(self):
  130. self.doValidationTest(validation.SourcedPropertiesValidator(),
  131. good=[
  132. {u'pname': ('{"a":"b"}', u'test')},
  133. ], bad=[
  134. None, 1, "hi",
  135. {u'pname': {'a': 'b'}}, # no source
  136. # name not unicode
  137. {'pname': ({'a': 'b'}, u'test')},
  138. # source not unicode
  139. {u'pname': ({'a': 'b'}, 'test')},
  140. # self is not json-able
  141. {u'pname': (self, u'test')},
  142. ])
  143. def test_MessageValidator(self):
  144. self.doValidationTest(validation.MessageValidator(
  145. events=['started', 'stopped'],
  146. messageValidator=validation.DictValidator(
  147. a=validation.BooleanValidator(),
  148. xid=validation.IntValidator(),
  149. yid=validation.IntValidator())),
  150. good=[
  151. (('thing', '1', '2', 'started'),
  152. {'xid': 1, 'yid': 2, 'a': True}),
  153. ], bad=[
  154. # routingKey is not a tuple
  155. ('thing', {}),
  156. # routingKey has wrong event
  157. (('thing', '1', '2', 'exploded'),
  158. {'xid': 1, 'yid': 2, 'a': True}),
  159. # routingKey element has wrong type
  160. (('thing', 1, 2, 'started'),
  161. {'xid': 1, 'yid': 2, 'a': True}),
  162. # routingKey element isn't in message
  163. (('thing', '1', '2', 'started'),
  164. {'xid': 1, 'a': True}),
  165. # message doesn't validate
  166. (('thing', '1', '2', 'started'),
  167. {'xid': 1, 'yid': 2, 'a': 'x'}),
  168. ])
  169. def test_Selector(self):
  170. sel = validation.Selector()
  171. sel.add(lambda x: x == 'int', validation.IntValidator())
  172. sel.add(lambda x: x == 'str', validation.StringValidator())
  173. self.doValidationTest(sel,
  174. good=[
  175. ('int', 1),
  176. ('str', u'hi'),
  177. ], bad=[
  178. ('int', u'hi'),
  179. ('str', 1),
  180. ('float', 1.0),
  181. ])