refactoring

This commit is contained in:
frankknoll
2022-12-14 11:35:25 +01:00
parent 1163f95a15
commit fdcc1369e2
3 changed files with 74 additions and 79 deletions

View File

@@ -8,6 +8,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"import os\n", "import os\n",
"import sys\n",
"\n", "\n",
"module_path = os.path.abspath(os.path.join('..'))\n", "module_path = os.path.abspath(os.path.join('..'))\n",
"if module_path not in sys.path:\n", "if module_path not in sys.path:\n",
@@ -235,85 +236,6 @@
" return pd.DataFrame(index = index, columns = columns, data = data).astype(dtypes)\n" " return pd.DataFrame(index = index, columns = columns, data = data).astype(dtypes)\n"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"id": "e4f8fa80",
"metadata": {},
"outputs": [],
"source": [
"class KreisOptionsSetterTest(unittest.TestCase):\n",
"\n",
" def test_setKreisOptions(self):\n",
" # Given\n",
" kreisOptionsSetter = KreisOptionsSetter()\n",
"\n",
" # When\n",
" htmlActual = kreisOptionsSetter.setKreisOptions(\n",
" html='''\n",
" <html>\n",
" <body>\n",
" <p>Test<p/>\n",
" <select id=\"kreisSelect\" name=\"kreis\">\n",
" <option selected=\"\" value=\"de\">Alle Landkreise</option>\n",
" <option value=\"Ahrweiler\">Ahrweiler</option>\n",
" <option value=\"Wiesbaden, Landeshauptstadt\">Wiesbaden, Landeshauptstadt</option>\n",
" <option value=\"Aichach-Friedberg\">Aichach-Friedberg</option>\n",
" </select>\n",
" </body>\n",
" </html>\n",
" ''',\n",
" options=[\n",
" '<option selected=\"\" value=\"de\">Alle Landkreise</option>',\n",
" '<option value=\"Ahrweiler\">Ahrweiler</option>',\n",
" '<option value=\"Aichach-Friedberg\">Aichach-Friedberg</option>'])\n",
"\n",
" # Then\n",
" assertEqualHTML(\n",
" htmlActual,\n",
" '''\n",
" <html>\n",
" <body>\n",
" <p>Test<p/>\n",
" <select id=\"kreisSelect\" name=\"kreis\">\n",
" <option selected=\"\" value=\"de\">Alle Landkreise</option>\n",
" <option value=\"Ahrweiler\">Ahrweiler</option>\n",
" <option value=\"Aichach-Friedberg\">Aichach-Friedberg</option>\n",
" </select>\n",
" </body>\n",
" </html>\n",
" ''')\n",
"\n",
"# adapted from https://stackoverflow.com/questions/8006909/pretty-print-assertequal-for-html-strings\n",
"def assertEqualHTML(string1, string2, file1='', file2=''):\n",
" u'''\n",
" Compare two unicode strings containing HTML.\n",
" A human friendly diff goes to logging.error() if they\n",
" are not equal, and an exception gets raised.\n",
" '''\n",
" from bs4 import BeautifulSoup as bs\n",
" import difflib\n",
"\n",
" def short(mystr):\n",
" max = 20\n",
" if len(mystr) > max:\n",
" return mystr[:max]\n",
" return mystr\n",
" p = []\n",
" for mystr, file in [(string1, file1), (string2, file2)]:\n",
" if not isinstance(mystr, str):\n",
" raise Exception(u'string ist not unicode: %r %s' %\n",
" (short(mystr), file))\n",
" soup = bs(mystr)\n",
" pretty = soup.prettify()\n",
" p.append(pretty)\n",
" if p[0] != p[1]:\n",
" for line in difflib.unified_diff(p[0].splitlines(), p[1].splitlines(), fromfile=file1, tofile=file2):\n",
" display(line)\n",
" display(p[0], ' != ', p[1])\n",
" raise Exception('Not equal %s %s' % (file1, file2))\n"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,

View File

@@ -0,0 +1,73 @@
import unittest
from intensivstationen.KreisOptionsSetter import KreisOptionsSetter
class KreisOptionsSetterTest(unittest.TestCase):
def test_setKreisOptions(self):
# Given
kreisOptionsSetter = KreisOptionsSetter()
# When
htmlActual = kreisOptionsSetter.setKreisOptions(
html='''
<html>
<body>
<p>Test<p/>
<select id="kreisSelect" name="kreis">
<option selected="" value="de">Alle Landkreise</option>
<option value="Ahrweiler">Ahrweiler</option>
<option value="Wiesbaden, Landeshauptstadt">Wiesbaden, Landeshauptstadt</option>
<option value="Aichach-Friedberg">Aichach-Friedberg</option>
</select>
</body>
</html>
''',
options=[
'<option selected="" value="de">Alle Landkreise</option>',
'<option value="Ahrweiler">Ahrweiler</option>',
'<option value="Aichach-Friedberg">Aichach-Friedberg</option>'])
# Then
assertEqualHTML(
htmlActual,
'''
<html>
<body>
<p>Test<p/>
<select id="kreisSelect" name="kreis">
<option selected="" value="de">Alle Landkreise</option>
<option value="Ahrweiler">Ahrweiler</option>
<option value="Aichach-Friedberg">Aichach-Friedberg</option>
</select>
</body>
</html>
''')
# 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)
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):
display(line)
display(p[0], ' != ', p[1])
raise Exception('Not equal %s %s' % (file1, file2))

View File