mardi 10 février 2015

Mock a MySQL database in Python

I use Python 3.4 from the Anaconda distribution. Within this distribution, I found the pymysql library to connect to an existing MySQL database, which is located on another computer.



import pymysql
config = {
'user': 'my_user',
'passwd': 'my_passwd',
'host': 'my_host',
'port': my_port
}

try:
cnx = pymysql.connect(**config)
except pymysql.err.OperationalError :
sys.exit("Invalid Input: Wrong username/database or password")


I now want to write test code for my application, in which I want to create a very small database at the setUp of every test case, preferably in memory. However, when I try this out of the blue with pymysql, it cannot make a connection.



def setUp(self):
config = {
'user': 'test_user',
'passwd': 'test_passwd',
'host': 'localhost'
}
cnx = pymysql.connect(**config)

pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 61] Connection refused)")


I have been googling around, and found some things about SQLite and MySQLdb. I have the following questions:



  1. Is sqlite3 or MySQLdb suitable for creating quickly a database in memory?

  2. How do I install MySQLdb within the Anaconda package?

  3. Is there an example of a test database, created in the setUp? Is this even a good idea?


I do not have a MySQL server running locally on my computer.


Aucun commentaire:

Enregistrer un commentaire