diff --git a/src/BatchcodeOptionsSetterTest.py b/src/BatchcodeOptionsSetterTest.py index 082a592aeab..53ea4318ea1 100644 --- a/src/BatchcodeOptionsSetterTest.py +++ b/src/BatchcodeOptionsSetterTest.py @@ -1,5 +1,6 @@ import unittest from BatchcodeOptionsSetter import BatchcodeOptionsSetter +from TestHelper import TestHelper class BatchcodeOptionsSetterTest(unittest.TestCase): @@ -26,7 +27,7 @@ class BatchcodeOptionsSetterTest(unittest.TestCase): '']) # Then - assertEqualHTML( + TestHelper.assertEqualHTML( htmlActual, ''' @@ -40,32 +41,3 @@ class BatchcodeOptionsSetterTest(unittest.TestCase): ''') - -# adapted from https://stackoverflow.com/questions/8006909/pretty-print-assertequal-for-html-strings -def assertEqualHTML(string1, string2, file1='', file2=''): - u''' - Compare two unicode strings containing HTML. - A human friendly diff goes to logging.error() if they - are not equal, and an exception gets raised. - ''' - from bs4 import BeautifulSoup as bs - import difflib - - def short(mystr): - max = 20 - if len(mystr) > max: - return mystr[:max] - return mystr - p = [] - for mystr, file in [(string1, file1), (string2, file2)]: - if not isinstance(mystr, str): - raise Exception(u'string ist not unicode: %r %s' % - (short(mystr), file)) - soup = bs(mystr, 'lxml') - pretty = soup.prettify() - p.append(pretty) - if p[0] != p[1]: - for line in difflib.unified_diff(p[0].splitlines(), p[1].splitlines(), fromfile=file1, tofile=file2): - print(line) - print(p[0], ' != ', p[1]) - raise Exception('Not equal %s %s' % (file1, file2)) diff --git a/src/SymptomsCausedByVaccines/OptionsSetterTest.py b/src/SymptomsCausedByVaccines/OptionsSetterTest.py index 123435c7f5f..200c19f91a5 100644 --- a/src/SymptomsCausedByVaccines/OptionsSetterTest.py +++ b/src/SymptomsCausedByVaccines/OptionsSetterTest.py @@ -1,5 +1,7 @@ import unittest from SymptomsCausedByVaccines.OptionsSetter import OptionsSetter +from TestHelper import TestHelper + class OptionsSetterTest(unittest.TestCase): @@ -24,7 +26,7 @@ class OptionsSetterTest(unittest.TestCase): '']) # Then - assertEqualHTML( + TestHelper.assertEqualHTML( htmlActual, ''' @@ -37,33 +39,3 @@ class OptionsSetterTest(unittest.TestCase): ''') - -# FK-TODO: DRY with BatchcodeOptionsSetterTest.assertEqualHTML() -# adapted from https://stackoverflow.com/questions/8006909/pretty-print-assertequal-for-html-strings -def assertEqualHTML(string1, string2, file1='', file2=''): - u''' - Compare two unicode strings containing HTML. - A human friendly diff goes to logging.error() if they - are not equal, and an exception gets raised. - ''' - from bs4 import BeautifulSoup as bs - import difflib - - def short(mystr): - max = 20 - if len(mystr) > max: - return mystr[:max] - return mystr - p = [] - for mystr, file in [(string1, file1), (string2, file2)]: - if not isinstance(mystr, str): - raise Exception(u'string ist not unicode: %r %s' % - (short(mystr), file)) - soup = bs(mystr, 'lxml') - pretty = soup.prettify() - p.append(pretty) - if p[0] != p[1]: - for line in difflib.unified_diff(p[0].splitlines(), p[1].splitlines(), fromfile=file1, tofile=file2): - print(line) - print(p[0], ' != ', p[1]) - raise Exception('Not equal %s %s' % (file1, file2)) diff --git a/src/TestHelper.py b/src/TestHelper.py index fe0f1634edd..89b36e76f26 100644 --- a/src/TestHelper.py +++ b/src/TestHelper.py @@ -12,3 +12,33 @@ class TestHelper: series = pd.Series(**kwargs) series.index.name = indexName return series + + # adapted from https://stackoverflow.com/questions/8006909/pretty-print-assertequal-for-html-strings + @staticmethod + def assertEqualHTML(string1, string2, file1='', file2=''): + u''' + Compare two unicode strings containing HTML. + A human friendly diff goes to logging.error() if they + are not equal, and an exception gets raised. + ''' + from bs4 import BeautifulSoup as bs + import difflib + + def short(mystr): + max = 20 + if len(mystr) > max: + return mystr[:max] + return mystr + p = [] + for mystr, file in [(string1, file1), (string2, file2)]: + if not isinstance(mystr, str): + raise Exception(u'string ist not unicode: %r %s' % + (short(mystr), file)) + soup = bs(mystr, 'lxml') + pretty = soup.prettify() + p.append(pretty) + if p[0] != p[1]: + for line in difflib.unified_diff(p[0].splitlines(), p[1].splitlines(), fromfile=file1, tofile=file2): + print(line) + print(p[0], ' != ', p[1]) + raise Exception('Not equal %s %s' % (file1, file2)) diff --git a/src/intensivstationen/KreisOptionsSetterTest.py b/src/intensivstationen/KreisOptionsSetterTest.py index bd319843741..2e28fa31527 100644 --- a/src/intensivstationen/KreisOptionsSetterTest.py +++ b/src/intensivstationen/KreisOptionsSetterTest.py @@ -43,6 +43,7 @@ class KreisOptionsSetterTest(unittest.TestCase): ''') +# FK-TODO: delegate to use TestHelper.assertEqualHTML() # adapted from https://stackoverflow.com/questions/8006909/pretty-print-assertequal-for-html-strings def assertEqualHTML(string1, string2, file1='', file2=''): u'''