-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #239 from qpv-research-group/register_iv_solvers
Create the IV registry
- Loading branch information
Showing
2 changed files
with
185 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,165 @@ | ||
import pytest | ||
|
||
|
||
def test_register_action(): | ||
def test_generic_register(): | ||
from solcore import registries | ||
from typing import Callable | ||
|
||
@registries.register_action("pre-process") | ||
def pre_process_cell(*args, **kwargs): | ||
REGISTRY = {} | ||
SIGNATURE = Callable | ||
|
||
register_a_solver = registries.generic_register( | ||
"a-solver", | ||
registrator_name="Cool features", | ||
registry=REGISTRY, | ||
signature=SIGNATURE, | ||
) | ||
|
||
@register_a_solver | ||
def solver(*args, **kwargs): | ||
pass | ||
|
||
assert "pre-process" in registries.ACTIONS_REGISTRY | ||
assert REGISTRY["a-solver"] == solver | ||
|
||
with pytest.raises(ValueError): | ||
|
||
@registries.register_action("pre-process") | ||
def custom_pre_process_cell(*args, **kwargs): | ||
pass | ||
registries.generic_register( | ||
"a-solver", | ||
registrator_name="Cool features", | ||
registry=REGISTRY, | ||
signature=SIGNATURE, | ||
) | ||
|
||
register_a_solver = registries.generic_register( | ||
"a-solver", | ||
registrator_name="Cool features", | ||
registry=REGISTRY, | ||
signature=SIGNATURE, | ||
overwrite=True, | ||
) | ||
|
||
@registries.register_action("pre-process", overwrite=True) | ||
def another_pre_process_cell(*args, **kwargs): | ||
@register_a_solver | ||
def second_solver(*args, **kwargs): | ||
pass | ||
|
||
assert registries.ACTIONS_REGISTRY["pre-process"] == another_pre_process_cell | ||
assert REGISTRY["a-solver"] == second_solver | ||
|
||
|
||
def test_register_optics(): | ||
def test_register_action(mocker): | ||
from solcore import registries | ||
|
||
@registries.register_optics("approximate") | ||
def approximate_optics(*args, **kwargs): | ||
mock_gr = mocker.patch("solcore.registries.generic_register") | ||
name = "pre-process" | ||
overwrite = False | ||
reason_to_exclude = None | ||
|
||
@registries.register_action( | ||
name, overwrite=overwrite, reason_to_exclude=reason_to_exclude | ||
) | ||
def solver(*args, **kwargs): | ||
pass | ||
|
||
assert "approximate" in registries.OPTICS_METHOD_REGISTRY | ||
mock_gr.assert_called_once_with( | ||
name=name, | ||
registrator_name="Action", | ||
registry=registries.ACTIONS_REGISTRY, | ||
signature=registries.ACTIONS_SIGNATURE, | ||
overwrite=overwrite, | ||
reason_to_exclude=reason_to_exclude, | ||
) | ||
|
||
|
||
with pytest.raises(ValueError): | ||
def test_register_optics(mocker): | ||
from solcore import registries | ||
|
||
@registries.register_optics("approximate") | ||
def custom_approximate_optics(*args, **kwargs): | ||
pass | ||
mock_gr = mocker.patch("solcore.registries.generic_register") | ||
name = "custom_optics" | ||
overwrite = False | ||
reason_to_exclude = None | ||
|
||
@registries.register_optics("approximate", overwrite=True) | ||
def another_approximate_optics(*args, **kwargs): | ||
@registries.register_optics( | ||
name, overwrite=overwrite, reason_to_exclude=reason_to_exclude | ||
) | ||
def solver(*args, **kwargs): | ||
pass | ||
|
||
assert ( | ||
registries.OPTICS_METHOD_REGISTRY["approximate"] == another_approximate_optics | ||
mock_gr.assert_called_once_with( | ||
name=name, | ||
registrator_name="Optics solver", | ||
registry=registries.OPTICS_METHOD_REGISTRY, | ||
signature=registries.OPTICS_METHOD_SIGNATURE, | ||
overwrite=overwrite, | ||
reason_to_exclude=reason_to_exclude, | ||
) | ||
|
||
@registries.register_optics("final_approximate", reason_to_exclude="Doesn't work") | ||
def final_approximate_optics(*args, **kwargs): | ||
|
||
def test_register_short_circuit_solver(mocker): | ||
from solcore import registries | ||
|
||
mock_gr = mocker.patch("solcore.registries.generic_register") | ||
name = "custom_short_circuit" | ||
overwrite = False | ||
reason_to_exclude = None | ||
|
||
@registries.register_short_circuit_solver( | ||
name, overwrite=overwrite, reason_to_exclude=reason_to_exclude | ||
) | ||
def solver(*args, **kwargs): | ||
pass | ||
|
||
assert "final_approximate" not in registries.OPTICS_METHOD_REGISTRY | ||
mock_gr.assert_called_once_with( | ||
name=name, | ||
registrator_name="Short circuit solver", | ||
registry=registries.SHORT_CIRCUIT_SOLVER_REGISTRY, | ||
signature=registries.SHORT_CIRCUIT_SOLVER_SIGNATURE, | ||
overwrite=overwrite, | ||
reason_to_exclude=reason_to_exclude, | ||
) | ||
|
||
|
||
def test_register_equilibrium_solver(mocker): | ||
from solcore import registries | ||
|
||
mock_gr = mocker.patch("solcore.registries.generic_register") | ||
name = "custom_equilibrium" | ||
overwrite = False | ||
reason_to_exclude = None | ||
|
||
@registries.register_equilibrium_solver( | ||
name, overwrite=overwrite, reason_to_exclude=reason_to_exclude | ||
) | ||
def solver(*args, **kwargs): | ||
pass | ||
|
||
mock_gr.assert_called_once_with( | ||
name=name, | ||
registrator_name="Equilibrium solver", | ||
registry=registries.EQUILIBRIUM_SOLVER_REGISTRY, | ||
signature=registries.EQUILIBRIUM_SOLVER_SIGNATURE, | ||
overwrite=overwrite, | ||
reason_to_exclude=reason_to_exclude, | ||
) | ||
|
||
|
||
def test_register_iv_solver(mocker): | ||
from solcore import registries | ||
|
||
mock_gr = mocker.patch("solcore.registries.generic_register") | ||
name = "custom_iv_solver" | ||
overwrite = False | ||
reason_to_exclude = None | ||
|
||
@registries.register_iv_solver( | ||
name, overwrite=overwrite, reason_to_exclude=reason_to_exclude | ||
) | ||
def solver(*args, **kwargs): | ||
pass | ||
|
||
mock_gr.assert_called_once_with( | ||
name=name, | ||
registrator_name="IV solver", | ||
registry=registries.IV_SOLVER_REGISTRY, | ||
signature=registries.IV_SOLVER_SIGNATURE, | ||
overwrite=overwrite, | ||
reason_to_exclude=reason_to_exclude, | ||
) |