Skip to content

Commit

Permalink
Strip whitespaces in BaseProcessor. Issue best-doctor#34
Browse files Browse the repository at this point in the history
  • Loading branch information
LerikP committed Sep 4, 2020
1 parent 988d8cc commit 3b3e2fb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
19 changes: 10 additions & 9 deletions import_me/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,20 @@ class BaseProcessor:
raise_error = True
none_if_error = False

def __init__(self, raise_error: bool = None, none_if_error: bool = None, **kwargs: Any):
def __init__(
self, raise_error: bool = None, none_if_error: bool = None,
strip_chars: str = None, strip_whitespace: bool = True,
**kwargs: Any,
):
if raise_error is not None:
self.raise_error = raise_error
if none_if_error is not None:
self.none_if_error = none_if_error

self.strip_chars = WHITESPACES if strip_whitespace else ''
if strip_chars:
self.strip_chars += strip_chars

def process_value(self, value: Any) -> Any:
return value

Expand All @@ -47,7 +55,7 @@ def process_value_error(self, value: Any, exc_info: Exception) -> Any:
return value

def __call__(self, value: Any) -> Any:
if value is None or (isinstance(value, str) and not value):
if value is None or (isinstance(value, str) and not value.strip(self.strip_chars)):
return None

try:
Expand Down Expand Up @@ -75,13 +83,6 @@ def process_value(self, value: Any) -> Any:


class StringProcessor(BaseProcessor):
def __init__(self, strip_chars: str = None, strip_whitespace: bool = True, **kwargs: Any) -> None:
super().__init__(**kwargs)

self.strip_chars = WHITESPACES if strip_whitespace else ''
if strip_chars:
self.strip_chars += strip_chars

def process_value(self, value: Any) -> Optional[str]:
if not isinstance(value, str):
value = str(value)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def test_multiple_processor_none_if_error():
(None, None, None, '20.07.2019', datetime.datetime(2019, 7, 20)),
(None, None, None, ' 20.07.2019 ', datetime.datetime(2019, 7, 20)),
(None, None, None, None, None),
(None, None, None, ' \xa0\n', None),
(None, None, None, '20.07.2019 12:43:52', datetime.datetime(2019, 7, 20, 12, 43, 52)),
(None, [], None, '20.07.2019 12:43:52', datetime.datetime(2019, 7, 20, 12, 43, 52)),
(None, '', None, '20.07.2019 12:43:52', datetime.datetime(2019, 7, 20, 12, 43, 52)),
Expand Down Expand Up @@ -199,6 +200,7 @@ def test_datetime_processor_error_date_value():
(None, None, '20.07.2019', datetime.date(2019, 7, 20)),
(None, None, ' 20.07.2019 ', datetime.date(2019, 7, 20)),
(None, None, None, None),
(None, None, ' \xa0\n', None),
(None, None, '20.07.2019 12:43:52', datetime.date(2019, 7, 20)),
([], None, '20.07.2019 12:43:52', datetime.date(2019, 7, 20)),
('', None, '20.07.2019 12:43:52', datetime.date(2019, 7, 20)),
Expand Down Expand Up @@ -300,6 +302,7 @@ def test_string_is_none_processor(none_symbols, value, expected_value):
('false', False),
(0, False),
('Нет', False),
(' \xa0\n', None),
),
)
def test_boolean_processor(value, expected_value):
Expand Down Expand Up @@ -336,6 +339,7 @@ def test_boolean_processor_exception():
(10, 10),
(10.0, 10),
(' 10 ', 10),
(' \xa0\n', None),
),
)
def test_integer_processor(value, expected_value):
Expand Down Expand Up @@ -372,6 +376,7 @@ def test_integer_processor_exception(value, expected_error_message):
(10.1, Decimal(10.1)),
('10.123', Decimal('10.123')),
(' 123,22 \n', Decimal('123.22')),
(' \xa0\n', None),
),
)
def test_decimal_processor(value, expected_value):
Expand Down Expand Up @@ -408,6 +413,7 @@ def test_decimal_processor_exception(value, expected_error_message):
(10.1, float(10.1)),
('10.123', float('10.123')),
(' 123,22 \n', float('123.22')),
(' \xa0\n', None),
),
)
def test_float_processor(value, expected_value):
Expand Down Expand Up @@ -440,6 +446,7 @@ def test_float_processor_exception(value, expected_error_message):
(
(None, None),
('', None),
(' \xa0\n', None),
('[email protected]', '[email protected]'),
('[email protected]', '[email protected]'),
('[email protected]', '[email protected]'),
Expand Down Expand Up @@ -479,6 +486,7 @@ def test_email_processor_exception(value, expected_error_message):
'value, choices, raw_value_processor, expected_value',
(
(None, None, None, None),
(' \xa0\n', None, None, None),
('2', {'1': 'First', '2': 'Second'}, None, 'Second'),
(2, {'1': 'First', '2': 'Second'}, None, 'Second'),
(' 2 ', {'1': 'First', '2': 'Second'}, None, 'Second'),
Expand Down

0 comments on commit 3b3e2fb

Please sign in to comment.