Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
BTheunissen committed Nov 9, 2023
1 parent 9a81e9e commit a5f25cd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ ignore = [
"G004", # Logging statement uses string formatting
"TCH002",
"TCH003",
"ANN202",
]
select = ["ALL"]
src = ["target_clickhouse"]
Expand Down
7 changes: 6 additions & 1 deletion target_clickhouse/sinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,12 @@ def handle_validation_error(record,

# Convert the problematic value to string only if it's not null
if problem_value is not None:
current_level[problem_key] = str(problem_value)
if isinstance(problem_value, dict):
# Convert the dict to JSON string
current_level[problem_key] = json.dumps(problem_value)
else:
current_level[problem_key] = str(problem_value)

if logger:
logger.warning("Validating converted record")
return record
Expand Down
32 changes: 32 additions & 0 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging
from typing import Optional

Expand Down Expand Up @@ -128,3 +129,34 @@ def test_nested_dict_with_nested_non_string():
isinstance(updated_record["address"]["city"], str)
), "The 'city' should have been converted to a string."
validator.validate(updated_record) # This should not raise an error

def test_single_level_schema_nested_dict_to_string():
record = {"name": {"first": "John", "last": "Doe"}, "age": 30}
try:
validator.validate(record)
except ValidationError as e:
updated_record = handle_validation_error(record, e, logger)
assert (
isinstance(updated_record["name"], str)
), "The 'name' should have been converted to a JSON string."
assert (
json.loads(updated_record["name"]) == {"first": "John", "last": "Doe"}
), "The JSON string is not correct."

def test_single_level_schema_deeply_nested_dict_to_string():
record = {"name":
{"first": "John", "last": "Doe",
"nicknames": {"short": "JD", "long": "Johnny"},
},
"age": 30,
}
try:
validator.validate(record)
except ValidationError as e:
updated_record = handle_validation_error(record, e, logger)
assert (
isinstance(updated_record["name"], str)
), "The 'name' field should have been converted to a JSON string."
assert (
"nicknames" in json.loads(updated_record["name"])
), "The JSON string does not correctly represent the nested dict."

0 comments on commit a5f25cd

Please sign in to comment.