jenkins-bot has submitted this change and it was merged.
Change subject: tests: Add pywikibot.diff.html_comparator() tests
......................................................................
tests: Add pywikibot.diff.html_comparator() tests
Add tests which ensure that html_comparator() is working as expected.
Bug: T134341
Change-Id: I165d79e8d0f4c817580baf6e4fdc07ba34c171b7
---
M tests/__init__.py
A tests/data/html/diff.html
A tests/diff_tests.py
3 files changed, 133 insertions(+), 0 deletions(-)
Approvals:
John Vandenberg: Looks good to me, approved
jenkins-bot: Verified
diff --git a/tests/__init__.py b/tests/__init__.py
index e61f37c..8c7e393 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -66,6 +66,7 @@
join_images_path = create_path_func(join_data_path, 'images')
join_xml_data_path = create_path_func(join_data_path, 'xml')
+join_html_data_path = create_path_func(join_data_path, 'html')
# Find the root directory of the checkout
_pwb_py = join_root_path('pwb.py')
@@ -86,6 +87,7 @@
'tools_ip',
'xmlreader',
'textlib',
+ 'diff',
'http',
'namespace',
'dry_api',
diff --git a/tests/data/html/diff.html b/tests/data/html/diff.html
new file mode 100644
index 0000000..7b0c36a
--- /dev/null
+++ b/tests/data/html/diff.html
@@ -0,0 +1,16 @@
+<!-- Example at
https://www.mediawiki.org/wiki/API:Compare -->
+<tr>
+ <td colspan="2" class="diff-lineno">Line 1:</td>
+ <td colspan="2" class="diff-lineno">Line 1:</td>
+</tr>
+<tr>
+ <td class="diff-marker">\u2212</td>
+ <td class="diff-deletedline"><div><del class="diffchange
diffchange-inline"><small><span
class="autosigned">\u2014&nbsp;Preceding</del> [[<del
class="diffchange diffchange-inline">Wikipedia</del>:<del
class="diffchange diffchange-inline">Signatures|unsigned]]</del>
<del class="diffchange diffchange-inline">comment added by
[[User:{{{1}}}|{{{1}}}</del>]]<del class="diffchange
diffchange-inline"> ([[User talk:{{{1}}}|talk]] \u2022
[[Special:Contributions/{{{1}}}|contribs]])
{{{2|}}}</span></small><!-- Template:Unsigned
--><noinclude></del></div></td>
+ <td class="diff-marker">+</td>
+ <td class="diff-addedline"><div><ins class="diffchange
diffchange-inline">#REDIRECT</ins> [[<ins class="diffchange
diffchange-inline">Template</ins>:<ins class="diffchange
diffchange-inline">Unsigned</ins> <ins class="diffchange
diffchange-inline">IP</ins>]]</div></td>
+</tr>
+<tr>
+ <td class="diff-marker">\u2212</td>
+ <td class="diff-deletedline"><div>{{documentation}} <!--
add categories to the /doc page, not here
--></noinclude></div></td>
+ <td colspan="2" class="diff-empty"> </td>
+</tr>
diff --git a/tests/diff_tests.py b/tests/diff_tests.py
new file mode 100644
index 0000000..402ad74
--- /dev/null
+++ b/tests/diff_tests.py
@@ -0,0 +1,115 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+"""Test diff module."""
+#
+# (C) Pywikibot team, 2016
+#
+# Distributed under the terms of the MIT license.
+from __future__ import absolute_import, unicode_literals
+
+__version__ = '$Id$'
+
+import sys
+
+from pywikibot.diff import html_comparator
+from pywikibot.tools import PY2
+
+from tests import join_html_data_path
+from tests.aspects import TestCase, require_modules, unittest
+
+if sys.version_info[0] > 2:
+ from unittest.mock import patch
+else:
+ from mock import patch
+
+
+@require_modules('bs4')
+class TestDryHTMLComparator(TestCase):
+
+ """Test html_comparator method with given strings as test
cases."""
+
+ net = False
+
+ def test_added_context(self):
+ """Test html_comparator's detection of
added-context."""
+ output = html_comparator('''
+<tr>
+ <td class="diff-addedline">line 1a</td>
+ <td class="diff-addedline">line \n2a</td>
+</tr>
+<tr>
+ <td class="diff-addedline"><span>line 1b</span></td>
+ <td class="diff-addedline">line
2b<i><span></i></span></td>
+</tr>''')
+ self.assertEqual(output['added-context'],
+ ['line 1a', 'line \n2a', 'line 1b',
'line 2b'])
+
+ def test_deleted_context(self):
+ """Test html_comparator's detection of
deleted-context."""
+ output = html_comparator('''
+<tr>
+ <td class="diff-deletedline">line 1a</td>
+ <td class="diff-deletedline">line \n2a</td>
+</tr>
+<tr>
+ <td class="diff-deletedline"><span>line
1b</span></td>
+ <td class="diff-deletedline">line
2b<i><span></i></span></td>
+</tr>''')
+ self.assertEqual(output['deleted-context'],
+ ['line 1a', 'line \n2a', 'line 1b',
'line 2b'])
+
+ def test_run(self):
+ """Test html_comparator using examples given in mw-api
docs."""
+ with open(join_html_data_path('diff.html')) as filed:
+ diff_html = filed.read()
+ output = html_comparator(diff_html)
+ self.assertEqual(
+ output,
+ {'added-context': ['#REDIRECT [[Template:Unsigned IP]]'],
+ 'deleted-context': [
+ '<small><span
class="autosigned">\\u2014 Preceding '
+ '[[Wikipedia:Signatures|unsigned]] comment added by '
+ '[[User:{{{1}}}|{{{1}}}]] ([[User talk:{{{1}}}|talk]] \\u2022 '
+ '[[Special:Contributions/{{{1}}}|contribs]])
{{{2|}}}</span>'
+ '</small><!-- Template:Unsigned
--><noinclude>',
+ '{{documentation}} <!-- add categories to the /doc page, '
+ 'not here --></noinclude>']})
+
+
+@require_modules('bs4')
+class TestHTMLComparator(TestCase):
+
+ """Test html_comparator using api.php in en:wiki."""
+
+ family = 'wikipedia'
+ code = 'en'
+
+ def test_wikipedia_rev_139992(self):
+ """Test html_comparator with revision 139992 in
en:wikipedia."""
+ site = self.get_site()
+ diff_html = site.compare(139992, 139993)
+ output = html_comparator(diff_html)
+ self.assertEqual(len(output['added-context']), 1)
+ self.assertEqual(len(output['deleted-context']), 1)
+
+
+(a)patch('{0}.__import__'.format('__builtin__' if PY2 else
'builtins'),
+ side_effect=ImportError)
+class TestNoBeautifulSoup(TestCase):
+
+ """Test functions when BeautifulSoup is not
installes."""
+
+ net = False
+
+ def test_html_comparator(self, mocked_import):
+ """Test html_comparator when bs4 not installed."""
+ self.assertRaises(ImportError, html_comparator, '')
+ mocked_import.assert_called_once()
+ self.assertIn('bs4', mocked_import.call_args[0])
+
+
+if __name__ == '__main__':
+ try:
+ unittest.main()
+ except SystemExit:
+ pass
--
To view, visit
https://gerrit.wikimedia.org/r/286791
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I165d79e8d0f4c817580baf6e4fdc07ba34c171b7
Gerrit-PatchSet: 5
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: AbdealiJK <abdealikothari(a)gmail.com>
Gerrit-Reviewer: AbdealiJK <abdealikothari(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: jenkins-bot <>