[SQL-CVS] SQLObject development repository branch master updated. 2.1.0-273-g4b241b9
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Oleg B. <no...@so...> - 2015-02-26 20:44:52
|
This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project "SQLObject development repository". The branch, master has been updated via 4b241b911877afce0f1625fc321faade2d8cfcf1 (commit) via 019201f9b1f4f7e77486cb9a07787bfdc43a1c76 (commit) via 2cd507722bebc742338fd82f412cdb1046d5d4ab (commit) via e817be9ee7081c0bd885d36cb937461483c6d9bb (commit) via 6d6dd4ce0d529a448833913d279b425b3dade034 (commit) from e647b7ae7af1bd21fd93da83187ca1250054e454 (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- http://sourceforge.net/p/sqlobject/sqlobject/ci/4b241b911877afce0f1625fc321faade2d8cfcf1 commit 4b241b911877afce0f1625fc321faade2d8cfcf1 Merge: e647b7a 019201f Author: Oleg Broytman <ph...@ph...> Date: Thu Feb 26 23:43:45 2015 +0300 Merge pull request #102 from drnlm/fix_hashcol Fix hashcol http://sourceforge.net/p/sqlobject/sqlobject/ci/019201f9b1f4f7e77486cb9a07787bfdc43a1c76 commit 019201f9b1f4f7e77486cb9a07787bfdc43a1c76 Author: Neil <drn...@gm...> Date: Thu Feb 26 22:23:45 2015 +0200 Use def to make flake8 happy diff --git a/sqlobject/include/tests/test_hashcol.py b/sqlobject/include/tests/test_hashcol.py index e0a0d19..0e9794c 100644 --- a/sqlobject/include/tests/test_hashcol.py +++ b/sqlobject/include/tests/test_hashcol.py @@ -9,11 +9,17 @@ from hashlib import sha256, md5 ######################################## if sys.version_info[0] == 2: - sha256_str = lambda x: sha256(x).hexdigest() - md5_str = lambda x: md5(x).hexdigest() + def sha256_str(x): + return sha256(x).hexdigest() + + def md5_str(x): + return md5(x).hexdigest() else: - sha256_str = lambda x: sha256(x.encode('utf8')).hexdigest() - md5_str = lambda x: md5(x.encode('utf8')).hexdigest() + def sha256_str(x): + return sha256(x.encode('utf8')).hexdigest() + + def md5_str(x): + return md5(x.encode('utf8')).hexdigest() class HashTest(SQLObject): http://sourceforge.net/p/sqlobject/sqlobject/ci/2cd507722bebc742338fd82f412cdb1046d5d4ab commit 2cd507722bebc742338fd82f412cdb1046d5d4ab Author: Neil <drn...@gm...> Date: Thu Feb 26 21:28:29 2015 +0200 Rework to handle python 3 encoding issues diff --git a/sqlobject/include/hashcol.py b/sqlobject/include/hashcol.py index 0e98937..d0e55a1 100644 --- a/sqlobject/include/hashcol.py +++ b/sqlobject/include/hashcol.py @@ -1,3 +1,4 @@ +import sys import sqlobject.col from sqlobject.compat import string_type @@ -82,7 +83,10 @@ class SOHashCol(sqlobject.col.SOStringCol): def __init__(self, **kw): if 'hashMethod' not in kw: from hashlib import md5 - self.hashMethod = lambda v: md5(v).hexdigest() + if sys.version_info[0] == 2: + self.hashMethod = lambda v: md5(v).hexdigest() + else: + self.hashMethod = lambda v: md5(v.encode('utf8')).hexdigest() if 'length' not in kw: kw['length'] = 32 else: diff --git a/sqlobject/include/tests/test_hashcol.py b/sqlobject/include/tests/test_hashcol.py index dcba622..e0a0d19 100644 --- a/sqlobject/include/tests/test_hashcol.py +++ b/sqlobject/include/tests/test_hashcol.py @@ -1,3 +1,4 @@ +import sys from sqlobject import * from sqlobject.tests.dbtest import * from sqlobject.include import hashcol @@ -7,7 +8,12 @@ from hashlib import sha256, md5 # HashCol test ######################################## -sha256_str = lambda x: sha256(x).hexdigest() +if sys.version_info[0] == 2: + sha256_str = lambda x: sha256(x).hexdigest() + md5_str = lambda x: md5(x).hexdigest() +else: + sha256_str = lambda x: sha256(x.encode('utf8')).hexdigest() + md5_str = lambda x: md5(x.encode('utf8')).hexdigest() class HashTest(SQLObject): @@ -41,7 +47,7 @@ def test_create(): ORDER BY count """) for count, col1, col2 in rows: - assert md5(data[count]).hexdigest() == col1 + assert md5_str(data[count]) == col1 assert sha256_str(data[count]) == col2 http://sourceforge.net/p/sqlobject/sqlobject/ci/e817be9ee7081c0bd885d36cb937461483c6d9bb commit e817be9ee7081c0bd885d36cb937461483c6d9bb Author: Neil <drn...@gm...> Date: Thu Feb 26 21:27:33 2015 +0200 Rewrite hashcol to use rich comparison diff --git a/sqlobject/include/hashcol.py b/sqlobject/include/hashcol.py index 18aeddf..0e98937 100644 --- a/sqlobject/include/hashcol.py +++ b/sqlobject/include/hashcol.py @@ -13,15 +13,48 @@ class DbHash: self.hash = hash self.hashMethod = hashMethod - def __cmp__(self, other): - if other is None: - if self.hash is None: - return 0 - return True + def _get_key(self, other): + """Create the hash of the other class""" if not isinstance(other, string_type): raise TypeError( "A hash may only be compared with a string, or None.") - return cmp(self.hashMethod(other), self.hash) + return self.hashMethod(other) + + def __eq__(self, other): + if other is None: + if self.hash is None: + return True + return False + other_key = self._get_key(other) + return other_key == self.hash + + def __lt__(self, other): + if other is None: + return False + other_key = self._get_key(other) + return other_key < self.hash + + def __gt__(self, other): + if other is None: + if self.hash is None: + return False + return True + other_key = self._get_key(other) + return other_key > self.hash + + def __le__(self, other): + if other is None: + if self.hash is None: + return True + return False + other_key = self._get_key(other) + return other_key <= self.hash + + def __ge__(self, other): + if other is None: + return True + other_key = self._get_key(other) + return other_key >= self.hash def __repr__(self): return "<DbHash>" http://sourceforge.net/p/sqlobject/sqlobject/ci/6d6dd4ce0d529a448833913d279b425b3dade034 commit 6d6dd4ce0d529a448833913d279b425b3dade034 Author: Neil <drn...@gm...> Date: Thu Feb 26 21:13:36 2015 +0200 Add test case for HashCol diff --git a/sqlobject/include/tests/__init__.py b/sqlobject/include/tests/__init__.py new file mode 100644 index 0000000..792d600 --- /dev/null +++ b/sqlobject/include/tests/__init__.py @@ -0,0 +1 @@ +# diff --git a/sqlobject/include/tests/test_hashcol.py b/sqlobject/include/tests/test_hashcol.py new file mode 100644 index 0000000..dcba622 --- /dev/null +++ b/sqlobject/include/tests/test_hashcol.py @@ -0,0 +1,72 @@ +from sqlobject import * +from sqlobject.tests.dbtest import * +from sqlobject.include import hashcol +from hashlib import sha256, md5 + +######################################## +# HashCol test +######################################## + +sha256_str = lambda x: sha256(x).hexdigest() + + +class HashTest(SQLObject): + count = IntCol(alternateID=True) + col1 = hashcol.HashCol() + col2 = hashcol.HashCol(hashMethod=sha256_str) + + +data = ['test', 'This is more text', 'test 2'] +items = [] + + +def setup(): + global items + items = [] + setupClass(HashTest) + for i, s in enumerate(data): + items.append(HashTest(count=i, col1=s, col2=s)) + + +def test_create(): + setup() + for s, item in zip(data, items): + assert item.col1 == s + assert item.col2 == s + + conn = HashTest._connection + rows = conn.queryAll(""" + SELECT count, col1, col2 + FROM hash_test + ORDER BY count + """) + for count, col1, col2 in rows: + assert md5(data[count]).hexdigest() == col1 + assert sha256_str(data[count]) == col2 + + +def test_select(): + for i, value in enumerate(data): + rows = list(HashTest.select(HashTest.q.col1 == value)) + assert len(rows) == 1 + rows = list(HashTest.select(HashTest.q.col2 == value)) + assert len(rows) == 1 + # Passing the hash in directly should fail + rows = list(HashTest.select(HashTest.q.col2 == sha256_str(value))) + assert len(rows) == 0 + rows = list(HashTest.select(AND( + HashTest.q.col1 == value, + HashTest.q.col2 == value + ))) + assert len(rows) == 1 + rows = list(HashTest.selectBy(col1=value)) + assert len(rows) == 1 + rows = list(HashTest.selectBy(col2=value)) + assert len(rows) == 1 + rows = list(HashTest.selectBy(col1=value, col2=value)) + assert len(rows) == 1 + rows = list(HashTest.select(OR( + HashTest.q.col1 == 'test 2', + HashTest.q.col2 == 'test' + ))) + assert len(rows) == 2 ----------------------------------------------------------------------- Summary of changes: sqlobject/include/hashcol.py | 51 +++++++++++++++--- sqlobject/include/{ => tests}/__init__.py | 0 sqlobject/include/tests/test_hashcol.py | 84 +++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 7 deletions(-) copy sqlobject/include/{ => tests}/__init__.py (100%) create mode 100644 sqlobject/include/tests/test_hashcol.py hooks/post-receive -- SQLObject development repository |