def test_write_code_size_metrics(csv_mock): """Test that the correct metrics are written to the csv file.""" metrics = { "Java": { "files": 201, "blank": 20, "code": 1003, "comment": 230 }, "C": { "files": 100, "blank": 7, "code": 1220, "comment": 30 }, "SUM": { "files": 301, "blank": 7, "code": 2120, "comment": 130 }, } csv_mock.writer = Mock(writerow=Mock()) calls = [ call.writerow([7, 2120, 130]), ] write_code_size_metrics(csv_mock.writer, metrics) csv_mock.writer.writerow.assert_has_calls(calls)
def test_that_metrics_are_saved_to_file_size_metrics_csv_in_metric_directory( create_report_directory_mock, csv_mock): """Test that the metrics are saved.""" report_dir = os.path.join("bla", "metrics") expected_report_file = os.path.join(report_dir, "file_size_metrics.csv") create_report_directory_mock.return_value = report_dir file_size_metrics = { "File1": { "language": "Java", "blank": 20, "code": 1003, "comment": 230 }, "File2": { "language": "C++", "blank": 7, "code": 1220, "comment": 30 }, "File3": { "language": "Python", "blank": 7, "code": 2120, "comment": 130 }, } csv_mock.writer = Mock(writerow=Mock()) calls = [ call.writerow([ "Filename", "Language", "Blank Lines", "Lines of Code", "Comment Lines" ]), call.writerow(["File1", "Java", 20, 1003, 230]), call.writerow(["File2", "C++", 7, 1220, 30]), call.writerow(["File3", "Python", 7, 2120, 130]), ] with patch("src.cloc.cloc_analyze_file_size.open", mock_open()) as mocked_file: save_file_size_metrics(file_size_metrics, report_dir) mocked_file.assert_called_once_with(expected_report_file, "w", encoding="utf-8") csv_mock.writer().assert_has_calls(calls)
def test_write_header(csv_mock): """Test that the correct header is written to the csv file.""" csv_mock.writer = Mock(writerow=Mock()) calls = [call.writerow(["Blank Lines", "Lines Of Code", "Comment Lines"])] write_code_size_header(csv_mock.writer) csv_mock.writer.writerow.assert_has_calls(calls)
def test_save_issues(csv_mock): """Test that the resulting issues can be saved.""" items = {"A": 15, "b": 8} csv_mock.writer = Mock(writerow=Mock()) report_file = r"/temp/temp.csv" calls = [ call.writerow(["caption", "Number of violations"]), call.writerow(["A", 15]), call.writerow(["b", 8]) ] with patch("src.resharper.resharper_profile.open", mock_open()) as mocked_file: save_issues(items, report_file, "caption") mocked_file.assert_called_once_with(report_file, "w", encoding="utf-8") csv_mock.writer().assert_has_calls(calls)
def test_save_coverage_per_namespace(csv_mock): """Test that the resulting issues can be saved.""" items = {"A": 15, "b": 8} csv_mock.writer = Mock(writerow=Mock()) report_dir = r"/bla/" calls = [ call.writerow(["Namespace", "Coverage"]), call.writerow(["A", 15]), call.writerow(["b", 8]) ] with patch("src.dotcover.dotcover.open", mock_open()) as mocked_file: save_coverage_per_namespace(items, report_dir) mocked_file.assert_called_once_with(report_dir + r"coverage_per_namespace.csv", "w", encoding="utf-8") csv_mock.writer().assert_has_calls(calls)
def test_profile_saved_correctly(csv_mock): """Test that the profile is saved to console correctly.""" # arrange regions = [ MetricRegion("0-15", 0, 15), MetricRegion("16-30", 16, 30), MetricRegion("31-60", 31, 60), MetricRegion("60+", 61), ] profile = MetricProfile("Function size", regions) profile.update_loc(2) profile.update_loc(15) profile.update_loc(16) profile.update_loc(17) profile.update_loc(60) profile.update_loc(61) profile.update_loc(1001) profile.update_loc(1001) csv_mock.writer = Mock(writerow=Mock()) calls = [ call.writerow(["Function size", "Lines Of Code"]), call.writerow(["0-15", 17]), call.writerow(["16-30", 33]), call.writerow(["31-60", 60]), call.writerow(["60+", 2063]), ] report_file = "report.csv" # act with patch("src.profile.MetricProfile.open", mock_open()) as mocked_file: profile.save(report_file) # assert mocked_file.assert_called_once_with(report_file, "w", encoding="utf-8") csv_mock.writer().assert_has_calls(calls)
def test_that_profile_is_saved_correctly(csv_mock): """Test that the code duplication profile is save correctly.""" # arrange metrics = {"duplicated_loc": 100, "total_loc": 200} csv_mock.writer = Mock(writerow=Mock()) calls = [ call.writerow(["Duplicated Lines Of Code", "Total Lines Of Code"]), call.writerow([100, 200]), ] # act with patch("src.cpd.cpd_analysis.open", mock_open()) as mocked_file: save_duplication_profile("code_duplication.csv", metrics) mocked_file.assert_called_once_with("code_duplication.csv", "w", encoding="utf-8") # assert csv_mock.writer().writerow.assert_has_calls(calls) assert csv_mock.writer().writerow.call_count == 2
def test_metrics_are_written_to_file(csv_mock): """Test that the metrics are written correctly to file.""" # arrange production_code_metrics = { "Python": { "files": 201, "blank": 20, "code": 1003, "comment": 230 }, "C#": { "files": 100, "blank": 7, "code": 1220, "comment": 30 }, } csv_mock.writer = Mock(writerow=Mock()) calls = [ call.writerow([ "Language", "Number Of Files", "Blank Lines", "Lines Of Code", "Comment Lines" ]), call.writerow(["Python", 201, 20, 1003, 230]), call.writerow(["C#", 100, 7, 1220, 30]), ] # act with patch("src.cloc.cloc_languages.open", mock_open()) as mocked_file: save_language_profile("language_profile.csv", production_code_metrics) mocked_file.assert_called_once_with("language_profile.csv", "w", encoding="utf-8") # assert csv_mock.writer().writerow.assert_has_calls(calls) assert csv_mock.writer().writerow.call_count == 3
def test_save_metrics(csv_mock): """Test that the metrics are saved.""" production_code_size_file = "code_size.csv" production_code_metrics = { "Java": { "files": 201, "blank": 20, "code": 1003, "comment": 230 }, "C#": { "files": 100, "blank": 7, "code": 1220, "comment": 30 }, "SUM": { "files": 301, "blank": 7, "code": 2120, "comment": 130 }, } csv_mock.writer = Mock(writerow=Mock()) calls = [ call.writerow(["Blank Lines", "Lines Of Code", "Comment Lines"]), call.writerow([7, 2120, 130]), ] with patch("src.cloc.cloc_code_size.open", mock_open()) as mocked_file: save_code_metrics(production_code_size_file, production_code_metrics) mocked_file.assert_called_once_with(production_code_size_file, "w", encoding="utf-8") csv_mock.writer().assert_has_calls(calls)