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

Add missing conversion tests in unit conversion #86434

Merged
merged 3 commits into from Jan 23, 2023

Conversation

epenet
Copy link
Contributor

@epenet epenet commented Jan 23, 2023

Proposed change

As follow-up to #86340
ElectricPotentialConverter, UnitlessRatioConverter didn't have unit conversion tested.
PressureConverter was also missing a test for UnitOfPressure.BAR.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

Copy link
Contributor Author

@epenet epenet left a comment

Choose a reason for hiding this comment

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

It seems that diff isn't very friendly on this PR.
Most of the code change is just moving stuff around, merging the parametrisation of all the test_XXX_convert into a single dictionary constant.

Comment on lines +430 to +441
unit_ratio_item = _GET_UNIT_RATIO[converter]
assert unit_ratio_item[0] != unit_ratio_item[1], "ratio units should be different"

assert converter in _CONVERTED_VALUE, "converter is not present in _CONVERTED_VALUE"
converted_value_items = _CONVERTED_VALUE[converter]
for valid_unit in converter.VALID_UNITS:
assert any(
item
for item in converted_value_items
# item[1] is from_unit, item[3] is to_unit
if valid_unit in {item[1], item[3]}
), f"Unit `{valid_unit}` is not tested in _CONVERTED_VALUE"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the second half of the code change: ensure that all units are in the _CONVERTED_VALUE parametrization dictionary.

Comment on lines +105 to +108
# Dict containing a conversion test for every know unit.
_CONVERTED_VALUE: dict[
type[BaseUnitConverter], list[tuple[float, str | None, float, str | None]]
] = {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the first half of the PR: merged the parametrization lists into a single dictionary.

Comment on lines +511 to +521
@pytest.mark.parametrize(
"converter,value,from_unit,expected,to_unit",
[
# Process all items in _CONVERTED_VALUE
(converter, list_item[0], list_item[1], list_item[2], list_item[3])
for converter, item in _CONVERTED_VALUE.items()
for list_item in item
],
)
def test_unit_conversion(
converter: type[BaseUnitConverter],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is where we use the new dictionary.

Copy link
Member

@frenck frenck left a comment

Choose a reason for hiding this comment

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

LGTM, Thanks @epenet

../Frenck

@frenck frenck merged commit 6453517 into home-assistant:dev Jan 23, 2023
@epenet epenet deleted the unit-conversion branch January 23, 2023 09:15
@github-actions github-actions bot locked and limited conversation to collaborators Jan 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants