|
37 | 37 | "gpu_energy": 0.0, |
38 | 38 | "ram_energy": 2.0, |
39 | 39 | "energy_consumed": 57.21874, |
| 40 | + "wue": 0, |
40 | 41 | } |
41 | 42 |
|
42 | 43 | EMISSION_1 = { |
|
53 | 54 | "gpu_energy": 0.0, |
54 | 55 | "ram_energy": 2.0, |
55 | 56 | "energy_consumed": 57.21874, |
| 57 | + "wue": 0, |
56 | 58 | } |
57 | 59 |
|
58 | 60 | EMISSION_2 = { |
|
69 | 71 | "gpu_energy": 0.0, |
70 | 72 | "ram_energy": 2.0, |
71 | 73 | "energy_consumed": 57.21874, |
| 74 | + "wue": 0, |
72 | 75 | } |
73 | 76 |
|
74 | 77 |
|
|
86 | 89 | "gpu_energy": 0.0, |
87 | 90 | "ram_energy": 2.0, |
88 | 91 | "energy_consumed": 57.21874, |
| 92 | + "wue": 0, |
89 | 93 | } |
90 | 94 |
|
91 | 95 |
|
@@ -196,3 +200,113 @@ def test_get_emissions_from_run_retreives_all_emissions_from_run( |
196 | 200 | assert not diff |
197 | 201 | assert len(actual_emission_ids_list) == len(set(actual_emission_ids_list)) |
198 | 202 | assert EMISSION_3["id"] not in actual_emission_ids_list |
| 203 | + |
| 204 | + |
| 205 | +def test_add_emission_with_default_wue_value(client, custom_test_server): |
| 206 | + """Test that WUE defaults to 0 when not provided""" |
| 207 | + # Prepare the test - create emission without WUE field |
| 208 | + emission_without_wue = { |
| 209 | + "timestamp": "2021-04-04T08:43:00+02:00", |
| 210 | + "run_id": "40088f1a-d28e-4980-8d80-bf5600056a14", |
| 211 | + "duration": 98745, |
| 212 | + "emissions_sum": 206.548444, |
| 213 | + "emissions_rate": 89.548444, |
| 214 | + "cpu_power": 0.3, |
| 215 | + "gpu_power": 0.0, |
| 216 | + "ram_power": 0.15, |
| 217 | + "cpu_energy": 55.21874, |
| 218 | + "gpu_energy": 0.0, |
| 219 | + "ram_energy": 2.0, |
| 220 | + "energy_consumed": 57.21874, |
| 221 | + # Note: wue is not provided, should default to 0 |
| 222 | + } |
| 223 | + |
| 224 | + repository_mock = mock.Mock(spec=EmissionRepository) |
| 225 | + repository_mock.add_emission.return_value = UUID(EMISSION_ID) |
| 226 | + |
| 227 | + # Setup the project token repository |
| 228 | + project_tokens_repository_mock = mock.Mock(spec=ProjectTokenRepository) |
| 229 | + PROJECT_ID = UUID("f52fe339-164d-4c2b-a8c0-f562dfce066d") |
| 230 | + PROJECT_TOKEN_ID = UUID("e60afb92-17b7-4720-91a0-1ae91e409ba7") |
| 231 | + PROJECT_TOKEN = ProjectToken( |
| 232 | + id=PROJECT_TOKEN_ID, |
| 233 | + project_id=PROJECT_ID, |
| 234 | + name="Project", |
| 235 | + token="token", |
| 236 | + access=AccessLevel.WRITE.value, |
| 237 | + ) |
| 238 | + project_tokens_repository_mock.get_project_token_by_run_id_and_token.return_value = ( |
| 239 | + PROJECT_TOKEN |
| 240 | + ) |
| 241 | + |
| 242 | + # Call the endpoint |
| 243 | + with custom_test_server.container.emission_repository.override( |
| 244 | + repository_mock |
| 245 | + ) and custom_test_server.container.project_token_repository.override( |
| 246 | + project_tokens_repository_mock |
| 247 | + ): |
| 248 | + response = client.post( |
| 249 | + "/emissions", json=emission_without_wue, headers={"x-api-token": "token"} |
| 250 | + ) |
| 251 | + |
| 252 | + # Asserts |
| 253 | + assert response.status_code == status.HTTP_201_CREATED |
| 254 | + |
| 255 | + # Verify that the repository was called with WUE defaulting to 0 |
| 256 | + called_emission = repository_mock.add_emission.call_args[0][0] |
| 257 | + assert called_emission.wue == 0, "WUE should default to 0 when not provided" |
| 258 | + |
| 259 | + |
| 260 | +def test_add_emission_with_custom_wue_value(client, custom_test_server): |
| 261 | + """Test that custom WUE value is properly saved""" |
| 262 | + # Prepare the test - create emission with custom WUE value |
| 263 | + emission_with_wue = { |
| 264 | + "timestamp": "2021-04-04T08:43:00+02:00", |
| 265 | + "run_id": "40088f1a-d28e-4980-8d80-bf5600056a14", |
| 266 | + "duration": 98745, |
| 267 | + "emissions_sum": 206.548444, |
| 268 | + "emissions_rate": 89.548444, |
| 269 | + "cpu_power": 0.3, |
| 270 | + "gpu_power": 0.0, |
| 271 | + "ram_power": 0.15, |
| 272 | + "cpu_energy": 55.21874, |
| 273 | + "gpu_energy": 0.0, |
| 274 | + "ram_energy": 2.0, |
| 275 | + "energy_consumed": 57.21874, |
| 276 | + "wue": 1.5, |
| 277 | + } |
| 278 | + |
| 279 | + repository_mock = mock.Mock(spec=EmissionRepository) |
| 280 | + repository_mock.add_emission.return_value = UUID(EMISSION_ID) |
| 281 | + |
| 282 | + # Setup the project token repository |
| 283 | + project_tokens_repository_mock = mock.Mock(spec=ProjectTokenRepository) |
| 284 | + PROJECT_ID = UUID("f52fe339-164d-4c2b-a8c0-f562dfce066d") |
| 285 | + PROJECT_TOKEN_ID = UUID("e60afb92-17b7-4720-91a0-1ae91e409ba7") |
| 286 | + PROJECT_TOKEN = ProjectToken( |
| 287 | + id=PROJECT_TOKEN_ID, |
| 288 | + project_id=PROJECT_ID, |
| 289 | + name="Project", |
| 290 | + token="token", |
| 291 | + access=AccessLevel.WRITE.value, |
| 292 | + ) |
| 293 | + project_tokens_repository_mock.get_project_token_by_run_id_and_token.return_value = ( |
| 294 | + PROJECT_TOKEN |
| 295 | + ) |
| 296 | + |
| 297 | + # Call the endpoint |
| 298 | + with custom_test_server.container.emission_repository.override( |
| 299 | + repository_mock |
| 300 | + ) and custom_test_server.container.project_token_repository.override( |
| 301 | + project_tokens_repository_mock |
| 302 | + ): |
| 303 | + response = client.post( |
| 304 | + "/emissions", json=emission_with_wue, headers={"x-api-token": "token"} |
| 305 | + ) |
| 306 | + |
| 307 | + # Asserts |
| 308 | + assert response.status_code == status.HTTP_201_CREATED |
| 309 | + |
| 310 | + # Verify that the repository was called with the correct WUE value |
| 311 | + called_emission = repository_mock.add_emission.call_args[0][0] |
| 312 | + assert called_emission.wue == 1.5, "WUE should be set to the provided value" |
0 commit comments