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

Improve tests for generic camera #63197

Merged
merged 7 commits into from
Jan 2, 2022

Conversation

davet2001
Copy link
Contributor

Breaking change

Proposed change

Improve the test coverage for generic integration camera component.

Also use simulated valid images to test connections rather than text strings.
Bringing into this PR to make #52360 smaller.

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)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

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.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

tests/components/generic/test_camera.py Outdated Show resolved Hide resolved
tests/components/generic/test_camera.py Outdated Show resolved Hide resolved
tests/components/generic/test_camera.py Outdated Show resolved Hide resolved
"verify_ssl": True,
},
)
assert cam.frame_interval == pytest.approx(0.2)
Copy link
Contributor

Choose a reason for hiding this comment

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

My impression is that the design standards are that it is preferred to have tests that exercise via APIs that are exposed rather than asserting on properties of entities -- though mocking out functions is OK.

However, I realize it looks like this is used as input into mjpeg sleep which is a little awkward to test.

Seems like that implies the preferred way to test this would be via mocking out the sleep call in the mpjeg cam?

Copy link
Contributor Author

@davet2001 davet2001 Jan 2, 2022

Choose a reason for hiding this comment

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

@allenporter Yes, it's complex to test this fully.

I think that the API is well tested by the other tests. This one is literally to increase coverage for one line - to verify that frame_interval() returns a valid value. So really, I suppose it's checking that these lines work:

self._frame_interval = 1 / device_info[CONF_FRAMERATE]

@property
def frame_interval(self):
"""Return the interval between frames of the mjpeg stream."""
return self._frame_interval

Whether that property does anything is useful or not is probably an extra test that could be added. But I would prefer to keep this PR simple.

Copy link
Contributor

@allenporter allenporter Jan 2, 2022

Choose a reason for hiding this comment

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

Ah i also see attributes were introduced recently so maybe the code can just go away and be replaced by setting _attr_frame_interval, then there is no coverage need.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, agreed, done.

"still_image_url": "https://example.com",
"stream_source": 'http://example.com/{{ states.sensor.temp.state + "a" }}',
"limit_refetch_to_url_change": True,
with patch(
Copy link
Contributor

Choose a reason for hiding this comment

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

Are the rest of these patches still needed or can they also use response testing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now replaced with respx.

@allenporter allenporter merged commit 89895c6 into home-assistant:dev Jan 2, 2022
@davet2001 davet2001 deleted the generic_tests_improvement branch January 2, 2022 21:27
@allenporter
Copy link
Contributor

Looks great, thanks @davet2001 👍🏼 👍🏼

@github-actions github-actions bot locked and limited conversation to collaborators Jan 3, 2022
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.

3 participants