Fix all PEP8 warnings, including a missing import (added the

`test_raise_sqlite_integrity_error`).
This commit is contained in:
Iuri de Silvio 2014-03-29 11:04:08 -03:00
parent e0d85fbc44
commit a746772bf9
2 changed files with 39 additions and 23 deletions

View File

@ -63,9 +63,10 @@ class SQLitePlugin(object):
''' Make sure that other installed plugins don't affect the same
keyword argument.'''
for other in app.plugins:
if not isinstance(other, SQLitePlugin): continue
if not isinstance(other, SQLitePlugin):
continue
if other.keyword == self.keyword:
raise PluginError("Found another sqlite plugin with "\
raise PluginError("Found another sqlite plugin with "
"conflicting settings (non-unique keyword).")
elif other.name == self.name:
self.name += '_%s' % self.keyword
@ -80,7 +81,8 @@ class SQLitePlugin(object):
_callback = route.callback
# Override global configuration with route-specific values.
if "sqlite" in config: # support for configuration before `ConfigDict` namespaces
if "sqlite" in config:
# support for configuration before `ConfigDict` namespaces
g = lambda key, default: config.get('sqlite', {}).get(key, default)
else:
g = lambda key, default: config.get('sqlite.' + key, default)
@ -100,20 +102,23 @@ class SQLitePlugin(object):
# Connect to the database
db = sqlite3.connect(dbfile)
# This enables column access by name: row['column_name']
if dictrows: db.row_factory = sqlite3.Row
if dictrows:
db.row_factory = sqlite3.Row
# Add the connection handle as a keyword argument.
kwargs[keyword] = db
try:
rv = callback(*args, **kwargs)
if autocommit: db.commit()
if autocommit:
db.commit()
except sqlite3.IntegrityError as e:
db.rollback()
raise HTTPError(500, "Database Error", e)
raise bottle.HTTPError(500, "Database Error", e)
except bottle.HTTPError as e:
raise
except bottle.HTTPResponse as e:
if autocommit: db.commit()
if autocommit:
db.commit()
raise
finally:
db.close()

27
test.py
View File

@ -1,8 +1,8 @@
import unittest
import os
import sqlite3
import bottle
from bottle.ext import sqlite
import sqlite3
class SQLiteTest(unittest.TestCase):
def setUp(self):
@ -14,20 +14,20 @@ class SQLiteTest(unittest.TestCase):
@self.app.get('/')
def test(db):
self.assertEqual(type(db), type(sqlite3.connect(':memory:')))
self.app({'PATH_INFO':'/', 'REQUEST_METHOD':'GET'}, lambda x, y: None)
self._request('/')
def test_without_keyword(self):
self.plugin = self.app.install(sqlite.Plugin())
@self.app.get('/')
def test():
def test_1():
pass
self.app({'PATH_INFO':'/', 'REQUEST_METHOD':'GET'}, lambda x, y: None)
self._request('/')
@self.app.get('/2')
def test(**kw):
def test_2(**kw):
self.assertFalse('db' in kw)
self.app({'PATH_INFO':'/2', 'REQUEST_METHOD':'GET'}, lambda x, y: None)
self._request('/2')
def test_install_conflicts(self):
self.app.install(sqlite.Plugin())
@ -38,8 +38,19 @@ class SQLiteTest(unittest.TestCase):
pass
# I have two plugins working with different names
self.app({'PATH_INFO': '/', 'REQUEST_METHOD': 'GET'}, lambda x, y: None)
self._request('/')
def test_raise_sqlite_integrity_error(self):
self.plugin = self.app.install(sqlite.Plugin())
@self.app.get('/')
def test(db):
raise sqlite3.IntegrityError()
self._request('/')
def _request(self, path, method='GET'):
self.app({'PATH_INFO': path, 'REQUEST_METHOD': method},
lambda x, y: None)
if __name__ == '__main__':
unittest.main()