From 7428fcb0617d1ee0302403144ecceeb5e157a96d Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Mon, 4 Nov 2013 14:47:13 -0800 Subject: [PATCH] Fix password type to allow saving a null password. --- sqlalchemy_utils/types/password.py | 3 +++ tests/types/test_password.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/sqlalchemy_utils/types/password.py b/sqlalchemy_utils/types/password.py index 2b050c9..5d95e80 100644 --- a/sqlalchemy_utils/types/password.py +++ b/sqlalchemy_utils/types/password.py @@ -143,6 +143,9 @@ class PasswordType(types.TypeDecorator, ScalarCoercible): return Password(value, self.context) def _coerce(self, value): + if value is None: + return + if not isinstance(value, Password): # Hash the password using the default scheme. value = self.context.encrypt(value).encode('utf8') diff --git a/tests/types/test_password.py b/tests/types/test_password.py index 7115ce6..8b2a116 100644 --- a/tests/types/test_password.py +++ b/tests/types/test_password.py @@ -100,3 +100,17 @@ class TestPasswordType(TestCase): # Not sure what to assert here; the test raised an error before. assert obj.password != other.password + + def test_set_none(self): + + obj = self.User() + obj.password = None + + assert obj.password is None + + self.session.add(obj) + self.session.commit() + + obj = self.session.query(self.User).get(obj.id) + + assert obj.password is None