Browse Source

Add tests on SettingValidator class for major methods

solves #58
Yann Weber 7 years ago
parent
commit
e2234c5768
1 changed files with 57 additions and 0 deletions
  1. 57
    0
      tests/settings/test_validator.py

+ 57
- 0
tests/settings/test_validator.py View File

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

Loading…
Cancel
Save