|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import tempfile |
| 4 | +import unittest |
| 5 | +from unittest.mock import MagicMock, patch |
| 6 | + |
| 7 | +import pandas as pd |
| 8 | + |
| 9 | +from codecarbon.output_methods.emissions_data import EmissionsData, TaskEmissionsData |
| 10 | +from codecarbon.output_methods.file import FileOutput |
| 11 | + |
| 12 | + |
| 13 | +class TestFileOutput(unittest.TestCase): |
| 14 | + def setUp(self): |
| 15 | + self.temp_dir = tempfile.mkdtemp() |
| 16 | + self.emissions_data = EmissionsData( |
| 17 | + timestamp="2023-01-01T00:00:00", |
| 18 | + project_name="test_project", |
| 19 | + run_id="test_run_id", |
| 20 | + experiment_id="test_experiment_id", |
| 21 | + duration=10, |
| 22 | + emissions=0.5, |
| 23 | + emissions_rate=0.05, |
| 24 | + cpu_power=20, |
| 25 | + gpu_power=30, |
| 26 | + ram_power=5, |
| 27 | + cpu_energy=200, |
| 28 | + gpu_energy=300, |
| 29 | + ram_energy=50, |
| 30 | + energy_consumed=550, |
| 31 | + water_consumed=0.1, |
| 32 | + country_name="Testland", |
| 33 | + country_iso_code="TS", |
| 34 | + region="Test Region", |
| 35 | + cloud_provider="Test Cloud", |
| 36 | + cloud_region="test-cloud-1", |
| 37 | + os="TestOS", |
| 38 | + python_version="3.8", |
| 39 | + codecarbon_version="2.0", |
| 40 | + cpu_count=4, |
| 41 | + cpu_model="Test CPU", |
| 42 | + gpu_count=1, |
| 43 | + gpu_model="Test GPU", |
| 44 | + longitude=0, |
| 45 | + latitude=0, |
| 46 | + ram_total_size=16, |
| 47 | + tracking_mode="machine", |
| 48 | + on_cloud="true", |
| 49 | + pue=1.5, |
| 50 | + wue=0.5, |
| 51 | + ) |
| 52 | + |
| 53 | + def tearDown(self): |
| 54 | + shutil.rmtree(self.temp_dir) |
| 55 | + |
| 56 | + def test_file_output_initialization(self): |
| 57 | + FileOutput("test.csv", self.temp_dir) |
| 58 | + |
| 59 | + def test_file_output_initialization_invalid_csv_write_mode(self): |
| 60 | + with self.assertRaises(ValueError): |
| 61 | + FileOutput("test.csv", self.temp_dir, on_csv_write="invalid_option") |
| 62 | + |
| 63 | + def test_file_output_initialization_invalid_dir(self): |
| 64 | + with self.assertRaises(OSError): |
| 65 | + FileOutput("test.csv", "/non/existent/dir") |
| 66 | + |
| 67 | + def test_has_valid_headers_success(self): |
| 68 | + file_output = FileOutput("test.csv", self.temp_dir) |
| 69 | + file_output.out(self.emissions_data, MagicMock()) |
| 70 | + |
| 71 | + self.assertTrue(file_output.has_valid_headers(self.emissions_data)) |
| 72 | + |
| 73 | + def test_has_valid_headers_failure(self): |
| 74 | + file_output = FileOutput("test.csv", self.temp_dir) |
| 75 | + file_output.out(self.emissions_data, MagicMock()) |
| 76 | + |
| 77 | + df = pd.read_csv(os.path.join(self.temp_dir, "test.csv")) |
| 78 | + df.rename(columns={"wue": "new_header"}, inplace=True) |
| 79 | + df.to_csv(os.path.join(self.temp_dir, "test.csv"), index=False) |
| 80 | + |
| 81 | + self.assertFalse(file_output.has_valid_headers(self.emissions_data)) |
| 82 | + |
| 83 | + @patch("codecarbon.output_methods.file.FileOutput.has_valid_headers") |
| 84 | + def test_file_output_out_file_exists_invalid_headers(self, mock_has_valid_headers): |
| 85 | + file_output = FileOutput("test.csv", self.temp_dir, on_csv_write="append") |
| 86 | + file_output.out(self.emissions_data, MagicMock()) |
| 87 | + |
| 88 | + mock_has_valid_headers.return_value = False |
| 89 | + file_output.out(self.emissions_data, MagicMock()) |
| 90 | + |
| 91 | + df = pd.read_csv(os.path.join(self.temp_dir, "test.csv.bak")) |
| 92 | + self.assertEqual(len(df), 1) |
| 93 | + df = pd.read_csv(os.path.join(self.temp_dir, "test.csv")) |
| 94 | + self.assertEqual(len(df), 1) |
| 95 | + |
| 96 | + def test_file_output_out_update_no_file_exists(self): |
| 97 | + file_output = FileOutput("test.csv", self.temp_dir, on_csv_write="update") |
| 98 | + file_output.out(self.emissions_data, MagicMock()) |
| 99 | + |
| 100 | + df = pd.read_csv(os.path.join(self.temp_dir, "test.csv")) |
| 101 | + self.assertEqual(len(df), 1) |
| 102 | + |
| 103 | + def test_file_output_out_append_no_file_exists(self): |
| 104 | + file_output = FileOutput("test.csv", self.temp_dir, on_csv_write="append") |
| 105 | + file_output.out(self.emissions_data, MagicMock()) |
| 106 | + |
| 107 | + df = pd.read_csv(os.path.join(self.temp_dir, "test.csv")) |
| 108 | + self.assertEqual(len(df), 1) |
| 109 | + |
| 110 | + def test_file_output_out_append_file_exists(self): |
| 111 | + file_output = FileOutput("test.csv", self.temp_dir, on_csv_write="append") |
| 112 | + file_output.out(self.emissions_data, MagicMock()) |
| 113 | + file_output.out(self.emissions_data, MagicMock()) |
| 114 | + |
| 115 | + df = pd.read_csv(os.path.join(self.temp_dir, "test.csv")) |
| 116 | + self.assertEqual(len(df), 2) |
| 117 | + |
| 118 | + def test_file_output_out_update_file_exists_no_matching_row(self): |
| 119 | + file_output = FileOutput("test.csv", self.temp_dir, on_csv_write="update") |
| 120 | + file_output.out(self.emissions_data, MagicMock()) |
| 121 | + |
| 122 | + updated_emissions_data = self.emissions_data |
| 123 | + updated_emissions_data.run_id = "new_test_run_id" |
| 124 | + file_output.out(updated_emissions_data, MagicMock()) |
| 125 | + |
| 126 | + df = pd.read_csv(os.path.join(self.temp_dir, "test.csv")) |
| 127 | + self.assertEqual(len(df), 2) |
| 128 | + |
| 129 | + def test_file_output_out_update_file_exists_multiple_matching_rows(self): |
| 130 | + file_output = FileOutput("test.csv", self.temp_dir, on_csv_write="update") |
| 131 | + file_output.out(self.emissions_data, MagicMock()) |
| 132 | + |
| 133 | + # Manually add a duplicate row to simulate the condition |
| 134 | + df = pd.read_csv(os.path.join(self.temp_dir, "test.csv")) |
| 135 | + df = pd.concat([df, df]) |
| 136 | + df.to_csv(os.path.join(self.temp_dir, "test.csv"), index=False) |
| 137 | + |
| 138 | + file_output.out(self.emissions_data, MagicMock()) |
| 139 | + |
| 140 | + df = pd.read_csv(os.path.join(self.temp_dir, "test.csv")) |
| 141 | + self.assertEqual(len(df), 3) |
| 142 | + |
| 143 | + def test_file_output_out_update_file_exists_one_matchingrows(self): |
| 144 | + file_output = FileOutput("test.csv", self.temp_dir, on_csv_write="update") |
| 145 | + file_output.out(self.emissions_data, MagicMock()) |
| 146 | + df = pd.read_csv(os.path.join(self.temp_dir, "test.csv")) |
| 147 | + self.assertEqual(df["cpu_power"].iloc[0], 20) |
| 148 | + |
| 149 | + new_data = self.emissions_data |
| 150 | + new_data.cpu_power = 2 |
| 151 | + file_output.out(new_data, MagicMock()) |
| 152 | + df = pd.read_csv(os.path.join(self.temp_dir, "test.csv")) |
| 153 | + self.assertEqual(df["cpu_power"].iloc[0], 2) |
| 154 | + |
| 155 | + def test_file_output_task_out(self): |
| 156 | + task_emissions_data = [ |
| 157 | + TaskEmissionsData( |
| 158 | + task_name="test_task", |
| 159 | + timestamp="2023-01-01T00:00:00", |
| 160 | + project_name="test_project", |
| 161 | + run_id="test_run_id", |
| 162 | + duration=10, |
| 163 | + emissions=0.5, |
| 164 | + emissions_rate=0.05, |
| 165 | + cpu_power=20, |
| 166 | + gpu_power=30, |
| 167 | + ram_power=5, |
| 168 | + cpu_energy=200, |
| 169 | + gpu_energy=300, |
| 170 | + ram_energy=50, |
| 171 | + energy_consumed=550, |
| 172 | + water_consumed=0.1, |
| 173 | + country_name="Testland", |
| 174 | + country_iso_code="TS", |
| 175 | + region="Test Region", |
| 176 | + cloud_provider="Test Cloud", |
| 177 | + cloud_region="test-cloud-1", |
| 178 | + os="TestOS", |
| 179 | + python_version="3.8", |
| 180 | + codecarbon_version="2.0", |
| 181 | + cpu_count=4, |
| 182 | + cpu_model="Test CPU", |
| 183 | + gpu_count=1, |
| 184 | + gpu_model="Test GPU", |
| 185 | + longitude=0, |
| 186 | + latitude=0, |
| 187 | + ram_total_size=16, |
| 188 | + tracking_mode="machine", |
| 189 | + on_cloud="true", |
| 190 | + ) |
| 191 | + ] |
| 192 | + file_output = FileOutput("test.csv", self.temp_dir) |
| 193 | + file_output.task_out(task_emissions_data, "test_experiment") |
| 194 | + |
| 195 | + expected_file = os.path.join( |
| 196 | + self.temp_dir, "emissions_test_experiment_test_run_id.csv" |
| 197 | + ) |
| 198 | + self.assertTrue(os.path.exists(expected_file)) |
| 199 | + df = pd.read_csv(expected_file) |
| 200 | + self.assertEqual(len(df), 1) |
0 commit comments