Skip to content

Commit

Permalink
mock in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
R-Palazzo committed Oct 12, 2023
1 parent 873a4b7 commit a6ca725
Showing 1 changed file with 52 additions and 49 deletions.
101 changes: 52 additions & 49 deletions tests/unit/reports/test_base_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,56 @@ 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_generate(self):
def test__convert_metadata_with_to_dict_method(self):
"""Test ``_convert_metadata`` when the metadata object has a ``to_dict`` method."""
# 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()

# 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.'
)
with pytest.raises(TypeError, match=expected_message):
BaseReport._convert_metadata(metadata)

result = BaseReport._convert_metadata(metadata_example)
assert result == metadata_example

@patch('sdmetrics.reports.base_report.BaseReport._convert_metadata')
def test_generate(self, mock__convert_metadata):
"""Test the ``generate`` method.
This test checks that the method calls the ``validate`` method and the ``get_score``
Expand Down Expand Up @@ -183,11 +232,13 @@ def test_generate(self):
'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 Expand Up @@ -253,54 +304,6 @@ def test_generate_verbose(self, mock_tqdm):
mock_tqdm.assert_has_calls(calls, any_order=True)
base_report._handle_results.assert_called_once_with(True)

def test__convert_metadata_with_to_dict_method(self):
"""Test ``_convert_metadata`` when the metadata object has a ``to_dict`` method."""
# 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()

# 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.'
)
with pytest.raises(TypeError, match=expected_message):
BaseReport._convert_metadata(metadata)

result = BaseReport._convert_metadata(metadata_example)
assert result == metadata_example

def test__check_report_generated(self):
"""Test the ``check_report_generated`` method."""
# Setup
Expand Down

0 comments on commit a6ca725

Please sign in to comment.