Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /promptdriven_pdd /tests /commands /test_utility.py
| """ | |
| Test Plan for pdd.commands.utility | |
| 1. Z3 Formal Verification (Input Generation): | |
| - Use Z3 to model the constraints for valid verification parameters (budget > 0, max_attempts > 0). | |
| - Solve these constraints to generate valid test data for the unit tests. | |
| - This ensures we are testing with mathematically valid configuration boundaries. | |
| 2. Unit Tests for `install_completion_cmd`: | |
| - Test Case 1: Successful installation (default parameters). | |
| - Mock `pdd.cli.install_completion`. | |
| - Verify it is called with `quiet=False`. | |
| - Test Case 2: Successful installation with quiet mode. | |
| - Pass `quiet=True` in context. | |
| - Verify `install_completion` is called with `quiet=True`. | |
| - Test Case 3: Error handling. | |
| - Mock `install_completion` to raise an exception. | |
| - Verify `handle_error` is called. | |
| 3. Unit Tests for `verify`: | |
| - Test Case 1: Successful verification run (Standard arguments). | |
| - Mock `fix_verification_main`. | |
| - Verify it is called with correct mapping of CLI arguments to function arguments. | |
| - Verify `loop=True` is enforced. | |
| - Test Case 2: Agentic fallback flags. | |
| - Test `--no-agentic-fallback` and ensure `agentic_fallback=False` is passed. | |
| - Test Case 3: Abort handling. | |
| - Mock `fix_verification_main` to raise `click.Abort`. | |
| - Verify the command aborts (non-zero exit code) and does not call `handle_error`. | |
| - Test Case 4: Generic Exception handling. | |
| - Mock `fix_verification_main` to raise a generic `ValueError`. | |
| - Verify `handle_error` is called. | |
| """ | |
| import pytest | |
| from unittest.mock import MagicMock, patch | |
| from click.testing import CliRunner | |
| from z3 import Solver, Real, Int, sat | |
| import click | |
| # Import the code under test | |
| from pdd.commands.utility import install_completion_cmd, verify | |
| # ----------------------------------------------------------------------------- | |
| # Z3 Formal Verification / Test Data Generation | |
| # ----------------------------------------------------------------------------- | |
| def generate_valid_verify_config(): | |
| """ | |
| Uses Z3 to generate a valid configuration for max_attempts and budget. | |
| Constraints: | |
| - max_attempts must be an integer > 0 | |
| - budget must be a float > 0.0 | |
| """ | |
| s = Solver() | |
| attempts = Int('attempts') | |
| budget = Real('budget') | |
| # Constraints | |
| s.add(attempts > 0) | |
| s.add(attempts < 100) # Reasonable upper bound for test generation | |
| s.add(budget > 0.0) | |
| s.add(budget < 1000.0) | |
| if s.check() == sat: | |
| m = s.model() | |
| # Convert Z3 types to Python types | |
| return m[attempts].as_long(), float(m[budget].numerator_as_long()) / float(m[budget].denominator_as_long()) | |
| else: | |
| raise RuntimeError("Could not generate valid test data using Z3") | |
| # ----------------------------------------------------------------------------- | |
| # Unit Tests | |
| # ----------------------------------------------------------------------------- | |
| def runner(): | |
| return CliRunner() | |
| def mock_fix_verification(): | |
| with patch('pdd.commands.utility.fix_verification_main') as mock: | |
| # Setup default return value structure: | |
| # (success, prog_code, code_content, attempts, total_cost, model_name) | |
| mock.return_value = (True, "print('hello')", "code", 1, 0.1, "gpt-4") | |
| yield mock | |
| def mock_handle_error(): | |
| with patch('pdd.commands.utility.handle_error') as mock: | |
| yield mock | |
| def mock_cli_install(): | |
| # We patch where the function is defined, as the module imports it | |
| with patch('pdd.cli.install_completion') as mock: | |
| yield mock | |
| def test_z3_generated_config_validity(): | |
| """ | |
| Verifies that Z3 can generate valid configuration parameters. | |
| """ | |
| attempts, budget = generate_valid_verify_config() | |
| assert isinstance(attempts, int) | |
| assert attempts > 0 | |
| assert isinstance(budget, float) | |
| assert budget > 0.0 | |
| def test_install_completion_success(runner, mock_cli_install): | |
| """ | |
| Test install_completion command calls the underlying CLI function correctly. | |
| """ | |
| result = runner.invoke(install_completion_cmd) | |
| assert result.exit_code == 0 | |
| mock_cli_install.assert_called_once_with(quiet=False) | |
| def test_install_completion_quiet(runner, mock_cli_install): | |
| """ | |
| Test install_completion passes the quiet flag from context. | |
| """ | |
| # We need to pass the context object. CliRunner allows passing obj. | |
| result = runner.invoke(install_completion_cmd, obj={"quiet": True}) | |
| assert result.exit_code == 0 | |
| mock_cli_install.assert_called_once_with(quiet=True) | |
| def test_install_completion_error(runner, mock_cli_install, mock_handle_error): | |
| """ | |
| Test that exceptions in install_completion are caught and handled. | |
| """ | |
| mock_cli_install.side_effect = Exception("Installation failed") | |
| result = runner.invoke(install_completion_cmd) | |
| # The command catches exception and calls handle_error, usually exiting cleanly or as defined by handle_error | |
| # Assuming handle_error doesn't re-raise by default for this test setup, or we check calls. | |
| mock_handle_error.assert_called_once() | |
| args = mock_handle_error.call_args | |
| assert isinstance(args[0][0], Exception) | |
| assert args[0][1] == "install_completion" | |
| def test_verify_command_basic(runner, mock_fix_verification): | |
| """ | |
| Test verify command with basic arguments and Z3 generated values. | |
| """ | |
| attempts, budget = generate_valid_verify_config() | |
| with runner.isolated_filesystem(): | |
| # Create dummy files required by arguments | |
| with open("prompt.txt", "w") as f: f.write("prompt") | |
| with open("code.py", "w") as f: f.write("code") | |
| with open("verify.py", "w") as f: f.write("verify") | |
| result = runner.invoke(verify, [ | |
| "prompt.txt", | |
| "code.py", | |
| "verify.py", | |
| "--max-attempts", str(attempts), | |
| "--budget", str(budget) | |
| ]) | |
| assert result.exit_code == 0 | |
| mock_fix_verification.assert_called_once() | |
| call_kwargs = mock_fix_verification.call_args[1] | |
| # Check argument mapping | |
| assert call_kwargs['prompt_file'] == "prompt.txt" | |
| assert call_kwargs['code_file'] == "code.py" | |
| assert call_kwargs['program_file'] == "verify.py" | |
| assert call_kwargs['verification_program'] == "verify.py" | |
| assert call_kwargs['max_attempts'] == attempts | |
| assert call_kwargs['budget'] == budget | |
| assert call_kwargs['loop'] is True # Requirement: Logic: Call fix_verification_main with loop=True | |
| assert call_kwargs['agentic_fallback'] is True # Default | |
| def test_verify_command_options(runner, mock_fix_verification): | |
| """ | |
| Test verify command with optional arguments like output paths and flags. | |
| """ | |
| with runner.isolated_filesystem(): | |
| with open("p.txt", "w") as f: f.write("p") | |
| with open("c.py", "w") as f: f.write("c") | |
| with open("v.py", "w") as f: f.write("v") | |
| result = runner.invoke(verify, [ | |
| "p.txt", "c.py", "v.py", | |
| "--output-code", "out_code.py", | |
| "--output-program", "out_prog.py", | |
| "--output-results", "results.json", | |
| "--no-agentic-fallback" | |
| ]) | |
| assert result.exit_code == 0 | |
| call_kwargs = mock_fix_verification.call_args[1] | |
| assert call_kwargs['output_code'] == "out_code.py" | |
| assert call_kwargs['output_program'] == "out_prog.py" | |
| assert call_kwargs['output_results'] == "results.json" | |
| assert call_kwargs['agentic_fallback'] is False | |
| def test_verify_abort_handling(runner, mock_fix_verification, mock_handle_error): | |
| """ | |
| Test that click.Abort is re-raised and not caught by handle_error. | |
| """ | |
| mock_fix_verification.side_effect = click.Abort() | |
| with runner.isolated_filesystem(): | |
| with open("p.txt", "w") as f: f.write("p") | |
| with open("c.py", "w") as f: f.write("c") | |
| with open("v.py", "w") as f: f.write("v") | |
| result = runner.invoke(verify, ["p.txt", "c.py", "v.py"]) | |
| # Click runner catches Abort and sets exit_code to 1 (or similar non-zero) | |
| assert result.exit_code != 0 | |
| # Ensure handle_error was NOT called | |
| mock_handle_error.assert_not_called() | |
| def test_verify_generic_exception(runner, mock_fix_verification, mock_handle_error): | |
| """ | |
| Test that generic exceptions are caught by handle_error. | |
| """ | |
| mock_fix_verification.side_effect = ValueError("Something went wrong") | |
| with runner.isolated_filesystem(): | |
| with open("p.txt", "w") as f: f.write("p") | |
| with open("c.py", "w") as f: f.write("c") | |
| with open("v.py", "w") as f: f.write("v") | |
| result = runner.invoke(verify, ["p.txt", "c.py", "v.py"]) | |
| # Should exit cleanly if handle_error handles it gracefully, or with error code | |
| # The key is that handle_error IS called. | |
| mock_handle_error.assert_called_once() | |
| args = mock_handle_error.call_args | |
| assert isinstance(args[0][0], ValueError) | |
| assert args[0][1] == "verify" |
Xet Storage Details
- Size:
- 9.19 kB
- Xet hash:
- f13529c4ee158f8ad6b129b97b417f17bf7804ddab9d12057de12806e433ad58
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.