2014-03-29 18:26:36 +00:00
|
|
|
import os
|
2011-09-01 17:21:53 +00:00
|
|
|
import unittest
|
2014-03-29 14:04:08 +00:00
|
|
|
import sqlite3
|
2014-03-29 18:26:36 +00:00
|
|
|
import tempfile
|
|
|
|
|
2011-09-01 17:21:53 +00:00
|
|
|
import bottle
|
|
|
|
from bottle.ext import sqlite
|
2014-03-29 14:04:08 +00:00
|
|
|
|
2011-09-01 17:21:53 +00:00
|
|
|
|
|
|
|
class SQLiteTest(unittest.TestCase):
|
2014-03-29 18:26:36 +00:00
|
|
|
|
2011-09-01 17:21:53 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.app = bottle.Bottle(catchall=False)
|
2014-03-29 18:26:36 +00:00
|
|
|
_, dbfile = tempfile.mkstemp(suffix='.sqlite')
|
|
|
|
self.plugin = self.app.install(sqlite.Plugin(dbfile=dbfile))
|
2011-09-01 17:21:53 +00:00
|
|
|
|
2014-03-29 18:26:36 +00:00
|
|
|
self.conn = sqlite3.connect(dbfile)
|
|
|
|
self.conn.execute("CREATE TABLE todo (id INTEGER PRIMARY KEY, task char(100) NOT NULL)")
|
|
|
|
self.conn.commit()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
os.unlink(self.plugin.dbfile)
|
2011-09-01 17:21:53 +00:00
|
|
|
|
2014-03-29 18:26:36 +00:00
|
|
|
def test_with_keyword(self):
|
2011-09-01 17:21:53 +00:00
|
|
|
@self.app.get('/')
|
|
|
|
def test(db):
|
|
|
|
self.assertEqual(type(db), type(sqlite3.connect(':memory:')))
|
2014-03-29 14:04:08 +00:00
|
|
|
self._request('/')
|
2011-09-01 17:21:53 +00:00
|
|
|
|
|
|
|
def test_without_keyword(self):
|
|
|
|
@self.app.get('/')
|
2014-03-29 14:04:08 +00:00
|
|
|
def test_1():
|
2011-09-01 17:21:53 +00:00
|
|
|
pass
|
2014-03-29 14:04:08 +00:00
|
|
|
self._request('/')
|
2011-09-01 17:21:53 +00:00
|
|
|
|
|
|
|
@self.app.get('/2')
|
2014-03-29 14:04:08 +00:00
|
|
|
def test_2(**kw):
|
2011-09-01 17:21:53 +00:00
|
|
|
self.assertFalse('db' in kw)
|
2014-03-29 14:04:08 +00:00
|
|
|
self._request('/2')
|
2014-03-29 13:57:02 +00:00
|
|
|
|
|
|
|
def test_install_conflicts(self):
|
|
|
|
self.app.install(sqlite.Plugin(keyword='db2'))
|
|
|
|
|
|
|
|
@self.app.get('/')
|
|
|
|
def test(db, db2):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# I have two plugins working with different names
|
2014-03-29 14:04:08 +00:00
|
|
|
self._request('/')
|
|
|
|
|
|
|
|
def test_raise_sqlite_integrity_error(self):
|
2014-03-29 18:26:36 +00:00
|
|
|
@self.app.get('/')
|
|
|
|
def test(db):
|
|
|
|
# task can not be null, raise an IntegrityError
|
|
|
|
db.execute("INSERT INTO todo (id) VALUES (1)")
|
|
|
|
|
|
|
|
# TODO: assert HTTPError 500
|
|
|
|
self._request('/')
|
|
|
|
self.assert_records(0)
|
2014-03-29 14:04:08 +00:00
|
|
|
|
2014-03-29 18:26:36 +00:00
|
|
|
def test_autocommit(self):
|
2014-03-29 14:04:08 +00:00
|
|
|
@self.app.get('/')
|
|
|
|
def test(db):
|
2014-03-29 18:26:36 +00:00
|
|
|
self._insert_into(db)
|
|
|
|
|
2014-03-29 14:04:08 +00:00
|
|
|
self._request('/')
|
2014-03-29 18:26:36 +00:00
|
|
|
self.assert_records(1)
|
|
|
|
|
|
|
|
def test_not_autocommit(self):
|
|
|
|
@self.app.get('/', sqlite={'autocommit': False})
|
|
|
|
def test(db):
|
|
|
|
self._insert_into(db)
|
|
|
|
|
|
|
|
self._request('/')
|
|
|
|
self.assert_records(0)
|
|
|
|
|
|
|
|
def test_commit_on_redirect(self):
|
|
|
|
@self.app.get('/')
|
|
|
|
def test(db):
|
|
|
|
self._insert_into(db)
|
|
|
|
bottle.redirect('/')
|
|
|
|
|
|
|
|
self._request('/')
|
|
|
|
self.assert_records(1)
|
|
|
|
|
|
|
|
def test_commit_on_abort(self):
|
|
|
|
@self.app.get('/')
|
|
|
|
def test(db):
|
|
|
|
self._insert_into(db)
|
|
|
|
bottle.abort()
|
|
|
|
|
|
|
|
self._request('/')
|
|
|
|
self.assert_records(0)
|
2014-03-29 13:57:02 +00:00
|
|
|
|
2014-03-29 14:04:08 +00:00
|
|
|
def _request(self, path, method='GET'):
|
2014-03-29 18:26:36 +00:00
|
|
|
return self.app({'PATH_INFO': path, 'REQUEST_METHOD': method},
|
|
|
|
lambda x, y: None)
|
|
|
|
|
|
|
|
def _insert_into(self, db):
|
|
|
|
sql = "INSERT INTO todo (task) VALUES ('PASS')"
|
|
|
|
db.execute(sql)
|
|
|
|
|
|
|
|
def assert_records(self, count):
|
|
|
|
cursor = self.conn.execute("SELECT COUNT(*) FROM todo")
|
|
|
|
self.assertEqual((count,), cursor.fetchone())
|
2014-03-29 13:57:02 +00:00
|
|
|
|
2011-09-01 17:21:53 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|