Creating a More Complex Test
Let's explore how we can create a more complex test using unittest module.
We'll cover the following...
Most of the codes are more complex than our my_math.py
example.
So let’s
create a piece of code that depends on a database being in existence. We
will create a simple script that can create the database with some
initial data if it doesn’t exist along with a few functions that will
allow us to query it, delete and update rows. We will name this script
simple_db.py
.
Creating a script
This is a fairly long example, so bear with us:
Press + to interact
import sqlite3def create_database():conn = sqlite3.connect("mydatabase.db")cursor = conn.cursor()# create a tablecursor.execute("""CREATE TABLE albums(title text, artist text, release_date text,publisher text, media_type text)""")# insert some datacursor.execute("INSERT INTO albums VALUES ""('Glow', 'Andy Hunter', '7/24/2012',""'Xplore Records', 'MP3')")# save data to databaseconn.commit()# insert multiple records using the more secure "?" methodalbums = [('Exodus', 'Andy Hunter', '7/9/2002','Sparrow Records', 'CD'),('Until We Have Faces', 'Red', '2/1/2011','Essential Records', 'CD'),('The End is Where We Begin', 'Thousand Foot Krutch','4/17/2012', 'TFKmusic', 'CD'),('The Good Life', 'Trip Lee', '4/10/2012','Reach Records', 'CD')]cursor.executemany("INSERT INTO albums VALUES (?,?,?,?,?)",albums)conn.commit()def delete_artist(artist):"""Delete an artist from the database"""conn = sqlite3.connect("mydatabase.db")cursor = conn.cursor()sql = """DELETE FROM albumsWHERE artist = ?"""cursor.execute(sql, [(artist)])conn.commit()cursor.close()conn.close()def update_artist(artist, new_name):"""Update the artist name"""conn = sqlite3.connect("mydatabase.db")cursor = conn.cursor()sql = """UPDATE albumsSET artist = ?WHERE artist = ?"""cursor.execute(sql, (new_name, artist))conn.commit()cursor.close()conn.close()def select_all_albums(artist):"""Query the database for all the albums by a particular artist"""conn = sqlite3.connect("mydatabase.db")cursor = conn.cursor()sql = "SELECT * FROM albums WHERE artist=?"cursor.execute(sql, [(artist)])result = cursor.fetchall()cursor.close()conn.close()return resultif __name__ == '__main__':import osif not os.path.exists("mydatabase.db"):create_database()delete_artist('Andy Hunter')update_artist('Red', 'Redder')print(select_all_albums('Thousand Foot Krutch'))
You can play around with this code a bit ...