Merge pull request #254 from frol/sqlite-passwordtype-fix

Added SQLite support for PasswordType
This commit is contained in:
Konsta Vesterinen
2016-11-19 10:36:02 +02:00
committed by GitHub
2 changed files with 29 additions and 7 deletions

View File

@@ -2,7 +2,7 @@ import weakref
import six import six
from sqlalchemy import types from sqlalchemy import types
from sqlalchemy.dialects import oracle, postgresql from sqlalchemy.dialects import oracle, postgresql, sqlite
from sqlalchemy.ext.mutable import Mutable from sqlalchemy.ext.mutable import Mutable
from ..exceptions import ImproperlyConfigured from ..exceptions import ImproperlyConfigured
@@ -192,12 +192,13 @@ class PasswordType(types.TypeDecorator, ScalarCoercible):
if dialect.name == 'postgresql': if dialect.name == 'postgresql':
# Use a BYTEA type for postgresql. # Use a BYTEA type for postgresql.
impl = postgresql.BYTEA(self.length) impl = postgresql.BYTEA(self.length)
return dialect.type_descriptor(impl) elif dialect.name == 'oracle':
if dialect.name == 'oracle':
# Use a RAW type for oracle. # Use a RAW type for oracle.
impl = oracle.RAW(self.length) impl = oracle.RAW(self.length)
return dialect.type_descriptor(impl) elif dialect.name == 'sqlite':
# Use a BLOB type for sqlite
impl = sqlite.BLOB(self.length)
else:
# Use a VARBINARY for all other dialects. # Use a VARBINARY for all other dialects.
impl = types.VARBINARY(self.length) impl = types.VARBINARY(self.length)
return dialect.type_descriptor(impl) return dialect.type_descriptor(impl)

View File

@@ -1,6 +1,10 @@
import mock import mock
import pytest import pytest
import sqlalchemy as sa import sqlalchemy as sa
import sqlalchemy.dialects.mysql
import sqlalchemy.dialects.oracle
import sqlalchemy.dialects.postgresql
import sqlalchemy.dialects.sqlite
from sqlalchemy import inspect from sqlalchemy import inspect
from sqlalchemy_utils import Password, PasswordType, types # noqa from sqlalchemy_utils import Password, PasswordType, types # noqa
@@ -52,6 +56,23 @@ def onload_callback(schemes, deprecated):
@pytest.mark.skipif('types.password.passlib is None') @pytest.mark.skipif('types.password.passlib is None')
class TestPasswordType(object): class TestPasswordType(object):
@pytest.mark.parametrize('dialect_module,impl', [
(sqlalchemy.dialects.sqlite, sa.dialects.sqlite.BLOB),
(sqlalchemy.dialects.postgresql, sa.dialects.postgresql.BYTEA),
(sqlalchemy.dialects.oracle, sa.dialects.oracle.RAW),
(sqlalchemy.dialects.mysql, sa.VARBINARY),
])
def test_load_dialect_impl(self, dialect_module, impl):
"""
Should produce the same impl type as Alembic would expect after
inspecing a database
"""
password_type = PasswordType()
assert isinstance(
password_type.load_dialect_impl(dialect_module.dialect()),
impl
)
def test_encrypt(self, User): def test_encrypt(self, User):
"""Should encrypt the password on setting the attribute.""" """Should encrypt the password on setting the attribute."""
obj = User() obj = User()