Python sqlite3 and unit testing

Source: Internet
Author: User

Import OS
Import unittest # contains the Unit Test Module
Import sqlite3 as SQLite # contains the sqlite3 Module

Def get_db_path ():
Return "sqlite_testdb"

Class transactiontests (unittest. testcase): # unit test Step 1: Derived from testcase
Def setup (Self): # unit test environment Configuration
Try:
OS. Remove (get_db_path ())
Except t:
Pass

Self. con1 = SQLite. Connect (get_db_path (), timeout = 0.1) # connect to the database
Self. cur1 = self. con1.cursor () # obtain the cursor

Self. con2 = SQLite. Connect (get_db_path (), timeout = 0.1)
Self. cur2 = self. con2.cursor ()

Def teardown (Self): # Clear unit test environment
Self. cur1.close () # Close the cursor
Self. con1.close () # Close the connection

Self. cur2.close ()
Self. con2.close ()

OS. Unlink (get_db_path ())

Def checkdmldoesautocommitbefore (Self ):
Self.cur1.exe cute ("create table test (I)") # execute an SQL query
Self.cur1.exe cute ("insert into test (I) values (5 )")
Self.cur1.exe cute ("create table Test2 (j )")
Self.cur2.exe cute ("select I from test ")
Res = self. cur2.fetchall ()
Self. failunlessequal (LEN (RES), 1) # Test

Def checkinsertstartstransaction (Self ):
Self.cur1.exe cute ("create table test (I )")
Self.cur1.exe cute ("insert into test (I) values (5 )")
Self.cur2.exe cute ("select I from test ")
Res = self. cur2.fetchall ()
Self. failunlessequal (LEN (RES), 0)

Def checkupdatestartstransaction (Self ):
Self.cur1.exe cute ("create table test (I )")
Self.cur1.exe cute ("insert into test (I) values (5 )")
Self. con1.commit ()
Self.cur1.exe cute ("Update test set I = 6 ")
Self.cur2.exe cute ("select I from test ")
Res = self. cur2.fetchone () [0]
Self. failunlessequal (Res, 5)

Def checkdeletestartstransaction (Self ):
Self.cur1.exe cute ("create table test (I )")
Self.cur1.exe cute ("insert into test (I) values (5 )")
Self. con1.commit ()
Self.cur1.exe cute ("delete from test ")
Self.cur2.exe cute ("select I from test ")
Res = self. cur2.fetchall ()
Self. failunlessequal (LEN (RES), 1)

Def checkreplacestartstransaction (Self ):
Self.cur1.exe cute ("create table test (I )")
Self.cur1.exe cute ("insert into test (I) values (5 )")
Self. con1.commit ()
Self.cur1.exe cute ("replace into test (I) values (6 )")
Self.cur2.exe cute ("select I from test ")
Res = self. cur2.fetchall ()
Self. failunlessequal (LEN (RES), 1)
Self. failunlessequal (RES [0] [0], 5)

Def checktoggleautocommit (Self ):
Self.cur1.exe cute ("create table test (I )")
Self.cur1.exe cute ("insert into test (I) values (5 )")
Self. con1.isolation _ Level = none
Self. failunlessequal (self. con1.isolation _ level, none)
Self.cur2.exe cute ("select I from test ")
Res = self. cur2.fetchall ()
Self. failunlessequal (LEN (RES), 1)

Self. con1.isolation _ Level = "Deferred"
Self. failunlessequal (self. con1.isolation _ level, "Deferred ")
Self.cur1.exe cute ("insert into test (I) values (5 )")
Self.cur2.exe cute ("select I from test ")
Res = self. cur2.fetchall ()
Self. failunlessequal (LEN (RES), 1)

Def checkraisetimeout (Self ):
Self.cur1.exe cute ("create table test (I )")
Self.cur1.exe cute ("insert into test (I) values (5 )")
Try:
Self.cur2.exe cute ("insert into test (I) values (5 )")
Self. Fail ("shocould have raised an operationalerror ")
Failed t SQLite. operationalerror:
Pass
Except t:
Self. Fail ("shocould have raised an operationalerror ")

Class specialcommandtests (unittest. testcase ):
Def setup (Self ):
Self. Con = SQLite. Connect (": Memory :")
Self. cur = self. Con. cursor ()

Def checkvacuum (Self ):
Self.cur.exe cute ("create table test (I )")
Self.cur.exe cute ("insert into test (I) values (5 )")
Self.cur.exe cute ("vacuum ")

Def checkdroptable (Self ):
Self.cur.exe cute ("create table test (I )")
Self.cur.exe cute ("insert into test (I) values (5 )")
Self.cur.exe cute ("Drop table test ")

Def checkpragma (Self ):
Self.cur.exe cute ("create table test (I )")
Self.cur.exe cute ("insert into test (I) values (5 )")
Self.cur.exe cute ("Pragma count_changes = 1 ")

Def teardown (Self ):
Self. cur. Close ()
Self. Con. Close ()

Def Suite (): # Step 2 of unit test
Default_suite = unittest. makesuite (transactiontests, "check ")
Special_command_suite = unittest. makesuite (specialcommandtests, "check ")
Return unittest. testsuite (default_suite, special_command_suite) # Use Case

Def test (): # Step 3 of unit test
Runner = unittest. texttestrunner ()
Runner. Run (Suite () # Run the unit test

If _ name _ = "_ main __":
Test ()
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.