From 20a5aca1ec3c9543ba0466820add5b70496e6d3b Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Mon, 13 Mar 2023 16:55:43 +0900 Subject: [PATCH 1/3] Add tests for #535 --- tests/test_errors.py | 56 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 tests/test_errors.py diff --git a/tests/test_errors.py b/tests/test_errors.py new file mode 100644 index 00000000..3a9fecaa --- /dev/null +++ b/tests/test_errors.py @@ -0,0 +1,56 @@ +import pytest +import MySQLdb.cursors +from configdb import connection_factory + + +_conns = [] +_tables = [] + + +def connect(**kwargs): + conn = connection_factory(**kwargs) + _conns.append(conn) + return conn + + +def teardown_function(function): + if _tables: + c = _conns[0] + cur = c.cursor() + for t in _tables: + cur.execute("DROP TABLE {}".format(t)) + cur.close() + del _tables[:] + + for c in _conns: + c.close() + del _conns[:] + + +def test_null(): + """Inserting NULL into non NULLABLE column""" + # https://github.com/PyMySQL/mysqlclient/issues/535 + table_name = "test_null" + conn = connect() + cursor = conn.cursor() + + cursor.execute(f"create table {table_name} (c1 int primary key)") + _tables.append(table_name) + + with pytest.raises(MySQLdb.IntegrityError): + cursor.execute(f"insert into {table_name} values (null)") + + +def test_duplicated_pk(): + """Inserting row with duplicated PK""" + # https://github.com/PyMySQL/mysqlclient/issues/535 + table_name = "test_duplicated_pk" + conn = connect() + cursor = conn.cursor() + + cursor.execute(f"create table {table_name} (c1 int primary key)") + _tables.append(table_name) + + cursor.execute(f"insert into {table_name} values (1)") + with pytest.raises(MySQLdb.IntegrityError): + cursor.execute(f"insert into {table_name} values (1)") From 8f6f5f21a1ddba9e33e1be794456eb3011b6d326 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Mon, 13 Mar 2023 17:01:16 +0900 Subject: [PATCH 2/3] Reformat tests/ with latest black --- tests/capabilities.py | 3 --- tests/test_MySQLdb_capabilities.py | 1 - 2 files changed, 4 deletions(-) diff --git a/tests/capabilities.py b/tests/capabilities.py index da753d15..034e88da 100644 --- a/tests/capabilities.py +++ b/tests/capabilities.py @@ -11,7 +11,6 @@ class DatabaseTest(unittest.TestCase): - db_module = None connect_args = () connect_kwargs = dict() @@ -20,7 +19,6 @@ class DatabaseTest(unittest.TestCase): debug = False def setUp(self): - db = connection_factory(**self.connect_kwargs) self.connection = db self.cursor = db.cursor() @@ -67,7 +65,6 @@ def new_table_name(self): i = i + 1 def create_table(self, columndefs): - """Create a table using a list of column definitions given in columndefs. diff --git a/tests/test_MySQLdb_capabilities.py b/tests/test_MySQLdb_capabilities.py index fc213b84..dbff27c2 100644 --- a/tests/test_MySQLdb_capabilities.py +++ b/tests/test_MySQLdb_capabilities.py @@ -12,7 +12,6 @@ class test_MySQLdb(capabilities.DatabaseTest): - db_module = MySQLdb connect_args = () connect_kwargs = dict( From 74abdce61be0dc089ac4938b580450a4ced3b0e7 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Mon, 13 Mar 2023 17:04:58 +0900 Subject: [PATCH 3/3] Convert ER_BAD_NULL_ERROR into IntegrityError --- MySQLdb/_mysql.c | 1 + 1 file changed, 1 insertion(+) diff --git a/MySQLdb/_mysql.c b/MySQLdb/_mysql.c index 7737dbe7..6c04ec99 100644 --- a/MySQLdb/_mysql.c +++ b/MySQLdb/_mysql.c @@ -180,6 +180,7 @@ _mysql_Exception(_mysql_ConnectionObject *c) #ifdef ER_NO_DEFAULT_FOR_FIELD case ER_NO_DEFAULT_FOR_FIELD: #endif + case ER_BAD_NULL_ERROR: e = _mysql_IntegrityError; break; #ifdef ER_WARNING_NOT_COMPLETE_ROLLBACK