Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add optional option #15

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions magicparse/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@ def __init__(self, options: dict) -> None:
PostProcessor.build(item) for item in options.get("post-processors", [])
]

self.optional = options.get("optional", False)

self.transforms = (
pre_processors + [type_converter] + validators + post_processors
)

def _process_raw_value(self, raw_value: str):
value = raw_value
if not raw_value:
if self.optional:
return None
else:
raise ValueError(
f"{self.key} field is required but the value was empty"
)
for transform in self.transforms:
value = transform.apply(value)
return value
Expand All @@ -34,10 +43,7 @@ def _read_raw_value(self, row) -> str:

def read_value(self, row):
raw_value = self._read_raw_value(row)
value = raw_value
for transform in self.transforms:
value = transform.apply(value)
return value
return self._process_raw_value(raw_value)

@abstractmethod
def error(self, exception: Exception):
Expand Down
43 changes: 43 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,46 @@ def test_columnar_error_format():
"error": "value 'hello' is not a valid decimal",
"field-key": "ratio",
}


def test_optional_field():
field = DummyField(
{
"key": "ratio",
"type": "decimal",
"optional": True,
"pre-processors": [
{
"name": "replace",
"parameters": {"pattern": "XXX", "replacement": "000"},
}
],
"post-processors": [{"name": "divide", "parameters": {"denominator": 100}}],
}
)
assert field.read_value("XXX150") == Decimal("1.50")
assert field.read_value("") is None


def test_required_field():
field = DummyField(
{
"key": "ratio",
"type": "decimal",
"optional": False,
}
)
assert field.read_value("1.5") == Decimal("1.50")


def test_require_field_with_empty_value():
field = DummyField(
{
"key": "pepito",
"type": "decimal",
}
)
with pytest.raises(
ValueError, match="pepito field is required but the value was empty"
):
field.read_value("")
Loading