-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Core tests #119
Core tests #119
Changes from all commits
03b1e04
2393b42
9172fcc
0b82bb0
56f935e
e0a7b9f
cb60cdc
ab6b77a
626a233
56e5fa4
2366644
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
.ruff_cache | ||
.mypy_cache | ||
.pytest_cache | ||
.coverage | ||
__pycache__ | ||
dockerfile | ||
|
||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import pytest | ||
|
||
from vessim.core.consumer import MockPowerMeter, ComputingSystem | ||
|
||
class TestMockPowerMeter: | ||
|
||
@pytest.fixture | ||
def power_meter(self) -> MockPowerMeter: | ||
return MockPowerMeter(p=20, power_config={ | ||
"high performance": 1, | ||
"normal": .8, | ||
"power-saving": .4 | ||
}) | ||
|
||
def test_initialize_fails_with_invalid_p_value(self): | ||
with pytest.raises(ValueError): | ||
MockPowerMeter(p=-1.0) | ||
|
||
@pytest.mark.parametrize("power_config", [ | ||
{ | ||
"high performance": 1, | ||
"normal": .7 | ||
}, | ||
{ | ||
"invalid_mode": 1, | ||
"normal": .7, | ||
"power-saving": 0.5 | ||
} | ||
]) | ||
def test_initialize_fails_with_invalid_power_config(self, power_config): | ||
with pytest.raises(ValueError): | ||
MockPowerMeter(p=10, power_config=power_config) | ||
|
||
def test_set_power_mode_fails_with_invalid_power_mode(self, power_meter): | ||
with pytest.raises(ValueError): | ||
power_meter.set_power_mode("invalid_mode") | ||
|
||
def test_measure(self, power_meter): | ||
assert power_meter.measure() == 20.0 | ||
power_meter.set_power_mode("normal") | ||
assert power_meter.measure() == 16.0 | ||
power_meter.set_power_mode("power-saving") | ||
assert power_meter.measure() == 8.0 | ||
|
||
|
||
class TestComputingSystem: | ||
|
||
@pytest.fixture | ||
def computing_system(self) -> ComputingSystem: | ||
return ComputingSystem( | ||
power_meters=[MockPowerMeter(p=5), MockPowerMeter(p=7)], pue=1.5 | ||
) | ||
|
||
def test_consumption(self, computing_system): | ||
assert computing_system.consumption() == 18.0 | ||
|
||
def test_finalize(self, computing_system): | ||
try: | ||
computing_system.finalize() | ||
except Exception as err: | ||
pytest.fail(f"Unexpected Error: {err}") | ||
Comment on lines
+58
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is being tested here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is just a test to check if the finalize function throws an error. I just wrote this test to achieve full code coverage, but I agree that it is not very useful. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import pytest | ||
from typing import Dict | ||
|
||
from vessim.core.microgrid import SimpleMicrogrid | ||
from vessim.core.storage import SimpleBattery | ||
|
||
class TestSimpleMicrogrid: | ||
|
||
@pytest.fixture | ||
def microgrid(self) -> SimpleMicrogrid: | ||
return SimpleMicrogrid() | ||
|
||
@pytest.fixture | ||
def microgrid_battery(self) -> SimpleMicrogrid: | ||
return SimpleMicrogrid( | ||
storage=SimpleBattery(capacity=100, charge_level=80, min_soc=0.1) | ||
) | ||
|
||
@pytest.fixture | ||
def power_values(self) -> Dict: | ||
return { | ||
"Consumer_0": -15, | ||
"Consumer_1": -10, | ||
"Generator_0": 20, | ||
"Generator_1": 0, | ||
} | ||
|
||
def test_power_flow(self, microgrid, power_values): | ||
assert microgrid.power_flow(power_values, 10) == -5 | ||
|
||
def test_power_flow_with_storage(self, microgrid_battery, power_values): | ||
p_delta = microgrid_battery.power_flow(power_values, 10) | ||
assert p_delta == 0 | ||
assert microgrid_battery.storage.charge_level == 30 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import pandas as pd | ||
import pytest | ||
|
||
from vessim.core.simulator import CarbonApi, Generator | ||
|
||
|
||
class TestCarbonApi: | ||
|
||
@pytest.fixture | ||
def ci_api(self) -> CarbonApi: | ||
index = [ | ||
pd.to_datetime("2023-01-01T00:00:00"), | ||
pd.to_datetime("2023-01-01T00:30:00"), | ||
pd.to_datetime("2023-01-01T01:00:00") | ||
] | ||
data = pd.DataFrame({"a": [1, 2, 3], "b": [0, 3, 0]}, index=index) | ||
return CarbonApi(data) | ||
|
||
def test_initialize_fails_if_unit_unsupported(self): | ||
with pytest.raises(ValueError): | ||
CarbonApi(pd.DataFrame(), unit="unsupported_unit") | ||
|
||
@pytest.mark.parametrize("dt, expected", [ | ||
(pd.to_datetime("2023-01-01T00:00:00"), pd.to_datetime("2023-01-01T00:30:00")), | ||
(pd.to_datetime("2023-01-01T00:10:00"), pd.to_datetime("2023-01-01T00:30:00")), | ||
(pd.to_datetime("2023-01-01T00:29:59"), pd.to_datetime("2023-01-01T00:30:00")), | ||
(pd.to_datetime("2023-01-01T00:40:00"), pd.to_datetime("2023-01-01T01:00:00")), | ||
]) | ||
def test_next_update(self, ci_api, dt, expected): | ||
assert ci_api.next_update(dt) == expected | ||
|
||
def test_zones(self, ci_api): | ||
assert ci_api.zones() == ["a", "b"] | ||
|
||
def test_carbon_intensity_at_single_zone(self): | ||
ci_api = CarbonApi(pd.DataFrame({"a": [1]})) | ||
assert ci_api.carbon_intensity_at(0) == 1 | ||
|
||
@pytest.mark.parametrize("dt, zone, expected", [ | ||
(pd.to_datetime("2023-01-01T00:00:00"), "a", 1), | ||
(pd.to_datetime("2023-01-01T00:00:10"), "a", 1), | ||
(pd.to_datetime("2023-01-01T01:00:00"), "a", 3), | ||
(pd.to_datetime("2023-01-01T10:00:00"), "a", 3), | ||
(pd.to_datetime("2023-01-01T00:29:59"), "b", 0), | ||
(pd.to_datetime("2023-01-01T00:30:00"), "b", 3), | ||
]) | ||
def test_carbon_intensity_at(self, ci_api, dt, zone, expected): | ||
assert ci_api.carbon_intensity_at(dt, zone) == expected | ||
|
||
def test_carbon_intensity_at_fails_if_zone_not_specified(self, ci_api): | ||
with pytest.raises(ValueError): | ||
ci_api.carbon_intensity_at(pd.to_datetime("2023-01-01T00:00:00")) | ||
|
||
def test_carbon_intensity_at_fails_if_now_too_early(self, ci_api): | ||
with pytest.raises(ValueError): | ||
ci_api.carbon_intensity_at(pd.to_datetime("2022-12-30T23:59:59"), "a") | ||
|
||
def test_carbon_intensity_at_fails_if_zone_does_not_exist(self, ci_api): | ||
with pytest.raises(ValueError): | ||
ci_api.carbon_intensity_at(pd.to_datetime("2023-01-01T00:00:00"), "c") | ||
|
||
def test_carbon_intensity_at_with_lb_per_mwh(self): | ||
data = pd.DataFrame({"a": [1, 2]}) | ||
ci_api = CarbonApi(data, unit="lb_per_MWh") | ||
assert 0.45 < ci_api.carbon_intensity_at(0) < 0.46 | ||
assert 0.9 < ci_api.carbon_intensity_at(1) < 0.91 | ||
|
||
|
||
class TestGenerator: | ||
|
||
@pytest.fixture | ||
def generator(self) -> Generator: | ||
index = [ | ||
pd.to_datetime("2023-01-01T00:00:00"), | ||
pd.to_datetime("2023-01-01T00:30:00"), | ||
pd.to_datetime("2023-01-01T01:00:00") | ||
] | ||
data = pd.Series([1, 2, 3], index=index) | ||
return Generator(data) | ||
|
||
@pytest.mark.parametrize("dt, expected", [ | ||
(pd.to_datetime("2023-01-01T00:00:00"), 1), | ||
(pd.to_datetime("2023-01-01T00:00:10"), 1), | ||
(pd.to_datetime("2023-01-01T01:00:00"), 3), | ||
(pd.to_datetime("2023-01-01T10:00:00"), 3), | ||
(pd.to_datetime("2023-01-01T00:29:59"), 1), | ||
(pd.to_datetime("2023-01-01T00:30:00"), 2), | ||
]) | ||
def test_carbon_intensity_at(self, generator, dt, expected): | ||
assert generator.power_at(dt) == expected | ||
|
||
def test_power_at_fails_if_now_too_early(self, generator): | ||
with pytest.raises(ValueError): | ||
generator.power_at(pd.to_datetime("2022-12-30T23:59:59")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how does the power meter know which power mode to chose initially? the first in the dict?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The initial power mode is "high performance" as specified in the docstring of the MockPowerMeter class