@@ -1441,41 +1441,115 @@ def test_get_trial_days_full_month(billing_process_instance):
14411441# # -----------------------------------------------------------------------------------
14421442# # - Test command
14431443@pytest .mark .parametrize (
1444- "opts" ,
1444+ ( "opts" , "not_allowed" , "error_message" ) ,
14451445 [
1446- {
1447- "year" : 2025 ,
1448- "month" : 8 ,
1449- "dry_run" : True ,
1450- "cutoff_day" : 1 ,
1451- },
1452- {
1453- "year" : 2024 ,
1454- "month" : 12 ,
1455- "dry_run" : False ,
1456- "authorization" : "AUTH123" ,
1457- "cutoff_day" : 1 ,
1458- },
1446+ (
1447+ {"year" : 2025 , "month" : 8 , "dry_run" : True , "cutoff_day" : 1 },
1448+ False ,
1449+ None ,
1450+ ),
1451+ (
1452+ {
1453+ "year" : 2024 ,
1454+ "month" : 12 ,
1455+ "dry_run" : False ,
1456+ "authorization" : "AUTH123" ,
1457+ "cutoff_day" : 1 ,
1458+ },
1459+ False ,
1460+ None ,
1461+ ),
1462+ (
1463+ {
1464+ "year" : date .today ().year + 1 ,
1465+ "month" : 1 ,
1466+ "dry_run" : True ,
1467+ "cutoff_day" : 5 ,
1468+ },
1469+ True ,
1470+ "The billing period cannot be in the future" ,
1471+ ),
1472+ (
1473+ {
1474+ "dry_run" : True ,
1475+ },
1476+ False ,
1477+ None ,
1478+ ),
1479+ (
1480+ {
1481+ "year" : date .today ().year ,
1482+ "dry_run" : True ,
1483+ "cutoff_day" : 5 ,
1484+ },
1485+ True ,
1486+ "The billing period cannot be in the future" ,
1487+ ),
1488+ (
1489+ {
1490+ "month" : max (1 , date .today ().month - 1 ),
1491+ "dry_run" : True ,
1492+ "cutoff_day" : 5 ,
1493+ },
1494+ False ,
1495+ None ,
1496+ ),
1497+ (
1498+ {
1499+ "month" : 20 ,
1500+ "dry_run" : True ,
1501+ },
1502+ True ,
1503+ "The billing month must be between 1 and 12 (inclusive)" ,
1504+ ),
1505+ (
1506+ {
1507+ "cutoff_day" : 100 ,
1508+ "dry_run" : True ,
1509+ },
1510+ True ,
1511+ "The cutoff-day must be between 1 and 28 (inclusive)" ,
1512+ ),
14591513 ],
14601514)
1461- def test_handle_run_command (mocker , opts ):
1462- fake_coro_obj = object ()
1463- process_billing_mock = Mock (return_value = fake_coro_obj )
1464- asyncio_run_mock = Mock ()
1465-
1466- mocker .patch ("ffc.management.commands.process_billing.process_billing" , process_billing_mock )
1467- mocker .patch ("ffc.management.commands.process_billing.asyncio.run" , asyncio_run_mock )
1468- call_command ("process_billing" , ** opts )
1469- expected_auth = opts .get ("authorization" )
1470- process_billing_mock .assert_called_once_with (
1471- opts ["year" ],
1472- opts ["month" ],
1473- authorization_id = expected_auth ,
1474- dry_run = opts ["dry_run" ],
1475- cutoff_day = opts ["cutoff_day" ],
1515+ def test_process_billing_command (mocker , opts , not_allowed , error_message , capsys ):
1516+ fake_coro = object ()
1517+ mock_process_billing = Mock (return_value = fake_coro )
1518+ mock_asyncio_run = Mock ()
1519+
1520+ mocker .patch (
1521+ "ffc.management.commands.process_billing.process_billing" ,
1522+ mock_process_billing ,
1523+ )
1524+ mocker .patch (
1525+ "ffc.management.commands.process_billing.asyncio.run" ,
1526+ mock_asyncio_run ,
14761527 )
14771528
1478- asyncio_run_mock .assert_called_once_with (fake_coro_obj )
1529+ if not_allowed :
1530+ with pytest .raises (SystemExit ) as excinfo :
1531+ call_command ("process_billing" , ** opts )
1532+
1533+ assert excinfo .value .code == 1
1534+
1535+ captured = capsys .readouterr ()
1536+ assert error_message in captured .err
1537+
1538+ mock_process_billing .assert_not_called ()
1539+ mock_asyncio_run .assert_not_called ()
1540+ else :
1541+ call_command ("process_billing" , ** opts )
1542+ if "year" in opts and "month" in opts :
1543+ mock_process_billing .assert_called_once_with (
1544+ opts ["year" ],
1545+ opts ["month" ],
1546+ authorization_id = opts .get ("authorization" ),
1547+ dry_run = opts ["dry_run" ],
1548+ cutoff_day = opts ["cutoff_day" ],
1549+ )
1550+ else :
1551+ mock_process_billing .assert_called_once ()
1552+ mock_asyncio_run .assert_called_once_with (fake_coro )
14791553
14801554
14811555# # -----------------------------------------------------------------------------------
0 commit comments