|
| 1 | +"""Tests for the ProficiencyScore class.""" |
| 2 | + |
| 3 | +from openproficiency import ProficiencyScore, ProficiencyScoreName |
| 4 | + |
| 5 | + |
| 6 | +class TestProficiencyScore: |
| 7 | + |
| 8 | + # Initializers |
| 9 | + def test_init_with_numeric_score(self): |
| 10 | + """Create a proficiency score with numeric value.""" |
| 11 | + |
| 12 | + # Arrange |
| 13 | + topic_id = "git-commit" |
| 14 | + score = 0.8 |
| 15 | + |
| 16 | + # Act |
| 17 | + ps = ProficiencyScore(topic_id=topic_id, score=score) |
| 18 | + |
| 19 | + # Assert |
| 20 | + assert ps.topic_id == topic_id |
| 21 | + assert ps.score == score |
| 22 | + |
| 23 | + def test_init_with_enum_score(self): |
| 24 | + """Create a proficiency score with ProficiencyScoreName enum.""" |
| 25 | + |
| 26 | + # Arrange |
| 27 | + topic_id = "git-commit" |
| 28 | + score_name = ProficiencyScoreName.PROFICIENT |
| 29 | + |
| 30 | + # Act |
| 31 | + ps = ProficiencyScore(topic_id=topic_id, score=score_name) |
| 32 | + |
| 33 | + # Assert |
| 34 | + assert ps.topic_id == topic_id |
| 35 | + assert ps.score == 0.8 |
| 36 | + |
| 37 | + def test_init_invalid_score_too_low(self): |
| 38 | + """Test that score below 0.0 raises ValueError.""" |
| 39 | + |
| 40 | + # Act & Assert |
| 41 | + try: |
| 42 | + ProficiencyScore(topic_id="test", score=-0.1) |
| 43 | + assert False, "Should have raised ValueError" |
| 44 | + except ValueError as e: |
| 45 | + assert "between 0.0 and 1.0" in str(e) |
| 46 | + |
| 47 | + def test_init_invalid_score_too_high(self): |
| 48 | + """Test that score above 1.0 raises ValueError.""" |
| 49 | + |
| 50 | + # Act & Assert |
| 51 | + try: |
| 52 | + ProficiencyScore(topic_id="test", score=1.1) |
| 53 | + assert False, "Should have raised ValueError" |
| 54 | + except ValueError as e: |
| 55 | + assert "between 0.0 and 1.0" in str(e) |
| 56 | + |
| 57 | + def test_init_invalid_score_type(self): |
| 58 | + """Test that invalid score type raises ValueError.""" |
| 59 | + |
| 60 | + # Act & Assert |
| 61 | + try: |
| 62 | + ProficiencyScore(topic_id="test", score="invalid") |
| 63 | + assert False, "Should have raised ValueError" |
| 64 | + except ValueError as e: |
| 65 | + assert "numeric or ProficiencyScoreName" in str(e) |
| 66 | + |
| 67 | + # Properties - Score |
| 68 | + def test_score_getter(self): |
| 69 | + """Test getting score property.""" |
| 70 | + |
| 71 | + # Arrange |
| 72 | + ps = ProficiencyScore(topic_id="test", score=0.5) |
| 73 | + |
| 74 | + # Act & Assert |
| 75 | + assert ps.score == 0.5 |
| 76 | + |
| 77 | + def test_score_setter_numeric(self): |
| 78 | + """Test setting score with numeric value.""" |
| 79 | + |
| 80 | + # Arrange |
| 81 | + ps = ProficiencyScore(topic_id="test", score=0.1) |
| 82 | + |
| 83 | + # Act |
| 84 | + ps.score = 0.9 |
| 85 | + |
| 86 | + # Assert |
| 87 | + assert ps.score == 0.9 |
| 88 | + |
| 89 | + def test_score_setter_enum(self): |
| 90 | + """Test setting score with enum value.""" |
| 91 | + |
| 92 | + # Arrange |
| 93 | + ps = ProficiencyScore(topic_id="test", score=0.1) |
| 94 | + |
| 95 | + # Act |
| 96 | + ps.score = ProficiencyScoreName.FAMILIAR |
| 97 | + |
| 98 | + # Assert |
| 99 | + assert ps.score == 0.5 |
| 100 | + |
| 101 | + def test_score_setter_invalid(self): |
| 102 | + """Test setting invalid score raises ValueError.""" |
| 103 | + |
| 104 | + # Arrange |
| 105 | + ps = ProficiencyScore(topic_id="test", score=0.1) |
| 106 | + |
| 107 | + # Act & Assert |
| 108 | + try: |
| 109 | + ps.score = 1.5 |
| 110 | + assert False, "Should have raised ValueError" |
| 111 | + except ValueError: |
| 112 | + pass |
| 113 | + |
| 114 | + # Properties - Score Name |
| 115 | + def test_score_name_unaware(self): |
| 116 | + """Test score_name property for UNAWARE level.""" |
| 117 | + |
| 118 | + # Arrange |
| 119 | + ps = ProficiencyScore(topic_id="test", score=0.0) |
| 120 | + |
| 121 | + # Act & Assert |
| 122 | + assert ps.score_name == ProficiencyScoreName.UNAWARE |
| 123 | + |
| 124 | + def test_score_name_aware(self): |
| 125 | + """Test score_name property for AWARE level.""" |
| 126 | + |
| 127 | + # Arrange |
| 128 | + ps = ProficiencyScore(topic_id="test", score=0.05) |
| 129 | + |
| 130 | + # Act & Assert |
| 131 | + assert ps.score_name == ProficiencyScoreName.AWARE |
| 132 | + |
| 133 | + def test_score_name_familiar(self): |
| 134 | + """Test score_name property for FAMILIAR level.""" |
| 135 | + |
| 136 | + # Arrange |
| 137 | + ps = ProficiencyScore(topic_id="test", score=0.3) |
| 138 | + |
| 139 | + # Act & Assert |
| 140 | + assert ps.score_name == ProficiencyScoreName.FAMILIAR |
| 141 | + |
| 142 | + def test_score_name_proficient(self): |
| 143 | + """Test score_name property for PROFICIENT level.""" |
| 144 | + |
| 145 | + # Arrange |
| 146 | + ps = ProficiencyScore(topic_id="test", score=0.8) |
| 147 | + |
| 148 | + # Act & Assert |
| 149 | + assert ps.score_name == ProficiencyScoreName.PROFICIENT |
| 150 | + |
| 151 | + def test_score_name_proficient_with_evidence(self): |
| 152 | + """Test score_name property for PROFICIENT_WITH_EVIDENCE level.""" |
| 153 | + |
| 154 | + # Arrange |
| 155 | + ps = ProficiencyScore(topic_id="test", score=1.0) |
| 156 | + |
| 157 | + # Act & Assert |
| 158 | + assert ps.score_name == ProficiencyScoreName.PROFICIENT_WITH_EVIDENCE |
| 159 | + |
| 160 | + def test_score_name_setter(self): |
| 161 | + """Test setting score_name property.""" |
| 162 | + |
| 163 | + # Arrange |
| 164 | + ps = ProficiencyScore(topic_id="test", score=0.1) |
| 165 | + |
| 166 | + # Act |
| 167 | + ps.score_name = ProficiencyScoreName.PROFICIENT |
| 168 | + |
| 169 | + # Assert |
| 170 | + assert ps.score == 0.8 |
| 171 | + |
| 172 | + def test_score_name_setter_invalid_type(self): |
| 173 | + """Test setting score_name with invalid type raises ValueError.""" |
| 174 | + |
| 175 | + # Arrange |
| 176 | + ps = ProficiencyScore(topic_id="test", score=0.1) |
| 177 | + |
| 178 | + # Act & Assert |
| 179 | + try: |
| 180 | + ps.score_name = 0.5 |
| 181 | + assert False, "Should have raised ValueError" |
| 182 | + except ValueError as e: |
| 183 | + assert "ProficiencyScoreName enum" in str(e) |
| 184 | + |
| 185 | + # Methods |
| 186 | + |
| 187 | + def test_to_json(self): |
| 188 | + """Test conversion to JSON-serializable dictionary.""" |
| 189 | + |
| 190 | + # Arrange |
| 191 | + topic_id = "git-commit" |
| 192 | + score = 0.8 |
| 193 | + ps = ProficiencyScore(topic_id=topic_id, score=score) |
| 194 | + |
| 195 | + # Act |
| 196 | + json_dict = ps.to_json() |
| 197 | + |
| 198 | + # Assert |
| 199 | + assert json_dict == { |
| 200 | + "topic_id": topic_id, |
| 201 | + "score": score |
| 202 | + } |
| 203 | + |
| 204 | + # Debugging |
| 205 | + def test_repr(self): |
| 206 | + """Test string representation of ProficiencyScore.""" |
| 207 | + |
| 208 | + # Arrange |
| 209 | + ps = ProficiencyScore(topic_id="git-commit", score=0.8) |
| 210 | + |
| 211 | + # Act |
| 212 | + repr_str = repr(ps) |
| 213 | + |
| 214 | + # Assert |
| 215 | + assert "ProficiencyScore" in repr_str |
| 216 | + assert "git-commit" in repr_str |
| 217 | + assert "0.8" in repr_str |
| 218 | + assert "PROFICIENT" in repr_str |
0 commit comments