Skip to content

Commit 40eb3fb

Browse files
committed
be more lenient when comparing string results
1 parent a068493 commit 40eb3fb

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

test/cmp_helper.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class StringFragmentCmpHelper:
2+
def compareStringFragments(self, s, fragments):
3+
"""Compare a string agains a list of fragments where a tuple denotes a
4+
set of alternatives
5+
"""
6+
pos = 0
7+
for frag in fragments:
8+
if type(frag) != tuple:
9+
self.assertEqual(s[pos:pos + len(frag)], frag)
10+
pos += len(frag)
11+
else:
12+
found = False
13+
for alt in frag:
14+
if s[pos:pos + len(alt)] == alt:
15+
pos += len(alt)
16+
found = True
17+
break
18+
19+
if not found:
20+
l = max(map(len, frag))
21+
raise AssertionError('%s != %s' %
22+
(repr(s[pos:pos + l]), str(frag)))
23+
self.assertEqual(s[pos:], '')

test/test_cgi.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
from . import db_test_base
2929
from .db_test_base import FormTestParent, setupTracker, FileUpload
30+
from .cmp_helper import StringFragmentCmpHelper
3031

3132
class FileList:
3233
def __init__(self, name, *files):
@@ -71,7 +72,7 @@ def testAddMessageNoEscape(self):
7172
self.assertEqual(cm([],'<i>x</i>\n<b>x</b>',False),
7273
['<i>x</i><br />\n<b>x</b>'])
7374

74-
class FormTestCase(FormTestParent, unittest.TestCase):
75+
class FormTestCase(FormTestParent, StringFragmentCmpHelper, unittest.TestCase):
7576

7677
def setUp(self):
7778
FormTestParent.setUp(self)
@@ -1066,9 +1067,19 @@ def wh(s):
10661067
# remove the X-REQUESTED-WITH header and get an xmlrpc fault returned
10671068
del(cl.env['HTTP_X-REQUESTED-WITH'])
10681069
cl.handle_xmlrpc()
1069-
output="<?xml version='1.0'?>\n<methodResponse>\n<fault>\n<value><struct>\n<member>\n<name>faultCode</name>\n<value><int>1</int></value>\n</member>\n<member>\n<name>faultString</name>\n<value><string>&lt;class 'roundup.exceptions.UsageError'&gt;:Required Header Missing</string></value>\n</member>\n</struct></value>\n</fault>\n</methodResponse>\n"
1070+
frag_faultCode = "<member>\n<name>faultCode</name>\n<value><int>1</int></value>\n</member>\n"
1071+
frag_faultString = "<member>\n<name>faultString</name>\n<value><string>&lt;class 'roundup.exceptions.UsageError'&gt;:Required Header Missing</string></value>\n</member>\n"
1072+
output_fragments = ["<?xml version='1.0'?>\n",
1073+
"<methodResponse>\n",
1074+
"<fault>\n",
1075+
"<value><struct>\n",
1076+
(frag_faultCode + frag_faultString,
1077+
frag_faultString + frag_faultCode),
1078+
"</struct></value>\n",
1079+
"</fault>\n",
1080+
"</methodResponse>\n"]
10701081
print(out[0])
1071-
self.assertEqual(output,out[0])
1082+
self.compareStringFragments(out[0], output_fragments)
10721083
del(out[0])
10731084

10741085
# change config to not require X-REQUESTED-WITH header

test/test_mailgw.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
#import db_test_base
4444
from . import memorydb
45+
from .cmp_helper import StringFragmentCmpHelper
4546

4647
def expectedFailure(method):
4748
""" For marking a failing test.
@@ -293,7 +294,7 @@ def testEmptyMessage(self):
293294
self.assertEqual(self.db.issue.get(nodeid, 'tx_Source'), 'email')
294295

295296

296-
class MailgwTestCase(MailgwTestAbstractBase, unittest.TestCase):
297+
class MailgwTestCase(MailgwTestAbstractBase, StringFragmentCmpHelper, unittest.TestCase):
297298

298299
def testTextHtmlMessage(self):
299300
html_message='''Content-Type: text/html;
@@ -351,12 +352,14 @@ def testTextHtmlMessage(self):
351352
</div>
352353
</body>
353354
'''
355+
text_fragments = ['Roundup\n Home\nDownload\nDocs\nRoundup Features\nInstalling Roundup\nUpgrading to newer versions of Roundup\nRoundup FAQ\nUser Guide\nCustomising Roundup\nAdministration Guide\nPrerequisites\n\nRoundup requires Python 2.6 or newer (but not Python 3) with a functioning\nanydbm module. Download the latest version from http://www.python.org/.\nIt is highly recommended that users install the latest patch version\nof python as these contain many fixes to serious bugs.\n\nSome variants of Linux will need an additional ', ('python dev', u2s(u'\u201cpython dev\u201d')), ' package\ninstalled for Roundup installation to work. Debian and derivatives, are\nknown to require this.\n\nIf you', (u2s(u'\u2019'), ''), 're on windows, you will either need to be using the ActiveState python\ndistribution (at http://www.activestate.com/Products/ActivePython/), or you', (u2s(u'\u2019'), ''), 'll\nhave to install the win32all package separately (get it from\nhttp://starship.python.net/crew/mhammond/win32/).']
354356

355357
self.db.config.MAILGW_CONVERT_HTMLTOTEXT = "dehtml"
356358
nodeid = self._handle_mail(html_message)
357359
assert not os.path.exists(SENDMAILDEBUG)
358360
msgid = self.db.issue.get(nodeid, 'messages')[0]
359-
self.assertEqual(self.db.msg.get(msgid, 'content'), '''Roundup\n Home\nDownload\nDocs\nRoundup Features\nInstalling Roundup\nUpgrading to newer versions of Roundup\nRoundup FAQ\nUser Guide\nCustomising Roundup\nAdministration Guide\nPrerequisites\n\nRoundup requires Python 2.6 or newer (but not Python 3) with a functioning\nanydbm module. Download the latest version from http://www.python.org/.\nIt is highly recommended that users install the latest patch version\nof python as these contain many fixes to serious bugs.\n\nSome variants of Linux will need an additional python dev package\ninstalled for Roundup installation to work. Debian and derivatives, are\nknown to require this.\n\nIf youre on windows, you will either need to be using the ActiveState python\ndistribution (at http://www.activestate.com/Products/ActivePython/), or youll\nhave to install the win32all package separately (get it from\nhttp://starship.python.net/crew/mhammond/win32/).''')
361+
self.compareStringFragments(self.db.msg.get(msgid, 'content'),
362+
text_fragments)
360363

361364
self.db.config.MAILGW_CONVERT_HTMLTOTEXT = "none"
362365
self.assertRaises(MailUsageError, self._handle_mail, html_message)
@@ -1278,7 +1281,7 @@ def testMultipartRFC822(self):
12781281
'''%html_doc
12791282

12801283
def testMultipartTextifyHTML(self):
1281-
mycontent='''Roundup\n Home\nDownload\nDocs\nRoundup Features\nInstalling Roundup\nUpgrading to newer versions of Roundup\nRoundup FAQ\nUser Guide\nCustomising Roundup\nAdministration Guide\nPrerequisites\n\nRoundup requires Python 2.5 or newer (but not Python 3) with a functioning\nanydbm module. Download the latest version from http://www.python.org/.\nIt is highly recommended that users install the latest patch version\nof python as these contain many fixes to serious bugs.\n\nSome variants of Linux will need an additional python dev package\ninstalled for Roundup installation to work. Debian and derivatives, are\nknown to require this.\n\nIf youre on windows, you will either need to be using the ActiveState python\ndistribution (at http://www.activestate.com/Products/ActivePython/), or youll\nhave to install the win32all package separately (get it from\nhttp://starship.python.net/crew/mhammond/win32/).\n\numlaut'''
1284+
text_fragments = ['Roundup\n Home\nDownload\nDocs\nRoundup Features\nInstalling Roundup\nUpgrading to newer versions of Roundup\nRoundup FAQ\nUser Guide\nCustomising Roundup\nAdministration Guide\nPrerequisites\n\nRoundup requires Python 2.5 or newer (but not Python 3) with a functioning\nanydbm module. Download the latest version from http://www.python.org/.\nIt is highly recommended that users install the latest patch version\nof python as these contain many fixes to serious bugs.\n\nSome variants of Linux will need an additional ', ('python dev', u2s(u'\u201cpython dev\u201d')), ' package\ninstalled for Roundup installation to work. Debian and derivatives, are\nknown to require this.\n\nIf you', (u2s(u'\u2019'), ''), 're on windows, you will either need to be using the ActiveState python\ndistribution (at http://www.activestate.com/Products/ActivePython/), or you', (u2s(u'\u2019'), ''), 'll\nhave to install the win32all package separately (get it from\nhttp://starship.python.net/crew/mhammond/win32/).\n\numlaut']
12821285

12831286
# \xc3\xa4\xc3\xb6\xc3\xbc\xc3\x84\xc3\x96\xc3\x9c\xc3\x9f
12841287
# append above with leading space to end of mycontent. It is the
@@ -1291,22 +1294,29 @@ def testMultipartTextifyHTML(self):
12911294
messages.sort()
12921295
msg = self.db.msg.getnode(messages[-1])
12931296
# html converted to utf-8 text
1294-
self.assertEqual(msg.content, mycontent+b2s(b" \xc3\xa4\xc3\xb6\xc3\xbc\xc3\x84\xc3\x96\xc3\x9c\xc3\x9f"))
1297+
self.compareStringFragments(msg.content,
1298+
text_fragments + [b2s(b" \xc3\xa4\xc3\xb6\xc3\xbc\xc3\x84\xc3\x96\xc3\x9c\xc3\x9f")])
12951299
self.assertEqual(msg.type, None)
12961300
self.assertEqual(len(msg.files), 2)
12971301
name = "unnamed" # no name for any files
12981302
types = { 0: "text/csv", 1: "text/html" }
12991303
# replace quoted printable string at end of html document
13001304
# with it's utf-8 encoded equivalent so comparison
13011305
# works.
1302-
content = { 0: "75,23,16,18\n", 1: self.html_doc.replace(" =E4=F6=FC=C4=D6=DC=DF",b2s(b" \xc3\xa4\xc3\xb6\xc3\xbc\xc3\x84\xc3\x96\xc3\x9c\xc3\x9f"))}
1306+
content = { 0: "75,23,16,18\n",
1307+
1: self.html_doc.replace(" =E4=F6=FC=C4=D6=DC=DF",
1308+
b2s(b" \xc3\xa4\xc3\xb6\xc3\xbc\xc3\x84\xc3\x96\xc3\x9c\xc3\x9f")) }
13031309
for n, id in enumerate (msg.files):
13041310
f = self.db.file.getnode (id)
13051311
self.assertEqual(f.name, name)
13061312
self.assertEqual(f.type, types[n])
13071313
self.assertEqual(f.content, content[n])
13081314

1309-
self.compareMessages(self._get_mail(),
1315+
self.compareMessages(self._get_mail()
1316+
.replace('=E2=80=99', '')
1317+
.replace('=E2=80=9C', '')
1318+
.replace('=E2=80=9D', '')
1319+
.replace('==\n', '===\n\n').replace('=\n', ''),
13101320
'''From [email protected] Thu Oct 10 02:42:14 2017
13111321
13121322
@@ -1358,8 +1368,7 @@ def testMultipartTextifyHTML(self):
13581368
known to require this.
13591369
13601370
If youre on windows, you will either need to be using the ActiveState python
1361-
distribution (at http://www.activestate.com/Products/ActivePython/), or you=
1362-
ll
1371+
distribution (at http://www.activestate.com/Products/ActivePython/), or youll
13631372
have to install the win32all package separately (get it from
13641373
http://starship.python.net/crew/mhammond/win32/).
13651374

0 commit comments

Comments
 (0)