|
| 1 | +import dataclasses |
| 2 | +import importlib.util |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from carbonserver.api import schemas as server_schemas |
| 8 | + |
| 9 | + |
| 10 | +def load_client_schemas(): |
| 11 | + repo_root = Path(__file__).resolve().parents[3] |
| 12 | + client_schema_path = repo_root / "codecarbon" / "core" / "schemas.py" |
| 13 | + spec = importlib.util.spec_from_file_location( |
| 14 | + "codecarbon_client_schemas", client_schema_path |
| 15 | + ) |
| 16 | + module = importlib.util.module_from_spec(spec) |
| 17 | + spec.loader.exec_module(module) |
| 18 | + return module |
| 19 | + |
| 20 | + |
| 21 | +client_schemas = load_client_schemas() |
| 22 | + |
| 23 | + |
| 24 | +CREATE_SCHEMA_PAIRS = [ |
| 25 | + (client_schemas.EmissionCreate, server_schemas.EmissionCreate), |
| 26 | + (client_schemas.RunCreate, server_schemas.RunCreate), |
| 27 | + (client_schemas.ExperimentCreate, server_schemas.ExperimentCreate), |
| 28 | + (client_schemas.ProjectCreate, server_schemas.ProjectCreate), |
| 29 | + (client_schemas.OrganizationCreate, server_schemas.OrganizationCreate), |
| 30 | +] |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.parametrize(("client_schema", "server_schema"), CREATE_SCHEMA_PAIRS) |
| 34 | +def test_client_create_schemas_match_server_fields(client_schema, server_schema): |
| 35 | + client_fields = {field.name for field in dataclasses.fields(client_schema)} |
| 36 | + server_fields = set(server_schema.model_fields) |
| 37 | + |
| 38 | + assert client_fields == server_fields |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize( |
| 42 | + ("client_payload", "server_schema"), |
| 43 | + [ |
| 44 | + ( |
| 45 | + client_schemas.EmissionCreate( |
| 46 | + timestamp="2021-04-04T08:43:00+02:00", |
| 47 | + run_id="40088f1a-d28e-4980-8d80-bf5600056a14", |
| 48 | + duration=98745, |
| 49 | + emissions_sum=1544.54, |
| 50 | + emissions_rate=1.548444, |
| 51 | + cpu_power=0.3, |
| 52 | + gpu_power=0.0, |
| 53 | + ram_power=0.15, |
| 54 | + cpu_energy=55.21874, |
| 55 | + gpu_energy=0.0, |
| 56 | + ram_energy=2.0, |
| 57 | + energy_consumed=57.21874, |
| 58 | + cpu_utilization_percent=12.5, |
| 59 | + gpu_utilization_percent=34.5, |
| 60 | + ram_utilization_percent=56.5, |
| 61 | + wue=0.8, |
| 62 | + ), |
| 63 | + server_schemas.EmissionCreate, |
| 64 | + ), |
| 65 | + ( |
| 66 | + client_schemas.RunCreate( |
| 67 | + timestamp="2021-04-04T08:43:00+02:00", |
| 68 | + experiment_id="8edb03e1-9a28-452a-9c93-a3b6560136d7", |
| 69 | + os="macOS-10.15.7-x86_64-i386-64bit", |
| 70 | + python_version="3.8.0", |
| 71 | + codecarbon_version="2.1.3", |
| 72 | + cpu_count=12, |
| 73 | + cpu_model="Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz", |
| 74 | + gpu_count=4, |
| 75 | + gpu_model="NVIDIA", |
| 76 | + longitude=-7.6174, |
| 77 | + latitude=33.5822, |
| 78 | + region="EUROPE", |
| 79 | + provider="AWS", |
| 80 | + ram_total_size=83948.22, |
| 81 | + tracking_mode="Machine", |
| 82 | + ), |
| 83 | + server_schemas.RunCreate, |
| 84 | + ), |
| 85 | + ( |
| 86 | + client_schemas.ExperimentCreate( |
| 87 | + timestamp="2021-04-04T08:43:00+02:00", |
| 88 | + name="Run on AWS", |
| 89 | + description="AWS API for Code Carbon", |
| 90 | + country_name="France", |
| 91 | + country_iso_code="FRA", |
| 92 | + region="france", |
| 93 | + on_cloud=True, |
| 94 | + cloud_provider="aws", |
| 95 | + cloud_region="eu-west-1a", |
| 96 | + project_id="8edb03e1-9a28-452a-9c93-a3b6560136d7", |
| 97 | + ), |
| 98 | + server_schemas.ExperimentCreate, |
| 99 | + ), |
| 100 | + ( |
| 101 | + client_schemas.ProjectCreate( |
| 102 | + name="API Code Carbon", |
| 103 | + description="API for Code Carbon", |
| 104 | + organization_id="8edb03e1-9a28-452a-9c93-a3b6560136d7", |
| 105 | + ), |
| 106 | + server_schemas.ProjectCreate, |
| 107 | + ), |
| 108 | + ( |
| 109 | + client_schemas.OrganizationCreate( |
| 110 | + name="Code Carbon", |
| 111 | + description="Save the world, one run at a time.", |
| 112 | + ), |
| 113 | + server_schemas.OrganizationCreate, |
| 114 | + ), |
| 115 | + ], |
| 116 | +) |
| 117 | +def test_client_create_payloads_validate_against_server_schemas( |
| 118 | + client_payload, server_schema |
| 119 | +): |
| 120 | + server_schema.model_validate(dataclasses.asdict(client_payload)) |
0 commit comments