fixed empty sets, maps, lists

This commit is contained in:
Jon Haddad
2013-06-27 14:22:47 -07:00
parent 01e9747411
commit 75d20b6920
2 changed files with 30 additions and 5 deletions

View File

@@ -473,14 +473,15 @@ class Set(BaseContainerColumn):
cq = cql_quote cq = cql_quote
return '{' + ', '.join([cq(v) for v in self.value]) + '}' return '{' + ', '.join([cq(v) for v in self.value]) + '}'
def __init__(self, value_type, strict=True, **kwargs): def __init__(self, value_type, strict=True, default=set, **kwargs):
""" """
:param value_type: a column class indicating the types of the value :param value_type: a column class indicating the types of the value
:param strict: sets whether non set values will be coerced to set :param strict: sets whether non set values will be coerced to set
type on validation, or raise a validation error, defaults to True type on validation, or raise a validation error, defaults to True
""" """
self.strict = strict self.strict = strict
super(Set, self).__init__(value_type, **kwargs)
super(Set, self).__init__(value_type, default=default, **kwargs)
def validate(self, value): def validate(self, value):
val = super(Set, self).validate(value) val = super(Set, self).validate(value)
@@ -495,11 +496,12 @@ class Set(BaseContainerColumn):
return {self.value_col.validate(v) for v in val} return {self.value_col.validate(v) for v in val}
def to_python(self, value): def to_python(self, value):
if value is None: return None if value is None: return set()
return {self.value_col.to_python(v) for v in value} return {self.value_col.to_python(v) for v in value}
def to_database(self, value): def to_database(self, value):
if value is None: return None if value is None: return None
if isinstance(value, self.Quoter): return value if isinstance(value, self.Quoter): return value
return self.Quoter({self.value_col.to_database(v) for v in value}) return self.Quoter({self.value_col.to_database(v) for v in value})
@@ -560,6 +562,9 @@ class List(BaseContainerColumn):
cq = cql_quote cq = cql_quote
return '[' + ', '.join([cq(v) for v in self.value]) + ']' return '[' + ', '.join([cq(v) for v in self.value]) + ']'
def __init__(self, value_type, default=set, **kwargs):
return super(List, self).__init__(value_type=value_type, default=default, **kwargs)
def validate(self, value): def validate(self, value):
val = super(List, self).validate(value) val = super(List, self).validate(value)
if val is None: return if val is None: return
@@ -668,7 +673,7 @@ class Map(BaseContainerColumn):
cq = cql_quote cq = cql_quote
return '{' + ', '.join([cq(k) + ':' + cq(v) for k,v in self.value.items()]) + '}' return '{' + ', '.join([cq(k) + ':' + cq(v) for k,v in self.value.items()]) + '}'
def __init__(self, key_type, value_type, **kwargs): def __init__(self, key_type, value_type, default=dict, **kwargs):
""" """
:param key_type: a column class indicating the types of the key :param key_type: a column class indicating the types of the key
:param value_type: a column class indicating the types of the value :param value_type: a column class indicating the types of the value
@@ -687,7 +692,7 @@ class Map(BaseContainerColumn):
else: else:
self.key_col = key_type self.key_col = key_type
self.key_type = self.key_col.__class__ self.key_type = self.key_col.__class__
super(Map, self).__init__(value_type, **kwargs) super(Map, self).__init__(value_type, default=default, **kwargs)
def get_column_def(self): def get_column_def(self):
""" """

View File

@@ -40,6 +40,16 @@ class TestSetColumn(BaseCassEngTestCase):
super(TestSetColumn, cls).tearDownClass() super(TestSetColumn, cls).tearDownClass()
delete_table(TestSetModel) delete_table(TestSetModel)
def test_empty_set_initial(self):
"""
tests that sets are set() by default, should never be none
:return:
"""
m = TestSetModel.create()
m.int_set.add(5)
m.save()
def test_io_success(self): def test_io_success(self):
""" Tests that a basic usage works as expected """ """ Tests that a basic usage works as expected """
m1 = TestSetModel.create(int_set={1, 2}, text_set={'kai', 'andreas'}) m1 = TestSetModel.create(int_set={1, 2}, text_set={'kai', 'andreas'})
@@ -129,6 +139,8 @@ class TestSetColumn(BaseCassEngTestCase):
column = columns.Set(columns.Text(min_length=100)) column = columns.Set(columns.Text(min_length=100))
assert isinstance(column.value_col, columns.Text) assert isinstance(column.value_col, columns.Text)
def test_to_python(self): def test_to_python(self):
""" Tests that to_python of value column is called """ """ Tests that to_python of value column is called """
column = columns.Set(JsonTestColumn) column = columns.Set(JsonTestColumn)
@@ -157,6 +169,10 @@ class TestListColumn(BaseCassEngTestCase):
super(TestListColumn, cls).tearDownClass() super(TestListColumn, cls).tearDownClass()
delete_table(TestListModel) delete_table(TestListModel)
def test_initial(self):
tmp = TestListModel.create()
tmp.int_list.append(1)
def test_io_success(self): def test_io_success(self):
""" Tests that a basic usage works as expected """ """ Tests that a basic usage works as expected """
m1 = TestListModel.create(int_list=[1, 2], text_list=['kai', 'andreas']) m1 = TestListModel.create(int_list=[1, 2], text_list=['kai', 'andreas'])
@@ -275,6 +291,10 @@ class TestMapColumn(BaseCassEngTestCase):
super(TestMapColumn, cls).tearDownClass() super(TestMapColumn, cls).tearDownClass()
delete_table(TestMapModel) delete_table(TestMapModel)
def test_empty_default(self):
tmp = TestMapModel.create()
tmp.int_map['blah'] = 1
def test_io_success(self): def test_io_success(self):
""" Tests that a basic usage works as expected """ """ Tests that a basic usage works as expected """
k1 = uuid4() k1 = uuid4()