diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/data/wrong/settings.yaml | 3 | ||||
-rw-r--r-- | tests/test_collection.py | 24 | ||||
-rw-r--r-- | tests/test_types.py | 258 |
3 files changed, 281 insertions, 4 deletions
diff --git a/tests/data/wrong/settings.yaml b/tests/data/wrong/settings.yaml index 83a542b..bf79572 100644 --- a/tests/data/wrong/settings.yaml +++ b/tests/data/wrong/settings.yaml @@ -23,3 +23,6 @@ fields: type: list list: string index: field + - name: cloud + type: cloud + help: 'There is no cloud type' diff --git a/tests/test_collection.py b/tests/test_collection.py index 832f421..f7ddf6d 100644 --- a/tests/test_collection.py +++ b/tests/test_collection.py @@ -305,6 +305,22 @@ class testComplexCollection(unittest.TestCase): self.assertIn('with_default', entry.yaml_data) self.assertIn('amount: 0', entry.yaml_data) + def test_load_field_loaders(self): + # Check that all fields have been loaded, with the right types + to_test = ( + ('name', lesana.types.LesanaString), + ('description', lesana.types.LesanaText), + ('position', lesana.types.LesanaString), + ('something', lesana.types.LesanaYAML), + ('tags', lesana.types.LesanaList), + ('keywords', lesana.types.LesanaList), + ('exists', lesana.types.LesanaBoolean), + ('with_default', lesana.types.LesanaString), + ('amount', lesana.types.LesanaInt), + ) + for f in to_test: + self.assertIsInstance(self.collection.fields[f[0]], f[1]) + class testCollectionWithErrors(unittest.TestCase): def setUp(self): @@ -320,8 +336,8 @@ class testCollectionWithErrors(unittest.TestCase): # check that the log contains a warning. with self.assertLogs(level=logging.WARNING) as cm: self.collection = lesana.Collection(self.tmpdir) - self.assertEqual(len(cm.output), 1) - self.assertIn("Invalid language", cm.output[0]) + self.assertEqual(len(cm.output), 2) + self.assertIn("Invalid language", cm.output[1]) # The collection will default to english, but should still work. self.collection.update_cache() self.assertIsNotNone(self.collection.settings) @@ -334,7 +350,7 @@ class testCollectionWithErrors(unittest.TestCase): self.assertIsNotNone(self.collection.settings) self.assertIsNotNone(self.collection.stemmer) # Fields with no "index" entry are not indexed - self.assertEqual(len(self.collection.settings['fields']), 7) + self.assertEqual(len(self.collection.settings['fields']), 8) self.assertEqual(len(self.collection.indexed_fields), 3) def test_init(self): @@ -343,7 +359,7 @@ class testCollectionWithErrors(unittest.TestCase): self.collection.settings['name'], "Lesana collection with certain errors", ) - self.assertEqual(len(self.collection.settings['fields']), 7) + self.assertEqual(len(self.collection.settings['fields']), 8) self.assertIsNotNone(self.collection.stemmer) self.assertEqual(len(self.collection.indexed_fields), 3) diff --git a/tests/test_types.py b/tests/test_types.py new file mode 100644 index 0000000..f27089d --- /dev/null +++ b/tests/test_types.py @@ -0,0 +1,258 @@ +import datetime +import decimal +import unittest + +from lesana import types + + +class testTypes(unittest.TestCase): + def setUp(self): + pass + + def tearDown(self): + pass + + def test_base(self): + checker = types.LesanaType() + + # The base class does not implement empty nor load + with self.assertRaises(NotImplementedError): + checker.empty() + + with self.assertRaises(NotImplementedError): + checker.load("") + + def test_string(self): + checker = types.LesanaString() + + s = checker.empty() + self.assertEqual(s, "") + + s = checker.load("Hello World!") + self.assertEqual(s, "Hello World!") + + s = checker.load(None) + self.assertEqual(s, None) + + def test_text(self): + checker = types.LesanaText() + + s = checker.empty() + self.assertEqual(s, "") + + s = checker.load("Hello World!") + self.assertEqual(s, "Hello World!") + + s = checker.load(None) + self.assertEqual(s, None) + + def test_int(self): + checker = types.LesanaInt() + + v = checker.empty() + self.assertEqual(v, 0) + + v = checker.load("10") + self.assertEqual(v, 10) + + v = checker.load(10.5) + self.assertEqual(v, 10) + + for d in ("ten", "10.5"): + with self.assertRaises(types.LesanaValueError): + checker.load(d) + + v = checker.load(None) + self.assertEqual(v, None) + + def test_float(self): + checker = types.LesanaFloat() + + v = checker.empty() + self.assertEqual(v, 0.0) + + v = checker.load("10") + self.assertEqual(v, 10) + + v = checker.load(10.5) + self.assertEqual(v, 10.5) + + v = checker.load("10.5") + self.assertEqual(v, 10.5) + + for d in ("ten"): + with self.assertRaises(types.LesanaValueError): + checker.load(d) + + v = checker.load(None) + self.assertEqual(v, None) + + def test_decimal(self): + checker = types.LesanaDecimal() + + v = checker.empty() + self.assertEqual(v, decimal.Decimal(0)) + + v = checker.load("10") + self.assertEqual(v, decimal.Decimal(10)) + + v = checker.load(10.5) + self.assertEqual(v, decimal.Decimal(10.5)) + + v = checker.load("10.5") + self.assertEqual(v, decimal.Decimal(10.5)) + + for d in ("ten"): + with self.assertRaises(types.LesanaValueError): + checker.load(d) + + v = checker.load(None) + self.assertEqual(v, None) + + def test_timestamp(self): + checker = types.LesanaTimestamp() + + v = checker.empty() + self.assertEqual(v, None) + + now = datetime.datetime.now() + v = checker.load(now) + self.assertEqual(v, now) + + v = checker.load("1600000000") + self.assertEqual(v, datetime.datetime(2020, 9, 13, 14, 26, 40)) + + today = datetime.date.today() + for d in ( + today, + "today", + "2020-13-01", "2020-01-01", + "2020-01-01 10:00" + ): + with self.assertRaises(types.LesanaValueError): + checker.load(d) + + v = checker.load(None) + self.assertEqual(v, None) + + def test_datetime(self): + checker = types.LesanaDatetime() + + v = checker.empty() + self.assertEqual(v, None) + + now = datetime.datetime.now() + v = checker.load(now) + self.assertEqual(v, now) + + today = datetime.date.today() + v = checker.load(today) + self.assertIsInstance(v, datetime.datetime) + for part in ('year', 'month', 'day'): + self.assertEqual(getattr(v, part), getattr(today, part)) + + v = checker.load("2020-01-01") + self.assertEqual(v, datetime.datetime(2020, 1, 1)) + + v = checker.load("2020-01-01 10:00") + self.assertEqual(v, datetime.datetime(2020, 1, 1, 10, 0)) + + for d in ("today", "2020-13-01"): + with self.assertRaises(types.LesanaValueError): + checker.load(d) + + v = checker.load(None) + self.assertEqual(v, None) + + def test_date(self): + checker = types.LesanaDate() + + v = checker.empty() + self.assertEqual(v, None) + + now = datetime.datetime.now() + v = checker.load(now) + self.assertIsInstance(v, datetime.date) + for part in ('year', 'month', 'day'): + self.assertEqual(getattr(v, part), getattr(now, part)) + + today = datetime.date.today() + v = checker.load(today) + self.assertEqual(v, today) + + v = checker.load("2020-01-01") + self.assertEqual(v, datetime.datetime(2020, 1, 1)) + + v = checker.load("2020-01-01 10:00") + self.assertEqual(v, datetime.datetime(2020, 1, 1, 10, 0)) + + for d in ("today", "2020-13-01"): + with self.assertRaises(types.LesanaValueError): + checker.load(d) + + v = checker.load(None) + self.assertEqual(v, None) + + def test_boolean(self): + checker = types.LesanaBoolean() + + v = checker.empty() + self.assertEqual(v, None) + + v = checker.load(True) + self.assertEqual(v, True) + + for d in ("maybe", "yes", "no"): + with self.assertRaises(types.LesanaValueError): + checker.load(d) + + v = checker.load(None) + self.assertEqual(v, None) + + def test_file(self): + checker = types.LesanaFile() + + v = checker.empty() + self.assertEqual(v, "") + + v = checker.load("relative/path/to/file") + self.assertEqual(v, "relative/path/to/file") + + v = checker.load(None) + self.assertEqual(v, None) + + # TODO: check for invalid file paths + + def test_url(self): + checker = types.LesanaURL() + + v = checker.empty() + self.assertEqual(v, "") + + v = checker.load("http://example.org") + self.assertEqual(v, "http://example.org") + + v = checker.load(None) + self.assertEqual(v, None) + + # TODO: check for invalid URLs + + def test_yaml(self): + checker = types.LesanaYAML() + + v = checker.empty() + self.assertEqual(v, None) + + some_data = { + 'anything': 'goes', + 'everything': 42 + } + v = checker.load(some_data) + self.assertEqual(v, some_data) + + v = checker.load(None) + self.assertEqual(v, None) + + +if __name__ == '__main__': + unittest.main() |