user1410756
user1410756

Reputation: 55

Mocking DB python

Is there a simple mock example to test connection db in python? I find much examples by google, but I'm confused. I'd like to test a connection db and execute a query, to understand concepts, for example cursor.

THANKS

Upvotes: 3

Views: 2318

Answers (4)

Niclas Nilsson
Niclas Nilsson

Reputation: 5901

I would as pcalcao said just advice you to play around with a database. Sqlite3 will get you up and running in no time. The sqlite3 module is implemented in python these days. Here are a (slightly modified) example from the official docs at: http://docs.python.org/2/library/sqlite3.html

import sqlite3

# You can change example.db to :memory: below if you don't want to save to 
# file. But be aware that the data is (obviously) lost after the program has
# terminated.
conn = sqlite3.connect('example.db')  

c = conn.cursor()

# Create table
c.execute('''CREATE TABLE stocks
             (date text, trans text, symbol text, qty real, price real)''')

# Insert a row of data
c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")

# Save (commit) the changes
conn.commit()

# Merged from another sample on the same page
t = ('RHAT',)
c.execute('SELECT * FROM stocks WHERE symbol=?', t)
print c.fetchone()

# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
conn.close()

Upvotes: 1

GSP
GSP

Reputation: 1076

If you want mocking, might look into unittest while you're at it. It works well with sqlite3.

You can create an environment where the database connection works as usual, but where any changes made do not last. Using a setUp method to create the environment that the tests depend on. It is easy to create a new database connection to an in-memory- only database, and populating that database with the needed tables and rows.

tearDown method to undo whatever the setUp method did, so that each test can run in an untouched version of the environment. Since the database is only in memory, all you need to do is close the connection.

Upvotes: 0

Szczad
Szczad

Reputation: 826

Like everyone else said, You should set up database on SQLite and test it straight away.

But if You need some test suite with mocking, You can use mockito package. It's easy to use. Just mock some objects, record behavior and check if it's ok.

Upvotes: 0

pcalcao
pcalcao

Reputation: 15965

If you want to understand the concepts, I think mocking isn't the best solution for you.

I would advise actually setting up a simple Database, for instance, SQLite3, creating some tables, connecting to it via Python, and doing some tests.

To be able to mock something, you need to know what behavior you are expecting from the object you are mocking, if you want to learn how it works, then your best answer is to actually try it out.

Sqlite3 is builtin into most recent versions of Python, so you can test it by simply opening a python shell and typing in import sqlite3.

Check out this tutorial for some hints on what you can do:

http://zetcode.com/db/sqlitepythontutorial/

Upvotes: 2

Related Questions