import asyncio import json from datetime import date from unittest.mock import AsyncMock, patch import pytest from app.modules.rentals.backend import router @pytest.mark.parametrize('months', [None, 0, 12]) def test_monthly_subscription_is_separate_from_startup_order(months): args = dict(customer_id=2,sag_id=3,start_date=date(2026,10,15),start_price=15000,freight_price=79,preparation_price=1500,operations_monthly_price=1000) if months is not None: args['initial_operations_months'] = months payload = router.QuickRentCreateInput(**args) with patch.object(router,'execute_query_single',side_effect=[{'id':1,'current_owner_type':'bmc','model':'PC'},{'id':2},{'id':3,'customer_id':2},None]), patch.object(router,'_assert_asset_booking_available'), patch.object(router,'create_sag_subscription',AsyncMock(return_value={'id':12})) as subscription, patch.object(router,'create_sag_asset_binding',AsyncMock(return_value={'id':8})), patch.object(router,'with_invoice_fee',side_effect=lambda lines,customer:lines), patch.object(router,'execute_query',return_value=[{'id':252}]) as query: result = asyncio.run(router.quick_rent_hardware(1,payload)) body = subscription.call_args.args[0] assert body['billing_interval'] == 'monthly' assert body['price'] == 1000 assert body['first_invoice_policy'] == 'start_date' params = query.call_args.args[1] lines = json.loads(params[2]) drift = [line for line in lines if line['line_key'] == 'rental-operations-initial'] if months == 12: # Explicit legacy API requests retain their existing behavior. assert drift[0]['quantity'] == 12 assert params[5] == date(2027,10,15) else: assert drift == [] assert sum(line['quantity']*line['unit_price'] for line in lines) == 16579 assert params[4:6] == (None,None) assert result['subscription_id'] == 12