Browse Source

Added tests for datetime and file datahandlers, with corresponding checks in the corresponding classes

Roland Haroutiounian 8 years ago
parent
commit
52564ee6db

+ 9
- 1
lodel/leapi/datahandlers/datas_base.py View File

@@ -1,6 +1,8 @@
1 1
 #-*- coding: utf-8 -*-
2 2
 import warnings
3 3
 import datetime
4
+import time
5
+import os
4 6
 
5 7
 from lodel.leapi.datahandlers.base_classes import DataField
6 8
 
@@ -94,10 +96,15 @@ class DateTime(DataField):
94 96
     def __init__(self, now_on_update=False, now_on_create=False, **kwargs):
95 97
         self.now_on_update = now_on_update
96 98
         self.now_on_create = now_on_create
99
+        self.datetime_format = '%Y-%m-%d' if 'format' not in kwargs else kwargs['format']
97 100
         super().__init__(**kwargs)
98 101
 
99 102
     def _check_data_value(self, value):
100 103
         error = None
104
+        try:
105
+            datetime_value = datetime.datetime.fromtimestamp(time.mktime(time.strptime(value, self.datetime_format)))
106
+        except ValueError:
107
+            error = ValueError("The value '%s' cannot be converted as a datetime" % value)
101 108
         return value, error
102 109
 
103 110
     def construct_data(self, emcomponent, fname, datas, cur_value):
@@ -132,7 +139,8 @@ class File(DataField):
132 139
     def __init__(self, upload_path=None, **kwargs):
133 140
         self.upload_path = upload_path
134 141
         super().__init__(**kwargs)
135
-        
142
+
143
+    # @todo Add here a check for the validity of the given value (should have a correct path syntax)
136 144
     def _check_data_value(self, value):
137 145
         error = None
138 146
         return value, error

+ 8
- 3
tests/datahandlers/test_concat.py View File

@@ -1,21 +1,26 @@
1 1
 import unittest
2 2
 
3
-from lodel.leapi.datahandlers.datas import Concat
3
+from lodel.leapi.datahandlers.datas import Concat, Varchar
4 4
 from lodel.editorial_model.components import EmClass
5
+from lodel.leapi.datahandlers.base_classes import DatasConstructor
5 6
 
6 7
 class ConcatTestCase(unittest.TestCase):
7 8
 
9
+    # @TODO use Data Constructors
8 10
     def test_construct_data(self):
9 11
         test_class = EmClass('testing', display_name='testing class')
10 12
         test_class.new_field('field1', 'varchar')
11 13
         test_class.new_field('field2', 'varchar')
12 14
 
15
+        datas = {'field1': 'o'*5, 'field2': 'k'*4}
16
+        datas2 = {'field1': 'o'*5, 'field2': 'k'*10}
17
+
13 18
         test_concat = Concat(['field1', 'field2'], '*')
14
-        concat_string_value = test_concat.construct_data(test_class, 'field', {'field1': 'o'*5, 'field2': 'k'*4}, '')
19
+        concat_string_value = test_concat.construct_data(test_class, 'field', datas, '')
15 20
         self.assertEqual('%s*%s' % ('o'*5, 'k'*4), concat_string_value)
16 21
 
17 22
         test_concat.max_length=10
18
-        concat_string_value = test_concat.construct_data(test_class, 'field', {'field1': 'o'*5, 'field2': 'k'*10}, '')
23
+        concat_string_value = test_concat.construct_data(test_class, 'field', datas2, '')
19 24
         test_value = '%s*%s' % ('o'*5, 'k'*10)
20 25
         self.assertNotEqual(test_value, concat_string_value)
21 26
         self.assertEqual(len(concat_string_value), test_concat.max_length)

+ 25
- 0
tests/datahandlers/test_datetime.py View File

@@ -0,0 +1,25 @@
1
+import unittest
2
+
3
+from lodel.leapi.datahandlers.datas import DateTime
4
+
5
+
6
+class DatetimeTestCase(unittest.TestCase):
7
+
8
+    def test_datetime_check_data_value(self):
9
+        test_datetime = DateTime()
10
+
11
+        test_value = '2016-01-01'
12
+        _, error = test_datetime.check_data_value(test_value)
13
+        self.assertIsNone(error)
14
+
15
+    def test_datetime_check_data_value_with_custom_format(self):
16
+        test_value = '2016-01-01T10:20:30Z'
17
+        test_datetime = DateTime(format='%Y-%m-%dT%H:%M:%SZ')
18
+        _, error = test_datetime.check_data_value(test_value)
19
+        self.assertIsNone(error)
20
+
21
+    def test_check_bad_value(self):
22
+        test_datetime = DateTime(now_on_create=True, now_on_update=True)
23
+        test_value = '2016-01-01-test'
24
+        _, error = test_datetime.check_data_value(test_value)
25
+        self.assertIsNotNone(error)

+ 40
- 0
tests/datahandlers/test_file.py View File

@@ -0,0 +1,40 @@
1
+import os
2
+import unittest
3
+import tempfile
4
+
5
+from lodel.leapi.datahandlers.datas import File, Varchar
6
+
7
+
8
+class FileTestCase(unittest.TestCase):
9
+
10
+    @classmethod
11
+    def setUpClass(cls):
12
+        cls.test_file, cls.test_file_path = tempfile.mkstemp()
13
+
14
+    def test_check_correct_data_value(self):
15
+
16
+        test_file = File()
17
+
18
+        test_value = os.path.abspath(os.path.join(os.path.curdir,'test_file.txt'))
19
+        _, error = test_file.check_data_value(test_value)
20
+        self.assertIsNone(error)
21
+
22
+    @unittest.skip
23
+    def test_check_uncorrect_data_value(self):
24
+        test_file = File()
25
+        test_bad_value = "invalid_path"
26
+        _, error = test_file.check_data_value(test_bad_value)
27
+        self.assertIsNotNone(test_bad_value)
28
+
29
+    def test_can_override(self):
30
+        test_file = File()
31
+
32
+        test_file2 = File()
33
+        self.assertTrue(test_file.can_override(test_file2))
34
+
35
+        test_varchar = Varchar()
36
+        self.assertFalse(test_file.can_override(test_varchar))
37
+
38
+    @classmethod
39
+    def tearDownClass(cls):
40
+        os.unlink(cls.test_file_path)

Loading…
Cancel
Save