Skip to content

Improve parametrization for unit conversion tests#84808

Closed
epenet wants to merge 8 commits into
home-assistant:devfrom
epenet:unit-conversion-tests
Closed

Improve parametrization for unit conversion tests#84808
epenet wants to merge 8 commits into
home-assistant:devfrom
epenet:unit-conversion-tests

Conversation

@epenet
Copy link
Copy Markdown
Contributor

@epenet epenet commented Dec 29, 2022

Proposed change

Improve parametrization for unit conversion tests

This will help ensure that new units do not creep into the unit enums or the converter valid units without the corresponding ratio.

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:

@epenet epenet force-pushed the unit-conversion-tests branch from 1b8cab9 to a481e59 Compare December 31, 2022 09:24
Comment thread tests/util/test_unit_conversion.py Outdated
Comment on lines +38 to +44
_CONVERTERS: list[type[BaseUnitConverter]] = [
obj
for _, obj in inspect.getmembers(unit_conversion)
if inspect.isclass(obj)
and issubclass(obj, BaseUnitConverter)
and obj != BaseUnitConverter
]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Another possibility is to make this list explicit, and then add a unit test to ensure that the explicit list contains all converters.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sometimes I wonder if we should use the Hypothesis library in HA for cases like these.

Comment thread tests/util/test_unit_conversion.py Outdated
Comment thread tests/util/test_unit_conversion.py Outdated
return next(
filter(
lambda v: v != converter.NORMALIZED_UNIT,
sorted(converter.VALID_UNITS),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Filtering and then sorting, means you need to sort fewer items, than sorting and then filtering.

INVALID_SYMBOL = "bob"
_CONVERTERS: dict[type[BaseUnitConverter], list[str]] = {
obj: sorted(obj.VALID_UNITS)
for _, obj in inspect.getmembers(unit_conversion)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's nice that we don't need to update this container, but it's not very readable what the contents is besides the type.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah I think it might be better to add a test to ensure all converters are tested instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@MartinHjelmare I've opened #86271 as an alternative.
I think it is better to keep the generated list just for comparing against the manual list and not for actually running the tests.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sounds good!

@epenet epenet marked this pull request as draft January 9, 2023 15:55
@epenet epenet mentioned this pull request Jan 20, 2023
19 tasks
@epenet
Copy link
Copy Markdown
Contributor Author

epenet commented Jan 27, 2023

Closing in favor of #86271, #86340, #86434

@epenet epenet closed this Jan 27, 2023
@epenet epenet deleted the unit-conversion-tests branch January 27, 2023 12:10
@github-actions github-actions Bot locked and limited conversation to collaborators Jan 28, 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.

4 participants