Browse Source

Proposed rewrite of test_regex

Here is a rewritten test suite for Regex DataHandler. This rewrite aims
at being lighter, more readable, accurate.
Quentin Bonaventure 7 years ago
parent
commit
a9e90b7368
1 changed files with 63 additions and 49 deletions
  1. 63
    49
      tests/datahandlers/test_regex.py

+ 63
- 49
tests/datahandlers/test_regex.py View File

@@ -1,56 +1,70 @@
1 1
 import unittest
2 2
 from lodel.exceptions import *
3
-from lodel.leapi.datahandlers.datas import Regex, Varchar, Integer
4
-
3
+from lodel.leapi.datahandlers.datas import Regex, Varchar, Integer, UniqID
4
+from lodel.leapi.datahandlers.base_classes import DataHandler
5
+from unittest.mock import Mock
6
+import re
5 7
 
6 8
 class RegexTestCase(unittest.TestCase):
7
-
8
-    def test_check_correct_data_value(self):
9
-        test_value = '126.205.255.12'
10
-        test_regex = Regex('^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$',
11
-                           max_length=100)
12
-        value = test_regex._check_data_value(test_value)
13
-        self.assertEqual(value, test_value)
14
-
15
-    def test_check_bad_data_value(self):
16
-        test_regex = Regex('^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$',
17
-                           max_length=15)
18
-        for test_value in ['800.9.10.5', 'test_string_value', '999.999.999.999']:
19
-            with self.assertRaises(FieldValidationError):
20
-                test_regex._check_data_value(test_value)
21
-
22
-    def test_check_bad_compile_regex(self):
23
-        test_max_length = 15
24
-        test_regex = Regex('^\d[a-z]+8?', max_length=15)
25
-        for test_value in ['cccc8']:
26
-            with self.assertRaises(FieldValidationError):
27
-                test_regex._check_data_value(test_value)
28
-
29
-    def test_check_bad_max_length(self):
30
-        test_max_length = 15
31
-        test_regex = Regex('[a-z]+8?', max_length=15)
32
-        for test_value in ['ccccccccccccccccccccccccccccccccc8']:
33
-            with self.assertRaises(FieldValidationError):
34
-                test_regex._check_data_value(test_value)
9
+                
10
+                
11
+    def test_invalid_regex_throws_TypeError(self):
12
+        self.assertRaises(TypeError, Regex().__init__, object)
13
+                
14
+                
15
+    def test_compiled_re_property_is_set(self):
16
+        self.assertIs(self.testee.compiled_re, re.compile(self.testee.regex))
17
+    
18
+    
19
+    def test_exceeding_value_length_throws_FieldValidationError(self):
20
+        self.assertRaises(
21
+            FieldValidationError,
22
+            self.testee._check_data_value,
23
+            self.valid_value*self.max_length
24
+        )
25
+        
26
+            
27
+    def test_invalid_field_value_throws_FieldValidationError(self):
28
+        self.assertRaises(
29
+            FieldValidationError,
30
+            self.testee._check_data_value,
31
+            ''
32
+        )
33
+        
35 34
             
36
-    def test_check_good_max_length(self):
37
-        test_max_length = 15
38
-        test_regex = Regex('^\d[a-z]+8?', max_length=15)
39
-        for test_value in ['3ccccccccc8']:
40
-            value = test_regex._check_data_value(test_value)
41
-            self.assertEqual(value, test_value)
35
+    def test_valid_field_value_is_returned(self):
36
+        self.assertEqual(self.valid_value, self.testee._check_data_value(self.valid_value))
37
+        
38
+        
39
+    def test_can_override_returns_false_if_different_datahandler_base_type(self):
40
+        mock = self._get_datahandler_mock()
41
+        mock.__class__.base_type = self.testee.base_type*2
42
+ 
43
+        self.assertFalse(self.testee.can_override(mock))
44
+        
45
+        
46
+    def test_can_override_returns_false_if_different_datahandler_maxlen(self):
47
+        mock = self._get_datahandler_mock()
48
+        mock.__class__.max_length = self.max_length*2
42 49
 
43
-    def test_can_override(self):
44
-        test_regex = Regex('^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$',
45
-                           max_length=15)
46
-        for test_varchar in [Varchar(), Varchar(max_length=15), Varchar(max_length=9)]:
47
-            if test_regex.max_length == test_varchar.max_length:
48
-                self.assertTrue(test_regex.can_override(test_varchar))
49
-            else:
50
-                self.assertFalse(test_regex.can_override(test_varchar))
50
+        self.assertFalse(self.testee.can_override(mock))
51 51
 
52
-    def test_cant_override(self):
53
-        test_regex = Regex('^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$',
54
-                           max_length=15)
55
-        test_int = Integer()
56
-        self.assertFalse(test_regex.can_override(test_int))
52
+        
53
+    def test_can_override_returns_true_if_overridable(self):
54
+        self.assertTrue(self.testee.can_override(self._get_datahandler_mock()))
55
+        
56
+    
57
+    def _get_datahandler_mock(self):
58
+        dataHandlerMock = Mock()
59
+        dataHandlerMock.__class__.base_type = self.testee.base_type
60
+        dataHandlerMock.__class__.max_length = self.testee.max_length
61
+        
62
+        return dataHandlerMock
63
+    
64
+    
65
+    def setUp(self):
66
+        self.max_length = 15
67
+        self.regex = '^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
68
+        self.valid_value = '126.205.255.12'
69
+        self.testee = Regex(self.regex, self.max_length)
70
+        

Loading…
Cancel
Save