diff --git a/twitch/management/commands/better_import_drops.py b/twitch/management/commands/better_import_drops.py index 96e31f7..4170016 100644 --- a/twitch/management/commands/better_import_drops.py +++ b/twitch/management/commands/better_import_drops.py @@ -521,7 +521,7 @@ class Command(BaseCommand): options=options, ) - if broken_dir: + if broken_dir is not None: # File already moved due to validation failure; signal caller to skip further handling. return False, broken_dir @@ -648,12 +648,13 @@ class Command(BaseCommand): def _get_or_update_benefit(self, benefit_schema: DropBenefitSchema) -> DropBenefit: """Return a DropBenefit, updating stale cached values when needed.""" + distribution_type: str = (benefit_schema.distribution_type or "").strip() benefit_defaults: dict[str, str | int | datetime | bool | None] = { "name": benefit_schema.name, "image_asset_url": benefit_schema.image_asset_url, "entitlement_limit": benefit_schema.entitlement_limit, "is_ios_available": benefit_schema.is_ios_available, - "distribution_type": benefit_schema.distribution_type, + "distribution_type": distribution_type, } if benefit_schema.created_at: @@ -872,7 +873,7 @@ class Command(BaseCommand): matched: str | None = detect_non_campaign_keyword(raw_text) if matched: if not options.get("skip_broken_moves"): - broken_dir: Path = move_file_to_broken_subdir( + broken_dir: Path | None = move_file_to_broken_subdir( file_path, matched, operation_name=operation_name, @@ -916,7 +917,7 @@ class Command(BaseCommand): if isinstance(parsed_json_local, (dict, list)) else None ) - broken_dir: Path = move_failed_validation_file(file_path, operation_name=op_name) + broken_dir = move_failed_validation_file(file_path, operation_name=op_name) return {"success": False, "broken_dir": str(broken_dir)} return {"success": False, "broken_dir": "(skipped)"} else: @@ -950,7 +951,7 @@ class Command(BaseCommand): matched: str | None = detect_non_campaign_keyword(raw_text) if matched: if not options.get("skip_broken_moves"): - broken_dir: Path = move_file_to_broken_subdir( + broken_dir: Path | None = move_file_to_broken_subdir( file_path, matched, operation_name=operation_name, @@ -1015,7 +1016,7 @@ class Command(BaseCommand): if isinstance(parsed_json_local, (dict, list)) else None ) - broken_dir: Path = move_failed_validation_file(file_path, operation_name=op_name) + broken_dir = move_failed_validation_file(file_path, operation_name=op_name) progress_bar.write(f"{Fore.RED}✗{Style.RESET_ALL} {file_path.name} → {broken_dir}/{file_path.name}") else: progress_bar.write(f"{Fore.RED}✗{Style.RESET_ALL} {file_path.name} (move skipped)") diff --git a/twitch/tests/test_better_import_drops.py b/twitch/tests/test_better_import_drops.py new file mode 100644 index 0000000..0b4de6b --- /dev/null +++ b/twitch/tests/test_better_import_drops.py @@ -0,0 +1,36 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING + +from django.test import TestCase + +from twitch.management.commands.better_import_drops import Command +from twitch.schemas import DropBenefitSchema + +if TYPE_CHECKING: + from twitch.models import DropBenefit + + +class GetOrUpdateBenefitTests(TestCase): + """Tests for the _get_or_update_benefit method in better_import_drops.Command.""" + + def test_defaults_distribution_type_when_missing(self) -> None: + """Ensure importer sets distribution_type to empty string when absent.""" + command = Command() + command.benefit_cache = {} + + benefit_schema: DropBenefitSchema = DropBenefitSchema.model_validate( + { + "id": "benefit-missing-distribution-type", + "name": "Test Benefit", + "imageAssetURL": "https://example.com/benefit.png", + "entitlementLimit": 1, + "isIosAvailable": False, + "__typename": "DropBenefit", + }, + ) + + benefit: DropBenefit = command._get_or_update_benefit(benefit_schema) # noqa: SLF001 + + benefit.refresh_from_db() + assert not benefit.distribution_type