Skip to content
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
19 changes: 19 additions & 0 deletions cdisc_rules_engine/services/data_readers/json_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ def from_file(self, file_path):
try:
with open(file_path, "r", encoding=self.encoding) as fp:
json_data = load(fp)
self._detect_whitespace_in_dataset_keys(json_data, file_path)
return json_data
except InvalidJSONFormat:
raise
except (UnicodeDecodeError, UnicodeError) as e:
raise InvalidJSONFormat(
f"\n Error reading JSON from: {file_path}"
Expand All @@ -23,5 +26,21 @@ def from_file(self, file_path):
f"\n {type(e).__name__}: {e}"
)

def _detect_whitespace_in_dataset_keys(self, json_data: dict, file_path: str):
offending = []
for dataset in json_data.get("datasets", []):
dataset_name = dataset.get("filename")
records = dataset.get("records", {})
for key in records:
if key != key.strip():
offending.append(f" dataset '{dataset_name}': {repr(key)}")
if offending:
offending_list = "\n".join(offending)
raise InvalidJSONFormat(
f"\n Error reading JSON from: {file_path}"
f"\n The following column keys contain leading/trailing whitespace:"
f"\n{offending_list}"
)

def read(self, data):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ def get_dataset(self, dataset_name: str, **params) -> DatasetInterface:
false_values=["False", "FALSE", "false", False, 0, "0"],
)
dataframe = dataframe.replace({nan: None})
offending = [col for col in dataframe.columns if col != col.strip()]
if offending:
raise ExcelTestDataError(
f"Sheet '{dataset_name}' has column headers with leading/trailing whitespace: "
f"{[repr(c) for c in offending]}."
)
dataset = PandasDataset(dataframe)
return dataset

Expand Down
Loading
Loading