|
| 1 | +"""Tests for data_quality: the shared dataset quality rules used by the |
| 2 | +Dataset Builder and the Data Doctor. |
| 3 | +
|
| 4 | +data_quality is pure stdlib, so these tests keep the suite's |
| 5 | +dependency-free contract (plain `python -m unittest`, no installs), |
| 6 | +the same contract CI relies on. |
| 7 | +""" |
| 8 | +import unittest |
| 9 | + |
| 10 | +import data_quality |
| 11 | +from data_quality import check, grounded, parts, script_of |
| 12 | + |
| 13 | + |
| 14 | +class PartsTest(unittest.TestCase): |
| 15 | + def test_messages_become_sft_with_last_turns(self): |
| 16 | + ex = {"messages": [ |
| 17 | + {"role": "user", "content": "first question"}, |
| 18 | + {"role": "assistant", "content": "first answer"}, |
| 19 | + {"role": "user", "content": "second question"}, |
| 20 | + {"role": "assistant", "content": "second answer"}, |
| 21 | + ]} |
| 22 | + p = parts(ex) |
| 23 | + self.assertEqual(p["kind"], "sft") |
| 24 | + self.assertEqual(p["prompt"], "second question") |
| 25 | + self.assertEqual(p["response"], "second answer") |
| 26 | + |
| 27 | + def test_chosen_rejected_become_dpo(self): |
| 28 | + p = parts({"prompt": "q", "chosen": "the good answer", "rejected": "the bad answer"}) |
| 29 | + self.assertEqual(p["kind"], "dpo") |
| 30 | + self.assertEqual(p["response"], "the good answer") |
| 31 | + self.assertEqual(p["rejected"], "the bad answer") |
| 32 | + |
| 33 | + def test_dpo_list_values_take_last_message(self): |
| 34 | + p = parts({"prompt": "q", |
| 35 | + "chosen": [{"role": "assistant", "content": "final good"}], |
| 36 | + "rejected": [{"role": "assistant", "content": "final bad"}]}) |
| 37 | + self.assertEqual(p["response"], "final good") |
| 38 | + self.assertEqual(p["rejected"], "final bad") |
| 39 | + |
| 40 | + def test_prompt_completion_becomes_sft(self): |
| 41 | + p = parts({"prompt": "a prompt", "completion": "a completion"}) |
| 42 | + self.assertEqual(p["kind"], "sft") |
| 43 | + self.assertEqual(p["prompt"], "a prompt") |
| 44 | + self.assertEqual(p["response"], "a completion") |
| 45 | + |
| 46 | + def test_text_key_becomes_text(self): |
| 47 | + p = parts({"text": "just some prose"}) |
| 48 | + self.assertEqual(p["kind"], "text") |
| 49 | + self.assertEqual(p["text"], "just some prose") |
| 50 | + |
| 51 | + def test_non_dict_becomes_text(self): |
| 52 | + p = parts("plain string input") |
| 53 | + self.assertEqual(p["kind"], "text") |
| 54 | + self.assertEqual(p["text"], "plain string input") |
| 55 | + |
| 56 | + |
| 57 | +class ScriptOfTest(unittest.TestCase): |
| 58 | + def test_latin(self): |
| 59 | + self.assertEqual(script_of("The quick brown fox jumps over the lazy dog"), "latin") |
| 60 | + |
| 61 | + def test_cyrillic(self): |
| 62 | + self.assertEqual(script_of("Привет, как дела у тебя сегодня"), "cyrillic") |
| 63 | + |
| 64 | + def test_greek(self): |
| 65 | + self.assertEqual(script_of("Καλημέρα, τι κάνεις σήμερα"), "greek") |
| 66 | + |
| 67 | + def test_cjk(self): |
| 68 | + self.assertEqual(script_of("今天天气很好我们去公园"), "cjk") |
| 69 | + |
| 70 | + def test_no_letters_is_none(self): |
| 71 | + self.assertIsNone(script_of("1234 !!! ???")) |
| 72 | + self.assertIsNone(script_of("")) |
| 73 | + |
| 74 | + |
| 75 | +class CheckCleanTest(unittest.TestCase): |
| 76 | + def test_clean_sft_scores_100(self): |
| 77 | + score, flags = check({ |
| 78 | + "prompt": "What is the capital of France?", |
| 79 | + "completion": "The capital of France is Paris, a city known for the Eiffel Tower.", |
| 80 | + }) |
| 81 | + self.assertEqual(flags, []) |
| 82 | + self.assertEqual(score, 100) |
| 83 | + |
| 84 | + def test_clean_text_scores_100(self): |
| 85 | + score, flags = check({"text": "A short, ordinary paragraph about cooking rice at home for dinner tonight."}) |
| 86 | + self.assertEqual(flags, []) |
| 87 | + self.assertEqual(score, 100) |
| 88 | + |
| 89 | + |
| 90 | +class CheckFlagsTest(unittest.TestCase): |
| 91 | + def test_empty_text_scores_zero(self): |
| 92 | + score, flags = check({"text": " "}) |
| 93 | + self.assertEqual(flags, ["empty"]) |
| 94 | + self.assertEqual(score, 0) |
| 95 | + |
| 96 | + def test_missing_response_is_empty(self): |
| 97 | + score, flags = check({"prompt": "A perfectly good question?", "completion": ""}) |
| 98 | + self.assertIn("empty", flags) |
| 99 | + self.assertEqual(score, 0) |
| 100 | + |
| 101 | + def test_placeholder_answer_scores_zero(self): |
| 102 | + score, flags = check({"prompt": "What is two plus two?", "completion": "TODO"}) |
| 103 | + self.assertEqual(flags, ["placeholder"]) |
| 104 | + self.assertEqual(score, 0) |
| 105 | + |
| 106 | + def test_identical_pair_is_flagged(self): |
| 107 | + same = "The answer is forty two, worked out by adding the two numbers together." |
| 108 | + score, flags = check({"prompt": "Pick the better reply please", "chosen": same, "rejected": same}) |
| 109 | + self.assertEqual(flags, ["pair_identical"]) |
| 110 | + self.assertEqual(score, 0) |
| 111 | + |
| 112 | + def test_echo_answer(self): |
| 113 | + text = "Explain how photosynthesis works in green plants" |
| 114 | + score, flags = check({"prompt": text, "completion": text}) |
| 115 | + self.assertEqual(flags, ["echo"]) |
| 116 | + self.assertEqual(score, 40) |
| 117 | + |
| 118 | + def test_tiny_response(self): |
| 119 | + score, flags = check({"prompt": "Is the sky blue on a clear day?", "completion": "Yes."}) |
| 120 | + self.assertEqual(flags, ["tiny_response"]) |
| 121 | + self.assertEqual(score, 60) |
| 122 | + |
| 123 | + def test_short_prompt(self): |
| 124 | + score, flags = check({ |
| 125 | + "prompt": "Hi", |
| 126 | + "completion": "Hello there, it is very nice to meet you today, how can I help?", |
| 127 | + }) |
| 128 | + self.assertEqual(flags, ["short_prompt"]) |
| 129 | + self.assertEqual(score, 80) |
| 130 | + |
| 131 | + def test_pii_in_prompt(self): |
| 132 | + score, flags = check({ |
| 133 | + "prompt": "Please email jane.doe@example.com about the quarterly report", |
| 134 | + "completion": "I will send the quarterly report summary over to that address today.", |
| 135 | + }) |
| 136 | + self.assertEqual(flags, ["pii"]) |
| 137 | + self.assertEqual(score, 85) |
| 138 | + |
| 139 | + def test_ai_boilerplate(self): |
| 140 | + score, flags = check({ |
| 141 | + "prompt": "Write a short poem about the sea", |
| 142 | + "completion": "As an AI language model, I cannot help with that request at all.", |
| 143 | + }) |
| 144 | + self.assertEqual(flags, ["ai_boilerplate"]) |
| 145 | + self.assertEqual(score, 65) |
| 146 | + |
| 147 | + def test_unclosed_code_fence_in_text(self): |
| 148 | + score, flags = check({"text": "Here is a longer piece of sample code for you:\n```python\nprint('hello there world')\n"}) |
| 149 | + self.assertEqual(flags, ["code_fence"]) |
| 150 | + self.assertEqual(score, 80) |
| 151 | + |
| 152 | + def test_repetitive_text(self): |
| 153 | + score, flags = check({"text": "the cat sat " * 20}) |
| 154 | + self.assertEqual(flags, ["repetition"]) |
| 155 | + self.assertEqual(score, 30) |
| 156 | + |
| 157 | + def test_mojibake_text(self): |
| 158 | + score, flags = check({"text": "The café near the station serves lunch every day of the week."}) |
| 159 | + self.assertEqual(flags, ["mojibake"]) |
| 160 | + self.assertEqual(score, 40) |
| 161 | + |
| 162 | + def test_language_mismatch(self): |
| 163 | + score, flags = check({ |
| 164 | + "prompt": "Please describe your home town in some detail for me", |
| 165 | + "completion": "Мой родной город находится на берегу большой реки, там много парков, старых улиц и красивых мостов через воду.", |
| 166 | + }) |
| 167 | + self.assertEqual(flags, ["language_mismatch"]) |
| 168 | + self.assertEqual(score, 60) |
| 169 | + |
| 170 | + def test_truncated_answer(self): |
| 171 | + resp = ("The history of the railway began in the early nineteenth century, when steam " |
| 172 | + "locomotives first carried passengers between industrial towns across the " |
| 173 | + "north of England and then spread quickly over the") |
| 174 | + self.assertGreaterEqual(len(resp), 120) |
| 175 | + score, flags = check({"prompt": "Tell me about the history of railways", "completion": resp}) |
| 176 | + self.assertEqual(flags, ["truncated"]) |
| 177 | + self.assertEqual(score, 70) |
| 178 | + |
| 179 | + def test_scores_stay_within_bounds(self): |
| 180 | + samples = [ |
| 181 | + {"text": ""}, |
| 182 | + {"text": "TODO"}, |
| 183 | + {"prompt": "q", "completion": "a"}, |
| 184 | + {"prompt": "What is two plus two?", "completion": "TODO"}, |
| 185 | + {"text": "the cat sat " * 20}, |
| 186 | + ] |
| 187 | + for ex in samples: |
| 188 | + score, flags = check(ex) |
| 189 | + self.assertGreaterEqual(score, 0) |
| 190 | + self.assertLessEqual(score, 100) |
| 191 | + self.assertEqual(score, max(0, 100 - sum(data_quality.FLAGS[f][0] for f in flags))) |
| 192 | + |
| 193 | + |
| 194 | +class FlagsTableTest(unittest.TestCase): |
| 195 | + def test_flags_are_well_formed(self): |
| 196 | + for name, entry in data_quality.FLAGS.items(): |
| 197 | + self.assertEqual(len(entry), 3, name) |
| 198 | + penalty, label, drop = entry |
| 199 | + self.assertGreaterEqual(penalty, 1, name) |
| 200 | + self.assertLessEqual(penalty, 100, name) |
| 201 | + self.assertTrue(label, name) |
| 202 | + self.assertIsInstance(drop, bool, name) |
| 203 | + |
| 204 | + def test_blockers_drop_by_default(self): |
| 205 | + for name in ("empty", "placeholder", "pair_identical"): |
| 206 | + penalty, _label, drop = data_quality.FLAGS[name] |
| 207 | + self.assertEqual(penalty, 100, name) |
| 208 | + self.assertTrue(drop, name) |
| 209 | + |
| 210 | + |
| 211 | +class GroundedTest(unittest.TestCase): |
| 212 | + CHUNK = "The sky appears blue on a clear day because sunlight is scattered by air molecules." |
| 213 | + QUESTION = "Why does the sky look blue on a clear day?" |
| 214 | + |
| 215 | + def test_supported_answer_is_grounded(self): |
| 216 | + self.assertTrue(grounded(self.QUESTION, "The sky appears blue because sunlight is scattered by air molecules.", self.CHUNK)) |
| 217 | + |
| 218 | + def test_unrelated_answer_is_not_grounded(self): |
| 219 | + self.assertFalse(grounded(self.QUESTION, "To cook pasta, boil salted water and stir the noodles often.", self.CHUNK)) |
| 220 | + |
| 221 | + def test_invented_number_is_not_grounded(self): |
| 222 | + chunk = "The village sits in a valley and is home to a few hundred families and a small school." |
| 223 | + self.assertFalse(grounded("How many people live in the village?", "The village has exactly 42000 residents living in it.", chunk)) |
| 224 | + |
| 225 | + def test_answer_without_content_words_is_not_grounded(self): |
| 226 | + self.assertFalse(grounded(self.QUESTION, "It is so.", self.CHUNK)) |
| 227 | + |
| 228 | + |
| 229 | +if __name__ == "__main__": |
| 230 | + unittest.main() |
0 commit comments