From 9b54ed0ba513c4fe2e7d403b014e9819a8e84fa5 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Wed, 7 Oct 2020 08:45:39 +0200 Subject: Validate also lists --- tests/test_types.py | 70 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 57 insertions(+), 13 deletions(-) (limited to 'tests/test_types.py') diff --git a/tests/test_types.py b/tests/test_types.py index 363d9a4..9e0c931 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -19,7 +19,7 @@ class testTypes(unittest.TestCase): } def test_base(self): - checker = types.LesanaType(self._get_field_def('base')) + checker = types.LesanaType(self._get_field_def('base'), {}) # The base class does not implement empty nor load with self.assertRaises(NotImplementedError): @@ -29,7 +29,7 @@ class testTypes(unittest.TestCase): checker.load("") def test_string(self): - checker = types.LesanaString(self._get_field_def('string')) + checker = types.LesanaString(self._get_field_def('string'), {}) s = checker.empty() self.assertEqual(s, "") @@ -41,7 +41,7 @@ class testTypes(unittest.TestCase): self.assertEqual(s, None) def test_text(self): - checker = types.LesanaText(self._get_field_def('text')) + checker = types.LesanaText(self._get_field_def('text'), {}) s = checker.empty() self.assertEqual(s, "") @@ -53,7 +53,7 @@ class testTypes(unittest.TestCase): self.assertEqual(s, None) def test_int(self): - checker = types.LesanaInt(self._get_field_def('integer')) + checker = types.LesanaInt(self._get_field_def('integer'), {}) v = checker.empty() self.assertEqual(v, 0) @@ -72,7 +72,7 @@ class testTypes(unittest.TestCase): self.assertEqual(v, None) def test_float(self): - checker = types.LesanaFloat(self._get_field_def('float')) + checker = types.LesanaFloat(self._get_field_def('float'), {}) v = checker.empty() self.assertEqual(v, 0.0) @@ -94,7 +94,7 @@ class testTypes(unittest.TestCase): self.assertEqual(v, None) def test_decimal(self): - checker = types.LesanaDecimal(self._get_field_def('decimal')) + checker = types.LesanaDecimal(self._get_field_def('decimal'), {}) v = checker.empty() self.assertEqual(v, decimal.Decimal(0)) @@ -116,7 +116,7 @@ class testTypes(unittest.TestCase): self.assertEqual(v, None) def test_timestamp(self): - checker = types.LesanaTimestamp(self._get_field_def('timestamp')) + checker = types.LesanaTimestamp(self._get_field_def('timestamp'), {}) v = checker.empty() self.assertEqual(v, None) @@ -142,7 +142,7 @@ class testTypes(unittest.TestCase): self.assertEqual(v, None) def test_datetime(self): - checker = types.LesanaDatetime(self._get_field_def('datetime')) + checker = types.LesanaDatetime(self._get_field_def('datetime'), {}) v = checker.empty() self.assertEqual(v, None) @@ -171,7 +171,7 @@ class testTypes(unittest.TestCase): self.assertEqual(v, None) def test_date(self): - checker = types.LesanaDate(self._get_field_def('date')) + checker = types.LesanaDate(self._get_field_def('date'), {}) v = checker.empty() self.assertEqual(v, None) @@ -200,7 +200,7 @@ class testTypes(unittest.TestCase): self.assertEqual(v, None) def test_boolean(self): - checker = types.LesanaBoolean(self._get_field_def('boolean')) + checker = types.LesanaBoolean(self._get_field_def('boolean'), {}) v = checker.empty() self.assertEqual(v, None) @@ -216,7 +216,7 @@ class testTypes(unittest.TestCase): self.assertEqual(v, None) def test_file(self): - checker = types.LesanaFile(self._get_field_def('file')) + checker = types.LesanaFile(self._get_field_def('file'), {}) v = checker.empty() self.assertEqual(v, "") @@ -230,7 +230,7 @@ class testTypes(unittest.TestCase): # TODO: check for invalid file paths def test_url(self): - checker = types.LesanaURL(self._get_field_def('url')) + checker = types.LesanaURL(self._get_field_def('url'), {}) v = checker.empty() self.assertEqual(v, "") @@ -244,7 +244,7 @@ class testTypes(unittest.TestCase): # TODO: check for invalid URLs def test_yaml(self): - checker = types.LesanaYAML(self._get_field_def('yaml')) + checker = types.LesanaYAML(self._get_field_def('yaml'), {}) v = checker.empty() self.assertEqual(v, None) @@ -259,6 +259,50 @@ class testTypes(unittest.TestCase): v = checker.load(None) self.assertEqual(v, None) + def test_list(self): + field_def = self._get_field_def('yaml') + # we use one type that is easy to check for correct validation + field_def['list'] = 'int' + checker = types.LesanaList(field_def, {'int': types.LesanaInt}) + + v = checker.empty() + self.assertEqual(v, []) + + some_data = [1, 2, 3] + v = checker.load(some_data) + self.assertEqual(v, some_data) + + v = checker.load(None) + self.assertEqual(v, []) + + for d in (['hello'], 1): + with self.assertRaises(types.LesanaValueError): + checker.load(d) + + def test_list_unknown_subtype(self): + field_def = self._get_field_def('yaml') + # we use one type that is easy to check for correct validation + field_def['list'] = 'int' + checker = types.LesanaList(field_def, {'yaml': types.LesanaYAML}) + + v = checker.empty() + self.assertEqual(v, []) + + some_data = [1, 2, 3] + v = checker.load(some_data) + self.assertEqual(v, some_data) + + some_data = ["hello"] + v = checker.load(some_data) + self.assertEqual(v, some_data) + + v = checker.load(None) + self.assertEqual(v, []) + + for d in (1, 1.0): + with self.assertRaises(types.LesanaValueError): + checker.load(d) + if __name__ == '__main__': unittest.main() -- cgit v1.2.3