summaryrefslogtreecommitdiffstats
path: root/tests/QtSql/qsqldatabaseandqueries_test.py
blob: 7a9d30a42562b38214a12f38a476048981405735 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python
'''Test cases for QtSql database creation, destruction and queries'''

import sys
import unittest

from PySide import QtSql
from PySide.QtCore import *

class SqlDatabaseCreationDestructionAndQueries(unittest.TestCase):
    '''Test cases for QtSql database creation, destruction and queries'''

    def setUp(self):
        #Acquire resources
        self.assertFalse(not QtSql.QSqlDatabase.drivers(), "installed Qt has no DB drivers")
        self.assertTrue("QSQLITE" in QtSql.QSqlDatabase.drivers(), "\"QSQLITE\" driver not available in this Qt version")
        self.db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
        self.db.setDatabaseName(":memory:")
        self.assertTrue(self.db.open())

    def tearDown(self):
        #Release resources
        self.db.close()
        QtSql.QSqlDatabase.removeDatabase(":memory:")
        del self.db

    def testTableCreationAndDestruction(self):
        #Test table creation and destruction
        query = QtSql.QSqlQuery()
        query.exec_("CREATE TABLE dummy(id int primary key, dummyfield varchar(20))")
        query.exec_("DROP TABLE dummy")
        query.clear()

    def testTableInsertionAndRetrieval(self):
        #Test table creation, insertion and retrieval
        query = QtSql.QSqlQuery()
        query.exec_("CREATE TABLE person(id int primary key, "
                    "firstname varchar(20), lastname varchar(20))")
        query.exec_("INSERT INTO person VALUES(101, 'George', 'Harrison')")
        query.prepare("INSERT INTO person (id, firstname, lastname) "
                      "VALUES (:id, :firstname, :lastname)")
        query.bindValue(":id", 102)
        query.bindValue(":firstname", "John")
        query.bindValue(":lastname", "Lennon")
        query.exec_()

        lastname = ''
        query.exec_("SELECT lastname FROM person where id=101")
        self.assertTrue(query.isActive())
        query.next()
        lastname = query.value(0)
        self.assertEqual(lastname, 'Harrison')

if __name__ == '__main__':
    unittest.main()