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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #-*- coding: utf-8 -*-
  2. import unittest
  3. from unittest import mock
  4. from unittest.mock import patch
  5. from lodel.exceptions import *
  6. from lodel.settings.validator import *
  7. class SettingValidatorTestCase(unittest.TestCase):
  8. def test_init_basic(self):
  9. """ Testing the SettingsValidator class instanciation"""
  10. valid = SettingValidator('string')
  11. #trying to call it
  12. valid('test')
  13. def test_init_badname(self):
  14. """ Testing SettingValidator instanciation with non existing validator
  15. name"""
  16. with self.assertRaises(LodelFatalError):
  17. SettingValidator('qklfhsdufgsdyfugigsdfsdlcknsdp')
  18. def test_noneswitch(self):
  19. """ Testing the none_is_valid switch given at validator instanciation
  20. """
  21. none_invalid = SettingValidator('int')
  22. none_valid = SettingValidator('int', none_is_valid = True)
  23. none_valid(None)
  24. with self.assertRaises(SettingsValidationError):
  25. none_invalid(None)
  26. def test_validator_registration(self):
  27. """ Testing the register_validator method of SettingValidator """
  28. mockfun = mock.MagicMock()
  29. vname = 'lfkjdshfkuhsdygsuuyfsduyf'
  30. testval = 'foo'
  31. SettingValidator.register_validator(vname, mockfun, 'test validator')
  32. #Using registered validator
  33. valid = SettingValidator(vname)
  34. valid(testval)
  35. mockfun.assert_called_once_with(testval)
  36. def test_validator_optargs_forwarding(self):
  37. """ Testing the ability for SettingValidator to forward optional
  38. arguments """
  39. mockfun = mock.MagicMock()
  40. vname = 'lkjdsfhsdiufhisduguig'
  41. testval = 'azertyuiop'
  42. SettingValidator.register_validator(vname, mockfun, 'test validator')
  43. #Using registered validator with more arguments
  44. valid = SettingValidator(vname,
  45. arga = 'a', argb = 42, argc = '1337')
  46. valid(testval)
  47. mockfun.assert_called_once_with(
  48. testval, arga='a', argb=42, argc='1337')