Skip to content

Commit

Permalink
remove _convert_metadata + new message
Browse files Browse the repository at this point in the history
  • Loading branch information
R-Palazzo committed Oct 12, 2023
1 parent a6ca725 commit 18d6e41
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 60 deletions.
18 changes: 3 additions & 15 deletions sdmetrics/reports/base_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,6 @@ def validate(self, real_data, synthetic_data, metadata):
def _handle_results(self, verbose):
raise NotImplementedError

@staticmethod
def _convert_metadata(metadata):
"""If the metadta is not a dict, try to convert it."""
if not isinstance(metadata, dict):
try:
metadata = metadata.to_dict()
except Exception:
raise TypeError(
'The provided metadata is not a dictionary and does not have a to_dict method.'
'Please convert the metadata to a dictionary.'
)

return metadata

@staticmethod
def convert_datetimes(real_data, synthetic_data, metadata):
"""Try to convert all datetime columns to datetime dtype.
Expand Down Expand Up @@ -112,7 +98,9 @@ def generate(self, real_data, synthetic_data, metadata, verbose=True):
verbose (bool):
Whether or not to print report summary and progress.
"""
metadata = self._convert_metadata(metadata)
if not isinstance(metadata, dict):
raise TypeError('The provided metadata is not a dictionary.')

self.validate(real_data, synthetic_data, metadata)
self.convert_datetimes(real_data, synthetic_data, metadata)

Expand Down
60 changes: 15 additions & 45 deletions tests/unit/reports/test_base_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,55 +152,27 @@ def test_convert_datetimes(self):
pd.testing.assert_frame_equal(real_data, expected_real_data)
pd.testing.assert_frame_equal(synthetic_data, expected_synthetic_data)

def test__convert_metadata_with_to_dict_method(self):
"""Test ``_convert_metadata`` when the metadata object has a ``to_dict`` method."""
def test_generate_metadata_not_dict(self):
"""Test the ``generate`` method with metadata not being a dict."""
# Setup
metadata_example = {
'column1': {'sdtype': 'numerical'},
'column2': {'sdtype': 'categorical'},
}

class Metadata:
def __init__(self):
self.columns = metadata_example

def to_dict(self):
return self.columns

metadata = Metadata()

# Run
converted_metadata = BaseReport._convert_metadata(metadata)

# Assert
assert converted_metadata == metadata_example

def test__convert_metadata_without_to_dict_method(self):
"""Test ``_convert_metadata`` when the metadata object has no ``to_dict`` method."""
# Setup
metadata_example = {
'column1': {'sdtype': 'numerical'},
'column2': {'sdtype': 'categorical'},
}

class Metadata:
def __init__(self):
self.columns = metadata_example

metadata = Metadata()
base_report = BaseReport()
real_data = pd.DataFrame({
'column1': [1, 2, 3],
'column2': ['a', 'b', 'c']
})
synthetic_data = pd.DataFrame({
'column1': [1, 2, 3],
'column2': ['a', 'b', 'c']
})
metadata = 'metadata'

# Run and Assert
expected_message = re.escape(
'The provided metadata is not a dictionary and does not have a to_dict method.'
'Please convert the metadata to a dictionary.'
expected_message = (
'The provided metadata is not a dictionary.'
)
with pytest.raises(TypeError, match=expected_message):
BaseReport._convert_metadata(metadata)

result = BaseReport._convert_metadata(metadata_example)
assert result == metadata_example
base_report.generate(real_data, synthetic_data, metadata, verbose=False)

@patch('sdmetrics.reports.base_report.BaseReport._convert_metadata')
def test_generate(self, mock__convert_metadata):
"""Test the ``generate`` method.
Expand Down Expand Up @@ -232,13 +204,11 @@ def test_generate(self, mock__convert_metadata):
'column2': {'sdtype': 'categorical'}
}
}
mock__convert_metadata.return_value = metadata

# Run
base_report.generate(real_data, synthetic_data, metadata, verbose=False)

# Assert
mock__convert_metadata.assert_called_once_with(metadata)
mock_validate.assert_called_once_with(real_data, synthetic_data, metadata)
mock_handle_results.assert_called_once_with(False)
base_report._properties['Property 1'].get_score.assert_called_with(
Expand Down

0 comments on commit 18d6e41

Please sign in to comment.