Skip to content

Commit

Permalink
Add tests for #535
Browse files Browse the repository at this point in the history
  • Loading branch information
methane committed Mar 13, 2023
1 parent b419bea commit 20a5aca
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
@@ -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)")

0 comments on commit 20a5aca

Please sign in to comment.