瀏覽代碼

checked_data_value passed test for datahandler class and derived class

m.orban 7 年之前
父節點
當前提交
c7c0952ffb

+ 14
- 13
lodel/leapi/datahandlers/base_classes.py 查看文件

@@ -1,4 +1,4 @@
1
-# -*- coding: utf-8 -*-
1
+#-*- co:ding: utf-8 -*-
2 2
 
3 3
 ## @package lodel.leapi.datahandlers.base_classes Define all base/abstract class for data handlers
4 4
 #
@@ -71,20 +71,18 @@ class DataHandler(object):
71 71
     def is_primary_key(self):
72 72
         return self.primary_key
73 73
 
74
+
74 75
     ##@brief checks if a fieldtype is internal
75 76
     # @return bool
76 77
     def is_internal(self):
77 78
         return self.internal is not False
78 79
 
79
-
80 80
     def _check_data_value(self, value):
81 81
         if value is None:
82 82
             if not self.nullable:
83
-                print(LodelExeption)
84 83
                 raise LodelExceptions("None value is forbidden")
85
-            print(self.nullable, value)
86 84
             raise DataNoneValid("None with a nullable. GOTO CHECK")
87
-        return value, None
85
+        return value
88 86
 
89 87
 #
90 88
 #    ##@brief calls the data_field defined _check_data_value() method
@@ -94,13 +92,12 @@ class DataHandler(object):
94 92
 #    #@return tuple (value, error|None)
95 93
     def check_data_value(self, value):
96 94
         try:
97
-            self._check_data_value(value)
95
+            value = self._check_data_value(value)
98 96
         except DataNoneValid as expt:
99 97
             return value, None
100
-        except LodelExceptions as expt:
98
+        except (LodelExceptions, FieldValidationError) as expt:
101 99
             return None, expt
102 100
         return value, None
103
-
104 101
 #
105 102
     ##@brief checks if this class can override the given data handler
106 103
     # @param data_handler DataHandler
@@ -298,11 +295,12 @@ class Reference(DataHandler):
298 295
     #@return tuple(value, exception)
299 296
     #@todo implement the check when we have LeObject to check value
300 297
     def _check_data_value(self, value):
301
-        super()._check_data_value(value)
298
+        value= super()._check_data_value(value)
302 299
         elt = self.__allowed_classes[0]
303 300
         uid = elt.uid_fieldname()[0]# TODO multiple uid is broken
304 301
         if (expt is None and not (isinstance(value, LeObject)) or (value is uid)):
305 302
             raise FieldValidationError("LeObject instance or id exxpected for a reference field")
303
+        return value
306 304
 
307 305
     def construct_data(self, emcomponent, fname, datas, cur_value):
308 306
         super().construct_data(emcomponent, fname, datas, cur_value)
@@ -347,10 +345,11 @@ class SingleRef(Reference):
347 345
     def __init__(self, allowed_classes = None, **kwargs):
348 346
         super().__init__(allowed_classes = allowed_classes)
349 347
  
350
-    def check_data_value(self, value):
351
-        super()._check_data_value(value)
348
+    def _check_data_value(self, value):
349
+        value = super()._check_data_value(value)
352 350
         if (expt is None and (len(val)>1)):
353 351
             raise FieldValidationError("List or string expected for a set field")
352
+        return value
354 353
 
355 354
 
356 355
 
@@ -367,17 +366,19 @@ class MultipleRef(Reference):
367 366
         self.max_item = max_item
368 367
         super().__init__(**kwargs)
369 368
 
370
-        
369
+       #@TODO  checkig one by one and put error in a list
371 370
     def _check_data_value(self, value):
372
-        super()._check_data_value(value)
371
+        value = super()._check_data_value(value)
373 372
         if not hasattr(value, '__iter__'):
374 373
             raise FieldValidationError("MultipleRef has to be an iterable or a string, '%s' found" % value)
375 374
         if self.max_item is not None:
376 375
             if self.max_item < len(value):
377 376
                 raise FieldValidationError("Too many items")
378 377
         ref_list = []
378
+        #Checked reference value one by one . 
379 379
         for v in value.items():
380 380
             ref_list.append(super()._check_data_value(v))
381
+        return value
381 382
 
382 383
     def construct_data(self, emcomponent, fname, datas, cur_value):
383 384
         cur_value = super().construct_data(emcomponent, fname, datas, cur_value)

+ 4
- 4
lodel/leapi/datahandlers/datas.py 查看文件

@@ -2,6 +2,7 @@
2 2
 import warnings
3 3
 import inspect
4 4
 from lodel.leapi.datahandlers.datas_base import *
5
+from lodel.leapi.datahandlers.base_classes import FieldValidationError
5 6
 import re
6 7
 
7 8
 ##@brief Data field designed to handle formated strings
@@ -46,12 +47,11 @@ max_length and regex'
46 47
         super(self.__class__, self).__init__(max_length=max_length, **kwargs)
47 48
 
48 49
     def _check_data_value(self, value):
49
-        error = None
50
+        value = super()._check_data_value(value)
50 51
         if not self.compiled_re.match(value) or len(value) > self.max_length:
51
-            value = ''
52 52
             msg = '"%s" doesn\'t match the regex "%s"' % (value, self.regex)
53
-            error = TypeError(msg)
54
-        return value, error
53
+            raise FieldValidationError(msg)
54
+        return value
55 55
 
56 56
     def can_override(self, data_handler):
57 57
         if not super().can_override(data_handler):

+ 31
- 41
lodel/leapi/datahandlers/datas_base.py 查看文件

@@ -4,7 +4,7 @@ import datetime
4 4
 import time
5 5
 import os
6 6
 
7
-from lodel.leapi.datahandlers.base_classes import DataField
7
+from lodel.leapi.datahandlers.base_classes import DataField, FieldValidationError
8 8
 
9 9
 ##@brief Data field designed to handle boolean values
10 10
 class Boolean(DataField):
@@ -14,18 +14,15 @@ class Boolean(DataField):
14 14
 
15 15
     ##@brief A boolean field
16 16
     def __init__(self, **kwargs):
17
-        if 'check_data_value' not in kwargs:
18
-            kwargs['check_data_value'] = self.check_data_value
17
+        #if 'check_data_value' not in kwargs:
18
+        #    kwargs['check_data_value'] = self._check_data_value
19 19
         super().__init__(ftype='bool', **kwargs)
20 20
 
21 21
     def _check_data_value(self, value):
22
-        error = None
23
-        try:
24
-            if type(value) != bool:
25
-                raise TypeError()
26
-        except(ValueError, TypeError):
27
-            error = TypeError("The value '%s' is not, and will never, be a boolean" % value)
28
-        return value, error
22
+        value = super()._check_data_value(value)   
23
+        if not isinstance(value, bool):
24
+            raise FieldValidationError("The value '%s' is not, and will never, be a boolean" % value)
25
+        return value
29 26
 
30 27
 ##@brief Data field designed to handle integer values
31 28
 class Integer(DataField):
@@ -37,17 +34,17 @@ class Integer(DataField):
37 34
     def __init__(self, **kwargs):
38 35
         super().__init__( **kwargs)
39 36
 
40
-    def _check_data_value(self, value):
41
-        error = None
37
+    def _check_data_value(self, value, strict = False):
38
+        value = super()._check_data_value(value)
39
+        if (strict and isinstance(value, int)):
40
+            raise FieldValidationError("The value '%s' is not a python type integer" % value)
42 41
         try:
43 42
             value = float(value)
44
-            if value % 1 == 0:
45
-                value = int(value)
46
-            else:
43
+            if value != int(value):
47 44
                 raise TypeError()
48 45
         except(ValueError, TypeError):
49
-            error = TypeError("The value '%s' is not, and will never, be an integer" % value)
50
-        return value, error
46
+            raise FieldValidationError("The value '%s' is not, and will never, be an integer" % value)
47
+        return value
51 48
 
52 49
 ##@brief Data field designed to handle string
53 50
 class Varchar(DataField):
@@ -72,17 +69,12 @@ class Varchar(DataField):
72 69
         return True
73 70
     
74 71
     def _check_data_value(self, value):
75
-        error = None
76
-        try:
77
-            value = str(value)
78
-            if len(value) > self.max_length:
79
-                raise ValueError
80
-        except TypeError:
81
-            error = TypeError("The value '%s' can't be a str" % value)
82
-        except ValueError:
83
-            error = ValueError("The value '%s' is longer than the maximum length of this field (%s)" % (value, self.max_length))
84
-        return value, error
85
-
72
+        value = super()._check_data_value(value)   
73
+        if not isinstance(value, str):
74
+            raise FieldValidationError("The value '%s' can't be a str" % value)
75
+        if len(value) > self.max_length:
76
+             raise FieldValidationError("The value '%s' is longer than the maximum length of this field (%s)" % (value, self.max_length))
77
+        return value
86 78
 ##@brief Data field designed to handle date & time 
87 79
 class DateTime(DataField):
88 80
 
@@ -100,15 +92,16 @@ class DateTime(DataField):
100 92
         super().__init__(**kwargs)
101 93
 
102 94
     def _check_data_value(self, value):
103
-        error = None
95
+        value = super()._check_data_value(value)
104 96
         if isinstance(value,str):
105 97
             try:
106 98
                 datetime_value = datetime.datetime.fromtimestamp(time.mktime(time.strptime(value, self.datetime_format)))
107
-            except ValueError:
108
-                error = ValueError("The value '%s' cannot be converted as a datetime" % value)
99
+            except ValueError: 
100
+                raise FieldValidationError("The value '%s' cannot be converted as a datetime" % value)
109 101
         elif not isinstance(value, datetime.datetime):
110
-            error = ValueError("Tue value has to be a string or a datetime")
111
-        return value, error
102
+            raise FieldValidationError("Tue value has to be a string or a datetime")
103
+        else:
104
+            return value
112 105
 
113 106
     def _construct_data(self, emcomponent, fname, datas, cur_value):
114 107
         if (self.now_on_create and cur_value is None) or self.now_on_update:
@@ -124,12 +117,10 @@ class Text(DataField):
124 117
         super(self.__class__, self).__init__(ftype='text', **kwargs)
125 118
     
126 119
     def _check_data_value(self, value):
127
-        error = None
128
-        try:
129
-            value = str(value)
130
-        except (ValueError, TypeError):
131
-            error = ValueError("The content passed to this Text field is not a convertible to a string")
132
-        return value, error
120
+        value = super()._check_data_value(value)   
121
+        if not isinstance(value, str):
122
+            raise FieldValidationError("The content passed to this Text field is not a convertible to a string")
123
+        return value
133 124
 
134 125
 ##@brief Data field designed to handle Files
135 126
 class File(DataField):
@@ -145,6 +136,5 @@ class File(DataField):
145 136
 
146 137
     # @todo Add here a check for the validity of the given value (should have a correct path syntax)
147 138
     def _check_data_value(self, value):
148
-        error = None
149
-        return value, error
139
+        return super()._check_data_value(value)   
150 140
 

+ 4
- 2
lodel/leapi/query.py 查看文件

@@ -336,9 +336,11 @@ field to use for the relational filter"
336 336
                     rel_filters.append((ret, operator, value))
337 337
             else:
338 338
                 # Casting value given datahandler
339
-                value, error = field_datahandler._check_data_value(value)
339
+                value_orig = value
340
+                value, error = field_datahandler.check_data_value(value)
341
+                if isinstance(error, Exception):
342
+                    value = value_orig
340 343
                 res_filters.append((field,operator, value))
341
-        
342 344
         if len(err_l) > 0:
343 345
             raise LeApiDataCheckErrors(
344 346
                                         "Error while preparing filters : ",

+ 1
- 1
tests/datahandlers/test_datetime.py 查看文件

@@ -22,4 +22,4 @@ class DatetimeTestCase(unittest.TestCase):
22 22
         test_datetime = DateTime(now_on_create=True, now_on_update=True)
23 23
         test_value = '2016-01-01-test'
24 24
         _, error = test_datetime.check_data_value(test_value)
25
-        self.assertIsNotNone(error)
25
+        self.assertIsNotNone(error)

+ 2
- 2
tests/datahandlers/test_regex.py 查看文件

@@ -18,7 +18,7 @@ class RegexTestCase(unittest.TestCase):
18 18
                            max_length=15)
19 19
         for test_value in ['800.9.10.5', 'test_string_value', '999.999.999.999']:
20 20
             value, error = test_regex.check_data_value(test_value)
21
-            self.assertEqual(value, '')
21
+            self.assertEqual(value, None)
22 22
             self.assertIsNotNone(error)
23 23
 
24 24
     def test_check_max_length(self):
@@ -27,7 +27,7 @@ class RegexTestCase(unittest.TestCase):
27 27
         for test_value in ['cccccc8', 'ccccccccccccccccccccccccccccccccc8']:
28 28
             value, error = test_regex.check_data_value(test_value)
29 29
             if len(test_value)>test_max_length:
30
-                self.assertEqual(value, '')
30
+                self.assertEqual(value, None)
31 31
                 self.assertIsNotNone(error)
32 32
             else:
33 33
                 self.assertIsNone(error)

+ 3
- 3
tests/datahandlers/test_varchar.py 查看文件

@@ -1,7 +1,7 @@
1 1
 import unittest
2 2
 
3 3
 from lodel.leapi.datahandlers.datas import Varchar, Integer
4
-
4
+from lodel.leapi.datahandlers.base_classes import FieldValidationError
5 5
 
6 6
 class VarcharTestCase(unittest.TestCase):
7 7
 
@@ -16,7 +16,7 @@ class VarcharTestCase(unittest.TestCase):
16 16
 
17 17
         _, error = test_varchar.check_data_value("c" * 11)
18 18
         self.assertIsNotNone(error)
19
-        self.assertIsInstance(error, ValueError)
19
+        self.assertIsInstance(error, FieldValidationError)
20 20
 
21 21
     def test_can_override(self):
22 22
         test_varchar1 = Varchar()
@@ -24,4 +24,4 @@ class VarcharTestCase(unittest.TestCase):
24 24
         test_varchar2 = Varchar()
25 25
 
26 26
         self.assertFalse(test_varchar1.can_override(test_integer))
27
-        self.assertTrue(test_varchar1.can_override(test_varchar2))
27
+        self.assertTrue(test_varchar1.can_override(test_varchar2))

Loading…
取消
儲存