Skip to content

tests: Add mock scenarios for bq27441 missing methods.#253

Merged
nedseb merged 2 commits into
mainfrom
test/add-mock-test-bq27441
Mar 28, 2026
Merged

tests: Add mock scenarios for bq27441 missing methods.#253
nedseb merged 2 commits into
mainfrom
test/add-mock-test-bq27441

Conversation

@Charly-sketch

Copy link
Copy Markdown
Contributor

Summary

Add missing mock tests for the bq27441 driver.
Closes #181

This PR improves test coverage by adding mock scenarios for:

  • is_valid_device()
  • temperature()
  • flags()

It also extends the mock_registers to support these additional read paths while preserving the existing test structure.

All mock tests pass successfully:

pytest tests/ -v --driver bq27441 -k mock
============================================================================================================================== test session starts ==============================================================================================================================
platform linux -- Python 3.10.17, pytest-9.0.2, pluggy-1.6.0 -- /usr/bin/python3.10
cachedir: .pytest_cache
rootdir: /home/charly-oliver/micropython-steami-lib
configfile: pyproject.toml
collected 600 items / 14 deselected / 586 selected                                                                                                                                                                                                                              

tests/test_scenarios.py::test_scenario[bq27441/Read voltage/mock] PASSED                                                                                                                                                                                                  [  9%]
tests/test_scenarios.py::test_scenario[bq27441/Read state of charge/mock] PASSED                                                                                                                                                                                          [ 18%]
tests/test_scenarios.py::test_scenario[bq27441/Read remaining capacity/mock] PASSED                                                                                                                                                                                       [ 27%]
tests/test_scenarios.py::test_scenario[bq27441/Read full capacity/mock] PASSED                                                                                                                                                                                            [ 36%]
tests/test_scenarios.py::test_scenario[bq27441/Read average current/mock] PASSED                                                                                                                                                                                          [ 45%]
tests/test_scenarios.py::test_scenario[bq27441/Read average power/mock] PASSED                                                                                                                                                                                            [ 54%]
tests/test_scenarios.py::test_scenario[bq27441/Read state of health/mock] PASSED                                                                                                                                                                                          [ 63%]
tests/test_scenarios.py::test_scenario[bq27441/Read flags register/mock] PASSED                                                                                                                                                                                           [ 72%]
tests/test_scenarios.py::test_scenario[bq27441/Battery temperature returns float from mock data/mock] PASSED                                                                                                                                                              [ 81%]
tests/test_scenarios.py::test_scenario[bq27441/Device type validation returns true/mock] PASSED                                                                                                                                                                           [ 90%]
tests/test_scenarios.py::test_scenario[bq27441/Reset sends CONTROL_RESET command/mock] PASSED                                                                                                                                                                             [100%]

======================================================================================================================= 11 passed, 14 deselected in 0.33s =======================================================================================================================

Changes

  • Add mock test for is_valid_device() using a mocked control word response (0x0421)
  • Add mock test for temperature() returning a float from mock data
  • Add mock test for flags() reading the FLAGS register
  • Extend mock_registers with required values (temperature register)
  • Keep existing tests and structure unchanged

Checklist

  • ruff check passes
  • Commit messages follow <scope>: <Description.> format
  • New mock tests added to bq27441.yaml
  • pytest tests/ -k "mock and bq27441" passes
  • Existing tests still pass: pytest tests/ -k mock

Add mock tests for is_valid_device(), temperature(), and flags() in tests/scenarios/bq27441.yaml.

Extend mock_registers with the values needed for the new read paths and keep the existing scenario structure unchanged.

This improves mock coverage for basic device validation and additional battery data reads without affecting current tests."

@nedseb nedseb left a comment

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.

Review — PR #253

Points positifs

  • 3 nouveaux tests pertinents (flags, temperature, is_valid_device)
  • Les mock_registers sont bien ordonnés par adresse avec commentaires clairs
  • Le monkey-patch pour is_valid_device est astucieux et bien isolé
  • Le test flags est simple et direct

Remarque sur le test température

Le test "Battery temperature returns float from mock data" attend 2981.0, qui est la valeur brute du registre en dixièmes de Kelvin (298.1 K = 24.95 °C). Le nom du test laisse croire qu'on obtient une température lisible alors que c'est un entier brut.

Pour cette PR, renomme le test en "Battery temperature returns raw register value in dK" et ajoute un commentaire :

  - name: "Battery temperature returns raw register value in dK"
    action: script
    script: |
      from bq27441.device import TempMeasureType
      # 2981 = 298.1 K = 24.95 °C (raw value in decikelvin)
      result = float(dev.temperature(TempMeasureType.BATTERY))
    expect: 2981.0
    mode: [mock]

Par ailleurs, la méthode temperature() du driver retourne des dixièmes de Kelvin, ce qui ne respecte pas la convention du projet (unité par défaut = °C, méthodes avec unités explicites sinon). D'après nos conventions :

  • temperature() devrait retourner des °C
  • temperature_dk() pour les dixièmes de Kelvin (si utile)
  • temperature_k() pour les Kelvin (si utile)

C'est hors scope de cette PR, mais je crée une issue séparée pour harmoniser.

Copilot AI left a comment

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.

Pull request overview

Adds missing mock scenario coverage for the bq27441 driver, extending the YAML scenario to exercise additional read paths in mock mode (issue #181).

Changes:

  • Extend mock_registers with temperature and reorganize mock register entries.
  • Add mock tests for flags() and temperature().
  • Add a mock test for is_valid_device() via scripted override of the control-word read.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/scenarios/bq27441.yaml Outdated
# FLAGS (0x06): CFGUPMODE | BAT_DET | DSG — needed for enterConfig/exitConfig
0x06: [0x19, 0x00]

# TEMPERATURE (0x02): 298.1 K raw value

Copilot AI Mar 27, 2026

Copy link

Choose a reason for hiding this comment

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

The TEMPERATURE mock register comment is inconsistent with the actual encoded value. Bytes [0xA5, 0x0B] decode to 0x0BA5 = 2981, which is typically the raw register value in 0.1 K units (i.e., 298.1 K), not “298.1 K raw value”. Please clarify the comment (and/or the expected units) so future readers don’t misinterpret what the driver returns.

Suggested change
# TEMPERATURE (0x02): 298.1 K raw value
# TEMPERATURE (0x02): raw 2981 (0.1 K units = 298.1 K)

Copilot uses AI. Check for mistakes.
Comment thread tests/scenarios/bq27441.yaml Outdated
Comment on lines +93 to +98
- name: "Battery temperature returns float from mock data"
action: script
script: |
from bq27441.device import TempMeasureType
result = float(dev.temperature(TempMeasureType.BATTERY))
expect: 2981.0

Copilot AI Mar 27, 2026

Copy link

Choose a reason for hiding this comment

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

This test forces the result to float(...) and then asserts 2981.0, but BQ27441.temperature() currently returns the raw int from read_word() (so the float cast doesn’t add coverage and the test name is a bit misleading). Consider either asserting the raw integer value directly, or explicitly converting to Kelvin/Celsius in the script (e.g., divide by 10) and using expect_range for a float assertion.

Suggested change
- name: "Battery temperature returns float from mock data"
action: script
script: |
from bq27441.device import TempMeasureType
result = float(dev.temperature(TempMeasureType.BATTERY))
expect: 2981.0
- name: "Battery temperature raw register value from mock data"
action: script
script: |
from bq27441.device import TempMeasureType
result = dev.temperature(TempMeasureType.BATTERY)
expect: 2981

Copilot uses AI. Check for mistakes.
Comment thread tests/scenarios/bq27441.yaml Outdated
Comment on lines +104 to +108
original_read_control_word = dev.read_control_word

def fake_read_control_word(function):
if function == 0x01:
return 0x0421

Copilot AI Mar 27, 2026

Copy link

Choose a reason for hiding this comment

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

Avoid hard-coding the control function / device ID magic numbers in the test. Import and use BQ27441_CONTROL_DEVICE_TYPE and BQ27441_DEVICE_ID from bq27441.const so the test stays correct if constants ever change and is easier to read.

Suggested change
original_read_control_word = dev.read_control_word
def fake_read_control_word(function):
if function == 0x01:
return 0x0421
from bq27441.const import BQ27441_CONTROL_DEVICE_TYPE, BQ27441_DEVICE_ID
original_read_control_word = dev.read_control_word
def fake_read_control_word(function):
if function == BQ27441_CONTROL_DEVICE_TYPE:
return BQ27441_DEVICE_ID

Copilot uses AI. Check for mistakes.
@nedseb

nedseb commented Mar 28, 2026

Copy link
Copy Markdown
Contributor

Les trois remarques de Copilot ont été corrigées dans a7ace9f :

  1. Commentaire TEMPERATURE clarifié : raw 2981 (0.1 K units = 298.1 K) au lieu de l'ambiguïté précédente.
  2. Test température simplifié : suppression du cast float() inutile, valeur attendue 2981 (entier), nom du test corrigé.
  3. Magic numbers remplacés par les constantes BQ27441_CONTROL_DEVICE_TYPE et BQ27441_DEVICE_ID dans le test is_valid_device.

Tous les tests mock passent (11/11). C'étaient des corrections rapides et évidentes, elles n'auraient pas dû rester en attente.

@nedseb nedseb merged commit 202cde1 into main Mar 28, 2026
7 checks passed
@nedseb nedseb deleted the test/add-mock-test-bq27441 branch March 28, 2026 06:00
@semantic-release-updater

Copy link
Copy Markdown

🎉 This PR is included in version 0.1.4 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tests: Add mock scenarios for bq27441 missing methods.

3 participants