No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_validator.py 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #
  2. # This file is part of Lodel 2 (https://github.com/OpenEdition)
  3. #
  4. # Copyright (C) 2015-2017 Cléo UMS-3287
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published
  8. # by the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import unittest
  20. from unittest import mock
  21. from unittest.mock import patch
  22. from lodel.exceptions import *
  23. from lodel.validator.validator import *
  24. class ValidatorTestCase(unittest.TestCase):
  25. def test_init_basic(self):
  26. """ Testing the SettingsValidator class instanciation"""
  27. valid = Validator('string')
  28. #trying to call it
  29. valid('test')
  30. def test_init_badname(self):
  31. """ Testing Validator instanciation with non existing validator
  32. name"""
  33. with self.assertRaises(LodelFatalError):
  34. Validator('qklfhsdufgsdyfugigsdfsdlcknsdp')
  35. def test_noneswitch(self):
  36. """ Testing the none_is_valid switch given at validator instanciation
  37. """
  38. none_invalid = Validator('int')
  39. none_valid = Validator('int', none_is_valid = True)
  40. none_valid(None)
  41. with self.assertRaises(ValidationError):
  42. none_invalid(None)
  43. def test_validator_registration(self):
  44. """ Testing the register_validator method of Validator """
  45. mockfun = mock.MagicMock()
  46. vname = 'lfkjdshfkuhsdygsuuyfsduyf'
  47. testval = 'foo'
  48. Validator.register_validator(vname, mockfun, 'test validator')
  49. #Using registered validator
  50. valid = Validator(vname)
  51. valid(testval)
  52. mockfun.assert_called_once_with(testval)
  53. def test_validator_optargs_forwarding(self):
  54. """ Testing the ability for Validator to forward optional
  55. arguments """
  56. mockfun = mock.MagicMock()
  57. vname = 'lkjdsfhsdiufhisduguig'
  58. testval = 'azertyuiop'
  59. Validator.register_validator(vname, mockfun, 'test validator')
  60. #Using registered validator with more arguments
  61. valid = Validator(vname,
  62. arga = 'a', argb = 42, argc = '1337')
  63. valid(testval)
  64. mockfun.assert_called_once_with(
  65. testval, arga='a', argb=42, argc='1337')