-
-
Notifications
You must be signed in to change notification settings - Fork 35.8k
Add tests for bbox #155750
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
base: dev
Are you sure you want to change the base?
Add tests for bbox #155750
Conversation
f0a28ef to
030dab9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think mik's review already covers a lot of things that can be imporved
|
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
1b58642 to
5664b83
Compare
|
Thanks a lot @mik-laj for your review! |
5664b83 to
8a043de
Compare
|
@sweenu I'm glad we've managed to get the code to this point now. I look forward to your next contributions! |
3c35fe7 to
8a043de
Compare
| box = pybbox.Bbox(ip=self.host) | ||
| result = box.get_all_connected_devices() | ||
| try: | ||
| box = pybbox.Bbox(ip=self.host) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can creating the Bbox object raise?
| with ( | ||
| patch("homeassistant.components.bbox.device_tracker.pybbox") as mock_pybbox_dt, | ||
| patch("homeassistant.components.bbox.sensor.pybbox") as mock_pybbox_sensor, | ||
| ): | ||
| mock_bbox = MagicMock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| with ( | |
| patch("homeassistant.components.bbox.device_tracker.pybbox") as mock_pybbox_dt, | |
| patch("homeassistant.components.bbox.sensor.pybbox") as mock_pybbox_sensor, | |
| ): | |
| mock_bbox = MagicMock() | |
| with ( | |
| patch("homeassistant.components.bbox.device_tracker.pybbox.Bbox", autospec=True) as mock_bbox_client, | |
| patch("homeassistant.components.bbox.sensor.pybbox", new=mock_bbox_client), | |
| ): | |
| mock_bbox = mock_bbox_client.return_value |
This way you don't have to assign it to 2 separate ones
| @pytest.fixture | ||
| def mock_add_entities() -> Generator[MagicMock]: | ||
| """Mock add_entities callback.""" | ||
| with patch( | ||
| "homeassistant.components.bbox.sensor.AddEntitiesCallback", spec=True | ||
| ) as mock_add_entities: | ||
| yield mock_add_entities |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not patch this one
| scanner = get_scanner(hass, device_tracker_config) | ||
| assert scanner is not None | ||
| assert scanner.success_init is True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead, we should let HA setup the integration with the YAML config, and then we can check if the device tracker entities that we expect are registered
| mock_bbox_api.get_all_connected_devices.side_effect = requests.exceptions.HTTPError( | ||
| "Connection failed" | ||
| ) | ||
|
|
||
| scanner = get_scanner(hass, device_tracker_config) | ||
| assert scanner is None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in this case we should check that there are no device tracker entities created after letting HA setting it up
(in other words, the tests shouldn't call get_scanner directly)
| async def test_get_device_name( | ||
| hass: HomeAssistant, | ||
| device_tracker_config: ConfigType, | ||
| mock_bbox_api: MagicMock, | ||
| ) -> None: | ||
| """Test getting device name by MAC address.""" | ||
| scanner = get_scanner(hass, device_tracker_config) | ||
| assert scanner is not None | ||
|
|
||
| # Test existing device | ||
| name = scanner.get_device_name("aa:bb:cc:dd:ee:ff") | ||
| assert name == "test_device" | ||
|
|
||
| # Test another existing device | ||
| name = scanner.get_device_name("ff:ee:dd:cc:bb:aa") | ||
| assert name == "another_device" | ||
|
|
||
| # Test non-existing device | ||
| name = scanner.get_device_name("11:22:33:44:55:66") | ||
| assert name is None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could test this via snapshots
| """Test sensor values for different sensor types.""" | ||
| # Setup platform with specific sensor type | ||
| sensor_config[SENSOR_DOMAIN][CONF_MONITORED_VARIABLES] = [sensor_type] | ||
| setup_platform(hass, sensor_config[SENSOR_DOMAIN], mock_add_entities) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't call setup platform ourselves. Instead let HA set the integration up, after which you can use snapshots
| assert entity._attr_native_value.replace(microsecond=0) == expected_time.replace( | ||
| microsecond=0 | ||
| ) | ||
| assert entity._attr_device_class == "timestamp" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we also shouldn't access entities directly
Proposed change
This is the first step towards: #155651
Type of change
Additional information
Checklist
ruff format homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all.To help with the load of incoming pull requests: