Skip to content

Commit

Permalink
Fix boolean attributes formatting (#1925)
Browse files Browse the repository at this point in the history
  • Loading branch information
m1n0 authored Aug 10, 2023
1 parent 8ca2c05 commit 24b67e2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions soda/core/soda/common/attributes_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ def validate_attribute(self, key: str, value: any, schema: list[dict]) -> bool:

def format_attribute(self, value: any):
# Introduce formatting methods similar to validation methods if this gets more complex.
if isinstance(value, bool):
# Bool is a subclass of int, so we need to check for bool first and exit to prevent weird behavior.
return value
if isinstance(value, date):
value = datetime.combine(value, datetime.min.time())

Expand Down
19 changes: 19 additions & 0 deletions soda/core/tests/unit/test_attributes_handler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import date, datetime

import pytest
from helpers.data_source_fixture import DataSourceFixture
from soda.common.attributes_handler import AttributeHandler

Expand Down Expand Up @@ -92,3 +93,21 @@ def test_validation_unsupported_type(data_source_fixture: DataSourceFixture):
assert sorted(invalid.keys()) == sorted(attributes.keys())
assert valid == {}
scan.assert_has_error("Unsupported attribute type 'unsupported'.")


@pytest.mark.parametrize(
"value, expected",
[
("something", "something"),
(1, "1"),
(1.1, "1.1"),
(datetime(2022, 1, 1, 12, 0, 0), "2022-01-01T00:00:00+00:00"),
(date(2022, 1, 1), "2022-01-01T00:00:00+00:00"),
(True, True),
],
)
def test_formatting(value, expected, data_source_fixture: DataSourceFixture):
scan = data_source_fixture.create_test_scan()
attributes_handler = AttributeHandler(scan._logs)

assert attributes_handler.format_attribute(value) == expected

0 comments on commit 24b67e2

Please sign in to comment.