|
42 | 42 | )
|
43 | 43 | from frequenz.sdk.timeseries._base_types import SystemBounds
|
44 | 44 | from frequenz.sdk.timeseries.battery_pool import BatteryPool
|
| 45 | +from frequenz.sdk.timeseries.formula_engine._formula_generators._formula_generator import ( |
| 46 | + FormulaGenerationError, |
| 47 | +) |
| 48 | +from tests.utils.graph_generator import GraphGenerator |
45 | 49 |
|
46 | 50 | from ...timeseries.mock_microgrid import MockMicrogrid
|
47 | 51 | from ...utils.component_data_streamer import MockComponentDataStreamer
|
@@ -488,10 +492,110 @@ async def run_test_battery_status_channel( # pylint: disable=too-many-arguments
|
488 | 492 |
|
489 | 493 |
|
490 | 494 | async def test_battery_pool_power(mocker: MockerFixture) -> None:
|
491 |
| - """Test `BatteryPool.{,production,consumption}_power` methods.""" |
| 495 | + """Test `BatteryPool.power` method.""" |
492 | 496 | mockgrid = MockMicrogrid(grid_meter=True, mocker=mocker)
|
493 | 497 | mockgrid.add_batteries(2)
|
| 498 | + await mockgrid.start(mocker) |
| 499 | + await _test_battery_pool_power(mockgrid) |
| 500 | + |
| 501 | + |
| 502 | +async def test_battery_pool_power_two_inverters_per_battery( |
| 503 | + mocker: MockerFixture, |
| 504 | +) -> None: |
| 505 | + """Test power method with two inverters per battery.""" |
| 506 | + gen = GraphGenerator() |
| 507 | + bat = gen.component(ComponentCategory.BATTERY) |
| 508 | + mockgrid = MockMicrogrid( |
| 509 | + graph=gen.to_graph((ComponentCategory.METER, gen.battery_with_inverter(bat, 2))) |
| 510 | + ) |
| 511 | + await mockgrid.start(mocker) |
| 512 | + await _test_battery_pool_power(mockgrid) |
| 513 | + |
| 514 | + |
| 515 | +async def test_batter_pool_power_two_batteries_per_inverter( |
| 516 | + mocker: MockerFixture, |
| 517 | +) -> None: |
| 518 | + """Test power method with two batteries per inverter.""" |
| 519 | + gen = GraphGenerator() |
| 520 | + mockgrid = MockMicrogrid( |
| 521 | + graph=gen.to_graph( |
| 522 | + [ |
| 523 | + ComponentCategory.METER, |
| 524 | + ( |
| 525 | + ComponentCategory.INVERTER, |
| 526 | + [ComponentCategory.BATTERY, ComponentCategory.BATTERY], |
| 527 | + ), |
| 528 | + ComponentCategory.METER, |
| 529 | + ( |
| 530 | + ComponentCategory.INVERTER, |
| 531 | + [ComponentCategory.BATTERY, ComponentCategory.BATTERY], |
| 532 | + ), |
| 533 | + ] |
| 534 | + ) |
| 535 | + ) |
| 536 | + await mockgrid.start(mocker) |
| 537 | + await _test_battery_pool_power(mockgrid) |
| 538 | + |
| 539 | + |
| 540 | +async def test_batter_pool_power_no_batteries(mocker: MockerFixture) -> None: |
| 541 | + """Test power method with no batteries.""" |
| 542 | + mockgrid = MockMicrogrid( |
| 543 | + graph=GraphGenerator().to_graph( |
| 544 | + ( |
| 545 | + ComponentCategory.METER, |
| 546 | + [ComponentCategory.INVERTER, ComponentCategory.INVERTER], |
| 547 | + ) |
| 548 | + ) |
| 549 | + ) |
| 550 | + await mockgrid.start(mocker) |
| 551 | + battery_pool = microgrid.battery_pool() |
| 552 | + power_receiver = battery_pool.power.new_receiver() |
| 553 | + |
| 554 | + await mockgrid.mock_resampler.send_non_existing_component_value() |
| 555 | + assert (await power_receiver.receive()).value == Power.from_watts(0) |
| 556 | + |
| 557 | + |
| 558 | +async def test_battery_pool_power_with_no_inverters(mocker: MockerFixture) -> None: |
| 559 | + """Test power method with no inverters.""" |
| 560 | + mockgrid = MockMicrogrid( |
| 561 | + graph=GraphGenerator().to_graph( |
| 562 | + (ComponentCategory.METER, ComponentCategory.BATTERY) |
| 563 | + ) |
| 564 | + ) |
| 565 | + await mockgrid.start(mocker) |
| 566 | + |
| 567 | + with pytest.raises(RuntimeError): |
| 568 | + microgrid.battery_pool() |
| 569 | + |
| 570 | + |
| 571 | +async def test_battery_pool_power_incomplete_bat_request(mocker: MockerFixture) -> None: |
| 572 | + """Test power method when not all requested ids are behind the same inverter.""" |
| 573 | + gen = GraphGenerator() |
| 574 | + bats = gen.components( |
| 575 | + ComponentCategory.BATTERY, ComponentCategory.BATTERY, ComponentCategory.BATTERY |
| 576 | + ) |
| 577 | + |
| 578 | + mockgrid = MockMicrogrid( |
| 579 | + graph=gen.to_graph( |
| 580 | + ( |
| 581 | + ComponentCategory.METER, |
| 582 | + gen.batteries_with_inverter(bats, 2), |
| 583 | + ) |
| 584 | + ) |
| 585 | + ) |
| 586 | + await mockgrid.start(mocker) |
| 587 | + |
| 588 | + with pytest.raises(FormulaGenerationError): |
| 589 | + # Request only two of the three batteries behind the inverters |
| 590 | + battery_pool = microgrid.battery_pool( |
| 591 | + battery_ids=set([bats[1].component_id, bats[0].component_id]) |
| 592 | + ) |
| 593 | + power_receiver = battery_pool.power.new_receiver() |
| 594 | + await mockgrid.mock_resampler.send_bat_inverter_power([2.0]) |
| 595 | + assert (await power_receiver.receive()).value == Power.from_watts(2.0) |
| 596 | + |
494 | 597 |
|
| 598 | +async def _test_battery_pool_power(mockgrid: MockMicrogrid) -> None: |
495 | 599 | async with mockgrid:
|
496 | 600 | battery_pool = microgrid.battery_pool()
|
497 | 601 | power_receiver = battery_pool.power.new_receiver()
|
|
0 commit comments