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

27
test.py
View File

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