Skip to content

Commit 75234f7

Browse files
#UNIT_TEST_BUG_FIX
Prompt Category: Unit Testing Prompt: Fix the Following error: ================================================ test session starts ================================================ platform darwin -- Python 3.9.6, pytest-8.3.5, pluggy-1.5.0 rootdir: /Users/surya.sandeep.boda/Desktop/Marscode Zero to One 3 plugins: cov-6.0.0 collected 6 items test_extract_data.py ..F.F. [100%] ===================================================== FAILURES ====================================================== __________________________________ TestGoogleSheetExtraction.test_invalid_ratings ___________________________________ self = <test_extract_data.TestGoogleSheetExtraction testMethod=test_invalid_ratings> mock_build = <MagicMock name='build' id='4886351728'> mock_credentials = <MagicMock name='Credentials' id='4886539712'> @patch('extract_data.Credentials') @patch('extract_data.build') def test_invalid_ratings(self, mock_build, mock_credentials): """Test handling of invalid rating values""" mock_data = self.mock_data.copy() mock_data[1][3] = 'invalid' # Invalid Context Awareness rating mock_service = MagicMock() mock_build.return_value = mock_service mock_service.spreadsheets().values().get().execute.return_value = { 'values': mock_data } result = get_google_sheet_data() self.assertIsNotNone(result) > self.assertTrue(pd.isna(result['Mean Rating'].iloc[0])) E AssertionError: False is not true test_extract_data.py:109: AssertionError _____________________________ TestGoogleSheetExtraction.test_result_status_calculation ______________________________ self = <test_extract_data.TestGoogleSheetExtraction testMethod=test_result_status_calculation> def test_result_status_calculation(self): """Test result status determination""" test_data = pd.DataFrame({ 'Difference': [-2, -0.5, 0, 0.5, 2] }) expected_results = ['Not ok', 'Ok', 'Ok', 'Ok', 'Not ok'] for diff, expected in zip(test_data['Difference'], expected_results): result = test_data['Difference'].apply( lambda x: 'Ok' if -1 <= x <= 1 else 'Not ok' ) > self.assertEqual(result.iloc[0], expected) E AssertionError: 'Not ok' != 'Ok' E - Not ok E + Ok test_extract_data.py:122: AssertionError ================================================= warnings summary ================================================== test_extract_data.py:3 /Users/surya.sandeep.boda/Desktop/Marscode Zero to One 3/test_extract_data.py:3: DeprecationWarning: Pyarrow will become a required dependency of pandas in the next major release of pandas (pandas 3.0), (to allow more performant data types, such as the Arrow string type, and better interoperability with other libraries) but was not found to be installed on your system. If this would cause problems for you, please provide us feedback at pandas-dev/pandas#54466 import pandas as pd -- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html ---------- coverage: platform darwin, python 3.9.6-final-0 ----------- Name Stmts Miss Cover Missing ----------------------------------------------- extract_data.py 65 12 82% 107, 136-149 ----------------------------------------------- TOTAL 65 12 82% ============================================== short test summary info ============================================== FAILED test_extract_data.py::TestGoogleSheetExtraction::test_invalid_ratings - AssertionError: False is not true FAILED test_extract_data.py::TestGoogleSheetExtraction::test_result_status_calculation - AssertionError: 'Not ok' != 'Ok' ====================================== 2 failed, 4 passed, 1 warning in 0.52s ======================================= User Observation: ================================================ test session starts ================================================ platform darwin -- Python 3.9.6, pytest-8.3.5, pluggy-1.5.0 rootdir: /Users/surya.sandeep.boda/Desktop/Marscode Zero to One 3 plugins: cov-6.0.0 collected 6 items test_extract_data.py ..F.F. [100%] ===================================================== FAILURES ====================================================== __________________________________ TestGoogleSheetExtraction.test_invalid_ratings ___________________________________ self = <test_extract_data.TestGoogleSheetExtraction testMethod=test_invalid_ratings> mock_build = <MagicMock name='build' id='4886351728'> mock_credentials = <MagicMock name='Credentials' id='4886539712'> @patch('extract_data.Credentials') @patch('extract_data.build') def test_invalid_ratings(self, mock_build, mock_credentials): """Test handling of invalid rating values""" mock_data = self.mock_data.copy() mock_data[1][3] = 'invalid' # Invalid Context Awareness rating mock_service = MagicMock() mock_build.return_value = mock_service mock_service.spreadsheets().values().get().execute.return_value = { 'values': mock_data } result = get_google_sheet_data() self.assertIsNotNone(result) > self.assertTrue(pd.isna(result['Mean Rating'].iloc[0])) E AssertionError: False is not true test_extract_data.py:109: AssertionError _____________________________ TestGoogleSheetExtraction.test_result_status_calculation ______________________________ self = <test_extract_data.TestGoogleSheetExtraction testMethod=test_result_status_calculation> def test_result_status_calculation(self): """Test result status determination""" test_data = pd.DataFrame({ 'Difference': [-2, -0.5, 0, 0.5, 2] }) expected_results = ['Not ok', 'Ok', 'Ok', 'Ok', 'Not ok'] for diff, expected in zip(test_data['Difference'], expected_results): result = test_data['Difference'].apply( lambda x: 'Ok' if -1 <= x <= 1 else 'Not ok' ) > self.assertEqual(result.iloc[0], expected) E AssertionError: 'Not ok' != 'Ok' E - Not ok E + Ok test_extract_data.py:122: AssertionError ================================================= warnings summary ================================================== test_extract_data.py:3 /Users/surya.sandeep.boda/Desktop/Marscode Zero to One 3/test_extract_data.py:3: DeprecationWarning: Pyarrow will become a required dependency of pandas in the next major release of pandas (pandas 3.0), (to allow more performant data types, such as the Arrow string type, and better interoperability with other libraries) but was not found to be installed on your system. If this would cause problems for you, please provide us feedback at pandas-dev/pandas#54466 import pandas as pd -- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html ---------- coverage: platform darwin, python 3.9.6-final-0 ----------- Name Stmts Miss Cover Missing ----------------------------------------------- extract_data.py 65 12 82% 107, 136-149 ----------------------------------------------- TOTAL 65 12 82% ============================================== short test summary info ============================================== FAILED test_extract_data.py::TestGoogleSheetExtraction::test_invalid_ratings - AssertionError: False is not true FAILED test_extract_data.py::TestGoogleSheetExtraction::test_result_status_calculation - AssertionError: 'Not ok' != 'Ok' ====================================== 2 failed, 4 passed, 1 warning in 0.52s ======================================= $ python -m pytest test_extract_data.py -v --cov=extract_data zsh: command not found: python $ python3 -m pytest test_extract_data.py -v --cov=extract_data ================================================ test session starts ================================================ platform darwin -- Python 3.9.6, pytest-8.3.5, pluggy-1.5.0 -- /Library/Developer/CommandLineTools/usr/bin/python3 cachedir: .pytest_cache rootdir: /Users/surya.sandeep.boda/Desktop/Marscode Zero to One 3 plugins: cov-6.0.0 collected 6 items test_extract_data.py::TestGoogleSheetExtraction::test_calculation_accuracy PASSED [ 16%] test_extract_data.py::TestGoogleSheetExtraction::test_empty_sheet PASSED [ 33%] test_extract_data.py::TestGoogleSheetExtraction::test_invalid_ratings FAILED [ 50%] test_extract_data.py::TestGoogleSheetExtraction::test_missing_columns PASSED [ 66%] test_extract_data.py::TestGoogleSheetExtraction::test_result_status_calculation PASSED [ 83%] test_extract_data.py::TestGoogleSheetExtraction::test_successful_data_extraction PASSED [100%] ===================================================== FAILURES ====================================================== __________________________________ TestGoogleSheetExtraction.test_invalid_ratings ___________________________________ self = <test_extract_data.TestGoogleSheetExtraction testMethod=test_invalid_ratings> mock_build = <MagicMock name='build' id='5357153440'> mock_credentials = <MagicMock name='Credentials' id='5357341424'> @patch('extract_data.Credentials') @patch('extract_data.build') def test_invalid_ratings(self, mock_build, mock_credentials): """Test handling of invalid rating values""" mock_data = [row[:] for row in self.mock_data] # Deep copy mock_data[1][3] = 'invalid' # Invalid Context Awareness rating mock_service = MagicMock() mock_build.return_value = mock_service mock_service.spreadsheets().values().get().execute.return_value = { 'values': mock_data } result = get_google_sheet_data() self.assertIsNotNone(result) # Check if the mean rating is NaN when one of the inputs is invalid > self.assertTrue(pd.isna(result['Mean Rating'].iloc[0])) E AssertionError: False is not true test_extract_data.py:110: AssertionError ================================================= warnings summary ================================================== test_extract_data.py:3 /Users/surya.sandeep.boda/Desktop/Marscode Zero to One 3/test_extract_data.py:3: DeprecationWarning: Pyarrow will become a required dependency of pandas in the next major release of pandas (pandas 3.0), (to allow more performant data types, such as the Arrow string type, and better interoperability with other libraries) but was not found to be installed on your system. If this would cause problems for you, please provide us feedback at pandas-dev/pandas#54466 import pandas as pd -- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html ---------- coverage: platform darwin, python 3.9.6-final-0 ----------- Name Stmts Miss Cover ------------------------------------- extract_data.py 65 12 82% ------------------------------------- TOTAL 65 12 82% ============================================== short test summary info ============================================== FAILED test_extract_data.py::TestGoogleSheetExtraction::test_invalid_ratings - AssertionError: False is not true ====================================== 1 failed, 5 passed, 1 warning in 0.52s ======================================= Response ID: 80e90428-de98-4c12-9135-d5f33317535b
1 parent e943e2e commit 75234f7

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

.coverage

0 Bytes
Binary file not shown.

test_extract_data.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def test_missing_columns(self, mock_build, mock_credentials):
9595
@patch('extract_data.build')
9696
def test_invalid_ratings(self, mock_build, mock_credentials):
9797
"""Test handling of invalid rating values"""
98-
mock_data = self.mock_data.copy()
98+
mock_data = [row[:] for row in self.mock_data] # Deep copy
9999
mock_data[1][3] = 'invalid' # Invalid Context Awareness rating
100100

101101
mock_service = MagicMock()
@@ -106,20 +106,18 @@ def test_invalid_ratings(self, mock_build, mock_credentials):
106106

107107
result = get_google_sheet_data()
108108
self.assertIsNotNone(result)
109+
# Check if the mean rating is NaN when one of the inputs is invalid
109110
self.assertTrue(pd.isna(result['Mean Rating'].iloc[0]))
110111

111112
def test_result_status_calculation(self):
112113
"""Test result status determination"""
113-
test_data = pd.DataFrame({
114-
'Difference': [-2, -0.5, 0, 0.5, 2]
115-
})
116-
114+
test_differences = [-2, -0.5, 0, 0.5, 2]
117115
expected_results = ['Not ok', 'Ok', 'Ok', 'Ok', 'Not ok']
118-
for diff, expected in zip(test_data['Difference'], expected_results):
119-
result = test_data['Difference'].apply(
120-
lambda x: 'Ok' if -1 <= x <= 1 else 'Not ok'
121-
)
122-
self.assertEqual(result.iloc[0], expected)
116+
117+
for diff, expected in zip(test_differences, expected_results):
118+
with self.subTest(difference=diff):
119+
result = 'Ok' if -1 <= diff <= 1 else 'Not ok'
120+
self.assertEqual(result, expected)
123121

124122
if __name__ == '__main__':
125123
unittest.main(verbosity=2)

0 commit comments

Comments
 (0)