1
from pysqlite2 import dbapi2 as sqlite
3
from storm.databases.sqlite import SQLite
4
from storm.database import *
6
from tests.helper import TestHelper, MakePath
9
class SQLiteMemoryTest(TestHelper):
14
TestHelper.setUp(self)
17
def setup_database(self):
18
self.database = SQLite()
20
def add_sample_data(self, connection):
21
connection.execute("CREATE TABLE test (id INT PRIMARY KEY, title VARCHAR)")
22
connection.execute("INSERT INTO test VALUES (1, 'Title 1')")
23
connection.execute("INSERT INTO test VALUES (2, 'Title 2')")
25
def test_create(self):
26
self.assertTrue(isinstance(self.database, Database))
28
def test_connection(self):
29
connection = self.database.connect()
30
self.assertTrue(isinstance(connection, Connection))
32
def test_execute_result(self):
33
connection = self.database.connect()
34
result = connection.execute("SELECT 1")
35
self.assertTrue(isinstance(result, Result))
37
def test_execute_params(self):
38
connection = self.database.connect()
39
result = connection.execute("SELECT 1 WHERE 1=?", (1,))
40
self.assertTrue(result.fetch_one())
41
result = connection.execute("SELECT 1 WHERE 1=?", (2,))
42
self.assertFalse(result.fetch_one())
44
def test_fetch_one(self):
45
connection = self.database.connect()
46
self.add_sample_data(connection)
47
result = connection.execute("SELECT * FROM test ORDER BY id")
48
self.assertEquals(result.fetch_one(), (1, "Title 1"))
50
def test_fetch_all(self):
51
connection = self.database.connect()
52
self.add_sample_data(connection)
53
result = connection.execute("SELECT * FROM test ORDER BY id")
54
self.assertEquals(result.fetch_all(), [(1, "Title 1"), (2, "Title 2")])
57
connection = self.database.connect()
58
self.add_sample_data(connection)
59
result = connection.execute("SELECT * FROM test ORDER BY id")
60
self.assertEquals([item for item in result],
61
[(1, "Title 1"), (2, "Title 2")])
63
class SQLiteFileTest(SQLiteMemoryTest):
65
def setup_database(self):
66
self.database = SQLite(self.make_path())
68
# Test iterating over two connections at the same time.