Skip to content

MPT-18701 Improve base model tests#225

Merged
albertsola merged 1 commit intomainfrom
model_unit_test
Mar 13, 2026
Merged

MPT-18701 Improve base model tests#225
albertsola merged 1 commit intomainfrom
model_unit_test

Conversation

@albertsola
Copy link
Contributor

@albertsola albertsola commented Mar 12, 2026

Closes MPT-18701

  • Consolidate and expand base model unit tests into tests/unit/models/test_model.py:
    • Add helper test models: TypedListItemDummy, TypedListContainerDummy, DictTypedContainerDummy, ScalarListContainerDummy
    • Cover to_snake_case behavior, ModelList operations (extend, insert, setitem), nested list processing, scalar list handling, and wrapping non-list targets into ModelList
    • Test BaseModel dynamic attribute access, private attribute handling, repr, and to_dict serialization
    • Validate typed-list processing that converts nested dicts to BaseModel instances and preserves BaseModel instances
  • Delete tests/unit/models/resource/test_resource_custom_key.py (remove test for custom data key handling)
  • Simplify mpt_api_client/models/model.py: remove class-level _data_key unwrapping so Model.from_response operates on the full response dict (still strips "$meta" and attaches Meta.from_response)
  • Remove _data_key usage from tests (DummyModel no longer declares _data_key = None)

@albertsola albertsola requested a review from a team as a code owner March 12, 2026 15:39
@albertsola albertsola requested review from alephsur and d3rky March 12, 2026 15:39
@coderabbitai
Copy link

coderabbitai bot commented Mar 12, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Removed a unit test and a test fixture _data_key, added comprehensive model tests, and changed Model.from_response to stop unwrapping response payloads by a class-level _data_key.

Changes

Cohort / File(s) Summary
Deleted Test File
tests/unit/models/resource/test_resource_custom_key.py
Deleted unit test that validated a resource model using _data_key (e.g., "charge") to extract nested JSON.
Expanded Model Tests
tests/unit/models/test_model.py
Added dummy model classes and many tests covering to_snake_case, ModelList operations (extend, insert), nested/typed-list conversion into BaseModel instances, scalar list handling, attribute/getattr/setattr/privacy behavior, and to_dict exclusions.
Test Fixture Change
tests/unit/conftest.py
Removed the _data_key = None attribute from DummyModel in test fixtures.
Core Model Behavior Changed
mpt_api_client/models/model.py
Removed the _data_key class variable and the logic in Model.from_response that unwrapped response payloads by that key; updated docstring accordingly and removed ClassVar import.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • softwareone-platform/mpt-api-python-client#222: Also modifies mpt_api_client/models/model.Model.from_response, addressing nested data-key extraction behavior.
🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Jira Issue Key In Title ✅ Passed PR title contains exactly one Jira issue key MPT-18701 in the required MPT-XXXX format at the beginning.
Test Coverage Required ✅ Passed PR modifies 1 code file (mpt_api_client/models/model.py) and includes 3 test file changes, satisfying test coverage requirements.
Single Commit Required ✅ Passed The PR contains exactly one commit (819020d) as verified by git rev-list, meeting the single commit requirement.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

📝 Coding Plan
  • Generate coding plan for human review comments

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
tests/unit/models/test_model.py (1)

5-5: Optional: Consider adding WPS rules to Ruff's lint.external config.

The static analysis reports that WPS347 is not a Ruff rule code. This noqa comment is intended for flake8/wemake-python-styleguide, which is fine since the project uses both tools. To suppress these Ruff warnings across the codebase, you could add WPS rules to the [tool.ruff.lint] external configuration option in pyproject.toml.

This is purely cosmetic and doesn't affect functionality.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/unit/models/test_model.py` at line 5, The import line in
tests/unit/models/test_model.py (importing BaseModel, ModelList, to_snake_case)
uses a noqa comment for WPS347 which Ruff doesn't recognize; update the Ruff
config instead of per-line noqa by adding the WPS rule prefix to the external
list in pyproject.toml: under [tool.ruff.lint] add "WPS" (or the appropriate WPS
rule codes) to lint.external so Ruff accepts WPS-style noqa comments across the
codebase.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@tests/unit/models/test_model.py`:
- Line 5: The import line in tests/unit/models/test_model.py (importing
BaseModel, ModelList, to_snake_case) uses a noqa comment for WPS347 which Ruff
doesn't recognize; update the Ruff config instead of per-line noqa by adding the
WPS rule prefix to the external list in pyproject.toml: under [tool.ruff.lint]
add "WPS" (or the appropriate WPS rule codes) to lint.external so Ruff accepts
WPS-style noqa comments across the codebase.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: 3a1fc864-f5c9-410c-804c-c9754f975f10

📥 Commits

Reviewing files that changed from the base of the PR and between baca528 and 0ba86c5.

📒 Files selected for processing (2)
  • tests/unit/models/resource/test_resource_custom_key.py
  • tests/unit/models/test_model.py
💤 Files with no reviewable changes (1)
  • tests/unit/models/resource/test_resource_custom_key.py


result = ChargeResourceMock.from_response(response)

assert result.id == "1"
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to use only one assert here?

expected_charge = ChargResouceMock(id=..., amount=...)

assert result == expected_charge

Otherwise tests are fragile a little bit if new properties are added

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 test it only tests if from_response gets the data correctly based on the _data_key if that has a value.

A quick search show that this seems to only be used in test. Orignially this was reused for collections but not anymore as collections use _collection_key now.

I'm happy to delete that code and test.

expected_charge = ChargResouceMock(id=..., amount=...)

assert result == expected_charge

that assertion will fail, as expected_charge would not have the Meta setup with the Response inside.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Code and test deleted

@albertsola albertsola force-pushed the model_unit_test branch 2 times, most recently from 9d6a1ad to 5adcaf9 Compare March 12, 2026 16:43
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@tests/unit/models/test_model.py`:
- Around line 236-241: In test_model_list_extend rename the single-letter
comprehension variable `m` to a descriptive name (e.g., `model` or `item`) to
satisfy WPS111; update the list comprehension in the assertion ([m.id for m in
ml] -> [model.id for model in ml]) inside the test_model_list_extend function
that uses ModelList so the test still asserts ["1","2","3"] but no longer uses a
one-letter variable.
- Line 5: The test import uses a flake8 WPS suppression "# noqa: WPS347" which
Ruff flags as RUF102; update lint config so both linters agree by adding the WPS
codes to Ruff’s external-ignore list or replace the suppression with a
Ruff-compatible one. Edit pyproject.toml to add "WPS347" (and other needed
"WPS*" codes) under tool.ruff.lint.external or remove the WPS-specific noqa on
the import of BaseModel, ModelList, to_snake_case and instead use a
Ruff-supported noqa token so the import line no longer triggers RUF102.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: 7d6186ed-b87a-475d-8bf6-90727f6c4a89

📥 Commits

Reviewing files that changed from the base of the PR and between 0ba86c5 and 9d6a1ad.

📒 Files selected for processing (3)
  • mpt_api_client/models/model.py
  • tests/unit/models/resource/test_resource_custom_key.py
  • tests/unit/models/test_model.py
💤 Files with no reviewable changes (1)
  • tests/unit/models/resource/test_resource_custom_key.py

@albertsola albertsola requested a review from d3rky March 12, 2026 16:45
@albertsola
Copy link
Contributor Author

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Mar 12, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.


ml.insert(1, {"id": "2"}) # act

assert len(ml) == 3
Copy link
Contributor

Choose a reason for hiding this comment

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

I think here you can also squash number of assets

[ml_item.id for ml_item in ml] == ["1", "2", "3"]

def test_model_list_process_item_scalar():
ml = ModelList(["a", "b", "c"]) # act

assert ml[0] == "a"
Copy link
Contributor

Choose a reason for hiding this comment

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

And here

assert ml == ["a", "b", "c"]

container = ScalarListContainerDummy(tags=["a", "b", "c"]) # act

assert isinstance(container.tags, ModelList)
assert container.tags[0] == "a"
Copy link
Contributor

Choose a reason for hiding this comment

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

and here

assert container.tags == ["a", "b", "c"]

@albertsola albertsola requested a review from d3rky March 13, 2026 10:07
@sonarqubecloud
Copy link

@albertsola albertsola merged commit ed1c921 into main Mar 13, 2026
4 checks passed
@albertsola albertsola deleted the model_unit_test branch March 13, 2026 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants