| 902 | |
| 903 | |
| 904 | class TestEscape(base.PyMySQLTestCase): |
| 905 | def test_escape_string(self): |
| 906 | con = self.connect() |
| 907 | cur = con.cursor() |
| 908 | |
| 909 | self.assertEqual(con.escape("foo'bar"), "'foo\\'bar'") |
| 910 | # added NO_AUTO_CREATE_USER as not including it in 5.7 generates warnings |
| 911 | # mysql-8.0 removes the option however |
| 912 | if self.mysql_server_is(con, (8, 0, 0)): |
| 913 | cur.execute("SET sql_mode='NO_BACKSLASH_ESCAPES'") |
| 914 | else: |
| 915 | cur.execute("SET sql_mode='NO_BACKSLASH_ESCAPES,NO_AUTO_CREATE_USER'") |
| 916 | self.assertEqual(con.escape("foo'bar"), "'foo''bar'") |
| 917 | |
| 918 | def test_escape_builtin_encoders(self): |
| 919 | con = self.connect() |
| 920 | |
| 921 | val = datetime.datetime(2012, 3, 4, 5, 6) |
| 922 | self.assertEqual(con.escape(val, con.encoders), "'2012-03-04 05:06:00'") |
| 923 | |
| 924 | def test_escape_custom_object(self): |
| 925 | con = self.connect() |
| 926 | |
| 927 | mapping = {Foo: escape_foo} |
| 928 | self.assertEqual(con.escape(Foo(), mapping), "bar") |
| 929 | |
| 930 | def test_escape_fallback_encoder(self): |
| 931 | con = self.connect() |
| 932 | |
| 933 | class Custom(str): |
| 934 | pass |
| 935 | |
| 936 | mapping = {str: pymysql.converters.escape_string} |
| 937 | self.assertEqual(con.escape(Custom("foobar"), mapping), "'foobar'") |
| 938 | |
| 939 | def test_escape_no_default(self): |
| 940 | con = self.connect() |
| 941 | |
| 942 | self.assertRaises(TypeError, con.escape, 42, {}) |
| 943 | |
| 944 | def test_escape_dict_raise_typeerror(self): |
| 945 | """con.escape(dict) should raise TypeError""" |
| 946 | con = self.connect() |
| 947 | |
| 948 | mapping = con.encoders.copy() |
| 949 | mapping[Foo] = escape_foo |
| 950 | # self.assertEqual(con.escape({"foo": Foo()}, mapping), {"foo": "bar"}) |
| 951 | with self.assertRaises(TypeError): |
| 952 | con.escape({"foo": Foo()}) |
| 953 | |
| 954 | def test_escape_list_item(self): |
| 955 | con = self.connect() |
| 956 | |
| 957 | mapping = con.encoders.copy() |
| 958 | mapping[Foo] = escape_foo |
| 959 | self.assertEqual(con.escape([Foo()], mapping), "(bar)") |
| 960 | |
| 961 | def test_previous_cursor_not_closed(self): |
nothing calls this directly
no outgoing calls
no test coverage detected