I'm trying to test my Database class. Here is simplified example of it.
class Database:
""" it has more methods but I show only the most important """
def __init__(self, name):
# let's think the db-file exists with tables
self.conn = sqlite3.connect(name)
self.cursor = self.conn.cursor()
def __del__(self):
""" Here I close connection if the object was destroyed """
self.conn.close()
def insert(self, col1, col2, col3):
""" The key method where problem is """
self.cursor.execute(insert_query.format(col1, col2, col3))
self.conn.commit() # here I do commit to apply changes with DB
So, I want to check insert
method. The test case class is:
class DatabaseTestCase(unittest.TestCase):
""" it has other methods but the problem is here """
@given(col1=text(col1_params), col2=text(col2_params), col3=text(col3_params))
def test_db_insert(self, col1, col2, col3):
db = Database("test.db")
input_data = col1, col2, col3
# insert with commit (see Database example above)
db.insert(*input_data)
# delete object and close connection
del db
# recreate the object to get sure my data was added and
# the changes were commited
db = Database("test.db")
# I use the way not to use my own methods of Database object
cursor = db.conn.execute("SELECT * FROM mytable WHERE col1 = '{}'".format(col1))
result = cursor.fetchone()
for input_item, row_item in zip(input_data, result):
pass # assert here
# close connection with deleting of the db object
del db
The problem is "database is locked" in traceback when db.insert
is called from test method. I see the code as next steps:
- open 1st connection
- insert data
- commit and close connection
- open 2nd connection (after first was closed)
- get data inserted on step 2 using select
- compare data
- assert if input and selected data are not equalled.
But... I have not to get the message about database blocking if the connections work with database one by one, have I? I had a idea the libs (unittest or hypothesis) use threading but I found nothing in the documentation.
Also I tried to run it in usual for
and insert enumerable data. It works fine.
If I am not wrong every call of commit
method must unblock the database even the connection is opened, but it's seems it is not happened.
Could anyone help me to understand why I see the "database is locked" message?