Skip to content

Commit

Permalink
refactor google-diff-match-patch tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jedie committed Aug 25, 2016
1 parent 8c20ac2 commit c4018f6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
5 changes: 2 additions & 3 deletions reversion_compare/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@
# http://code.google.com/p/google-diff-match-patch/
from diff_match_patch import diff_match_patch
except ImportError:
google_diff_match_patch = False
dmp = None
else:
google_diff_match_patch = True
dmp = diff_match_patch()


Expand Down Expand Up @@ -118,7 +117,7 @@ def html_diff(value1, value2, cleanup=SEMANTIC):
"""
value1 = force_text(value1)
value2 = force_text(value2)
if google_diff_match_patch:
if dmp is not None:
# Generate the diff with google-diff-match-patch
diff = dmp.diff_main(value1, value2)
if cleanup == SEMANTIC:
Expand Down
37 changes: 21 additions & 16 deletions tests/test_simple_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

from __future__ import absolute_import, division, print_function

import unittest

from reversion import is_registered
from reversion.models import Version, Revision
from reversion_compare import helpers
Expand Down Expand Up @@ -109,22 +111,25 @@ def test_diff(self):
'<blockquote>simply change the CharField text.</blockquote>', # edit comment
)

if self.google_diff_match_patch:
# google-diff-match-patch is available
helpers.google_diff_match_patch = True
try:
self.assertContainsHtml(
response,
"""
<p><span>version </span>
<del style="background:#ffe6e6;">one</del>
<ins style="background:#e6ffe6;">two</ins>
</p>
""",
'<blockquote>simply change the CharField text.</blockquote>', # edit comment
)
finally:
helpers.google_diff_match_patch = False # revert
@unittest.skipIf(not hasattr(helpers, "diff_match_patch"), "No google-diff-match-patch available")
def test_google_diff_match_patch(self):
self.activate_google_diff_match_patch()
response = self.client.get(
"/admin/tests/simplemodel/%s/history/compare/" % self.item1.pk,
data={"version_id2": self.version_ids1[0], "version_id1": self.version_ids1[1]}
)
# debug_response(response) # from django-tools
self.assertContainsHtml(
response,
"""
<p><span>version </span>
<del style="background:#ffe6e6;">one</del>
<ins style="background:#e6ffe6;">two</ins>
</p>
""",
'<blockquote>simply change the CharField text.</blockquote>', # edit comment
)


def test_prev_next_buttons(self):
base_url = "/admin/tests/simplemodel/%s/history/compare/" % self.item2.pk
Expand Down
11 changes: 6 additions & 5 deletions tests/test_utils/test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ def setUp(self):
)

# http://code.google.com/p/google-diff-match-patch/
if helpers.google_diff_match_patch:
if hasattr(helpers, "diff_match_patch"):
# run all tests without google-diff-match-patch as default
# some tests can activate it temporary
helpers.google_diff_match_patch = False
self.google_diff_match_patch = True
else:
self.google_diff_match_patch = False
helpers.dmp = None

def activate_google_diff_match_patch(self):
assert hasattr(helpers, "diff_match_patch")
helpers.dmp = helpers.diff_match_patch()

def tearDown(self):
super(BaseTestCase, self).tearDown()
Expand Down

0 comments on commit c4018f6

Please sign in to comment.