forked from automaciej/whatsapp-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhatsapp_archive_test.py
executable file
·145 lines (128 loc) · 5.85 KB
/
whatsapp_archive_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/python3
import unittest
import datetime
import re
import whatsapp_archive
INPUT_1 = ["13/01/18, 01:23 - Fake Name: line1\n", "line2\n"]
INPUT_2 = ["13/01/18, 01:23 - Fake Name: line1\n", "line2\n",
"13/01/18, 01:24 - Name Two: single line\n"]
INPUT_3 = ["13/01/18, 01:23 - Fake Name: line1\n", "line2\n",
"13/01/18, 01:24 - Fake Name: line3\n",
"13/01/18, 01:25 - Name Two: single line\n"]
INPUT_4 = ["14/04/18, 22:08 - Nesta conversa, (…)\n",
"14/04/18, 22:08 - Alguém: Olá!\n"]
# Format from a different locale setting.
INPUT_5 = ["19-02-18 17:02 - Los mensajes y llamadas en este chat ahora están "
"protegidos con cifrado de extremo a extremo. Toca para más "
"información.\n",
"19-02-18 17:02 - human1: Hola\n",
"19-02-18 17:14 - human2: como estás?\n"]
# Based on https://github.com/automatthias/whatsapp-archive/issues/1
# 12-hour format.
INPUT_6 = ["2016-06-27, 8:04:08 AM: Neil: Hi\n",]
class IdentifyMessagesTest(unittest.TestCase):
def testInputMultiline(self):
self.assertEqual(whatsapp_archive.IdentifyMessages(INPUT_1), [
(datetime.datetime(2018, 1, 13, 1, 23), 'Fake Name', 'line1\nline2'),
])
def testInputTwoMultiline(self):
self.assertEqual(whatsapp_archive.IdentifyMessages(INPUT_2), [
(datetime.datetime(2018, 1, 13, 1, 23), 'Fake Name', 'line1\nline2'),
(datetime.datetime(2018, 1, 13, 1, 24), 'Name Two', 'single line'),
])
def testTemplateData(self):
messages = whatsapp_archive.IdentifyMessages(INPUT_3)
template_data = whatsapp_archive.TemplateData(messages, "fake_filename")
self.assertEqual(template_data, {
'by_user': [
('Fake Name', [
(datetime.datetime(2018, 1, 13, 1, 23), 'Fake Name', 'line1\nline2'),
(datetime.datetime(2018, 1, 13, 1, 24), 'Fake Name', 'line3')
]),
('Name Two', [
(datetime.datetime(2018, 1, 13, 1, 25), 'Name Two', 'single line')
])
],
'input_basename': 'fake_filename',
'input_full_path': 'fake_filename'})
def testFirstLineNoColon(self):
messages = whatsapp_archive.IdentifyMessages(INPUT_4)
template_data = whatsapp_archive.TemplateData(messages, "fake_filename")
self.assertEqual(template_data, {
'by_user': [
('nobody', [
(datetime.datetime(2018, 4, 14, 22, 8), 'nobody', 'Nesta conversa, (…)'),
]),
('Alguém', [
(datetime.datetime(2018, 4, 14, 22, 8), 'Alguém', 'Olá!'),
]),
],
'input_basename': 'fake_filename',
'input_full_path': 'fake_filename'})
def testDifferentFormat(self):
self.maxDiff = None
messages = whatsapp_archive.IdentifyMessages(INPUT_5)
template_data = whatsapp_archive.TemplateData(messages, "fake_filename")
self.assertEqual(template_data, {
'by_user': [
('nobody', [
(datetime.datetime(2018, 2, 19, 17, 2),
'nobody', 'Los mensajes y llamadas en este chat ahora '
'están protegidos con cifrado de extremo a extremo. '
'Toca para más información.'),
]),
('human1', [
(datetime.datetime(2018, 2, 19, 17, 2), 'human1', 'Hola'),
]),
('human2', [
(datetime.datetime(2018, 2, 19, 17, 14), 'human2', 'como estás?'),
]),
],
'input_basename': 'fake_filename',
'input_full_path': 'fake_filename'})
def testNeil(self):
self.maxDiff = None
messages = whatsapp_archive.IdentifyMessages(INPUT_6)
template_data = whatsapp_archive.TemplateData(messages, "fake_filename")
self.assertEqual(template_data, {
'by_user': [
('Neil', [
(datetime.datetime(2016, 6, 27, 8, 4, 8),
'Neil', 'Hi'),
]),
],
'input_basename': 'fake_filename',
'input_full_path': 'fake_filename'})
def testEwoutTime(self):
INPUT = """[02-12-18 22:55:45]"""
result = re.match(whatsapp_archive.DATETIME_RE, INPUT)
self.assertIsNotNone(result)
self.assertEqual(('02-12-18', '22:55:45', None), result.groups())
def testEwoutName(self):
INPUT = """[02-12-18 22:55:45] Ewout:"""
pattern = (whatsapp_archive.DATETIME_RE + whatsapp_archive.SEPARATOR_RE
+ whatsapp_archive.NAME_RE)
result = re.match(pattern, INPUT)
self.assertIsNotNone(result)
self.assertEqual(('02-12-18', '22:55:45', None, ' ', 'Ewout'), result.groups())
def testEwout1(self):
INPUT = ["""[02-12-18 22:55:45] Ewout: Test\n""",]
self.maxDiff = None
messages = whatsapp_archive.IdentifyMessages(INPUT)
self.assertEqual(
[(datetime.datetime(2018, 12, 2, 22, 55, 45), 'Ewout', 'Test')],
messages)
def testEwout2(self):
INPUT = ["[02-12-18 22:55:45] Ewout: Test\n",
"[02-12-18 22:56:00] Ewout: Does this work?\n",
"[02-12-18 22:56:20] Ewout: Sending a message to myself\n",
]
self.maxDiff = None
messages = whatsapp_archive.IdentifyMessages(INPUT)
self.assertEqual([
(datetime.datetime(2018, 12, 2, 22, 55, 45), 'Ewout', 'Test'),
(datetime.datetime(2018, 12, 2, 22, 56), 'Ewout', 'Does this work?'),
(datetime.datetime(2018, 12, 2, 22, 56, 20), 'Ewout', 'Sending a message to myself'),
], messages)
if __name__ == '__main__':
unittest.main()