summaryrefslogtreecommitdiffstats
path: root/tests/QtSql/qsqldatabaseandqueries_test.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/QtSql/qsqldatabaseandqueries_test.py')
-rwxr-xr-xtests/QtSql/qsqldatabaseandqueries_test.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/tests/QtSql/qsqldatabaseandqueries_test.py b/tests/QtSql/qsqldatabaseandqueries_test.py
new file mode 100755
index 0000000..49f2209
--- /dev/null
+++ b/tests/QtSql/qsqldatabaseandqueries_test.py
@@ -0,0 +1,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 QVariant, QString
+
+class SqlDatabaseCreationDestructionAndQueries(unittest.TestCase):
+ '''Test cases for QtSql database creation, destruction and queries'''
+
+ def setUp(self):
+ #Acquire resources
+ self.assertFalse(QtSql.QSqlDatabase.drivers().isEmpty(), "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", QVariant(102))
+ query.bindValue(":firstname", QVariant("John"))
+ query.bindValue(":lastname", QVariant("Lennon"))
+ query.exec_()
+
+ lastname = ''
+ query.exec_("SELECT lastname FROM person where id=101")
+ self.assertTrue(query.isActive())
+ query.next()
+ lastname = query.value(0).toString()
+ self.assertEqual(lastname, 'Harrison')
+
+if __name__ == '__main__':
+ unittest.main()
+