Skip to content

Commit 20a5aca

Browse files
committed
Add tests for #535
1 parent b419bea commit 20a5aca

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

tests/test_errors.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import pytest
2+
import MySQLdb.cursors
3+
from configdb import connection_factory
4+
5+
6+
_conns = []
7+
_tables = []
8+
9+
10+
def connect(**kwargs):
11+
conn = connection_factory(**kwargs)
12+
_conns.append(conn)
13+
return conn
14+
15+
16+
def teardown_function(function):
17+
if _tables:
18+
c = _conns[0]
19+
cur = c.cursor()
20+
for t in _tables:
21+
cur.execute("DROP TABLE {}".format(t))
22+
cur.close()
23+
del _tables[:]
24+
25+
for c in _conns:
26+
c.close()
27+
del _conns[:]
28+
29+
30+
def test_null():
31+
"""Inserting NULL into non NULLABLE column"""
32+
# https://github.com/PyMySQL/mysqlclient/issues/535
33+
table_name = "test_null"
34+
conn = connect()
35+
cursor = conn.cursor()
36+
37+
cursor.execute(f"create table {table_name} (c1 int primary key)")
38+
_tables.append(table_name)
39+
40+
with pytest.raises(MySQLdb.IntegrityError):
41+
cursor.execute(f"insert into {table_name} values (null)")
42+
43+
44+
def test_duplicated_pk():
45+
"""Inserting row with duplicated PK"""
46+
# https://github.com/PyMySQL/mysqlclient/issues/535
47+
table_name = "test_duplicated_pk"
48+
conn = connect()
49+
cursor = conn.cursor()
50+
51+
cursor.execute(f"create table {table_name} (c1 int primary key)")
52+
_tables.append(table_name)
53+
54+
cursor.execute(f"insert into {table_name} values (1)")
55+
with pytest.raises(MySQLdb.IntegrityError):
56+
cursor.execute(f"insert into {table_name} values (1)")

0 commit comments

Comments
 (0)