Ja die bösen Strings, nur so als Übung selber bauen macht aber schon Sinn, Testen dabei nicht vergessen und man ist bereit richtig robustes Zeugt zu erstellen. Demo: https://onlinegdb.com/BJwPAKJQH
Immer wieder erstaunlich, wie gut man sowas mit Python runter hacken kann und was man dabei lernt
[edit]
Du brauchst allerdings eher eine Klasse, die dir Irgendwas in Float umbaut und da ich nicht weiß, wie lang der Code dort lebt - eine Klasse die "Irgendwas" in Text umbaut inklusive Tests:
Code:
import unittest
class my_sql_text_test(unittest.TestCase):
'''
test class
- I want to be able to use text, numbers and none as text in sql statements
- I want to be able to seperate types
'''
class some_type:
'''
just a helper class to test the type limitation of the my_sql_text class
'''
def __init__(self, data):
self.data = data
def test_prepare(self):
for val in (self.some_type('bla'), complex(1, 2)):
with self.assertRaises(TypeError):
my_sql_text.prepare(val)
self.assertEqual(my_sql_text.prepare(""), "''")
self.assertEqual(my_sql_text.prepare("'"), "''''")
self.assertEqual(my_sql_text.prepare("''"), "''''''")
self.assertEqual(my_sql_text.prepare(''), "''")
self.assertEqual(my_sql_text.prepare(-1), "'-1'")
self.assertEqual(my_sql_text.prepare(1), "'1'")
self.assertEqual(my_sql_text.prepare(1.2), "'1.2'")
self.assertEqual(my_sql_text.prepare(None), "NULL")
def test_internals(self):
'''
NOTE: I want 100% code coverage and there are no private
members in python
I therefore also need to test private functions
I have already tested the "good" pathes of private
member functions
-> all I need to test are the type limitations
(to proof I should turn on code coverage; it have to be 100%)
'''
for function in (my_sql_text._none, my_sql_text._number, my_sql_text._str):
for val in (self.some_type('bla'), complex(1, 2)):
with self.assertRaises(TypeError):
function(val)
class my_sql_text:
'''
A simple (static) class to prepare any int, float or str value
to be used as text which can be used with any sql statement
NOTE: if a member name starts with _, it is considered to be private
sa https://docs.python.org/3/tutorial/classes.html
'''
def prepare(val):
if isinstance(val, (float, int)):
return my_sql_text._number(val)
if isinstance(val, type(None)):
return my_sql_text._none(val)
if isinstance(val, str):
return my_sql_text._str(val)
raise TypeError("input type must be float, int, NoneType or str")
def _none(val):
if not isinstance(val, type(None)):
raise TypeError("input type must be NoneType")
return "NULL"
def _number(val):
if not isinstance(val, (float, int)):
raise TypeError("input type must be float or int")
return "'{}'".format(val)
def _str(val):
if not isinstance(val, str):
raise TypeError("input type must be str")
return "'{}'".format(val.replace("'", "''"))
test = False # set to True to run tests in the my_sql_text_test class
if test:
unittest.main()
sql = "INSERT INTO tbl_foo (col_bar) VALUES"
s = "\n "
for val in (0, 1.2, "3.45°", '6.789s', None, "';SELECT 1;--"):
sql += "{}({})".format(s, my_sql_text.prepare(val))
s = "\n ,"
sql += "\n;"
print(sql)
Lesezeichen