Skip to content

Commit

Permalink
HashField cannot be marked as unique (this doesn't make any sense)
Browse files Browse the repository at this point in the history
  • Loading branch information
twidi committed Jan 26, 2018
1 parent ecccf56 commit f1f71ac
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions limpyd/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class RedisField(RedisProxyCommand):
'kwargs': ['lockable', 'default'],
'attrs': ['name', '_instance', '_model', 'indexable', 'unique']
}
_unique_supported = True

def __init__(self, *args, **kwargs):
"""
Expand All @@ -187,6 +188,8 @@ def __init__(self, *args, **kwargs):
self.indexable = kwargs.get("indexable", False)
self.unique = kwargs.get("unique", False)
if self.unique:
if not self._unique_supported:
raise ImplementationError('%s field cannot be unique' % self.__class__.__name__)
if hasattr(self, "default"):
raise ImplementationError('Cannot set "default" and "unique" together!')
self.indexable = True
Expand Down Expand Up @@ -819,6 +822,7 @@ def _call_linsert(self, command, where, refvalue, value):

class HashField(MultiValuesField):

_unique_supported = False
proxy_getter = "hgetall"
proxy_setter = "hmset"

Expand Down
6 changes: 6 additions & 0 deletions tests/fields/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import unicode_literals

from limpyd import fields
from limpyd.exceptions import ImplementationError

from ..model import TestRedisModel, BaseModelTest

Expand Down Expand Up @@ -182,3 +183,8 @@ def test_hlen_should_return_number_of_keys(self):
self.assertEqual(obj.headers.hlen(), 0)
obj.headers.hmset(**headers)
self.assertEqual(obj.headers.hlen(), 2)

def test_hashfields_cannot_be_unique(self):
with self.assertRaises(ImplementationError):
class TestUniquenessHashField(TestRedisModel):
data = fields.HashField(indexable=True, unique=True)

0 comments on commit f1f71ac

Please sign in to comment.