Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/MojoLlama/blob/main/test_process_data.py # MojoLlama Archived Mojo/MAX quantizer and serving research repository (read-only history) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `MojoLlama / test_process_data.py` 109 lines ยท 3484 bytes Raw Blame History 1 import unittest 2 3 from process_data import ( 4 InvalidItemTypeError, 5 MissingFieldError, 6 process_data, 7 process_item, 8 ) 9 10 11 class TestProcessItem(unittest.TestCase): 12 def test_user_item(self): 13 item = {"type": "user", "id": 1, "name": "Alice"} 14 result = process_item(item) 15 self.assertEqual(result, {"id": 1, "name": "Alice", "role": "user"}) 16 17 def test_admin_item(self): 18 item = {"type": "admin", "id": 2, "name": "Bob"} 19 result = process_item(item) 20 self.assertEqual( 21 result, 22 { 23 "id": 2, 24 "name": "Bob", 25 "role": "admin", 26 "permissions": ["read", "write", "delete"], 27 }, 28 ) 29 30 def test_guest_item_with_name(self): 31 item = {"type": "guest", "id": 3, "name": "Charlie"} 32 result = process_item(item) 33 self.assertEqual(result, {"id": 3, "name": "Charlie", "role": "guest"}) 34 35 def test_guest_item_without_name(self): 36 item = {"type": "guest", "id": 4} 37 result = process_item(item) 38 self.assertEqual(result, {"id": 4, "name": "Guest", "role": "guest"}) 39 40 def test_missing_type_raises_error(self): 41 with self.assertRaises(InvalidItemTypeError): 42 process_item({"id": 1, "name": "Alice"}) 43 44 def test_unknown_type_raises_error(self): 45 with self.assertRaises(InvalidItemTypeError): 46 process_item({"type": "unknown", "id": 1, "name": "Alice"}) 47 48 def test_user_missing_id_raises_error(self): 49 with self.assertRaises(MissingFieldError): 50 process_item({"type": "user", "name": "Alice"}) 51 52 def test_user_missing_name_raises_error(self): 53 with self.assertRaises(MissingFieldError): 54 process_item({"type": "user", "id": 1}) 55 56 def test_admin_missing_id_raises_error(self): 57 with self.assertRaises(MissingFieldError): 58 process_item({"type": "admin", "name": "Bob"}) 59 60 def test_guest_missing_id_raises_error(self): 61 with self.assertRaises(MissingFieldError): 62 process_item({"type": "guest"}) 63 64 65 class TestProcessData(unittest.TestCase): 66 def test_mixed_items(self): 67 data = [ 68 {"type": "user", "id": 3, "name": "Charlie"}, 69 {"type": "admin", "id": 1, "name": "Alice"}, 70 {"type": "guest", "id": 2}, 71 ] 72 result = process_data(data) 73 self.assertEqual( 74 result, 75 [ 76 { 77 "id": 1, 78 "name": "Alice", 79 "role": "admin", 80 "permissions": ["read", "write", "delete"], 81 }, 82 {"id": 2, "name": "Guest", "role": "guest"}, 83 {"id": 3, "name": "Charlie", "role": "user"}, 84 ], 85 ) 86 87 def test_empty_list(self): 88 self.assertEqual(process_data([]), []) 89 90 def test_sorts_by_id(self): 91 data = [ 92 {"type": "user", "id": 10, "name": "Zara"}, 93 {"type": "user", "id": 1, "name": "Amy"}, 94 ] 95 result = process_data(data) 96 self.assertEqual(result[0]["id"], 1) 97 self.assertEqual(result[1]["id"], 10) 98 99 def test_invalid_item_raises_error(self): 100 data = [ 101 {"type": "user", "id": 1, "name": "Alice"}, 102 {"type": "invalid"}, 103 ] 104 with self.assertRaises(InvalidItemTypeError): 105 process_data(data) 106 107 108 if __name__ == "__main__": 109 unittest.main()