|
| 1 | +"""Tests for extras: helpers, course_codes, sentiment.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from rmp_client import ( |
| 6 | + build_course_mapping, |
| 7 | + clean_course_label, |
| 8 | + is_valid_comment, |
| 9 | + normalize_comment, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +class TestNormalizeComment: |
| 14 | + """normalize_comment.""" |
| 15 | + |
| 16 | + def test_lowercases_and_collapses_whitespace(self) -> None: |
| 17 | + assert normalize_comment(" Hello World! ") == "hello world!" |
| 18 | + |
| 19 | + def test_empty_after_strip(self) -> None: |
| 20 | + assert normalize_comment(" ") == "" |
| 21 | + |
| 22 | + def test_single_word(self) -> None: |
| 23 | + assert normalize_comment("GREAT") == "great" |
| 24 | + |
| 25 | + def test_newlines_collapsed(self) -> None: |
| 26 | + assert normalize_comment("a\nb\nc") == "a b c" |
| 27 | + |
| 28 | + def test_unicode_preserved(self) -> None: |
| 29 | + assert normalize_comment(" Café ") == "café" |
| 30 | + |
| 31 | + def test_strips_html_by_default(self) -> None: |
| 32 | + assert normalize_comment("<b>Loved</b> this class") == "loved this class" |
| 33 | + |
| 34 | + def test_strip_html_option(self) -> None: |
| 35 | + assert normalize_comment("<b>Bold</b>", strip_html=False) == "<b>bold</b>" |
| 36 | + |
| 37 | + def test_strip_punctuation_option(self) -> None: |
| 38 | + assert normalize_comment("Hello, world!", strip_punctuation=True) == "hello world" |
| 39 | + |
| 40 | + |
| 41 | +class TestIsValidComment: |
| 42 | + """is_valid_comment.""" |
| 43 | + |
| 44 | + def test_valid_with_default_min_len(self) -> None: |
| 45 | + assert is_valid_comment("this is ten!!").valid is True |
| 46 | + assert is_valid_comment("short").valid is False |
| 47 | + |
| 48 | + def test_empty_is_false(self) -> None: |
| 49 | + assert is_valid_comment("").valid is False |
| 50 | + assert is_valid_comment(" ").valid is False |
| 51 | + |
| 52 | + def test_custom_min_len(self) -> None: |
| 53 | + assert is_valid_comment("five!", min_len=5).valid is True |
| 54 | + assert is_valid_comment("four", min_len=5).valid is False |
| 55 | + |
| 56 | + def test_exactly_min_len_with_alpha(self) -> None: |
| 57 | + assert is_valid_comment("hello", min_len=5).valid is True |
| 58 | + |
| 59 | + def test_returns_issues_for_invalid_comments(self) -> None: |
| 60 | + short = is_valid_comment("short") |
| 61 | + assert short.valid is False |
| 62 | + assert any(i.code == "too_short" for i in short.issues) |
| 63 | + |
| 64 | + all_caps = is_valid_comment("WORST PROF EVER") |
| 65 | + assert all_caps.valid is False |
| 66 | + assert any(i.code == "all_caps" for i in all_caps.issues) |
| 67 | + |
| 68 | + no_alpha = is_valid_comment("12345", min_len=5) |
| 69 | + assert no_alpha.valid is False |
| 70 | + assert any(i.code == "no_alpha" for i in no_alpha.issues) |
| 71 | + |
| 72 | + excessive = is_valid_comment("sooooooo bad") |
| 73 | + assert excessive.valid is False |
| 74 | + assert any(i.code == "excessive_repeats" for i in excessive.issues) |
| 75 | + |
| 76 | + |
| 77 | +class TestCleanCourseLabel: |
| 78 | + """clean_course_label.""" |
| 79 | + |
| 80 | + def test_removes_count_parens(self) -> None: |
| 81 | + assert clean_course_label("MATH 101 (12)") == "MATH 101" |
| 82 | + assert clean_course_label("CS 50 (3)") == "CS 50" |
| 83 | + |
| 84 | + def test_collapses_whitespace(self) -> None: |
| 85 | + assert clean_course_label(" ANAT 215 ") == "ANAT 215" |
| 86 | + |
| 87 | + def test_no_parens_unchanged_except_trim(self) -> None: |
| 88 | + assert clean_course_label("MATH 101") == "MATH 101" |
| 89 | + |
| 90 | + |
| 91 | +class TestBuildCourseMapping: |
| 92 | + """build_course_mapping.""" |
| 93 | + |
| 94 | + def test_exact_match_case_insensitive(self) -> None: |
| 95 | + valid = ["MATH 101", "ANAT 215"] |
| 96 | + scraped = ["MATH 101", "math 101", "ANAT 215"] |
| 97 | + mapping = build_course_mapping(scraped, valid) |
| 98 | + assert mapping["MATH 101"] == {"MATH 101"} |
| 99 | + assert mapping["math 101"] == {"MATH 101"} |
| 100 | + assert mapping["ANAT 215"] == {"ANAT 215"} |
| 101 | + |
| 102 | + def test_prefix_number_match(self) -> None: |
| 103 | + valid = ["ANAT 215"] |
| 104 | + scraped = ["ANAT215", "anat 215"] |
| 105 | + mapping = build_course_mapping(scraped, valid) |
| 106 | + assert mapping["ANAT215"] == {"ANAT 215"} |
| 107 | + assert mapping["anat 215"] == {"ANAT 215"} |
| 108 | + |
| 109 | + def test_unknown_returns_none(self) -> None: |
| 110 | + valid = ["MATH 101"] |
| 111 | + scraped = ["UNKNOWN 999"] |
| 112 | + mapping = build_course_mapping(scraped, valid) |
| 113 | + assert mapping["UNKNOWN 999"] is None |
| 114 | + |
| 115 | + def test_empty_valid(self) -> None: |
| 116 | + mapping = build_course_mapping(["MATH 101"], []) |
| 117 | + assert mapping["MATH 101"] is None |
0 commit comments