|
| 1 | +import unittest |
| 2 | +import pandas as pd |
| 3 | +from io import BytesIO |
| 4 | +from app.utils.file_parser import parse_whatsapp_chat |
| 5 | + |
| 6 | +class TestParseWhatsappChat(unittest.TestCase): |
| 7 | + |
| 8 | + def test_parse_standard_format(self): |
| 9 | + chat_data = b"[12/01/23, 03:45:12 p.m.] John: Hello!\n[12/01/23, 03:46:12 p.m.] Jane: Hi, how are you?" |
| 10 | + df = parse_whatsapp_chat(BytesIO(chat_data)) |
| 11 | + self.assertEqual(len(df), 2) |
| 12 | + self.assertEqual(df.iloc[0]['Sender'], 'John') |
| 13 | + self.assertEqual(df.iloc[1]['Sender'], 'Jane') |
| 14 | + |
| 15 | + def test_parse_non_standard_format(self): |
| 16 | + chat_data = b"[01/12/23, 03:45:12 p.m.] John: Hello!\n[01/12/23, 03:46:12 p.m.] Jane: Hi, how are you?" |
| 17 | + df = parse_whatsapp_chat(BytesIO(chat_data)) |
| 18 | + self.assertEqual(len(df), 2) |
| 19 | + self.assertEqual(df.iloc[0]['Sender'], 'John') |
| 20 | + self.assertEqual(df.iloc[1]['Sender'], 'Jane') |
| 21 | + |
| 22 | + def test_parse_day_month_format(self): |
| 23 | + chat_data = b"[12/01/23, 03:45:12 p.m.] John: Hello!\n[12/01/23, 03:46:12 p.m.] Jane: Hi, how are you?" |
| 24 | + df = parse_whatsapp_chat(BytesIO(chat_data)) |
| 25 | + self.assertEqual(len(df), 2) |
| 26 | + self.assertEqual(df.iloc[0]['Sender'], 'John') |
| 27 | + self.assertEqual(df.iloc[1]['Sender'], 'Jane') |
| 28 | + |
| 29 | + def test_parse_with_media_omitted(self): |
| 30 | + chat_data = b"[12/01/23, 03:45:12 p.m.] John: audio omitted\n[12/01/23, 03:46:12 p.m.] Jane: Hi, how are you?" |
| 31 | + df = parse_whatsapp_chat(BytesIO(chat_data)) |
| 32 | + self.assertEqual(len(df), 1) # One message should be skipped |
| 33 | + self.assertEqual(df.iloc[0]['Sender'], 'Jane') |
| 34 | + |
| 35 | + def test_invalid_date_format(self): |
| 36 | + chat_data = b"[99/99/99, 03:45:12 p.m.] John: Hello!" |
| 37 | + with self.assertRaises(ValueError): |
| 38 | + parse_whatsapp_chat(BytesIO(chat_data)) |
| 39 | + |
| 40 | +if __name__ == '__main__': |
| 41 | + unittest.main() |
0 commit comments