setting counter column default instantiation value to 0 (not None)

This commit is contained in:
Blake Eggleston
2013-08-14 15:48:24 -07:00
parent 3c24995481
commit 3ee88ecf93
2 changed files with 20 additions and 2 deletions

View File

@@ -246,9 +246,18 @@ class Integer(Column):
return self.validate(value)
class CounterValueManager(BaseValueManager):
def __init__(self, instance, column, value):
super(CounterValueManager, self).__init__(instance, column, value)
self.value = self.value or 0
self.previous_value = self.previous_value or 0
class Counter(Integer):
db_type = 'counter'
value_manager = CounterValueManager
def __init__(self,
index=False,
db_field=None,

View File

@@ -59,6 +59,15 @@ class TestCounterColumn(BaseCassEngTestCase):
def test_update_from_none(self):
""" Tests that updating from None uses a create statement """
instance = TestCounterModel()
instance.counter += 1
instance.save()
new = TestCounterModel.get(partition=instance.partition)
assert new.counter == 1
def test_new_instance_defaults_to_zero(self):
""" Tests that instantiating a new model instance will set the counter column to zero """
instance = TestCounterModel()
assert instance.counter == 0
def test_multiple_inserts(self):
""" Tests inserting over existing data works as expected """