Skip to content
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

Unit test base network #1438

Merged
merged 28 commits into from
Dec 12, 2024
Merged

Unit test base network #1438

merged 28 commits into from
Dec 12, 2024

Conversation

finozzifa
Copy link
Collaborator

@finozzifa finozzifa commented Nov 28, 2024

Closes # (if applicable).

Changes proposed in this Pull Request

The pull request proposes the following unit tests:

test/test_base_network.py:

  • test_get_country
  • test_get_linetypes_config
  • test_get_linetype_by_voltage
  • test_get_oid
  • test_load_buses
  • test_load_converters_from_eg
  • test_load_converters_from_osm
  • test_load_lines
  • test_load_links_from_eg
  • test_load_links_from_osm
  • test_load_transformers
  • test_reconnect_crimea
  • test_set_electrical_parameters_converters
  • test_set_electrical_parameters_lines_eg
  • test_set_electrical_parameters_lines_osm
  • test_set_electrical_parameters_links_osm
  • test_set_electrical_parameters_transformers

Checklist

  • I tested my contribution locally and it works as intended.
  • Code and workflow changes are sufficiently documented.
  • Changed dependencies are added to envs/environment.yaml.
  • Changes in configuration options are added in config/config.default.yaml.
  • Changes in configuration options are documented in doc/configtables/*.csv.
  • Sources of newly added data are documented in doc/data_sources.rst.
  • A release note doc/release_notes.rst is added.

pre-commit-ci bot and others added 22 commits November 28, 2024 16:56
@finozzifa finozzifa marked this pull request as ready for review December 4, 2024 14:11
Copy link
Member

@lkstrp lkstrp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for continuing the unit testing journey @finozzifa!
I was mostly just skimming through things, but looks good to me.
If you haven't already done so locally, we could think about integrating codecov like in the other package repos. See here. Not as a check (yet), but just to get an overview to focus on the most useful tests.

@@ -28,3 +30,89 @@ def config():
with open(path_config, "r") as file:
config_dict = yaml.safe_load(file)
return config_dict


@pytest.fixture(scope="function")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have any best practices for choosing scopes? I think the function scope works well for DataFrames because they initialise quickly. But in PyPSA I've been playing around with the scope of test networks because they are sometimes modified and sometimes not in the tests, which adds up if function is used as scope.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And same goes for the two network fixtures you created here already

Copy link
Collaborator Author

@finozzifa finozzifa Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @lkstrp,

indeed great question!

I usually used function being the default one and (most likely) the safest one.

However, now that I think about it, the config fixture can be initialized only once per test session. Changing the scope for this fixture actually leads to a speed up of the test execution (almost 30%). You can find the results below. The scope session and package behave equally well.

This is of course not very relevant now, but it will become once we add more and more tests.

@pytest.fixture(scope="function")
def config():

hyperfine pytest test

Time (mean ± σ):      2.223 s ±  0.108 s    [User: 1.949 s, System: 0.251 s]
Range (min … max):    2.106 s …  2.459 s    10 runs

@pytest.fixture(scope="session")
def config():

hyperfine pytest test

Time (mean ± σ):      1.568 s ±  0.004 s    [User: 1.333 s, System: 0.228 s]
Range (min … max):    1.563 s …  1.573 s    10 runs

@pytest.fixture(scope="package")
def config():

hyperfine pytest test

Time (mean ± σ):      1.570 s ±  0.003 s    [User: 1.334 s, System: 0.229 s]
Range (min … max):    1.567 s …  1.574 s    10 runs

@lkstrp lkstrp merged commit 212f9df into PyPSA:master Dec 12, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants