Skip to content

Commit

Permalink
Fixed bug in json formatter (#190)
Browse files Browse the repository at this point in the history
Closes #189 

Fixed bug, and added a pytest since there wasn't one
  • Loading branch information
jcadam14 authored May 15, 2024
1 parent 2da1812 commit 0eab98f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/regtech_data_validator/data_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,11 @@ def df_to_json(df: pd.DataFrame) -> str:
# for tying those objects back together. Grouping adds a little more processing
# time for smaller datasets but keeps really larger ones from crashing.
json_results = []
grouped_df = df.groupby('validation_id')
for group_name, group_data in grouped_df:
json_results.append(process_chunk(group_data, group_name))
json_results = sorted(json_results, key=lambda x: x['validation']['id'])
if not df.empty:
grouped_df = df.groupby('validation_id')
for group_name, group_data in grouped_df:
json_results.append(process_chunk(group_data, group_name))
json_results = sorted(json_results, key=lambda x: x['validation']['id'])
return ujson.dumps(json_results, indent=4, escape_forward_slashes=False)


Expand Down
7 changes: 7 additions & 0 deletions tests/test_output_formats.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pandas as pd
import ujson

from regtech_data_validator.data_formatters import df_to_csv, df_to_str, df_to_json, df_to_table, df_to_download
from regtech_data_validator.checks import Severity
Expand Down Expand Up @@ -85,6 +86,12 @@ def test_output_csv(self):
actual_output = df_to_csv(self.input_df)
assert actual_output.strip('\n') == expected_output

def test_empty_results_json(self):
expected_output = ujson.dumps([], indent=4, escape_forward_slashes=False)
actual_output = df_to_json(pd.DataFrame())

assert actual_output == expected_output

def test_output_json(self):
expected_output = dedent(
"""
Expand Down

0 comments on commit 0eab98f

Please sign in to comment.