home · contact · privacy
Extend Todo tests.
[plomtask] / tests / misc.py
index b0fb872bbdc2f34b65f2c3f843762c451c34baa0..1713432576e779d72d7f47a35f120ce05e4d47ed 100644 (file)
@@ -2,7 +2,7 @@
 from unittest import TestCase
 from tests.utils import TestCaseWithServer
 from plomtask.http import InputsParser
-from plomtask.exceptions import BadFormatException
+from plomtask.exceptions import BadFormatException, NotFoundException
 
 
 class TestsSansServer(TestCase):
@@ -14,13 +14,13 @@ class TestsSansServer(TestCase):
         self.assertEqual('', parser.get_str('foo'))
         self.assertEqual('bar', parser.get_str('foo', 'bar'))
         parser.strict = True
-        with self.assertRaises(BadFormatException):
+        with self.assertRaises(NotFoundException):
             parser.get_str('foo')
-        with self.assertRaises(BadFormatException):
+        with self.assertRaises(NotFoundException):
             parser.get_str('foo', 'bar')
         parser = InputsParser({'foo': []}, False)
         self.assertEqual('bar', parser.get_str('foo', 'bar'))
-        with self.assertRaises(BadFormatException):
+        with self.assertRaises(NotFoundException):
             InputsParser({'foo': []}, True).get_str('foo', 'bar')
         for strictness in (False, True):
             parser = InputsParser({'foo': ['baz']}, strictness)
@@ -99,10 +99,6 @@ class TestsSansServer(TestCase):
     def test_InputsParser_get_float(self) -> None:
         """Test InputsParser.get_float on strict and non-strict."""
         for strictness in (False, True):
-            with self.assertRaises(BadFormatException):
-                InputsParser({}, strictness).get_float('foo')
-            with self.assertRaises(BadFormatException):
-                InputsParser({'foo': []}, strictness).get_float('foo')
             with self.assertRaises(BadFormatException):
                 InputsParser({'foo': ['']}, strictness).get_float('foo')
             with self.assertRaises(BadFormatException):
@@ -113,6 +109,41 @@ class TestsSansServer(TestCase):
             self.assertEqual(0.1, parser.get_float('foo'))
             parser = InputsParser({'foo': ['1.23', '456']}, strictness)
             self.assertEqual(1.23, parser.get_float('foo'))
+        if strictness:
+            with self.assertRaises(NotFoundException):
+                InputsParser({}, strictness).get_float('foo')
+            with self.assertRaises(NotFoundException):
+                InputsParser({'foo': []}, strictness).get_float('foo')
+        else:
+            with self.assertRaises(BadFormatException):
+                InputsParser({}, strictness).get_float('foo')
+            with self.assertRaises(BadFormatException):
+                InputsParser({'foo': []}, strictness).get_float('foo')
+
+    def test_InputsParser_get_float_or_none(self) -> None:
+        """Test InputsParser.get_float_or_none on strict and non-strict."""
+        for strictness in (False, True):
+            with self.assertRaises(BadFormatException):
+                InputsParser({'foo': ['bar']}, strictness).\
+                        get_float_or_none('foo')
+            parser = InputsParser({'foo': ['']}, strictness)
+            self.assertEqual(None, parser.get_float_or_none('foo'))
+            parser = InputsParser({'foo': ['0']}, strictness)
+            self.assertEqual(0, parser.get_float_or_none('foo'))
+            parser = InputsParser({'foo': ['0.1']}, strictness)
+            self.assertEqual(0.1, parser.get_float_or_none('foo'))
+            parser = InputsParser({'foo': ['1.23', '456']}, strictness)
+            self.assertEqual(1.23, parser.get_float_or_none('foo'))
+        if strictness:
+            with self.assertRaises(NotFoundException):
+                InputsParser({}, strictness).get_float_or_none('foo')
+            with self.assertRaises(NotFoundException):
+                InputsParser({'foo': []}, strictness).get_float_or_none('foo')
+        else:
+            parser = InputsParser({}, strictness)
+            self.assertEqual(None, parser.get_float_or_none('foo'))
+            parser = InputsParser({'foo': []}, strictness)
+            self.assertEqual(None, parser.get_float_or_none('foo'))
 
     def test_InputsParser_get_all_str(self) -> None:
         """Test InputsParser.get_all_str on strict and non-strict."""
@@ -151,7 +182,7 @@ class TestsWithServer(TestCaseWithServer):
     """Tests against our HTTP server/handler (and database)."""
 
     def test_do_GET(self) -> None:
-        """Test / redirect, and unknown targets failing."""
+        """Test GET / redirect, and unknown targets failing."""
         self.conn.request('GET', '/')
         self.check_redirect('/day')
         self.check_get('/foo', 404)