Skip to content

Commit

Permalink
adjust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
codingfabi committed May 5, 2024
1 parent 4251134 commit 4d3e879
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 232 deletions.
232 changes: 0 additions & 232 deletions tests/unit/test_calculate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,238 +9,6 @@

import co2calculator.calculate as candidate
from co2calculator.constants import RangeCategory
from pydantic import ValidationError

from co2calculator.exceptions import ConversionFactorNotFound, EmissionFactorNotFound


@pytest.mark.parametrize(
"distance,passengers,size,fuel_type,expected_emissions",
[
pytest.param(100, None, None, None, 21.5, id="defaults"),
pytest.param(444, 3, "medium", "gasoline", 34.19, id="all optional arguments"),
pytest.param(10, 1, "small", None, 1.79, id="size: 'small'"),
pytest.param(10, 1, "medium", None, 2.09, id="size: 'medium'"),
pytest.param(10, 1, "large", None, 2.74, id="size: 'large'"),
pytest.param(10, 1, "average", None, 2.15, id="size: 'average'"),
pytest.param(10, 1, None, "diesel", 2.01, id="fuel_type: 'diesel'"),
pytest.param(10, 1, None, "gasoline", 2.24, id="fuel_type: 'gasoline'"),
pytest.param(10, 1, None, "cng", 2.37, id="fuel_type: 'cng'"),
pytest.param(10, 1, None, "electric", 0.51, id="fuel_type: 'electric'"),
pytest.param(10, 1, None, "hybrid", 1.2, id="fuel_type: 'hybrid'"),
pytest.param(
10, 1, None, "plug-in_hybrid", 0.93, id="fuel_type: 'plug-in_hybrid'"
),
pytest.param(10, 1, None, "average", 2.15, id="fuel_type: 'average'"),
],
)
def test_calc_co2_car(
distance: float,
passengers: Optional[int],
size: Optional[str],
fuel_type: Optional[str],
expected_emissions: float,
):
"""Test: Calculate car-trip emissions based on given distance.
Expect: Returns emissions and distance.
"""
actual_emissions = candidate.calc_co2_car(
distance=distance,
passengers=passengers,
size=size,
fuel_type=fuel_type,
)

assert round(actual_emissions, 2) == expected_emissions


@pytest.mark.parametrize(
"distance,size,expected_emissions",
[
pytest.param(100, None, 11.36, id="defaults"),
pytest.param(100, "small", 8.31, id="size: 'small'"),
pytest.param(100, "medium", 10.09, id="size: 'medium'"),
pytest.param(100, "large", 13.24, id="size: 'large'"),
pytest.param(100, "average", 11.36, id="size: 'average'"),
],
)
def test_calc_co2_motorbike(
distance: float, size: Optional[str], expected_emissions: float
):
"""Test: Calculate motorbike-trip emissions based on given distance.
Expect: Returns emissions and distance.
"""
actual_emissions = candidate.calc_co2_motorbike(distance=distance, size=size)

assert round(actual_emissions, 2) == expected_emissions


@pytest.mark.parametrize(
"distance,size,fuel_type,occupancy,vehicle_range,expected_emissions",
[
pytest.param(549, None, None, None, None, 21.63, id="defaults"),
pytest.param(
549, "large", "diesel", 80, "long-distance", 12.3, id="optional arguments"
),
pytest.param(10, "medium", None, None, None, 0.42, id="size: 'medium'"),
pytest.param(10, "large", None, None, None, 0.33, id="size: 'large'"),
pytest.param(10, "average", None, None, None, 0.39, id="size: 'average'"),
pytest.param(10, None, None, 20, None, 0.92, id="occupancy: 20"),
pytest.param(10, None, None, 50, None, 0.39, id="occupancy: 50"),
pytest.param(10, None, None, 80, None, 0.26, id="occupancy: 80"),
pytest.param(10, None, None, 100, None, 0.22, id="occupancy: 100"),
pytest.param(10, None, None, None, "local", 0.39, id="vehicle_range: 'local'"),
pytest.param(
10,
None,
None,
None,
"long-distance",
0.39,
id="vehicle_range: 'long-distance'",
),
# pytest.param(
# 10,
# "small",
# "diesel",
# None,
# "long-distance",
# 0.39,
# id="size: 'small', fuel_type: `diesel`, vehicle_range: 'long-distance'",
# ),
# pytest.param(
# 10,
# "medium",
# "cng",
# None,
# "long-distance",
# 0.62,
# id="fuel_type: `cng` and size",
# ),
# pytest.param(
# 10,
# "small",
# "hydrogen",
# None,
# "local",
# 0.25,
# id="fuel_type: `hydrogen` and size",
# ),
],
)
def test_calc_co2_bus(
distance: float,
size: Optional[str],
fuel_type: Optional[str],
occupancy: Optional[int],
vehicle_range: Optional[str],
expected_emissions: float,
):
"""Test: Calculate bus-trip emissions based on given distance.
Expect: Returns emissions and distance.
"""

# Calculate co2e
actual_emissions = candidate.calc_co2_bus(
distance=distance,
size=size,
fuel_type=fuel_type,
occupancy=occupancy,
vehicle_range=vehicle_range,
)

assert round(actual_emissions, 2) == expected_emissions


@pytest.mark.parametrize(
"distance,fuel_type,vehicle_range,expected_emissions",
[
pytest.param(1162, None, None, 38.23, id="defaults"),
pytest.param(
1162, "electric", "long-distance", 37.18, id="all optional arguments"
),
pytest.param(10, "electric", None, 0.32, id="fuel_type: 'electric'"),
pytest.param(10, "diesel", None, 0.7, id="fuel_type: 'diesel'"),
pytest.param(10, "average", None, 0.33, id="fuel_type: 'average'"),
# pytest.param(10, None, "local", 0.6, id="vehicle_range: 'local'"),
pytest.param(
10, None, "long-distance", 0.33, id="vehicle_range: 'long-distance'"
),
],
)
def test_calc_co2_train(
distance: float,
fuel_type: Optional[str],
vehicle_range: Optional[str],
expected_emissions: float,
):
"""Test: Calculate train-trip emissions based on given distance.
Expect: Returns emissions and distance.
"""

actual_emissions = candidate.calc_co2_train(
distance=distance, fuel_type=fuel_type, vehicle_range=vehicle_range
)

assert round(actual_emissions, 2) == expected_emissions


@pytest.mark.parametrize(
"distance,seating_class,expected_emissions",
[
pytest.param(1000, None, 153.53, id="defaults, short-haul"),
pytest.param(2000, None, 307.06, id="defaults, long-haul"),
pytest.param(1000, "economy_class", 151.52, id="seating_class"),
],
)
def test_calc_co2_plane(
distance: float,
seating_class: Optional[str],
expected_emissions: float,
):
"""Test: Calculate plane-trip emissions based on given distance.
Expect: Returns emissions and distance.
"""

actual_emissions = candidate.calc_co2_plane(
distance=distance, seating=seating_class
)

assert round(actual_emissions, 2) == expected_emissions


def test_calc_co2_plane__failed() -> None:
"""Test: Calculation on plane-trip emissions fails due to false input.
Expect: Raises ValidationError.
"""
with pytest.raises(ValidationError):
candidate.calc_co2_plane(distance=5000, seating="NON-EXISTENT")


def test_calc_co2_plane__invalid_distance_seating_combo() -> None:
"""Test: Calculation on plane-trip emissions fails due to false input.
Expect: Raises ValueError.
"""
# Check if raises warning (premium economy class is not available for short-haul flights)
with pytest.raises(EmissionFactorNotFound):
candidate.calc_co2_plane(distance=800, seating="premium_economy_class")


@pytest.mark.parametrize(
"seating_class,expected_emissions",
[
pytest.param(None, 11.29, id="defaults"),
pytest.param("average", 11.29, id="seating_class: 'average'"),
pytest.param("foot_passenger", 1.87, id="seating_class: 'Foot passenger'"),
pytest.param("car_passenger", 12.95, id="seating_class: 'Car passenger"),
],
)
def test_calc_ferry(seating_class: Optional[str], expected_emissions: float) -> None:
"""Test: Calculate ferry-trip emissions based on given distance.
Expect: Returns emissions and distance.
"""
actual_emissions = candidate.calc_co2_ferry(distance=100, seating=seating_class)
assert round(actual_emissions, 2) == expected_emissions


# @pytest.mark.skip(
Expand Down
Loading

0 comments on commit 4d3e879

Please sign in to comment.