File: test_diff_utils.py

package info (click to toggle)
odoo 18.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 878,716 kB
  • sloc: javascript: 927,937; python: 685,670; xml: 388,524; sh: 1,033; sql: 415; makefile: 26
file content (356 lines) | stat: -rw-r--r-- 13,557 bytes parent folder | download
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import odoo.tests

from odoo.tests.common import BaseCase
from odoo.addons.web_editor.models.diff_utils import (
    generate_patch,
    generate_comparison,
    apply_patch,
)


@odoo.tests.tagged("post_install", "-at_install", "html_history")
class TestPatchUtils(BaseCase):
    def test_new_content_add_line(self):
        initial_content = "<p>foo</p><p>baz</p>"
        new_content = "<p>foo</p><p>bar</p><p>baz</p>"

        patch = generate_patch(new_content, initial_content)
        # Even if we added content in the new_content, we expect a remove
        # operation, because the patch would be used to restore the initial
        # content from the new content.
        self.assertEqual(patch, "-@3,4")

        restored_initial_content = apply_patch(new_content, patch)
        self.assertEqual(restored_initial_content, initial_content)

        comparison = generate_comparison(new_content, initial_content)
        self.assertEqual(
            comparison, "<p>foo</p><p><removed>bar</removed></p><p>baz</p>"
        )

    def test_new_content_remove_line(self):
        initial_content = "<p>foo</p><p>bar</p><p>baz</p>"
        new_content = "<p>foo</p><p>baz</p>"

        patch = generate_patch(new_content, initial_content)
        self.assertEqual(patch, "+@2:<p>bar</p>")

        restored_initial_content = apply_patch(new_content, patch)
        self.assertEqual(restored_initial_content, initial_content)

        comparison = generate_comparison(new_content, initial_content)
        self.assertEqual(
            comparison, "<p>foo</p><p><added>bar</added></p><p>baz</p>"
        )

    def test_new_content_replace_line(self):
        initial_content = "<p>foo</p><p>bar</p><p>bor</p><p>bir</p><p>baz</p>"
        new_content = "<p>foo</p><p>buz</p><p>baz</p>"

        patch = generate_patch(new_content, initial_content)
        self.assertEqual(patch, "R@3:<p>bar</p><p>bor</p><p>bir")

        restored_initial_content = apply_patch(new_content, patch)
        self.assertEqual(restored_initial_content, initial_content)

        comparison = generate_comparison(new_content, initial_content)
        self.assertEqual(
            comparison,
            "<p>foo</p>"
            "<p><added>bar</added></p>"
            "<p><added>bor</added></p>"
            "<p><added>bir</added><removed>buz</removed></p>"
            "<p>baz</p>",
        )

    def test_new_content_is_falsy(self):
        initial_content = "<p>foo</p><p>bar</p>"
        new_content = ""

        patch = generate_patch(new_content, initial_content)
        self.assertEqual(patch, "+@0:<p>foo</p><p>bar</p>")

        restored_initial_content = apply_patch(new_content, patch)
        self.assertEqual(restored_initial_content, initial_content)

        comparison = generate_comparison(new_content, initial_content)
        self.assertEqual(
            comparison, "<p><added>foo</added></p><p><added>bar</added></p>"
        )

    def test_new_content_is_equal(self):
        initial_content = "<p>foo</p><p>bar</p>"
        new_content = "<p>foo</p><p>bar</p>"

        patch = generate_patch(new_content, initial_content)
        self.assertEqual(patch, "")
        restored_initial_content = apply_patch(new_content, patch)
        self.assertEqual(restored_initial_content, initial_content)

        initial_content = ""
        new_content = ""

        patch = generate_patch(new_content, initial_content)
        self.assertEqual(patch, "")
        restored_initial_content = apply_patch(new_content, patch)
        self.assertEqual(restored_initial_content, initial_content)

    def test_new_content_multiple_operation(self):
        initial_content = "<p>foo</p><p>bar</p><p>baz</p><p>buz</p><p>boz</p>"
        new_content = (
            "<p>foo</p><div>new1<b>new2</b>new3</div>"
            "<p>bar</p><p>baz</p><p>boz</p><p>end</p>"
        )

        patch = generate_patch(new_content, initial_content)
        self.assertEqual(
            patch,
            """-@3,6
+@10:<p>buz</p>
-@13,14""",
        )

        restored_initial_content = apply_patch(new_content, patch)
        self.assertEqual(restored_initial_content, initial_content)

        comparison = generate_comparison(new_content, initial_content)
        self.assertEqual(
            comparison,
            "<p>foo</p>"
            "<div><removed>new1</removed>"
            "<b><removed>new2</removed></b>"
            "<removed>new3</removed></div>"
            "<p>bar</p><p>baz</p><p><added>buz</added></p>"
            "<p>boz</p><p><removed>end</removed></p>",
        )

    def test_multiple_revision(self):
        contents = [
            "<p>foo</p><p>bar</p>",
            "<p>foo</p>",
            "<p>f<b>u</b>i</p><p>baz</p>",
            "<p>fi</p><p>boz</p>",
            "<div><h1>something</h1><p>completely different</p></div>",
            "<p>foo</p><p>boz</p><p>buz</p>",
            "<p>buz</p>",
        ]
        patches = []
        for i in range(len(contents) - 1):
            patches.append(generate_patch(contents[i + 1], contents[i]))

        patches.reverse()
        reconstruct_content = contents[-1]
        for patch in patches:
            reconstruct_content = apply_patch(reconstruct_content, patch)

        self.assertEqual(reconstruct_content, contents[0])

    def test_replace_tag(self):
        initial_content = "<blockquote>foo</blockquote>"
        new_content = "<code>foo</code>"

        comparison = generate_comparison(new_content, initial_content)
        self.assertEqual(
            comparison,
            "<blockquote><added>foo</added></blockquote>"
            "<code><removed>foo</removed></code>",
        )

    def test_replace_complex(self):
        initial_content = (
            "<blockquote>foo</blockquote>"
            "<blockquote>bar</blockquote>"
            "<blockquote>baz</blockquote>"
            "<p>---<span>***</span>---</p>"
            "<blockquote>only content change</blockquote>"
            "<p>+++<span>~~~</span>+++</p>"
            "<blockquote>content and tag change</blockquote>"
            "<p>???<span>===</span>???</p>"
            "<blockquote>111</blockquote>"
            "<blockquote>222</blockquote>"
            "<blockquote>333</blockquote>"
        )
        new_content = (
            "<code>foo</code>"
            "<code>bar</code>"
            "<code>baz</code>"
            "<p>---<span>***</span>---</p>"
            "<blockquote>lorem ipsum</blockquote>"
            "<p>+++<span>~~~</span>+++</p>"
            "<code>dolor sit amet</code>"
            "<p>???<span>===</span>???</p>"
            "<blockquote>aaa</blockquote>"
            "<blockquote>bbb</blockquote>"
            "<blockquote>ccc</blockquote>"
        )

        comparison = generate_comparison(new_content, initial_content)
        self.assertEqual(
            comparison,
            "<blockquote><added>foo</added></blockquote>"
            "<blockquote><added>bar</added></blockquote>"
            "<blockquote><added>baz</added></blockquote>"
            "<code><removed>foo</removed></code>"
            "<code><removed>bar</removed></code>"
            "<code><removed>baz</removed></code>"
            "<p>---<span>***</span>---</p>"
            "<blockquote><added>only content change</added>"
            "<removed>lorem ipsum</removed></blockquote>"
            "<p>+++<span>~~~</span>+++</p>"
            "<blockquote><added>content and tag change</added></blockquote>"
            "<code><removed>dolor sit amet</removed></code>"
            "<p>???<span>===</span>???</p>"
            "<blockquote><added>111</added><removed>aaa</removed></blockquote>"
            "<blockquote><added>222</added><removed>bbb</removed></blockquote>"
            "<blockquote><added>333</added><removed>ccc</removed></blockquote>",
        )

    def test_replace_tag_multiline(self):
        initial_content = (
            "<blockquote>foo</blockquote>"
            "<code>bar lorem ipsum dolor</code>"
            "<blockquote>baz</blockquote>"
        )
        new_content = (
            "<code>foo</code>"
            "<blockquote>bar lorem ipsum dolor</blockquote>"
            "<code>baz</code>"
        )

        comparison = generate_comparison(new_content, initial_content)
        self.assertEqual(
            comparison,
            "<blockquote><added>foo</added></blockquote>"
            "<code><added>bar lorem ipsum dolor</added>"
            "<removed>foo</removed></code>"
            "<blockquote><added>baz</added>"
            "<removed>bar lorem ipsum dolor</removed></blockquote>"
            "<code><removed>baz</removed></code>",
        )

    def test_replace_nested_divs(self):
        initial_content = "<div class='A1'><div class='A2'><b>A</b></div></div>"
        new_content = "<div class='B1'><div class='B2'><i>B</i></div></div>"

        comparison = generate_comparison(new_content, initial_content)
        # This is a trade-off because of the limitation of the current
        # comparison system :
        # We can't easily generate comparison when only the tag parameters
        # changes, because the diff system will not contain the closing tags
        # in this case.
        #
        # This is why we choose to have the comparison below instead of :
        # <div class='A1'><div class='A2'>
        #     <b><removed>A</removed></b>
        # </div></div>
        # <div class='B1'><div class='B2'>
        #     <i><added>B</added></i>
        # </div></div>
        #
        # If we need to improve this in the future, we would probably have to
        # change drastically the comparison system to add a way to parse HTML.
        self.assertEqual(
            comparison,
            "<div class='A1'><div class='A2'>"
            "<b><added>A</added></b>"
            "<i><removed>B</removed></i>"
            "</div></div>",
        )

    def test_same_tag_replace_fixer(self):
        initial_content = "<div><p><b>A</b><b>B</b></p></div>"
        new_content = "<div>X<p><b>B</b></p></div>"

        comparison = generate_comparison(new_content, initial_content)
        # This is a trade-off, see explanation in test_replace_nested_divs.
        self.assertEqual(
            comparison,
            "<div><removed>X</removed>"
            "<p><b><added>A</added></b>"
            "<b>B</b></p></div>",
        )

    def test_simple_removal(self):
        initial_content = "<div><p>A</p></div>"
        new_content = "<div>X<p>A</p></div>"

        comparison = generate_comparison(new_content, initial_content)
        # This is a trade-off, see explanation in test_replace_nested_divs.
        self.assertEqual(
            comparison,
            "<div><removed>X</removed><p>A</p></div>",
        )

    def test_simple_addition(self):
        initial_content = "<div>X<p>A</p></div>"
        new_content = "<div><p>A</p></div>"

        comparison = generate_comparison(new_content, initial_content)
        # This is a trade-off, see explanation in test_replace_nested_divs.
        self.assertEqual(
            comparison,
            "<div><added>X</added><p>A</p></div>",
        )

    def test_replace_just_class(self):
        initial_content = "<div class='A1'>A</div>"
        new_content = "<div class='B1'>A</div>"

        comparison = generate_comparison(new_content, initial_content)
        # This is a trade-off, see explanation in test_replace_nested_divs.
        self.assertEqual(
            comparison,
            "<div class='A1'>A</div>",
        )

    def test_replace_twice_just_class(self):
        initial_content = (
            "<div class='A1'>A</div><p>abc</p><div class='D1'>D</div>"
        )
        new_content = "<div class='B1'>A</div><p>abc</p><div class='E1'>D</div>"

        comparison = generate_comparison(new_content, initial_content)
        # This is a trade-off, see explanation in test_replace_nested_divs.
        self.assertEqual(
            comparison,
            "<div class='A1'>A</div><p>abc</p><div class='D1'>D</div>",
        )

    def test_replace_with_just_class(self):
        initial_content = "<p>abc</p><div class='A1'>A</div>"
        new_content = "<p>def</p><div class='B1'>A</div>"

        comparison = generate_comparison(new_content, initial_content)
        # This is a trade-off, see explanation in test_replace_nested_divs.
        self.assertEqual(
            comparison,
            "<p><added>abc</added><removed>def</removed></p>"
            "<div class='A1'>A</div>",
        )

    def test_replace_class_and_content(self):
        initial_content = "<div class='A1'>A</div>"
        new_content = "<div class='B1'>B</div>"

        comparison = generate_comparison(new_content, initial_content)
        # This is a trade-off, see explanation in test_replace_nested_divs.
        self.assertEqual(
            comparison,
            "<div class='A1'><added>A</added><removed>B</removed></div>",
        )

    def test_replace_class_and_deep_content(self):
        initial_content = "<div class='A1'><p><i>A</i></p></div>"
        new_content = "<div class='B1'><p><i>B</i></p></div>"

        comparison = generate_comparison(new_content, initial_content)
        # This is a trade-off, see explanation in test_replace_nested_divs.
        self.assertEqual(
            comparison,
            "<div class='A1'><p><i>"
            "<added>A</added><removed>B</removed>"
            "</i></p></div>",
        )