Sfoglia il codice sorgente

Fix append in BodyMailsParts when follow is true and list is empty + tests

Guénaël Muller 6 anni fa
parent
commit
902eb78d50

+ 12 - 7
tracim/tracim/lib/email_processing/models.py Vedi File

@@ -48,13 +48,17 @@ class BodyMailParts(object):
48 48
         BodyMailParts._check_value(value)
49 49
         self._append(value)
50 50
 
51
-    def _append(self, value) -> None:
52
-        same_type_as_last = len(self._list) > 0 and \
53
-                            self._list[-1].part_type == value.part_type
54
-        if same_type_as_last or self.follow:
55
-            self._list[-1].text += value.text
56
-        else:
51
+    def _append(self, value, follow=None) -> None:
52
+        if follow is None:
53
+            follow = self.follow
54
+
55
+        if len(self._list) < 1:
57 56
             self._list.append(value)
57
+        else:
58
+            if self._list[-1].part_type == value.part_type or follow:
59
+                self._list[-1].text += value.text
60
+            else:
61
+                self._list.append(value)
58 62
 
59 63
     @classmethod
60 64
     def _check_value(cls, value) -> None:
@@ -69,9 +73,10 @@ class BodyMailParts(object):
69 73
         """
70 74
         new_list = [x for x in self._list if x.part_type != part_type]
71 75
         self._list = []
76
+
72 77
         # INFO - G.M - 2017-11-27 - use append() to have a consistent list
73 78
         for elem in new_list:
74
-            self.append(elem)
79
+            self._append(elem, follow=False)
75 80
 
76 81
     def get_nb_part_type(self, part_type: str) -> int:
77 82
         """

+ 37 - 0
tracim/tracim/tests/library/test_email_body_parser.py Vedi File

@@ -148,6 +148,25 @@ class TestBodyMailsParts(TestStandard):
148 148
         assert mail_parts[0] == a
149 149
         assert mail_parts[1] == b
150 150
 
151
+    def test_unit__append_follow(self):
152
+        mail_parts = BodyMailParts()
153
+        mail_parts.follow = True
154
+        a = BodyMailPart('a', BodyMailPartType.Main)
155
+        mail_parts._append(a)
156
+        b = BodyMailPart('b', BodyMailPartType.Quote)
157
+        mail_parts._append(b)
158
+        assert len(mail_parts) == 1
159
+        assert mail_parts[0].part_type == BodyMailPartType.Main
160
+        assert mail_parts[0].text == 'ab'
161
+
162
+    def test_unit__append_dont_follow_when_first(self):
163
+        mail_parts = BodyMailParts()
164
+        a = BodyMailPart('a', BodyMailPartType.Main)
165
+        mail_parts._append(a,follow=True)
166
+        assert len(mail_parts) == 1
167
+        assert mail_parts[0].part_type == BodyMailPartType.Main
168
+        assert mail_parts[0].text == 'a'
169
+
151 170
     @raises(TypeError)
152 171
     def test_unit__check_value__type_error(self):
153 172
         mail_parts = BodyMailParts()
@@ -174,6 +193,23 @@ class TestBodyMailsParts(TestStandard):
174 193
         assert mail_parts[1].text == 'c'
175 194
         assert mail_parts[1].part_type == BodyMailPartType.Signature
176 195
 
196
+    def test_unit__drop_part_type_verify_no_follow_incidence(self):
197
+        mail_parts = BodyMailParts()
198
+        a = BodyMailPart('a', BodyMailPartType.Main)
199
+        mail_parts._list.append(a)
200
+        b = BodyMailPart('b', BodyMailPartType.Quote)
201
+        mail_parts._list.append(b)
202
+        c = BodyMailPart('c', BodyMailPartType.Signature)
203
+        mail_parts._list.append(c)
204
+        mail_parts.follow = True
205
+        mail_parts.drop_part_type(BodyMailPartType.Quote)
206
+        assert len(mail_parts) == 2
207
+        assert mail_parts[0].text == 'a'
208
+        assert mail_parts[0].part_type == BodyMailPartType.Main
209
+        assert len(mail_parts) == 2
210
+        assert mail_parts[1].text == 'c'
211
+        assert mail_parts[1].part_type == BodyMailPartType.Signature
212
+
177 213
     def test_unit__drop_part_type_consistence(self):
178 214
         mail_parts = BodyMailParts()
179 215
         a = BodyMailPart('a', BodyMailPartType.Main)
@@ -187,6 +223,7 @@ class TestBodyMailsParts(TestStandard):
187 223
         assert mail_parts[0].text == 'ac'
188 224
         assert mail_parts[0].part_type == BodyMailPartType.Main
189 225
 
226
+
190 227
     def test_unit__get_nb_part_type(self):
191 228
         mail_parts = BodyMailParts()
192 229
         assert mail_parts.get_nb_part_type(BodyMailPartType.Main) == 0