Browse Source

Modified and added test for varchar. Modified strict test for datahandler integer

m.orban 8 years ago
parent
commit
08f07d45a8

+ 3
- 5
lodel/leapi/datahandlers/datas_base.py View File

50
             raise FieldValidationError("The value '%s' is not a python type integer" % value)
50
             raise FieldValidationError("The value '%s' is not a python type integer" % value)
51
         try:
51
         try:
52
             if strict:
52
             if strict:
53
-                if float(value) != int(value):
54
-                    raise FieldValidationError("The value '%s' is castable \
55
-into integer. But the DataHandler is strict and there is a floating part")
56
-            else:
57
                 value = int(value)
53
                 value = int(value)
54
+            else:
55
+                value = int(float(value))
58
         except(ValueError, TypeError):
56
         except(ValueError, TypeError):
59
             raise FieldValidationError("The value '%s' is not, and will never, be an integer" % value)
57
             raise FieldValidationError("The value '%s' is not, and will never, be an integer" % value)
60
-        return int(value)
58
+        return value
61
 
59
 
62
 ##@brief Data field designed to handle string
60
 ##@brief Data field designed to handle string
63
 class Varchar(DataField):
61
 class Varchar(DataField):

+ 8
- 10
tests/datahandlers/test_boolean.py View File

1
 import unittest
1
 import unittest
2
+from lodel.exceptions import *
2
 
3
 
3
 from lodel.leapi.datahandlers.datas import Boolean, Varchar, Integer
4
 from lodel.leapi.datahandlers.datas import Boolean, Varchar, Integer
4
 
5
 
5
 
6
 
7
+test_boolean = Boolean()
6
 class BooleanTestCase(unittest.TestCase):
8
 class BooleanTestCase(unittest.TestCase):
7
 
9
 
8
-    def test_boolean_check_data_value(self):
9
-        test_boolean = Boolean()
10
-
11
-        # correct values
10
+    def test_boolean_good_check_data_value(self):
12
         for test_value in [True, False]:
11
         for test_value in [True, False]:
13
-            _, error = test_boolean.check_data_value(test_value)
14
-            self.assertIsNone(error)
12
+            value = test_boolean._check_data_value(test_value)
13
+            self.assertEqual(value, bool(test_value))
15
 
14
 
16
-        # incorrect values
15
+    def test_boolean_bad_check_data_value(self):
17
         for test_value in ['ok', 'True', 'False']:
16
         for test_value in ['ok', 'True', 'False']:
18
-            _, error = test_boolean.check_data_value(test_value)
19
-            self.assertIsNotNone(error)
17
+            with self.assertRaises(FieldValidationError):
18
+                test_boolean._check_data_value(test_value)
20
 
19
 
21
     def test_can_override(self):
20
     def test_can_override(self):
22
-        test_boolean = Boolean()
23
 
21
 
24
         test_varchar = Varchar()
22
         test_varchar = Varchar()
25
         test_int = Integer()
23
         test_int = Integer()

+ 2
- 4
tests/datahandlers/test_integer.py View File

1
 import unittest
1
 import unittest
2
-
3
 from lodel.leapi.datahandlers.datas import Integer, Boolean
2
 from lodel.leapi.datahandlers.datas import Integer, Boolean
4
-
5
 from lodel.exceptions import *
3
 from lodel.exceptions import *
6
 
4
 
7
 test_int = Integer()
5
 test_int = Integer()
10
     
8
     
11
     def test_integer_check_bad_data_value(self):
9
     def test_integer_check_bad_data_value(self):
12
         # Incorrect values
10
         # Incorrect values
13
-        for test_bad_value in ['ok','ceci est un test', '15.2']:
11
+        for test_bad_value in ['ok','ceci est un test']:
14
             with self.assertRaises(FieldValidationError):
12
             with self.assertRaises(FieldValidationError):
15
                 test_int._check_data_value(test_bad_value)
13
                 test_int._check_data_value(test_bad_value)
16
 
14
 
17
     def test_integer_check_good_data_value(self):
15
     def test_integer_check_good_data_value(self):
18
         # Correct values
16
         # Correct values
19
-        for test_correct_value in [10, '15', 15.0, '-15.0']:
17
+        for test_correct_value in [10, '15.2', 15.0, '-15.0']:
20
             value = test_int._check_data_value(test_correct_value)
18
             value = test_int._check_data_value(test_correct_value)
21
             self.assertEqual(value, int(float(test_correct_value)))
19
             self.assertEqual(value, int(float(test_correct_value)))
22
 
20
 

+ 10
- 12
tests/datahandlers/test_varchar.py View File

1
 import unittest
1
 import unittest
2
-
3
 from lodel.leapi.datahandlers.datas import Varchar, Integer
2
 from lodel.leapi.datahandlers.datas import Varchar, Integer
4
 from lodel.leapi.datahandlers.base_classes import FieldValidationError
3
 from lodel.leapi.datahandlers.base_classes import FieldValidationError
4
+from lodel.exceptions import *
5
 
5
 
6
+test_varchar = Varchar(max_length=10)
6
 class VarcharTestCase(unittest.TestCase):
7
 class VarcharTestCase(unittest.TestCase):
7
 
8
 
8
-    def test_check_data_value(self):
9
-        test_varchar = Varchar(max_length=10)
10
-
11
-        _, error = test_varchar.check_data_value("c" * 10)
12
-        self.assertIsNone(error)
13
-
14
-        _, error = test_varchar.check_data_value("c" * 9)
15
-        self.assertIsNone(error)
9
+    def test_check_good_data_value(self):
10
+        for test_value in ["c" * 10, "c" * 9]:
11
+            value = test_varchar._check_data_value(test_value)
12
+            self.assertEqual(value, test_value)
16
 
13
 
17
-        _, error = test_varchar.check_data_value("c" * 11)
18
-        self.assertIsNotNone(error)
19
-        self.assertIsInstance(error, FieldValidationError)
14
+    def test_check_bad_data_value(self):
15
+        for test_value in ["c" * 11]:
16
+            with self.assertRaises(FieldValidationError):
17
+                value = test_varchar._check_data_value(test_value)
20
 
18
 
21
     def test_can_override(self):
19
     def test_can_override(self):
22
         test_varchar1 = Varchar()
20
         test_varchar1 = Varchar()

Loading…
Cancel
Save