|
| 1 | +"""Tests for the package __init__ module.""" |
| 2 | + |
| 3 | +import importlib |
| 4 | +import re |
| 5 | +import sys |
| 6 | +from unittest.mock import patch |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +import my_python_package |
| 11 | + |
| 12 | + |
| 13 | +def test_package_version(): |
| 14 | + """Test that the package has a valid version string.""" |
| 15 | + assert hasattr(my_python_package, "__version__") |
| 16 | + assert isinstance(my_python_package, str) |
| 17 | + # Check that it follows semantic versioning (major.minor.patch) |
| 18 | + assert re.match(r"^\d+\.\d+\.\d+", my_python_package.__version__) |
| 19 | + |
| 20 | + |
| 21 | +def test_package_author(): |
| 22 | + """Test that the package has an author string.""" |
| 23 | + assert hasattr(my_python_package, "__author__") |
| 24 | + assert isinstance(my_python_package.__author__, str) |
| 25 | + assert my_python_package.__author__ == "Diogo Ribeiro" |
| 26 | + |
| 27 | + |
| 28 | +def test_package_exports(): |
| 29 | + """Test that the package exports the expected functions.""" |
| 30 | + # Check __all__ contents |
| 31 | + assert hasattr(my_python_package, "__all__") |
| 32 | + assert isinstance(my_python_package.__all__, list) |
| 33 | + |
| 34 | + # Check expected functions are in __all__ |
| 35 | + expected_functions = [ |
| 36 | + "hello", |
| 37 | + "generate_greeting", |
| 38 | + "random_greeting", |
| 39 | + "validate_name", |
| 40 | + "create_greeting_list", |
| 41 | + "format_greeting", |
| 42 | + ] |
| 43 | + for func in expected_functions: |
| 44 | + assert func in my_python_package.__all__ |
| 45 | + |
| 46 | + # Check functions are actually exported |
| 47 | + for func in my_python_package.__all__: |
| 48 | + assert hasattr(my_python_package, func) |
| 49 | + assert callable(getattr(my_python_package, func)) |
| 50 | + |
| 51 | + |
| 52 | +def test_module_imports(): |
| 53 | + """Test that all package imports work properly.""" |
| 54 | + # Test importing the main module |
| 55 | + importlib.reload(my_python_package) |
| 56 | + |
| 57 | + # Test importing submodules |
| 58 | + from my_python_package import config |
| 59 | + from my_python_package import core |
| 60 | + from my_python_package import cli |
| 61 | + from my_python_package import logging |
| 62 | + |
| 63 | + # Verify the modules loaded correctly |
| 64 | + assert hasattr(config, "Config") |
| 65 | + assert hasattr(core, "hello") |
| 66 | + assert hasattr(cli, "main") |
| 67 | + assert hasattr(logging, "logger") |
| 68 | + |
| 69 | + |
| 70 | +def test_main_function(): |
| 71 | + """Test the _main function that handles module execution.""" |
| 72 | + # Create a mock for sys.exit and cli.main |
| 73 | + with patch("my_python_package.cli.main", return_value=42) as mock_main: |
| 74 | + with patch("sys.exit") as mock_exit: |
| 75 | + # Call _main function |
| 76 | + my_python_package._main() |
| 77 | + |
| 78 | + # Verify cli.main was called |
| 79 | + mock_main.assert_called_once() |
| 80 | + |
| 81 | + # Verify sys.exit was called with the return value from cli.main |
| 82 | + mock_exit.assert_called_once_with(42) |
| 83 | + |
| 84 | + |
| 85 | +def test_direct_execution(): |
| 86 | + """Test module execution behavior.""" |
| 87 | + # We can't directly test __name__ == "__main__" code paths in an imported module, |
| 88 | + # but we can test the behavior indirectly by simulating it |
| 89 | + |
| 90 | + # Save original __name__ |
| 91 | + original_name = my_python_package.__name__ |
| 92 | + |
| 93 | + try: |
| 94 | + # Set up the module as if it's being run directly |
| 95 | + with patch.object(my_python_package, "__name__", "__main__"): |
| 96 | + with patch("my_python_package._main") as mock_main: |
| 97 | + # Re-execute the module code |
| 98 | + exec(open(my_python_package.__file__).read(), vars(my_python_package)) |
| 99 | + |
| 100 | + # Verify _main was called |
| 101 | + mock_main.assert_called_once() |
| 102 | + finally: |
| 103 | + # Restore original __name__ |
| 104 | + my_python_package.__name__ = original_name |
| 105 | + |
| 106 | + |
| 107 | +def test_doctest_examples(): |
| 108 | + """Test that the doctest examples in __init__ work correctly.""" |
| 109 | + import doctest |
| 110 | + |
| 111 | + # Run doctests on the module |
| 112 | + result = doctest.testmod(my_python_package) |
| 113 | + |
| 114 | + # Verify all tests passed (failures == 0) |
| 115 | + assert result.failed == 0 |
| 116 | + assert result.attempted > 0 # Make sure some tests were actually run |
0 commit comments