mirror of
https://github.com/yweber/lodel2.git
synced 2026-01-06 23:42:13 +01:00
Added tests for datetime and file datahandlers, with corresponding checks in the corresponding classes
This commit is contained in:
parent
3700b4cc83
commit
52564ee6db
4 changed files with 82 additions and 4 deletions
|
|
@ -1,6 +1,8 @@
|
||||||
#-*- coding: utf-8 -*-
|
#-*- coding: utf-8 -*-
|
||||||
import warnings
|
import warnings
|
||||||
import datetime
|
import datetime
|
||||||
|
import time
|
||||||
|
import os
|
||||||
|
|
||||||
from lodel.leapi.datahandlers.base_classes import DataField
|
from lodel.leapi.datahandlers.base_classes import DataField
|
||||||
|
|
||||||
|
|
@ -94,10 +96,15 @@ class DateTime(DataField):
|
||||||
def __init__(self, now_on_update=False, now_on_create=False, **kwargs):
|
def __init__(self, now_on_update=False, now_on_create=False, **kwargs):
|
||||||
self.now_on_update = now_on_update
|
self.now_on_update = now_on_update
|
||||||
self.now_on_create = now_on_create
|
self.now_on_create = now_on_create
|
||||||
|
self.datetime_format = '%Y-%m-%d' if 'format' not in kwargs else kwargs['format']
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
def _check_data_value(self, value):
|
def _check_data_value(self, value):
|
||||||
error = None
|
error = None
|
||||||
|
try:
|
||||||
|
datetime_value = datetime.datetime.fromtimestamp(time.mktime(time.strptime(value, self.datetime_format)))
|
||||||
|
except ValueError:
|
||||||
|
error = ValueError("The value '%s' cannot be converted as a datetime" % value)
|
||||||
return value, error
|
return value, error
|
||||||
|
|
||||||
def construct_data(self, emcomponent, fname, datas, cur_value):
|
def construct_data(self, emcomponent, fname, datas, cur_value):
|
||||||
|
|
@ -132,7 +139,8 @@ class File(DataField):
|
||||||
def __init__(self, upload_path=None, **kwargs):
|
def __init__(self, upload_path=None, **kwargs):
|
||||||
self.upload_path = upload_path
|
self.upload_path = upload_path
|
||||||
super().__init__(**kwargs)
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
|
# @todo Add here a check for the validity of the given value (should have a correct path syntax)
|
||||||
def _check_data_value(self, value):
|
def _check_data_value(self, value):
|
||||||
error = None
|
error = None
|
||||||
return value, error
|
return value, error
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,26 @@
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from lodel.leapi.datahandlers.datas import Concat
|
from lodel.leapi.datahandlers.datas import Concat, Varchar
|
||||||
from lodel.editorial_model.components import EmClass
|
from lodel.editorial_model.components import EmClass
|
||||||
|
from lodel.leapi.datahandlers.base_classes import DatasConstructor
|
||||||
|
|
||||||
class ConcatTestCase(unittest.TestCase):
|
class ConcatTestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
# @TODO use Data Constructors
|
||||||
def test_construct_data(self):
|
def test_construct_data(self):
|
||||||
test_class = EmClass('testing', display_name='testing class')
|
test_class = EmClass('testing', display_name='testing class')
|
||||||
test_class.new_field('field1', 'varchar')
|
test_class.new_field('field1', 'varchar')
|
||||||
test_class.new_field('field2', 'varchar')
|
test_class.new_field('field2', 'varchar')
|
||||||
|
|
||||||
|
datas = {'field1': 'o'*5, 'field2': 'k'*4}
|
||||||
|
datas2 = {'field1': 'o'*5, 'field2': 'k'*10}
|
||||||
|
|
||||||
test_concat = Concat(['field1', 'field2'], '*')
|
test_concat = Concat(['field1', 'field2'], '*')
|
||||||
concat_string_value = test_concat.construct_data(test_class, 'field', {'field1': 'o'*5, 'field2': 'k'*4}, '')
|
concat_string_value = test_concat.construct_data(test_class, 'field', datas, '')
|
||||||
self.assertEqual('%s*%s' % ('o'*5, 'k'*4), concat_string_value)
|
self.assertEqual('%s*%s' % ('o'*5, 'k'*4), concat_string_value)
|
||||||
|
|
||||||
test_concat.max_length=10
|
test_concat.max_length=10
|
||||||
concat_string_value = test_concat.construct_data(test_class, 'field', {'field1': 'o'*5, 'field2': 'k'*10}, '')
|
concat_string_value = test_concat.construct_data(test_class, 'field', datas2, '')
|
||||||
test_value = '%s*%s' % ('o'*5, 'k'*10)
|
test_value = '%s*%s' % ('o'*5, 'k'*10)
|
||||||
self.assertNotEqual(test_value, concat_string_value)
|
self.assertNotEqual(test_value, concat_string_value)
|
||||||
self.assertEqual(len(concat_string_value), test_concat.max_length)
|
self.assertEqual(len(concat_string_value), test_concat.max_length)
|
||||||
|
|
|
||||||
25
tests/datahandlers/test_datetime.py
Normal file
25
tests/datahandlers/test_datetime.py
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from lodel.leapi.datahandlers.datas import DateTime
|
||||||
|
|
||||||
|
|
||||||
|
class DatetimeTestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_datetime_check_data_value(self):
|
||||||
|
test_datetime = DateTime()
|
||||||
|
|
||||||
|
test_value = '2016-01-01'
|
||||||
|
_, error = test_datetime.check_data_value(test_value)
|
||||||
|
self.assertIsNone(error)
|
||||||
|
|
||||||
|
def test_datetime_check_data_value_with_custom_format(self):
|
||||||
|
test_value = '2016-01-01T10:20:30Z'
|
||||||
|
test_datetime = DateTime(format='%Y-%m-%dT%H:%M:%SZ')
|
||||||
|
_, error = test_datetime.check_data_value(test_value)
|
||||||
|
self.assertIsNone(error)
|
||||||
|
|
||||||
|
def test_check_bad_value(self):
|
||||||
|
test_datetime = DateTime(now_on_create=True, now_on_update=True)
|
||||||
|
test_value = '2016-01-01-test'
|
||||||
|
_, error = test_datetime.check_data_value(test_value)
|
||||||
|
self.assertIsNotNone(error)
|
||||||
40
tests/datahandlers/test_file.py
Normal file
40
tests/datahandlers/test_file.py
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
import os
|
||||||
|
import unittest
|
||||||
|
import tempfile
|
||||||
|
|
||||||
|
from lodel.leapi.datahandlers.datas import File, Varchar
|
||||||
|
|
||||||
|
|
||||||
|
class FileTestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
cls.test_file, cls.test_file_path = tempfile.mkstemp()
|
||||||
|
|
||||||
|
def test_check_correct_data_value(self):
|
||||||
|
|
||||||
|
test_file = File()
|
||||||
|
|
||||||
|
test_value = os.path.abspath(os.path.join(os.path.curdir,'test_file.txt'))
|
||||||
|
_, error = test_file.check_data_value(test_value)
|
||||||
|
self.assertIsNone(error)
|
||||||
|
|
||||||
|
@unittest.skip
|
||||||
|
def test_check_uncorrect_data_value(self):
|
||||||
|
test_file = File()
|
||||||
|
test_bad_value = "invalid_path"
|
||||||
|
_, error = test_file.check_data_value(test_bad_value)
|
||||||
|
self.assertIsNotNone(test_bad_value)
|
||||||
|
|
||||||
|
def test_can_override(self):
|
||||||
|
test_file = File()
|
||||||
|
|
||||||
|
test_file2 = File()
|
||||||
|
self.assertTrue(test_file.can_override(test_file2))
|
||||||
|
|
||||||
|
test_varchar = Varchar()
|
||||||
|
self.assertFalse(test_file.can_override(test_varchar))
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def tearDownClass(cls):
|
||||||
|
os.unlink(cls.test_file_path)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue