|
| 1 | +""" |
| 2 | +Tests for Project 01 — Parametrize |
| 3 | +
|
| 4 | +This file demonstrates @pytest.mark.parametrize, which lets you run one |
| 5 | +test function with many different inputs. Instead of writing ten separate |
| 6 | +test functions that all look the same, you write one and give it a table |
| 7 | +of inputs and expected outputs. |
| 8 | +
|
| 9 | +Run with: pytest tests/test_utils.py -v |
| 10 | +The -v flag shows each parametrize case as a separate line in the output. |
| 11 | +""" |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +# Import the functions we are testing. The ".." means "go up one directory" |
| 16 | +# but pytest handles this automatically when you run from the project root. |
| 17 | +from project import validate_email, celsius_to_fahrenheit, is_palindrome, clamp |
| 18 | + |
| 19 | + |
| 20 | +# ── validate_email ────────────────────────────────────────────────────── |
| 21 | +# WHY: Email validation has many edge cases. A parametrized test lets us |
| 22 | +# list them all in one place and add new cases without writing new functions. |
| 23 | + |
| 24 | +@pytest.mark.parametrize( |
| 25 | + "email, expected", |
| 26 | + [ |
| 27 | + # --- Valid emails --- |
| 28 | + ("user@example.com", True), # Standard email |
| 29 | + ("name.tag@domain.org", True), # Dots in local part |
| 30 | + ("user+filter@gmail.com", True), # Plus addressing (common in Gmail) |
| 31 | + ("x@y.io", True), # Minimal valid email |
| 32 | + ("UPPER@CASE.COM", True), # Case should not matter for format |
| 33 | +
|
| 34 | + # --- Invalid emails --- |
| 35 | + ("missing-at-sign", False), # No @ symbol at all |
| 36 | + ("", False), # Empty string |
| 37 | + ("@no-local-part.com", False), # Nothing before @ |
| 38 | + ("spaces in@email.com", False), # Spaces are not allowed |
| 39 | + ("double@@at.com", False), # Two @ symbols |
| 40 | + ], |
| 41 | + # ids makes the test output readable. Without it, pytest shows the raw |
| 42 | + # parameter tuple. With it, you get a human-friendly label. |
| 43 | + ids=[ |
| 44 | + "valid-user@example.com", |
| 45 | + "valid-name.tag@domain.org", |
| 46 | + "valid-plus-addressing", |
| 47 | + "valid-minimal", |
| 48 | + "valid-uppercase", |
| 49 | + "invalid-missing-at", |
| 50 | + "invalid-empty-string", |
| 51 | + "invalid-no-local-part", |
| 52 | + "invalid-spaces", |
| 53 | + "invalid-double-at", |
| 54 | + ], |
| 55 | +) |
| 56 | +def test_validate_email(email, expected): |
| 57 | + """Each row in the parametrize table becomes a separate test case.""" |
| 58 | + assert validate_email(email) == expected |
| 59 | + |
| 60 | + |
| 61 | +# WHY: We also want to make sure non-string inputs are handled gracefully. |
| 62 | +# This is a separate parametrize because the input type is different. |
| 63 | + |
| 64 | +@pytest.mark.parametrize( |
| 65 | + "bad_input", |
| 66 | + [None, 42, [], {}], |
| 67 | + ids=["none", "integer", "list", "dict"], |
| 68 | +) |
| 69 | +def test_validate_email_rejects_non_strings(bad_input): |
| 70 | + """Non-string inputs should always return False, never crash.""" |
| 71 | + assert validate_email(bad_input) is False |
| 72 | + |
| 73 | + |
| 74 | +# ── celsius_to_fahrenheit ─────────────────────────────────────────────── |
| 75 | +# WHY: Temperature conversion has well-known reference points. We test |
| 76 | +# the famous ones (freezing, boiling, body temp, the crossover point) |
| 77 | +# to make sure the formula is implemented correctly. |
| 78 | + |
| 79 | +@pytest.mark.parametrize( |
| 80 | + "celsius, expected_fahrenheit", |
| 81 | + [ |
| 82 | + (0, 32.0), # Freezing point of water |
| 83 | + (100, 212.0), # Boiling point of water |
| 84 | + (-40, -40.0), # The crossover point (same in both scales!) |
| 85 | + (37, 98.6), # Human body temperature |
| 86 | + (-273.15, -459.67), # Absolute zero |
| 87 | + ], |
| 88 | + ids=["freezing", "boiling", "crossover", "body-temp", "absolute-zero"], |
| 89 | +) |
| 90 | +def test_celsius_to_fahrenheit(celsius, expected_fahrenheit): |
| 91 | + """Verify known temperature conversion pairs.""" |
| 92 | + # We use pytest.approx because floating-point math can introduce tiny |
| 93 | + # rounding errors. pytest.approx checks that the values are "close enough" |
| 94 | + # (within a very small tolerance, by default 1e-6). |
| 95 | + assert celsius_to_fahrenheit(celsius) == pytest.approx(expected_fahrenheit) |
| 96 | + |
| 97 | + |
| 98 | +# ── is_palindrome ─────────────────────────────────────────────────────── |
| 99 | +# WHY: Palindrome checking involves string cleaning (removing punctuation, |
| 100 | +# ignoring case). We need to test both the core logic and the cleaning. |
| 101 | + |
| 102 | +@pytest.mark.parametrize( |
| 103 | + "text, expected", |
| 104 | + [ |
| 105 | + ("racecar", True), # Classic palindrome |
| 106 | + ("hello", False), # Clearly not a palindrome |
| 107 | + ("A man a plan a canal Panama", True), # Sentence palindrome with spaces |
| 108 | + ("Was it a car or a cat I saw?", True), # Punctuation and mixed case |
| 109 | + ("", True), # Empty string is a palindrome |
| 110 | + ("a", True), # Single character |
| 111 | + ("ab", False), # Two different characters |
| 112 | + ("Madam", True), # Mixed case palindrome |
| 113 | + ], |
| 114 | + ids=[ |
| 115 | + "racecar", |
| 116 | + "hello", |
| 117 | + "sentence-palindrome", |
| 118 | + "punctuation-mixed-case", |
| 119 | + "empty-string", |
| 120 | + "single-char", |
| 121 | + "two-different-chars", |
| 122 | + "mixed-case-madam", |
| 123 | + ], |
| 124 | +) |
| 125 | +def test_is_palindrome(text, expected): |
| 126 | + """Test palindrome detection with various inputs including edge cases.""" |
| 127 | + assert is_palindrome(text) == expected |
| 128 | + |
| 129 | + |
| 130 | +# ── clamp ─────────────────────────────────────────────────────────────── |
| 131 | +# WHY: Clamp has three distinct behaviors (below min, above max, in range) |
| 132 | +# plus edge cases at the boundaries. Parametrize lets us cover them all |
| 133 | +# in a compact table. |
| 134 | + |
| 135 | +@pytest.mark.parametrize( |
| 136 | + "value, min_val, max_val, expected", |
| 137 | + [ |
| 138 | + (5, 0, 10, 5), # Value already in range — returned unchanged |
| 139 | + (-3, 0, 10, 0), # Below minimum — clamped up to min |
| 140 | + (15, 0, 10, 10), # Above maximum — clamped down to max |
| 141 | + (0, 0, 10, 0), # Exactly at minimum — boundary case |
| 142 | + (10, 0, 10, 10), # Exactly at maximum — boundary case |
| 143 | + (5, 5, 5, 5), # Min equals max — only one valid value |
| 144 | + (-100, -50, 50, -50), # Negative range |
| 145 | + (3.5, 0, 10, 3.5), # Float value in range |
| 146 | + ], |
| 147 | + ids=[ |
| 148 | + "in-range", |
| 149 | + "below-minimum", |
| 150 | + "above-maximum", |
| 151 | + "at-minimum", |
| 152 | + "at-maximum", |
| 153 | + "single-point-range", |
| 154 | + "negative-range", |
| 155 | + "float-value", |
| 156 | + ], |
| 157 | +) |
| 158 | +def test_clamp(value, min_val, max_val, expected): |
| 159 | + """Test clamp with values inside, outside, and at the boundaries.""" |
| 160 | + assert clamp(value, min_val, max_val) == expected |
| 161 | + |
| 162 | + |
| 163 | +# WHY: Clamp should raise an error when min > max. This is a separate test |
| 164 | +# because we are testing for an exception, not a return value. |
| 165 | + |
| 166 | +def test_clamp_raises_on_invalid_range(): |
| 167 | + """Passing min > max is a programming error and should raise ValueError.""" |
| 168 | + with pytest.raises(ValueError, match="must not be greater than"): |
| 169 | + clamp(5, 10, 0) |
0 commit comments