def __init__(self, dict_: dict[str, list[str]],
strictness: bool = True) -> None:
self.inputs = dict_
- self.strict = strictness
+ self.strict = strictness # return None on absence of key, or fail?
def get_str(self, key: str, default: str = '',
ignore_strict: bool = False) -> str:
"""Retrieve single/first string value of key, or default."""
if key not in self.inputs.keys() or 0 == len(self.inputs[key]):
if self.strict and not ignore_strict:
- raise BadFormatException(f'no value found for key {key}')
+ raise NotFoundException(f'no value found for key {key}')
return default
return self.inputs[key][0]
def do_POST_day(self) -> str:
"""Update or insert Day of date and Todos mapped to it."""
# pylint: disable=too-many-locals
- date = self._params.get_str('date')
- day_comment = self._form_data.get_str('day_comment')
- make_type = self._form_data.get_str('make_type')
+ try:
+ date = self._params.get_str('date')
+ day_comment = self._form_data.get_str('day_comment')
+ make_type = self._form_data.get_str('make_type')
+ except NotFoundException as e:
+ raise BadFormatException(e) from e
old_todos = self._form_data.get_all_int('todo_id')
new_todos = self._form_data.get_all_int('new_todo')
comments = self._form_data.get_all_str('comment')
"""Update or insert Process of ?id= and fields defined in postvars."""
# pylint: disable=too-many-locals
# pylint: disable=too-many-statements
- title = self._form_data.get_str('title')
- description = self._form_data.get_str('description')
- effort = self._form_data.get_float('effort')
+ try:
+ title = self._form_data.get_str('title')
+ description = self._form_data.get_str('description')
+ effort = self._form_data.get_float('effort')
+ except NotFoundException as e:
+ raise BadFormatException from e
conditions = self._form_data.get_all_int('conditions')
blockers = self._form_data.get_all_int('blockers')
enables = self._form_data.get_all_int('enables')
@_delete_or_post(Condition, '/conditions')
def do_POST_condition(self, condition: Condition) -> str:
"""Update/insert Condition of ?id= and fields defined in postvars."""
- is_active = self._form_data.get_str('is_active') == 'True'
- title = self._form_data.get_str('title')
- description = self._form_data.get_str('description')
+ try:
+ is_active = self._form_data.get_str('is_active') == 'True'
+ title = self._form_data.get_str('title')
+ description = self._form_data.get_str('description')
+ except NotFoundException as e:
+ raise BadFormatException(e) from e
condition.is_active = is_active
condition.title.set(title)
condition.description.set(description)
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):
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)
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):
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_all_str(self) -> None:
"""Test InputsParser.get_all_str on strict and non-strict."""