Files
kleinanzeigen-bot/tests/unit/test_download_selection.py
Jens 98985e447f refactor: extract delete and download state helpers (#1085)
## ℹ️ Description

- Link to the related issue(s): Issue #
- Extract browser-free delete and download state decisions from
`kleinanzeigen_bot.__init__` into focused modules.
- This keeps `KleinanzeigenBot` responsible for browser and workflow
orchestration while making the state helpers directly unit-testable.

## 📋 Changes Summary

- Added `kleinanzeigen_bot.ad_state` for relative ad paths and
after-delete state cleanup.
- Added `kleinanzeigen_bot.download_selection` for numeric selector
matching and download active/ownership resolution.
- Updated `KleinanzeigenBot` to call the extracted helpers directly and
removed the class-level download state resolver wrapper.
- Added explicit `__all__` declarations for the new domain modules and
removed leftover local path renaming wrapper methods from
`KleinanzeigenBot`.
- Added focused unit tests for the extracted helpers and reduced
duplicate resolver/delete-state coverage in `test_init.py`.

### ⚙️ Type of Change
Select the type(s) of change(s) included in this pull request:
- [ ] 🐞 Bug fix (non-breaking change which fixes an issue)
- [x]  New feature (adds new functionality without breaking existing
usage)
- [ ] 💥 Breaking change (changes that might break existing user setups,
scripts, or configurations)


##  Checklist
Before requesting a review, confirm the following:
- [x] I have reviewed my changes to ensure they meet the project's
standards.
- [x] I have tested my changes and ensured that all tests pass (`pdm run
test`).
- [x] I have formatted the code (`pdm run format`).
- [x] I have verified that linting passes (`pdm run lint`).
- [x] I have updated documentation where necessary.

By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Refactor**
* Reorganized internal code structure by consolidating ad state
management, download selection logic, and path renaming utilities into
dedicated modules for improved code organization and maintainability.

* **Tests**
* Added comprehensive unit tests covering ad state handling, download
selection behavior, and path renaming functionality to enhance code
reliability.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-06-05 16:30:59 +02:00

39 lines
1.5 KiB
Python

# SPDX-FileCopyrightText: © Jens Bergmann and contributors
# SPDX-License-Identifier: AGPL-3.0-or-later
# SPDX-ArtifactOfProjectHomePage: https://github.com/Second-Hand-Friends/kleinanzeigen-bot/
from kleinanzeigen_bot.download_selection import NUMERIC_IDS_RE, is_numeric_ids_selector, resolve_download_ad_activity
def test_resolve_download_ad_activity_reports_owned_active_ad() -> None:
resolved = resolve_download_ad_activity(123, {123: {"id": 123, "state": "active"}})
assert resolved.active is True
assert resolved.owned is True
def test_resolve_download_ad_activity_reports_owned_inactive_ad_when_state_is_missing_or_not_active() -> None:
resolved_with_state = resolve_download_ad_activity(123, {123: {"id": 123, "state": "paused"}})
resolved_without_state = resolve_download_ad_activity(123, {123: {"id": 123}})
assert resolved_with_state.active is False
assert resolved_with_state.owned is True
assert resolved_without_state.active is False
assert resolved_without_state.owned is True
def test_resolve_download_ad_activity_reports_unknown_ad_as_not_owned() -> None:
resolved = resolve_download_ad_activity(123, {})
assert resolved.active is False
assert resolved.owned is False
def test_numeric_ids_regex_accepts_comma_separated_numbers() -> None:
assert NUMERIC_IDS_RE.match("123,456")
assert is_numeric_ids_selector("123,456")
def test_numeric_ids_regex_rejects_mixed_selectors() -> None:
assert NUMERIC_IDS_RE.match("123,abc") is None
assert not is_numeric_ids_selector("123,abc")