setting counter column default instantiation value to 0 (not None)
This commit is contained in:
		@@ -246,9 +246,18 @@ class Integer(Column):
 | 
				
			|||||||
        return self.validate(value)
 | 
					        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):
 | 
					class Counter(Integer):
 | 
				
			||||||
    db_type = 'counter'
 | 
					    db_type = 'counter'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    value_manager = CounterValueManager
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self,
 | 
					    def __init__(self,
 | 
				
			||||||
                 index=False,
 | 
					                 index=False,
 | 
				
			||||||
                 db_field=None,
 | 
					                 db_field=None,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -59,6 +59,15 @@ class TestCounterColumn(BaseCassEngTestCase):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    def test_update_from_none(self):
 | 
					    def test_update_from_none(self):
 | 
				
			||||||
        """ Tests that updating from None uses a create statement """
 | 
					        """ 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 """
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user